Get startedGet started for free

Order sizing functions

1. Order sizing functions

Finally, one last important aspect about using rules is the ability to use order sizing functions.

2. What are rules?

Order sizing functions are, as you may imagine, functions that determine how much of an asset you buy or sell, if you do not use the orderqty argument. Order sizing functions create variable position sizes, based on the inputs to the order sizing function.

3. What are rules?

While writing order sizing functions involves introducing complex syntax and is outside the scope of this class, the ability to call them is fairly straightforward. When using an order sizing function, the orderqty argument of ruleSignal effectively becomes deprecated-

4. What are rules?

or no longer meaningful to the function's arguments, and replaced by the order sizing function's output. In order to use an order sizing function, you pass in the order sizing function as an object to the osFUN argument. Furthermore, the argument for the order sizing function fit under the same list of arguments as the rest of the inputs for ruleSignal. For your strategy, you will use order sizing function called osMaxDollar. This function simply obtains a position equal to tradeSize of the asset, rounded to the nearest unit of the asset. So if your tradeSize is a hundred and five dollars, and the price is twenty dollars, you will get five units (more colloquially called shares) of the asset. Similarly, the maxSize is an argument for the maximum amount of dollars your position can be. Usually, this amount is equal to the trade size, which means that once you have a position, it most likely will not change in size. Essentially, one can think of using an order sizing function as a sort of nested apply statement. Whereas an apply statement in R takes in one function as an argument, a ruleSignal that takes in an order sizing function is similar to passing in a function to an apply statement that needs another function as input. Essentially, order sizing functions require one more level of function encapsulation, while the arguments to these order sizing functions share space with the arguments to ruleSignal. If this seems somewhat confusing, the next exercise, the final one in the chapter, will illustrate how simple this process is with a function that spends all the allotted trade size in one transaction to buy a constant dollar amount of shares.

5. Let's practice!

Good luck.