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

Creating Automated Trading Systems

As a geek and a trader, I’ve always been fascinated by the thought of “automated trading”. The idea is to have algorithms recognise patterns in stock market data, and automatically take on positions when something is favourable. But this is not just random pattern recognition – the whole area of technical analysis is a science that goes back 200 years (or some crazy number, basically it existed before I was born. And before you were born)

So the idea is that you first create a hypothesis. The hypothesis can be something simple (“Stocks go up in April”) to something complex (“A positive slope ADX with DI+ greater than DI-, combined with a reversing slower acceleration parabolic SAR switchover has a positive expectancy”). If you think the second one is a joke, think again – a number of traders use it.

The hypothesis can then be tested using historical data, automatically, by a computer program. The concept is simple – start with some equity at some past date. With the data on that date, would you take the position? If so, allocate the equity appropriately, reducing brokerage and other transaction costs. Also deal with “slippage” – a difference between noted price and what you will actually get (effect of a moving market).

Then go to the next available date, and consider your options again. Would you get out of your current position? (a “stop-loss” or “book-profit”) Would you reverse and short? (for systems that go both ways) Would you take on a fresh position?

All these are trading rules.

Consider also: Position sizing. How much do you allocate per trade? At what point does it make sense to get out on a portfolio level? Say you allocate maximum 2% risk per position – meaning for a 10 lakh portfolio the stop loss per position is Rs. 20,000 regardless of other considerations.

Some of these rules come together as “money management” rules.

The results of a historical back-test will show:

  • Is the system profitable? Obviously you should be net positive over a reasonably long period. But in the interim smaller periods can be net negative – a system that is profitable over a year, could lose money on a monthly basis but have one big month that makes it work. (Such systems should be handled with care)
  • What’s the maximum drawdown? A system will have a drawdown – the highest peak-to-trough difference. This is an indication of the highest percentage you could have lost had you entered at a different point.
  • Number of trades, no. of wins and losses, and average win and loss. Important figures to find out probabilities.
  • Expectancy: This is mathematically (No. of wins)x(average win) – (No. of losses)x(average loss(). If this figure is negative, the system isn’t quite working for you. But this figure is only relevant for a very large number of trades – don’t bother about this below 40-50 trades.

Once we have a back tested system, we have to analyse what works well and what does not. A system with phenomenal profitability – like 500% or something – is unrealistic , so you should be skeptical. Lots of things can make good results, but be a bad system in reality. Firstly, bad data – not all data available is representative of a real world – for example a few weeks ago, NSE reports that Reliance opened at Rs. 3,000 and that was the high of the day. The problem is it could be one trade of 1 share at this price, but if that makes your system look good, you are going to be in trouble when you really implement it. So discard such odd data points.

Second, you can overfit the system to past data. Let’s say the system was “Stocks go up in April”. That didn’t work. SO you said, ok let’s do “Stocks go up in May”. That doesn’t work either. So you add more rules like “April of every year which is an even number, but ignoring April 15, 16 and 17, unless one of them is a weekend, in which case you don’t take April 10, 11 and 12”. This may give you a phenomenal result tested in the past 10 years, but you have an overfit system – it is very unlikely to work in the future.

Once you have a system you like – I will talk about how to choose a system in a different post – you can then “paper trade” it. Meaning, run it and take the next few signals on some “virtual” money, and see if it works over a good period (of say 40-50 trades).

And if both the back test and paper-trade yield good results, you can start putting real money behind the calls. Remember you should continue to backtest as you move forward to see if the market scenario changes results dramatically.

If we were able to identify, back-test and paper trade systems, then can we set up a computer to trade some money automatically? This saves us time and effort to have to keep watching tickers – and instead, focus on building better systems. In theory this is possible but most brokers in India are loath to provide API access to their systems. There could be regulatory hassles to this too.

But my geekiness has prevailed and I think it’s possible to do – create a system, paper trade it and convert it to auto-trade. I have built two such systems, but what I will do is to take a simple system in another post and demonstrate how a system can be built, tested and paper-traded.

This is an exciting field – very old in the U.S. but extremely worthwhile in India to investigate. Even if one avoids technicals, a system based on “news” and “fundamentals” can be build – the code required for it is not dramatically different.

This, both Kaushik and I think, is the future of the stock markets in India. In the U.S. nearly 30% of volumes come from automated systems. It’s time this happens in India – not just will volumes increase, but more discipline will be maintained as computers can be made to follow rules strictly.

Let’s look at this some more in subsequent posts, but tell me – what do you think?

Like our content? Join Capitalmind Premium.

  • Equity, fixed income, macro and personal finance research
  • Model equity and fixed-income portfolios
  • Exclusive apps, tutorials, and member community
Subscribe Now Or start with a free-trial