Implementing a rule with an order sizing function
In quantstrat, the amount of an asset transacted may not always be a fixed quantity in regards to the actual shares. The constructs that allow quantstrat to vary the amount of shares bought or sold are called order sizing functions. Due to the extensive additional syntax in creating a proper order sizing function, coding your own order sizing function from scratch is outside the scope of this course.
However, using a pre-coded order sizing function is straightforward. The first thing to know is that when using an order sizing function, the orderqty
argument is no longer relevant, as the order quantity is determined by the order sizing function. Calling an order sizing function with your add.rule()
call is fairly straightforward. The inputs for the order sizing function are mixed in with the rest of the inputs inside the arguments that you have been working with throughout this chapter.
In this exercise, you will use the osFUN
argument to specify a function called osMaxDollar
. This is not passed in as a string, but rather as an object. The only difference is that the name of the order sizing function is not encased in quotes.
The additional arguments to this function are tradeSize
and maxSize
, both of which should take tradesize
, which you defined several chapters earlier. This has been made available in your workspace.
This exercise is part of the course
Financial Trading in R
Exercise instructions
- The
add.rule()
command used in previous exercises is printed in your workspace. - Add an order sizing function to this rule by specifying
osFUN
,tradeSize
, andmaxSize
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Add a rule that uses an osFUN to size an entry position
add.rule(strategy = strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longentry", sigval = TRUE, ordertype = "market",
orderside = "long", replace = FALSE, prefer = "Open",
# Use the osFUN called osMaxDollar
osFUN = ___,
# The tradeSize argument should be equal to tradesize (defined earlier)
tradeSize = ___,
# The maxSize argument should be equal to tradesize as well
maxSize = ___),
type = "enter")