START WITH BACKTESTING AN ALGO TRADING STRATEGY

START WITH BACKTESTING AN ALGO TRADING STRATEGY

START WITH BACKTESTING AN ALGO TRADING STRATEGY






If you are into trading, you would’ve heard of the term “ backtesting” at least once. As the name suggests backtesting is basically testing something as to how it would have performed if the decision was taken back in time. In terms of trading, it is testing the performance of any strategy through the historical time series data of the asset. 


Let us take a look at one example: 

Suppose I want to see if I would have applied the Moving Average Crossover Strategy on any security, would I have been in a loss or profit? How would you decide?

We answer it by going back in time using the historical data and take decisions based on it.

Let us take a very simple example of the application of a strategy, which will help us understand the backtesting process better. In this strategy, we have used the Exponential Moving Average technical indicator for the strategy.


 

Exponential Moving Average (EMA)

Exponential Moving Average (EMA) is used to estimate the trend direction during a particular time period. EMA is sensitive to price changes but EMA can help identify the trends earlier.

The formula for EMA is:


Constant Multiplier = (2 / (Time periods (look back period) + 1) )

EMA: {Close - EMA(previous day)} x Constant Multiplier + EMA(previous day).


For example if we take a look back period of 10 then the constant multiplier would be 2/(10+1) i.e. 0.1818, and for the first EMA calculation we take SMA instead of EMA as the EMA of the previous day which if you see in the sheet that is attached in row I, the Closing Price is 514.75 and the EMA of the previous day is 514.885 which in this case is the SMA, the EMA is calculated as  ( 514.75 - 514.885)*0.1818 + 514.885 which is equal to 514.860.



The strategy:


We will take an EMA of period 10 on a 15 minutes data on BHARTI-AIRTEL Futures Contract, and whenever the closing price of that particular candle (period) crosses the EMA from below to above at that particular period i.e. Closing Price is less than the EMA in the previous period and Closing Price becomes greater than the EMA in the current period, then a buy signal will be generated which you can see in the below figure.


And whenever the Closing Price of that particular candle (period) crosses the EMA from above to below at that particular period i.e. Closing Price is greater than the EMA in the previous period and Closing Price becomes less than the EMA in the current period, then a “sell” signal will be generated as shown below.



There is another case in which if the security is already in the long position and then a Short/Sell signal is generated, then in that case the long position will be squared off first, and then the short position will be taken and vice versa.


Performance Parameters


There are several parameters through which we can know about the performance of the strategy some of them are Sharpe Ratio, Return on Investment (ROI), Sortino Ratio, Maximum Drawdown, etc. In this blog, we will evaluate the strategy performance through ROI only.


How to backtest it?


For backtesting we will download the historical data with the help of Historical Data Open APIs that are provided by IIFL Securities at no extra cost (visit https://api.iiflsecurities.com/Historical-Candle-Data.html for more information). I have taken 10 days of 15 minutes data for BHARTI-AIRTEL , and according to my strategy I will buy 1 lot whenever I get a buy signal and sell 1 lot if I get a sell signal and in case if I have already bought 1 lot then we will square off the position first and then sell 1 lot & vice-versa. The datasheet attached shows the calculations of Mark to Market Calculations & the net return in 10 days in the strategy. In this case, the initial amount that I invested was 10,00,000 and the final profit amount that I received i.e. the  total return that we can see  in the excel sheet is 34,325.2

therefore,

Return on Investment (ROI) = (34,325.2/10,00,000)*100

I.e. ROI = 3.43%

Similarly several other parameters can be calculated which will further explain the usefulness of any strategy will be explained in the further blogs.



Stay tuned every Friday for more information on Algo-Trading & the Revolution that APIs are bringing in modern day trading.


    • Sticky Posts

    • Demystifying Open APIs

      Trading industry has evolved from “I need to call the dealer to trade” to “I can trade by simple click on my mobile”. After the literacy rate and computer literacy wave, India is facing a financial literacy wave for the last 3-4 years. Automation has
    • Find Trading Opportunities using Technical Analysis

      What is Technical Analysis? There are various ways to understand the market, whether it be through intuition, fundamental or technical analysis. Like others, technical analysis is also used to predict the course of prices of a security according to the
    • DECODING ALGORITHMIC TRADING

      Trading in India Overview(Paradigm shift in the recent years)   India has seen an enormous change in people's interest towards trading in recent years. There has been a huge shift from traditional ways of saving money in the form of bank deposits, gold