Assistance Required for coding Pairs Trading AFL

pankajpari

RSITradeMaster
#21
Hi a1b1,
Pls check if you have keyed in the Independent scrip in Parameters as "<'HDFCBANK-I'>". If yes, pls key in the same without the <' and '>, i.e., for example, the Independent scrip should be keyed in as "HDFCBANK-I" without the <' & '> and without the double quotes. Scrip A will be HDFCBANK-I and Scrip B will have to be selected from the Ticker List. Check if you are then able to view the chart.
 

ethan hunt

Well-Known Member
#22
@pankajpari

refer attached picture

first chart: Independent Scrip is ICICIBANK / Depenedent Scrip is SBIN.
AFL recomends: buy LOW (icicibank) priced scrip & sell HIGH (sbin) priced scrip.

second chart: Independent Scrip is SBIN / Depenedent Scrip is ICICIBANK.
AFL recomends: buy HIGH (sbin) priced scrip & sell LOW (icicibank) priced scrip.

The date/time is same for both the charts: 06-03-*2012 ; 15:15

It is a contradictory reccomendation/result.

How is it possible ?????

 

pankajpari

RSITradeMaster
#23
@ethan hunt

The selection of Independent & Dependent scrips cannot be adhoc. The following rule needs to be used while selecting the Independent Scrip :
(i) Independent Scrip is the one with higher Volatility of the 2 scrips.
(ii) But if one scrip is an Index, then the Index will become the Independent Scrip, irrespective of it having a lower volatility as compared to the non-Index scrip.
(iii) Again, if both the scrips of the pair are Indices, then the one with the higher Volatility will be the Independent Scrip.

Further, this AFL is for academic purpose only and was actually meant for checking the trend of the Spread. One cannot use only this AFL to trade pairs. This AFL is just a tool to assist us in Pairs Trading.

If you are really interested in learning pairs trading, I would first recommend you to go thru the following links :
(i) http://www.mudraa.com/trading/139373/0/pair-trading-for-july-2012.html
(ii) http://www.mudraa.com/trading/143077/0/pair-trading-for-august-2012.html
(iii) http://www.mudraa.com/singlepost.php?newp=1&messid=146808

In the last link, you will find an excel based app for pairs trading for the month of September 2012. It automatically fetches the data from the NSE website and depending upon the position of the Current Spread, it generates Buy & Sell signals. It can also be linked to NOW for getting Real Time data.

But before you go ahead with actual trades, would recommend that you first do some paper trades. Also, just to clarify, we are still experimenting with this strategy as there is not much info available on pairs trading on the net.

Though Pairs Trading can make profits irrespective of where the market is going, it has it own risks and hence needs to be traded only after having understood all the risks involved.
 

ethan hunt

Well-Known Member
#24
@ethan hunt

The selection of Independent & Dependent scrips cannot be adhoc. The following rule needs to be used while selecting the Independent Scrip :
(i) Independent Scrip is the one with higher Volatility of the 2 scrips.
(ii) But if one scrip is an Index, then the Index will become the Independent Scrip, irrespective of it having a lower volatility as compared to the non-Index scrip.
(iii) Again, if both the scrips of the pair are Indices, then the one with the higher Volatility will be the Independent Scrip.

Further, this AFL is for academic purpose only and was actually meant for checking the trend of the Spread. One cannot use only this AFL to trade pairs. This AFL is just a tool to assist us in Pairs Trading.

If you are really interested in learning pairs trading, I would first recommend you to go thru the following links :
(i) http://www.mudraa.com/trading/139373/0/pair-trading-for-july-2012.html
(ii) http://www.mudraa.com/trading/143077/0/pair-trading-for-august-2012.html
(iii) http://www.mudraa.com/singlepost.php?newp=1&messid=146808

In the last link, you will find an excel based app for pairs trading for the month of September 2012. It automatically fetches the data from the NSE website and depending upon the position of the Current Spread, it generates Buy & Sell signals. It can also be linked to NOW for getting Real Time data.

But before you go ahead with actual trades, would recommend that you first do some paper trades. Also, just to clarify, we are still experimenting with this strategy as there is not much info available on pairs trading on the net.

Though Pairs Trading can make profits irrespective of where the market is going, it has it own risks and hence needs to be traded only after having understood all the risks involved.
Thanks for the clarification.

Will visit the links.

But can you list the trading rules here ?
 

pankajpari

RSITradeMaster
#25
@ethan hunt, Apologies for the late revert.

1156 hits so far on this Thread.....wow. Didn't know that a lot of guys are interested in Pairs Trading. If thats true, apologies to all.

Pls find below some more information on 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 :
o 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'].

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

o Ratios : One can use Jensen's Alpha Ratio, Sharpe Ratio & Treynor Ratio to identify the expensive and cheaper stock. The stock with higher Jensen Alpha Ratio, higher Sharpe Ratio and higher Treynor Ratio is expensive and hence needs to be sold, while the cheaper stock needs to be bought.
Jensens Alpha Ratio = Return on the stock 1 - (Interest Rate + Stock Beta * (Return on Stock 2 - Interest Rate))
Sharpe Ratio = (Return given by the stock – Risk free Interest rate) / Annual Volatility
Treynor Ratio (Reward to Volatility) = (Return on stock – Risk free Interest) / Beta of the stock.
* I use the Risk Free Interest Rate of 3.50%

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.

Apart from checking the links provided above (Post # 29), I would recommend those interested in Pairs Trading to also visit "http://www.smartfinancein.com/" and register for the free webinars on this topic. They have uploaded on their site some e-books and videos on various strategies including Pairs Trading. (Kindly note that I do not have any commercial interest in recommending Smart Finance. One of my friends recommended me this website and it has immensely helped me.) Further, I would also like to inform that I have read mixed reviews about them and there are a quite a few people who do not find their strategies profitable but it is up to us as to how we can benefit from them. Also, I would like to update that the above mentioned details on Pairs Trading have been taken from a free webinar given by Mr. Soumya Ranjan Panda, the Proprietor of Smart Finance.

One should also visit the following sites for more information on Pairs Trading :
http://www.yats.com/doc/cointegration-en.html
http://blog.echen.me/2011/04/16/what-is-cointegration-2/
https://www.tradestation.com/en/education/labs/analysis-concepts/market-neutral-pairs-trading
http://www.investopedia.com/articles/trading/04/090804.asp#axzz1pa3ppYGV
http://www.tradingmarkets.com/.site/stocks/how_to/articles/-76543.cfm
http://yeforex.blogspot.in/2010/02/pairs-trading.html

Do revert in case of any further clarifications. Shall try my level best to answer the same to the best of my abilities. [I would like to inform that I am no expert on this topic (so far). But the research is on and I am confident that some day (sooner than later), I will be one.]

Hence, would urge the seniors & experts on this topic to kindly share their findings & observations so that every one can benefit from it.
 

pankajpari

RSITradeMaster
#26
I would like to request the seniors / experts in AFL to help me in writing a MACD AFL using the Current Spread of the pair as the base, instead of using the Closing Price of a scrip.

The Current Spread is the Difference between the Closing Prices of any 2 Scrips. Kindly refer the AFL attached in the previous post for more information.

MACD(12,26,9) for Current Spread should use the following :
(i) 12 Day EMA of Current Spread
(ii) 26 Day EMA of Current Spread
(iii) MLCS = MACD Line of Current Spread = Difference between 12 Day & 26 Day EMA
(iv) SLCS = Signal Line of Current Spread = 9 Day EMA of MACD Line of Current Spread
(v) CS-MACD Histogram = MACD Histogram of Current Spread = Difference between MLCS & SLCS

I intend to use the same to identify the trend direction of the Current Spread.
 

MANISH_DAMANI

Well-Known Member
#28
Your spread system is very good specially in lead zinc spread because i have tested it there in real time trading . Showing very good results after using brain. One can make Rs 3-5000 daily with your spread Intraday. Well more days to test to show more confidence. Now my work become easy. Unfortunately I get it too late. But anyways I got it. Many many thanz. Could we convert the spread in candlestcik. So that we could be more comfortable. Anyways nice job.
 

pankajpari

RSITradeMaster
#29
@ Manish Damani, its really heartening to see that someone is benefiting from this system. I don't think it will be right for me to say that this is my system. I have learnt it from other forums & by doing some research on this subject. I have incorporated some changes to this system as per my testing & experience.

As regards to the conversion of the line chart to candlestick chart, though it is possible, it is giving some wrong readings with negative Spread. Though I am not an expert in AmiBroker, I am trying to work on the same. I would like to request the experts if they can assist in doing so.

So far, this thread has not received much interest from the guys on this forum - may be becos this strategy is a quite different as compared to most of the directional strategies. Many feel that why should one take a profit in one scrip and loss in another, thereby reducing the profits. But most fail to understand that though this strategy gives less profits, the losses are also small and more importantly, the profits are more consistent when compared to the directional strategies.

I have tried this strategy only on Stock & Index Futures and it has given good results here as well. I have not tried it on commodities. It will be good if you can share some more information on the trades that you are taking - the amount employed per pair, the no. of trades taken per day, etc. This will give us more insight on this topic.

Thanks once again Manish for you nice words.....it has given me a reason to share more of my learnings and experience. (Though the no. of guys viewing this thread is increasing by the day, I was feeling a little discouraged as not many guys were coming forward with their queries or observations.)

I would hence like to call upon the others to come forward and share their experience and contribute so that all of us can benefit from it.
 

sr114

Well-Known Member
#30
@ Manish Damani, its really heartening to see that someone is benefiting from this system. I don't think it will be right for me to say that this is my system. I have learnt it from other forums & by doing some research on this subject. I have incorporated some changes to this system as per my testing & experience.

As regards to the conversion of the line chart to candlestick chart, though it is possible, it is giving some wrong readings with negative Spread. Though I am not an expert in AmiBroker, I am trying to work on the same. I would like to request the experts if they can assist in doing so.

So far, this thread has not received much interest from the guys on this forum - may be becos this strategy is a quite different as compared to most of the directional strategies. Many feel that why should one take a profit in one scrip and loss in another, thereby reducing the profits. But most fail to understand that though this strategy gives less profits, the losses are also small and more importantly, the profits are more consistent when compared to the directional strategies.

I have tried this strategy only on Stock & Index Futures and it has given good results here as well. I have not tried it on commodities. It will be good if you can share some more information on the trades that you are taking - the amount employed per pair, the no. of trades taken per day, etc. This will give us more insight on this topic.

Thanks once again Manish for you nice words.....it has given me a reason to share more of my learnings and experience. (Though the no. of guys viewing this thread is increasing by the day, I was feeling a little discouraged as not many guys were coming forward with their queries or observations.)

I would hence like to call upon the others to come forward and share their experience and contribute so that all of us can benefit from it.
Pankaj

dont be disheartened by the general apathy / lack of interest in spread/pair trading. there are lot of silent boarders who go thru ur post.

spread trading is a wonderful instrument in commoditie - specially the lead zinc pair - due to their close price resemblence and also the movement.

so it can be used very efficiently in commodities.

lastly a big thanx for ur initiative

regards
sr
 

Similar threads