Capitalmind
Capitalmind
Actionable insights on equities, fixed-income, macros and personal finance Start 14-Days Free Trial
Actionable investing insights Get Free Trial
Concepts & Tutorials

LearnTA: BackTesting a Systematic Trading Strategy

Let’s build a system. Where we have well defined rules for entry, exit and position size and we check the performance of that strategy historically.

Many of you are familiar with technology. But I’m going to try and make it really simple. Let’s take a moving average strategy:

  • Buy when the stock’s 20 Simple Moving Average (SMA) crosses over the 50 SMA.
  • Sell (and go short) when the 20 SMA crosses below the 50 SMA.
  • Use only 100 shares at one time.

Simple enough, we should think. But how has such a strategy performed in the past? Here’s the “eyeball” approach:

LearnTA: BackTesting a Systematic Trading Strategy

Looks interesting. The green arrows are buys and the red arrows, sell. At the point of crossover, after a few whipsaws you see that there are some good trades – a nice short between Feb and May 2013, another short from June to October 2013, a long move from October 2013 to Feb 2014, and then a massive long move from March 2014 onwards.

But this looks good on the screen, how has it actually done over the longer term? We need a better way to test the historical performance of a system. We can use technology – if we just got all the prices, calculated the moving averages, and then did a back-test to see how things performed? And then, what if we used a 15 DMA and 30 DMA instead? Let’s jump in.

But first, let’s get some “infrastructure” ready.

Download and install NinjaTrader

You must download and install a tool that works on Windows, called Ninjatrader. This is free for use at this point, and you will need a windows machine to run it. (I don’t know if they have Mac or Linux versions yet).

Get The Data

We’ll use free data as much as we can. So first, create a connection called Yahoo. This should be under the “Tools” menu, under “Connections”. If it’s not there, you can set it up easily (click here for directions).

Then you will connect to Yahoo, by clicking on File, Connect, Yahoo.

LearnTA: BackTesting a Systematic Trading Strategy

 

Then, let’s get the data of, say, ICICI Bank. UseFile, Utilities, Import Stock Symbol List and type in ICICIBANK.NS in the symbol list.

(The suffix .NS is what Yahoo Finance uses to denote stocks traded on the NSE)

Change the “Traded on” to Nse, and Currency to IndianRupee.LearnTA: BackTesting a Systematic Trading Strategy

I created an Instrument List called “NSE” to store all Indian stocks.

Once you’ve done that, you’ve got the symbol and the data will be automatically downloaded.

Use a “sample” strategy

On the main window, go to File, New, Strategy Analyzer. Choose the stock (ICICIBANK) and click the (b) button on top as this shows:

LearnTA: BackTesting a Systematic Trading Strategy

And then, you’ll see a little popup on the right. This is where we’ll run a strategy on the ICICI Bank stock:

LearnTA: BackTesting a Systematic Trading Strategy

There’s a sample strategy called “Sample MA Crossover” which is a great starting point. What it does is, if the FastMA crosses over the SlowMA, it goes long. If the SlowMA crosses over, it goes short.

You need to also change:

  • Fast to 20, and Slow to 50 (we desire a 20 DMA / 50 DMA crossover)
  • Type to Day. Yahoo has no minute data for Ninjatrader, so it’ll have to be a “day” chart.
  • Timeframe to a longer time – from 2007 to now.

Let’s hit “Run Backtest” and see the result.

LearnTA: BackTesting a Systematic Trading Strategy

On the window we see an grid with a summary. The point is that trades have been tested from 2007 till now.

The idea is that we buy 100 shares each time (that’s the default setting on Ninjatrader)

This means we’ve earned a total of Rs. 51,140 if we followed the strategy over the last seven years.

Most of the profit has been made in long trades – since long trade profit is Rs. 50,050, and on short trades is Rs. 1090.

The profit is decent, at 70%, but the max drawdown is 36%! This means if you invested Rs. 100,000 you could have seen your portfolio dwindle to Rs. 64,000 before turning around. Would you have the stomach for it?

Further, look at the rest of the grid:

LearnTA: BackTesting a Systematic Trading Strategy

Out of all trades, only 42% are profitable.

However, the reason the strategy makes a profit is that the average winning trade makes 15.84%, while the losing trade loses 7.25%. You win not because you win more often than you lose (since your win ratio is less than 50%) but because you win more when you win and lose less when you lose.

In other statistics, the consecutive wins and losses matters because in this case there are six continuous losing trades one after the other. Over a long time, this hurts and we would consider a strategy with more than 20 consecutive losing trades as too difficult to trade. You may have a stronger stomach lining.

LearnTA: BackTesting a Systematic Trading Strategy

And you can see factors like MAE, MFE etc.

  • MAE is Maximum Adverse Excursion which is the worst price that was reached after an entry (average for all trades). We saw an 8.52% average MAE, which mean on average our trades could have seen an 8% loss. A very high figure here can cause irrational behaviour because we tend to panic.
  • MFE is Maximum Favourable Excursion or the best price reached after an entry, the “feel-good” you felt while in the trade.
  • ETD is the End Trade Drawdown. This helps with completeness and in effect tells you how much of your profit you gave up because your exit was delayed. Here our MFE was 17.8% but the average profit per trade is just 2.2% which means we do give up large parts of our gains. (You can attempt to change this by setting a stop loss or such).

These statistics are useful in comparing strategy back-tests.

The “Automated” Chart

Go to the “Chart” tab. Here’s how the chart looks now.

LearnTA: BackTesting a Systematic Trading Strategy

You can visually see all the positions on the chart and the green lines show profitable trades, while the pink lines are loss making ones. You can try to see here if there are too many whipsaws, or trades that just go up and down and cause you losses.

This is much better than the “eyeball” chart we saw earlier, and combined with the statistics, we get  better feel of how our rules performed in the past.

Awesome! Can I just go trade this strategy now?

Wait. Let’s go to the “periods” tab, and use the Annual period to see how this strategy did, year wise:

LearnTA: BackTesting a Systematic Trading Strategy

In many full years, the strategy lost money, and as much as 24% in 2007! In fact a good portion of the profit has happened in 2008 and 2009. We would probably want to use a strategy with less of a loss at least annually!

It’s not very useful to just trade this. There’s a lot to understand and learn before we get into real trading.

What kind of strategy, then?

While this was a start, obviously it’s not good enough. We want to:

  • Find out which parameters make the returns better  (20 DMA or 25 DMA? 50 or 60?), or “optimization”.
  • Handle commissions and slippage (which is the difference between what you will actually get when you trade versus the “close” price we assume in the strategy).
  • A better performance – at least, no negative years and few negative months.
  • Lower maximum drawdown.
  • Test with multiple stocks, and indeed, a portfolio level test to check performance if you diversify.

You can do much of this with NinjaTrader, or with tools like Amibroker or WealthLab (both of which cost money).

Changing the Strategy

How can we change this strategy? If we “edit” the strategy (Tools, Edit NinjaScript, Strategy) then you will see code that looks like this:

        protected override void Initialize()
{
SMA(Fast).Plots.Pen.Color = Color.Orange;
SMA(Slow).Plots.Pen.Color = Color.Green;

Add(SMA(Fast));
Add(SMA(Slow));

CalculateOnBarClose    = true;
}

/// <summary>
/// Called on each bar update event (incoming tick).
/// </summary>
protected override void OnBarUpdate()
{
             if (CrossAbove(SMA(Fast), SMA(Slow), 1))
EnterLong();
else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
EnterShort();

}

The bulk of the strategy is in a few lines of code (marked in bold). The code for Ninjatrader is in C#, and if you’re not a programmer you won’t even understand what to do. We aren’t going to teach you programming or how to change code – that would stretch lessons into years. But if you have the inclination and the time, you can learn how to change this code and retest those changes each time.

Writing strategies is a complex beast or a simple one depending on how well you know programming already. But the important part is to understand how trading works, along with the technology. It’s not enough to know your rules but to codify them there can be no “subjective” rule – you can’t codify something like “I didn’t feel like taking that trade”. We’ll learn about pitfalls and things to avoid as we go along.

You can get a few Ninjascript strategies at their forum, or view a tutorial playlist on YouTube.

We hope this has helped you understand how to build a trading system and back test it. Do try it with different combinations to understand the nuances, and get back to us with your questions.

Did you like this post? Please let us know at premium@capitalmind.in or in our Premium Google Newsgroup. We’ll also do a premium webinar this week or the next to help you through some of the above changes. Please let us know what else you’d like us to cover!

LearnTA: BackTesting a Systematic Trading Strategy

Disclaimer

Nothing in this newsletter is financial advice and should not be construed as such. Please do not take trading decisions based solely on the matter above; if you do, it is entirely at your own risk without any liability to Capital Mind. This is educational or informational matter only, and is provided as an opinion.

Disclosure: The authors at Capital Mind have positions in the market and some of them may support or contradict the material given above, or may involve a direction derived from independent analysis.

Footer

  • rtrsaravanan says:

    I am loving these ones on technical trading. I wish these are more frequent.
    I would personally like to see more detailed articles on fund allocations (how much of your total do u risk at a time), what are the ways of calculating risk, and ways of linking risk to % allocation (or other such factors), a rough way calculating the risk (uni-directonal trades, or combinations with options etc).. big list, yes ;o).
    I am also wishing you’d bring your data product sooner. Obviously you havent told us what its going to look like, but I was hoping it would be one place to access all the data for all the data related articles you have publishing so far in the blog (like the IIP, CPI, WPI, FII inflows etc etc). That might be very useful as well.

  • dineshsidhpura says:

    downloaded the software…its asking for a key!