Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

VJAY

Well-Known Member
"ncube, post: 1298753, member: 266449"]@nurav, based on the excitement you have shown in calculating the profit and loss, I can easily presume that you are new to trading. I dont want to discourage you, but if it can help you avoid blowing up couple of trading accounts I will be happy and you will be thankful to me later..:)

For all new traders it is very important to understand the following points:
1. There is no easy money in trading.
2. Rewards are directly proportional to Risk.
3. If any strategy seems too good, its unsually isnt.
4. If someone says he has a strategy which gives high rewards with low risk, run away from him as far as possible it is clear that the person has not traded the strategy enough.
5. Success in trading requires hard-work, discipline,confidence and patience to stick to 1 or atmost 2 strategies and not hopping systems. A given strategy may not work in all situations but if you keep hopping systems then you can never master any strategy.

Now coming to the Banknifty Option spread trade example you have mentioned, it is not a low risk strategy, in this you are buying out of money call options and sponsoring it with ATM/ITM call options.There is no clear rationale for doing this, one reason to take this trade could be that one is super bullish on BANKNIFTY as this trade will start making profit only when the price moves above 27900, if the price stays below 27500, then you can lose a max amt of 200-Credit from Short 27500 + 4 * debit from Long 27700. There are much better option strategies one can apply to trade bullish opinions, even a simple ATM call option buy trade can give you a better returns. Trading option are more complex than trading stocks, hence always try to plot the strategy pay-off diagram first to understand the risks before placing the option trades.

Very well said ncube brother...its reality of trading...if not traded a method for long time one not get consistent in trading....also if one switching from one method to another its sure he never succed in trading
 

UberMachine

Well-Known Member
I have modified the initial pairs trading strategy
  1. Calculate ratio instead of price differential (you can use price differential also)
  2. Calculate ratio on the open prices instead of the close price so that you could factor in any pre-open bias
  3. z-score calculated on 10 day rolling mean and standard deviation (again, this window could be changed)
  4. co-integration based on last 6 months
I haven't adjusted for dividends
I am uploading the sample Excel sheet for a single pair so that this could be used for reference purpose (I have included pairs-closing and BPCL, HINDPETRO prices also)

Would develop a script to automatically update these prices
Would start with NIFTY BANK
Interesting to see how stocks with no co-integration behaves in intra-day (I expect a significant difference in returns between co-integrated and non co-integrated stocks)
 

Attachments

ncube

Well-Known Member
@VJAY and others who are using my PairTrading.ipynb Jupyter notebook for pair trading, please note there was an error in the python script due to which the zscore was not correctly calculated. Thanks to @UberMachine post in which he suggested using ratio instead of spread which I also felt is a better option for day trading. While analyzing the script for making the changes I realized that I have used the returns data frame instead of the price data frame to calculate the zScore. This would have led to incorrect zScore calculations. My apologies.

I have updated the jupyter notebook and have uploaded the latest script to my google drive. Please use this notebook for your pair analysis.

https://drive.google.com/open?id=1a40Ih__otr99E1TAQe7WEjsTEsiBDKQQ

Changes:
1. Need to set SS1, SS2 values to the stocks price dataframe before calling the plot_pairs function:
2. Use -1 or +1 SD for your zscore threshold as that is sufficient for day trading the pairs.
3. Using the price ratio instead of the spread.
1533663518756.png
 
Last edited:

VJAY

Well-Known Member
@VJAY and others who are using my PairTrading.ipynb Jupyter notebook for pair trading, please note there was an error in the python script due to which the zscore was not correctly calculated. Thanks to @UberMachine post in which he suggested using ratio instead of spread which I also felt is a better option for day trading. While analyzing the script for making the changes I realized that I have used the returns data frame instead of the price data frame to calculate the zScore. This would have led to incorrect zScore calculations. My apologies.

I have updated the jupyter notebook and have uploaded the latest script to my google drive. Please use this notebook for your pair analysis.

https://drive.google.com/open?id=1a40Ih__otr99E1TAQe7WEjsTEsiBDKQQ

Changes:
1. Need to set SS1, SS2 values to the stocks price dataframe before calling the plot_pairs function:
2. Use -1 or +1 SD for your zscore threshold as that is sufficient for day trading the pairs.
3. Using the price ratio instead of the spread.
View attachment 27249
Dear ncube,
After I uploaded newscript am getting errors in every cells :(
Untitled.png
 
Last edited:

VJAY

Well-Known Member
I have modified the initial pairs trading strategy
  1. Calculate ratio instead of price differential (you can use price differential also)
  2. Calculate ratio on the open prices instead of the close price so that you could factor in any pre-open bias
  3. z-score calculated on 10 day rolling mean and standard deviation (again, this window could be changed)
  4. co-integration based on last 6 months
I haven't adjusted for dividends
I am uploading the sample Excel sheet for a single pair so that this could be used for reference purpose (I have included pairs-closing and BPCL, HINDPETRO prices also)

Would develop a script to automatically update these prices
Would start with NIFTY BANK
Interesting to see how stocks with no co-integration behaves in intra-day (I expect a significant difference in returns between co-integrated and non co-integrated stocks)
Dear UB,
are this file can use manually find pair trades if I want to trade with one or two pairs?
 

ncube

Well-Known Member
Dear ncube,
After I uploaded newscript am getting errors in every cells :(
View attachment 27251
@VJAY , first time you need to run all the cells from the beginning...

Today I plan to test a new pair Buy-HINDZINC-Sell-NMDC I will try to post the trade in real time. Just observe do not trade as I have not tested this pair earlier.

HINDZINC -> SL BUY 288.60 --> Cancelled as trade not triggered.
NMDC -> SL SELL 111.85 --> Executed --> SL BUY 115.10 --> Closed at 113.60 --> Loss 395

Analysis: Trades as per rules, however NMDC did not fall much to take the trade into profit. Cost of doing business..:)
 
Last edited:

UberMachine

Well-Known Member
Dear UB,
are this file can use manually find pair trades if I want to trade with one or two pairs?
Yes, but since this is based on open price you need to manually update it in the morning to enter into a position.
And you need to do it for every pair (you must need atleast 20 pairs to trade intraday, in my opinion)
Also, this sheet doesn't find pairs. It works if the pairs are already found and would work as a cross check for real-time trades
 

VJAY

Well-Known Member
Dear ncube,
What modification I need to do for getting SD +2 &-2 instead of SD +1&-1 in this new scrip?Please post the code for it....
Thanks
 

ncube

Well-Known Member
Dear ncube,
What modification I need to do for getting SD +2 &-2 instead of SD +1&-1 in this new scrip?Please post the code for it....
Thanks
@VJAY, its just line drawn on the plot, you can change the values in the function cell:

plt.axhline(1.0, color='red', linestyle='--');
plt.axhline(-1.0, color='green', linestyle='--');

change this to

plt.axhline(2.0, color='red', linestyle='--');
plt.axhline(-2.0, color='green', linestyle='--');

Based on the value you give there, the red and green lines will be drawn at that point in the plot. (You can give any value, say 2.5/-2.5 etc), Adding both statement in the function will plot 4 lines (1/-1/2/-2).

Note: after changing this value you need to run the function cell again so that the changes are registered.