#twitter-follow { border: 0;position: fixed; top: 240px; left:0;} #live-support { border: 0;position: fixed; top: 370px; left:0;} #knowledge-base { border: 0;position: fixed; top: 170px; left:0;}

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

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:

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);

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:

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);

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