Click here to Login




Backtesting Your Trading Systems: Symbol Segmentation

Updated on 2013-10-29





Say you have a trading system and you want to see how it performs for different set of symbols. Of course, you can have the trading system backtested for each different sector or industry like it is shown in this post. But you can also define your own segments and have the trading system backtested for each of your segments, so that you can easily and quickly determine for which segment your trading system performs best.


Example of Segments

Here are some examples of segments you can use:

- Stocks
- ETFs
- Futures
- Forex
- Stocks with ROI > 10
- S&P 500 Stocks
- Stocks in the Gold industry
...


Create the Trading System

For this post, we will create a very simple trading system. Buy when the price crosses above the 50-bar moving average and sell when it crosses below the same moving average.

Here is how to create a trading system: Create a trading system

And here is the formula that you should type to implement this trading strategy:

buy = cross(close, sma(50));
sell = cross(sma(50), close);


// close: Price of the analyzed security
// sma: Function that creates a simple moving average
// cross: Function that detects when a time-series crossed above/below another time-series


Create the Segmentation Function

Select "Tools -> Create Functions" to create a custom function using C#.

If you are not a programmer or just don't want to learn how to use C# programming language, you can skip this paragraph and download the function directly from here: Backtest different Segments in your Trading System.

After you open the functions editor, click on "Add" to create a new function then type a name ("Segment") for this indicator.
Create two parameters: segments (string) and index (number)

In the editor, type the following formula:

string[] seg = segments[0].Split(';');
if(index[0] < 0)
{
result.Assign(seg.Length);
}
else
{
int i = (int)index[0];
QSFormula formula = cFunctions.CompileFormula("a=" + seg[i] + ";");
result = formula.GetVectorDouble("a");
}



Explanation:

- The formula gets two parameters:
Segments: Defines segment formulas separated by semi-colons. Example: rsi(14)>70;close>sma(30)
Index: Defines the segment index to use. Set a negative value to get the number of segments

- The script separates segments and stores them in an array (seg).
- It then checks the value of "index"
- If it is negative then it returns the number of segments
- If it is positive then it gets the segment referenced by this index and compiles the formula behind that segment
- Finally, the result of that formula is returned by the function (result = …)

Once compiled and saved, you can use this function in the QuantShare programming language by typing for example:

a = Segment("rsi(14)>70;close>sma(30)", 1);

// This will calculate and return the result of "close>sma(30)". It is equivalent to:
// a = close>sma(30);


Updating Your Trading System

Let us update our previous trading system and include the "Segment" function we have just created. The idea is to create multiple segments then use an optimizable variable to calculate a different segment formula each time the simulation is run.

Example of segments:

Stocks:
stringcontains(group(), 'stock')

S&P 500 Stocks:
stringcontains(index(), 's+p 500')

Medial Industry Stocks:
stringcontains(industry(), 'medical')

Stocks listed on NASDAQ:
stringcontains(market(), 'nasd')

Stocks listed on NYSE:
stringcontains(market(), 'nyse')

Stocks that hit a monthly high during the last 25 trading days:
hhv(high == hhv(high, 25), 25)


Formula:

buy = cross(close, sma(50));
sell = cross(sma(50), close);

seg1 = "stringcontains(group(), 'stock');stringcontains(index(), 's+p 500');stringcontains(industry(), 'medical');stringcontains(market(), 'nasd');stringcontains(market(), 'nyse');hhv(high == hhv(high, 25), 25)";

nb = Segment(seg1, -1);
Optimize("a", 0, nb, 1);
buy = buy and Segment(seg1, a);



Save the new trading system then in the simulator manager click on "Optimize" to backtest your strategy each time using a different segment.












no comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts

Create Graphs using the Grid Tool
Posted 1449 days ago

Profile Graphs
Posted 1554 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.