Tuesday, September 3, 2013

AmiBroker AFL for Pairs Trading



• Pairs Trading is a process of buying the cheaper stock and selling the costly stock simultaneously in the Futures segment to benefit from all kinds of market condition.

• When two correlated stocks are chosen for the trade with market neutral approach then the trading method is known as Pairs Trading. Example: HDFC Bank and Axis Bank both in the private banking sector and have close similarity in the business model.

• How to choose pair of stocks for trading?


o Both the stocks must be from the same industry.
o Stocks must be a component of a sectorial index.
o Stocks must have a close correlation in business model and business structure.


o Technical Parameters :


 The volatility in annual terms for the pair must not defer by 50%, i.e., if the annual volatility of one stock is 50%, then the other stock should have an annual volatility of more than 25% & less than 75%. A plus / minus 4-5% in the annual volatility of other stock is considered ok.
 The Annual Volatility of both the scrips should be more than 20%.
 The high volatile stock (as per the % Annual Volatility) will be the dominating partner in the pair, i.e., it becomes the Independent Stock (X), while the other becomes the Dependent Stock (Y). But if the pair has one Index and one Stock, then the Index becomes Independent, while the Stock becomes Dependent.
 The correlation coefficient of the pair must exist either in the +ve or –ve zone
 The spread difference between the stock must show some correction

• Important Statistical parameters for pairs trade :


Correlation Coefficient :

 This statistical parameter gives a value between -1 to +1.
 A Positive correlation means both the stocks in the pair will move in the same direction, while a Negative correlation means both the stocks will move in the opposite direction.
 If the correlation is '0' means both are moving randomly.
 An ideal correlation is +/- 0.6018.
 For Positive Correlation, the recommendation to buy & sell should be followed as is. But in case of Negative Correlation, the recommendation received should be reversed and then traded. This means that if the recommendation received is to [Buy Stock 'A' and Sell Stock 'B'] and in case of Pairs with Negative Correlation, one should do the opposite, i.e, [Buy Stock 'B' and Sell Stock 'A'].

Hedging Using Options : It is always a good practice to hedge the pairs using Options as Pairs can sometimes lead to huge losses if not managed properly. Though this will reduce the profits, but it will also ensure that the losses are minimised.

• Trade Initiation Process : A trade will be initiated if one of the following criteria is met,
o If the Current Spread is above or below 2% of the Mean Spread. (The Current Spread is the price difference between the two stocks.)
o If the correlation changes from + ve to – ve or vice versa.
o If the correlation increases or decreases by 50% from its previous recorded data.

These are the basics of Pairs Trading. Those who are really interested will need to do a lot of research on it and experiment with Paper Trading before actually going ahead with real trades.


Pairs Trading Background :

1. Pairs Trading is a process of buying the cheaper stock and selling the costly stock simultaneously in the futures segment to benefit from all kinds of market condition.
2. When two correlated stocks are chosen for the trade with market neutral approach then the trading method known as Pairs Trading. Example : ICICI Bank and HDFC Bank both in the private banking sector and have close similarity in the business model.
3. In Pairs Trading, the 2 stocks can be traded in 2 following ways :
a) Mean Reverting Stocks : If the 2 stocks are highly correlated & cointegrated, however far the Spread (between the 2 stocks) moves away (increases) from the Mean Spread, at some point in time it will start moving back towards its Mean. In such pairs, a trade can be entered into whenever the Spread moves (2% or more) away from the Mean and then squared off, when the Spread moves back towards its Mean.
b) Directional Strategies : Pairs that are not Mean Reverting can be traded similar to trading any individual scrip, with the only difference being that the Spread between the two stocks is to be used for trading. We can use Moving Average, MACD, RSI, Pivot Points, Fibonacci, etc., on the Spread.
4. A Spread is the Difference between 2 Stocks (OR 1 Index & 1 Stock OR 2 Indices). This difference should not be an Absolute number, i.e., the differential can be Positive as well as Negative. The advantage of trading Spreads is that it is not as Volatile as the individual scrips. (But this does not mean that there won’t be any losses……if not traded & managed properly, this strategy can lead to huge losses.)
5. Current Spread is the Difference between Last Traded Price (LTP) or Current Market Price (CMP) of the 2 stocks.
6. Mean Spread is the Average Spread [Sum of Spread divided by No. of Observations]
7. Upper Spread is Average Spread multiplied by 102% [Average Spread + (Average Spread x 2%)]
8. Lower Spread is Average Spread multiplied by 98% [Average Spread - (Average Spread x 2%)]
9. Maximum Spread is the Maximum Difference between the 2 stocks over a period of time, say 1 month, 3 months, 1 year, etc.
10. Minimum Spread is the Minimum Difference between the 2 stocks over a period of time, say 1 month, 3 months, 1 year, etc.

Trade Initiation Criteria :-
 (we will take the price of SBI - Rs. 1920/-, & ICICI Bank - Rs. 975/-, into consideration as an example)


1. Current Spread > Upper Spread : When the Current Spread trades above the Upper Spread, the following signal should be generated,
Buy Inexpensive Stock and Sell Expensive Stock (Example – Buy ICICI Bank and Sell SBI)
2. Current Spread < Lower Spread : When the Current Spread trades below the Lower Spread, the following signal should be generated,
Buy Expensive Stock and Sell Inexpensive Stock (Example – Buy SBI and Sell ICICI Bank)

AmiBroker Requirements :


I would like to have the following requirements in AmiBroker,
A. The Chart Style should be a Line chart.
B. The AFL should ask the user to select the 2 required scrips.
C. The various Spreads (Lines) should be differently coloured - Current Spread (Black), Mean Spread (Blue), Upper Spread (Brown), Lower Spread (Teal), Maximum Spread (Purple) & Minimum Spread (Magenta / Fuchsia).
D. If the Current Spread is going up, it should be coloured Green and if it is going down, Red.
E. Maximum & Minimum Spread period should be 1 calendar month.
F. An Arrow should be displayed above / below the Current Spread whenever the Buy & Sell signal is generated.
G. The current values of the various Spreads should be displayed on the Right hand side of the X-Axis (Price Margin).
H. A Box should be displayed in one corner of the chart and should include the following,
(i) Buy & Sell Signal as per the position of the Current Spread vis-ร -vis the Upper & Lower Spread.
(ii) 3 Year, 1 Year, 30 Day & 5 Day Correlation Coefficients of the 2 scrips.
(iii) Profit or Loss of Individual scrips (Difference of Buy / Sell Price and the Last Traded Price)
(iv) Total Profit or Loss (Net Profit / Loss of both the scrips)



Have changed the following :

(i)         I have replaced ‘Independent’ & ‘Dependent’ scrips (which were based of the Annual Volatility of the scrips) to ‘High Priced Scrip’ & ‘Low Priced Scrip’. This is being done to simplify the AFL as many may not know how to find which scrip of the 2 scrips is more volatile. This will also take care of the ‘contradictory recommendations’ as there will be no confusion in selecting the ‘High Priced’ scrip (as compared to selecting the more volatile scrip).
(ii)         I have changed the method to calculate the Upper & Lower Spread. It is now based on +/- 2% of the Mean Spread. This will reduce the number of recommendations received as the distance between the Mean Spread and Upper / Lower Spread will increase. With this method, one will notice that in most of the pairs, 95% of Current Spread is enveloped by the Upper & Lower Spread and hence can be used similar to Bollinger Bands.
(iii)        The variables can now be changed using the Parameters dialog box.
(iv)        The Recommendation will now display the name of the scrips instead of just stating “Buy High Priced Scrip & Sell Low Priced Scrip” or “Buy Low Priced Scrip & Sell High Priced Scrip”.

Now you have a complete suite of Tools to Trade Pairs :-
(i) Primary AFL for identifying opportunities in Pairs Trading :
http://www.4shared.com/office/rAPfcV-u/pankaj_parimal_-_pairs_trade__.html
(ii) Trend Following AFL (MACD) for identifying the trend of Spread :
http://www.4shared.com/office/R1vfP3Xz/Pankaj_Parimal_-_MACD__Spread_.html
(iii) Oscillator AFL (RSI 8 & 21) for identifying Over Bought & Over Sold markets :
http://www.4shared.com/office/JCr6ge1G/Pankaj_Parimal_-_RSI__Spread_.html

Link : https://docs.google.com/file/d/0B1y9IhKnJKiPSFNGVTlRaXFjQ1k/edit?usp=sharing

Link : https://docs.google.com/file/d/0B1y9IhKnJKiPYlp4RGpqVXItMFU/edit?usp=sharing

Link : https://docs.google.com/file/d/0B1y9IhKnJKiPYXNLOEVPSlJoWDQ/edit?usp=sharing

Link : https://docs.google.com/file/d/0B1y9IhKnJKiPRHJTaWYtOW8weVk/edit?usp=sharing



2 comments:

  1. Hello and Congratulations,
    i have found very interesting information here.
    How could i access to the link?
    Thank You.

    ReplyDelete
  2. Hello and congratulations,
    You have a very nice blog and i have found very interesting information.
    How could i access to the links?
    Thank You.

    ReplyDelete

Powered By Blogger And Premium Template By Lord HTML