In a long & short trading system, each category (long or short) gets an equal amount of money and positions. Therefore, if you create, for example, a trading system that can go long and short and that has $100.000 as initial equity and 10 as a maximum number of positions, QuantShare will create two different systems or categories and assign $50.000 and 5 maximum positions to each one of them. In this post, we will show you how to change this default behavior manually or using pre-defined conditions.
Money Management and Long/Short Systems
After you define your trading system buy, sell, short and cover rules, select the "Money Management" tab then create a new script.
In the new money management script, select the "OnStartSimulation" event and type the following code:
Portfolio.UpdateCategorySettings("long", 60, 6, null);
Portfolio.UpdateCategorySettings("short", 40, 4, null);
We have associated 60% of capital and 6 maximum positions to the "long" category. The "short" category will get 40% of capital and 4 maximum positions.
The last function parameter is used to rebalance the portfolio based on the new settings. If "null" is specified then the rebalance is ignored, otherwise we should enter an order type (Example: Enter/exit at tomorrow open) to scale-in and scale-out positions so the new categories requirements are met. In the latter case, "long" trades will be scaled-in and "short" trades will be scaled-out (The amount of money associated with this system/category has decreased).
Create a Money Management Input
This money management input will allow us to adjust "long" strategy settings without entering the money management control and modifying the code. Money management variables panel appears in the "Simulator" form when you select a trading system.
Here is how it works:
- Open the money management script
- Select the "OnStartSimulation" event
- Add two input variables
Functions.SetNumericInput("Capital - Long", 50, "Capital to invest");
Functions.SetNumericInput("Positions - Long", 5, "Maximum number of positions");
- Add the "UpdateCategorySettings" function
if(!Variables.IsVariableExists("Capital - Long")) return;
double capitalLong = (double)Variables.GetVariable("Capital - Long");
double positionsLong = (double)Variables.GetVariable("Positions - Long");
double capitalShort = 100 - capitalLong;
double positionsShort = TradingSystemSettings.NbPositions - positionsLong;
Portfolio.UpdateCategorySettings("long", capitalLong, (int)positionsLong, null);
Portfolio.UpdateCategorySettings("short", capitalShort, (int)positionsShort, null);
We first obtain the input variables using the "GetVariable" function, then we set the "short" system settings and finally we update category settings.
- Open the simulator/backtester form, select your trading system, update the money management variables then click on "Save Money Management inputs".
Note that inputs can be optimized by specifying a start, end and increment values then clicking on "Optimize" instead of "Simulate / Backtest".
Adjusting long system capital based on Portfolio Stats
In the above example, we have defined static "long" strategy settings. These settings will be applied during the lifetime of the portfolio or simulation.
In the next example, we will define a portfolio-based condition and modify the strategy settings based on this condition.
For example, we can check the portfolio performance for the "long" strategy for the last 60 days and then use the result to increase or decrease market exposure.
Script Code (Should be defined in the OnEndPeriod event because we have to check the portfolio return every trading day):
double return1 = Portfolio.GetReturn(60, "long");
if(return1 < 0)
Portfolio.UpdateCategorySettings("long", 30, 3, null);
Portfolio.UpdateCategorySettings("short", 70, 7, null);
Portfolio.UpdateCategorySettings("long", 70, 7, null);
Portfolio.UpdateCategorySettings("short", 30, 3, null);
Besides the default "long" and "short" categories, it is possible to add as many new categories as you want. Based on your settings, the application will take care of position sizing when buying or short selling new trades.
Trades entered when "buy" variable delivers a signal (Variable is true in the vector-based language) are associated with the "long" category and trades entered when the "short" variable delivers a signal are associated with the "short" category. For custom categories, trades and new positions should be entered using the money management script.
Buy = rsi(14) > 70;
Short = rsi(14) < 30;
Positions that are taken when the relative strength index increases above 70 are associated with the "long" system. Positions that are taken when the relative strength index decreases below level 30 are associated with the "short" system.
To associate a position or trade to a custom category, use the "Functions.AddLongPosition" or "Functions. AddShortPosition" functions.
Functions.AddLongPosition("MSFT", Orders.OpenMarketOrder(), Functions.CreatePositionSettings("My Category"));
Adding a "Cash" category
In the following example, we will create a "Cash" category with no trades. It will be used to define the percentage of capital to keep in cash.
Portfolio.UpdateCategorySettings("long", 30, 3, Orders.OpenMarketOrder());
Portfolio.UpdateCategorySettings("short", 30, 3, Orders.OpenMarketOrder());
Portfolio.UpdateCategorySettings("cash", 40, 4, Orders.OpenMarketOrder());
One example of its use would be to reduce the number and size of long and short trades when the market is trending down and to increase them (fully invested) when the market is trending up.
- Instead of creating this new category, we could have simply reduced the percentage of capital associated to "long" and "short" categories. One hundred minus the sum of "long" and "short" percentage of capital invested would be the percentage of capital to keep in cash.
Alternately, one could use the "Portfolio.UpdatePercentInvested" function to decrease the percent of invested capital.
- Unlike default categories, a custom category can contain both long and short trades.