Click here to Login




How to combine long, short and custom portfolio strategies within a trading system

Updated on 2011-09-06





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);
}
else
{
Portfolio.UpdateCategorySettings("long", 70, 7, null);
Portfolio.UpdateCategorySettings("short", 30, 3, null);
}



Custom Strategies/Categories

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.

Example:
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.

Example:
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.

Notes:
- 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.











no comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1467 days ago

Profile Graphs
Posted 1572 days ago

QuantShare
Previous Posts

More Posts

Back







QuantShare
Product
QuantShare
Features
Create an account
Affiliate Program
Support
Contact Us
Trading Forum
How-to Lessons
Manual
Company
About Us
Privacy
Terms of Use

Copyright © 2024 QuantShare.com
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Google+
Follow us on Google+
RSS Trading Items



Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.