Tuesday, June 30, 2015

Back-Testing Systematic Strategies Part 1: A Flow Chart Based Approach

Systematic trading brings on a certain discipline and the advantage in terms of risk management that comes along with it. And with today's cheap computing power, it is available to anyone. A proper back-testing is almost always at the core of a systematic trading strategy. A wisely designed back-test is the litmus test for an investment hypothesis. At the same time, it helps in sizing the trades and risk management given a particular ability and appetite to take on risks.

However, for the individual traders and investors who has developed market insights over the years, but not very familiar or eager or able to get their hands dirty with coding, running a back-test with dependable results can be a huge headache. Here I try to show how we can approach this problem in a structured manner.

The very essence of back-testing is defining some rules of trading (buy or sell signals) and risk management (stop loss or take profit etc.) based on a market price (of the instrument we are trading) and probably some more indicators (which can be as simple as a technical indicator, or a complex function defined by the user). The R package quantstrat handles this problem in a very well defined manner and split the functionalities in different blocks or modules so that designing a back test, running the strategy and analyzing the data all becomes simple and modular. We take a simple example of running a intraday momentum strategy on the National Stock Exchange flagship index NIFTY50 to highlight this point.

The core strategy we want to back-test is simple. Every trading day we wait for a certain amount of time (or price bars) after the market opens to see the initial momentum. If the open is positive (negative) beyond certain threshold, we then go long (short). Before the end of the trading day we square-off the position. Optionally we can also add a stop loss and take profit target. 

Thinking backwards, the back-test will be driven by our buy and sell trading rules and square-off rule. In turn they will be generated based on:

a) if the opening price criterion meets the condition and 
b) if we are at the beginning of the day, and finally 
c) if we are approaching end of day to place square off. 

This in turn means, the signals that need to go in is: 

i) a price move indicator for positive move
ii) a price move indicator for negative move
iii) a start-of-day indicator and 
iv) an end-of-day indicator

We would need i) and iii) both to be true (logical AND) for a buy signal, and ii) and iii) for a sell signal. The indicator iv) is enough to drive square-off. Schematically, the entire scheme looks below


And if we can design this schematic logically, in terms of a simple, easy to understand flow chart as above, it becomes rather straight forward to develop the underlying codes to run the strategies. As an example I ran the strategy for 2-minute bar of intraday data since 19th of May to 26th of June, and here are the results with different combinations (with 100 units for buying or selling for each trade).



For the underlying strategy without any stop-loss and take profit targets, the trade positions and evolution of profit and loss looks as below (click to enlarge).


And once we run the back-test, we can further delve in to the details and analytics, like the chart below which plot the Maximum Adverse Excursion plot for the base strategy
In short, a very simple strategy, quickly designed in to a simple flow charts. The back test shows it really works, and also throws lights on how to size the trades given your investible capital and risk tolerance.

We will further follow up on how to translate any back-testing in to a similar flow chart, which enables quick and reliable back-testing.

(Those who are interested in the underlying code, it is available here.)

1 comment:

  1. You can find more flowchart templates in creately diagram community. There are 1000s examples and templates to be used freely.

    ReplyDelete