Click here to Login

How to Backtest Your Trading System for Each Industry

Updated on 2013-03-13

Some trading systems, particularly fundamental-based ones, may perform very well on stocks that belong to some particular industries and perform badly on other industries.
Just like backtesting a trading system for each asset individually, you can use QuantShare to implement a system then perform a backtest for each industry. The system will be optimized and each optimization shows the result of a backtest on stocks belonging to one particular industry.

Industry Data

Is your QuantShare database populated with industry data?

To make sure it is, select "Symbol -> Auto Manage Symbols", check one or several exchanges you are interested in, check "Force the update of symbol information" then click on "Save".
After few seconds, QuantShare will update your symbols list and add if necessary industry information (Industry of each stock in your database)

Trading System

The following is the description of the trading system that is used as an example in this article.

Buy when price crosses above 25-bar simple moving average
Sell when price crosses below 25-bar simple moving average

How to implement this trading system:
- Select "Analysis -> Simulator"
- Click on "New" to create a new trading system
- Select "Create trading system using the formula editor" tab
- Type the following formula:

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

"Close" gets the stock's price series
"SMA" calculates the simple moving average
"Cross" function detects when the first array crosses above the second array

- Click on "Create Trading System" to save your strategy

Count the Number of Industries

First, let us count the number of industries we have in the database.
Select "Symbol -> Categories", click on "Industry" tab to display all industries. There are probably several hundred of industries there so we have to use the script editor to count these.
Open the script editor (Tools -> Script Editor), create a new script then type the following formula:

MessageBox.Show("Industry Count: " + Symbols.GetIndustryList().Length.ToString());

Click on "Execute" the display the number of industries.

Backtest Each Industry

Select the previously created trading system then click on "Update".
We have to use a custom function (IndustryPosition) to get the index position based on its name. The function can be downloaded here: Stock Industry Position.

In the formula editor, type the following lines:

Optimize("a", 0, 256, 1);
b = IndustryPosition(industry());
rule1 = a == b;
buy = cross(close, sma(25)) and rule1;
sell = cross(sma(25), close);

The first line instructs QuantShare to optimize the trading system by varying the variable "a" from 0 to the total number of industries (We used 256 here).
The second line gets the industry position within industry list.
The third line compares the optimizable variable and the industry position. This means that the trading system will buy only stocks that belong to the industry that are in the position defined by the variable "a".

After updating your trading system, click on "Optimize" to start the optimization process.

Adding Industry Name to the Report

As you can see from the earlier backtesting, the report shows only the industry position within the list. It does not show the industry name directly in the report table.
There is no way to see for which industry a simulation was based unless you open the report then select "S.I.M.I -> Avg. Trade Performance" tab.

In order to add the industry name to the report, we have to use the money management script.

- Select the trading system then click on "Update"
- Select "Money Management" tab
- Click on "Add a new money management script"
- Select "OnStartSimulation" event then type the following line:

Functions.AddReportMetric("Industry", "");

- Select "OnEndSimulation" event then type the following lines:

MMPosition[] pos = Portfolio.GetAllPositions();
if(pos.Length > 0)
Symbol sym = Data.GetSymbolInfo(pos[0].Symbol);
Variables.SetVariable("Industry", sym.Industry);

Run the optimization again to display the industry name in a separate column in the backtesting report.

no comments (Log in)

QuantShare Blog
Search Posts

Recent Posts

Create Graphs using the Grid Tool
Posted 1298 days ago

Profile Graphs
Posted 1403 days ago

Previous Posts

More Posts


Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2024
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
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.