Click here to Login




How to Backtest Each Stock or Asset Individually

Updated on 2012-11-15





QuantShare simulator is a true portfolio backtester. This means that the backtesting or simulation generates trading orders by taking into account things such as the available cash, pending orders, open positions, margin, equity... at any moment during the process.

Some traders want to backtest their trading system for each stock or security in their list and have individual report for each stock. How each stock would have performed if it were the only security allowed to be bought by the trading system?

In this post, I will show you how to create to create a trading system and optimize it so that each optimization backtests a single stock. After that, I will show you how to get a single page report of each individual stock backtest.


Prepare your list of stocks

First, let us prepare the list of stocks or securities that we want to backtest individually.
This list must contain one ticker symbol per line and it must be saved under a file.

Using the watchlist tool, create your watchlist, right click on the table then click on "Copy all symbols to clipboard"
Using the screener too, create a scan, run it then click on the "Copy symbols to clipboard button"

Now, using any text editor, open a new file then paste the symbols list using CONTROL+V shortcut. This will copy the data contained in the clipboard to the text editor.

Save the file under a specific location (Example: c:\mylist.txt)


Custom trading indicator

This trading indicator reads the previous list we have created and returns true or "1" if the symbol corresponds to the index in the list. Note that both the symbol and the index are passed to the function.

Example:

Our list:
ARL
ARO
ARSD
ART
ARW
ARX
ASH
ASI


Function:
a = SymbolIndex("ARO", 1); // Returns true
a = SymbolIndex("ARO", 0); // Returns false



Here is how to implement this custom indicator:

- Select "Tools -> Create Functions"
- Click on "Add" to create a new indicator/function
- Type "SymbolIndex" then click on "Save"
- In "Parameters" panel (bottom/right), add two parameters:

Format: Name/Type:
1/ Symbol/String
2/ Index/Number

- Type the following code in the right panel then click on "Save"

string fileLocation = @"c:\mylist.txt";
string[] lines = System.IO.File.ReadAllLines(fileLocation);

int index1 = (int)index[0];
string symbol1 = symbol[0].ToLowerInvariant();

if(index1 < 0)
{
result.Assign(lines.Length);
}
else if(index1 < lines.Length)
{
string symbol2 = lines[index1].Trim().ToLowerInvariant();
if(symbol1 == symbol2)
{
result.Assign(1);
}
}


This trading indicator is already available in the sharing server: Ticker Symbol Index


Create the trading system

You probably already have the trading system you want to backtest for each stock individually.

If you don't then create a new one (Analysis -> Simulator -> New), select "Create a trading system using the formula editor" then type these simple buy and sell rules:

buy = rsi(14) > 70;
sell = rsi(14) < 30;


Now that we have our trading system, we must do three things:

- Maximum number of symbols:

Set the number of positions field to "1". It is important if we want to invest 100% of the portfolio cash in a single asset.

- Set the list of symbols:

In the trading system editor, select "Symbols & Dates" tab at the top, remove all symbols filters, add a new "Custom Symbols" condition, click on the cell under "Values" then paste the symbols list (Copy it from the text file CONTROL+C and paste it here CONTROL+V).

- Update the buy/short rule:

In the strategy formula editor of your trading system, we must add an additional rule that instructs the simulator to analyze only one stock on each run.

The additional rule is as follows:

Optimize("sindex", 0, SymbolIndex(name(), -1), 1);
buy = buy and SymbolIndex(name(), sindex);


It adds up to the original buy rule and returns true only if the symbol name (name() function) corresponds to the "sindex" index. Here, "sindex" is an optimizable variable that vary from 0 to the number of symbols in the text file (minus 1 set as second parameter of the 'SymbolIndex' function).

Save your trading system before reading the next paragraph.


Backtest Stocks Individually

In the "Simulator Manager" (Analysis -> Simulator), select the previously updated trading system then click on "Optimize" to backtest the trading system N-times, each time using a different stock or security.

In the simulation report, each row contains the backtest results of a different stock.
The unique problem here is that the symbol being backtested is not displayed.




Here is how to fix this:
- Update the trading system
- Select "money management" tab
- Click on "Add a new money management script"
- Select "OnStartSimulation" event then type:

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

- Select "OnEndSimulation" event then type:

MMPosition[] pos = Portfolio.GetAllPositions();
if(pos.Length > 0)
{
Variables.SetVariable("Symbol", pos[0].Symbol);
}


- Optimize your trading system again to display the symbol name in the simulation report table.




Backtest Report

After the individual stock backtesting is completed, select your trading system in the simulator manager and click on "Tools -> Optimization Report" to display different statistics of each backtest.

You can export the data by selecting it then using CONTROL+C. Use CONTROL+V to paste the previously copied data.




After reading and using the technique described here, you may want to combine two or more securities and backtest the different combinations. We have a nice tutorial about how to accomplish this: Select the best ETFs combination to maximize your return and reduce your investment risk












6 comments (Log in)

QuantShare Blog
QuantShare
Search Posts




QuantShare
Recent Posts 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 2012 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.