- Wealth PMS (50L+)
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:
Simple enough, we should think. But how has such a strategy performed in the past? Here’s the “eyeball” approach:
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.
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).
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.
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.
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.
On the main window, go to File, New, Strategy Analyzer. Choose the stock (ICICIBANK) and click the (b) button on top as this shows:
And then, you’ll see a little popup on the right. This is where we’ll run a strategy on the ICICI Bank stock:
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:
Let’s hit “Run Backtest” and see the result.
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:
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.
And you can see factors like MAE, MFE etc.
These statistics are useful in comparing strategy back-tests.
Go to the “Chart” tab. Here’s how the chart looks now.
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.
Wait. Let’s go to the “periods” tab, and use the Annual period to see how this strategy did, year wise:
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.
While this was a start, obviously it’s not good enough. We want to:
You can do much of this with NinjaTrader, or with tools like Amibroker or WealthLab (both of which cost money).
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;
CalculateOnBarClose = true;
/// Called on each bar update event (incoming tick).
protected override void OnBarUpdate()
if (CrossAbove(SMA(Fast), SMA(Slow), 1))
else if (CrossBelow(SMA(Fast), SMA(Slow), 1))
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.
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 email@example.com 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!
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.