Showing posts with label Intraday data. Show all posts
Showing posts with label Intraday data. Show all posts

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.)

Sunday, December 21, 2014

NIFTY: Day traders Vs. Investors (+ A Christmas Present !!)

Here are some interesting charts comparing how S&P CNX Nifty has performed over last many years - split between day-session performance vs. overnight. The pattern is very interesting. In 2007-2008, the day traders dominated, both in profits and in losses. Be it the run up to the pre-crisis top in early 2008, or the crash. It was again the day traders who profited most in the comeback in 2009. This continued till the peak in 2010. 

However, after that, something changed. 2010 was the last great year for the day traders. Since 2011, the overnight returns dominated returns during the day session, far and steady. That was the case during the mild bearish runs in 2011, the sideways market in 2012. And the trend continues strongly in to the current bull period. 

The overnight returns now dominates day session so much that if this continues, going long overnight and shorting the markets during the day is now a super profitable strategy !!



What is driving this? Well to start with: the vols are down, and NIFTY (like most emerging markets) is perhaps influenced by the Feds and the BoJ much more than it used to be back in 2007. I would suspect most emerging markets will show very similar patterns. And this is VERY different than, say , S&P 500, where overnight and day-session has their fare share of misery and joy.

Will this continue? Well, the flow of funds that world-wide QEs initiated is still churning around, and will perhaps take a long time before the dust settles down. But it is an altogether different scenario if we enter a high vol regime in 2015, irrespective of market direction.

The tail piece: for folks looking for public source of intraday data on stocks - here is a quick and dirty R scripts. Feel free to use and modify as you please. Quantmod of course does a wonderful job for daily data. This routines are similar and extend to intraday.


. Merry Christmas and happy holidays everyone!