Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

travi

Well-Known Member
Hi @ncube

I browsed through a few files and found this one very interesting

pydata12_zipline_twiecki.ipynb on GitHub
https://gist.github.com/twiecki/3962843#file-pydata12_zipline_twiecki-ipynb

anything useful that can be picked up from it ?

Has some good visuals with B/S signals etc, although its inclined to swing/positional rather than intraday but thought to share with others.
& ofc more learning curve for py :)
 

ncube

Well-Known Member
Hi @ncube

I browsed through a few files and found this one very interesting

pydata12_zipline_twiecki.ipynb on GitHub
https://gist.github.com/twiecki/3962843#file-pydata12_zipline_twiecki-ipynb

anything useful that can be picked up from it ?

Has some good visuals with B/S signals etc, although its inclined to swing/positional rather than intraday but thought to share with others.
& ofc more learning curve for py :)
@travi, In this jupyter notebook (Previously it was called ipython notebook), the author has demonstrated how it can be used to easily backtest dual moving average & pair trading strategies. He has used Zipline which is a robust python based open sourced backtesting library. Zipline is used and maintained by quantopian and its the core of their platform and very popular.

Initially I had tried to use it for backtesting, but at that time it was optimized for US markets and the documentation was not good and difficult to troubleshoot and hence I had to settle for "Backtrader". However these days quantopian is focusing on Indian quant talents, so high chances that they may provide good documentations and trainings targetted for Indian trading community.
 

ncube

Well-Known Member
@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..:)
HINDZINC - NMDC is a good pair, One can track this pair as it is respecting the co-integration beautifully....NMDC short is in good profit today..mean reverted in just 2 days...:)

Screenshot_2018-08-09-11-56-04-308_com.zerodha.kite.png
Screenshot_2018-08-09-11-59-50-597_com.zerodha.kite.png
 
Last edited:
@ncube,
Thanks for your help in installing python.
I have installed python, it's working as expected. But facing problem in auto updating stockdata.csv from eod.txt.
Downloaded bhavcopy.csv file, saved as eod.txt. When trying to run the program to update, getting an error "ValueError: Usecols do not match columns, columns expected but not found: [5]"
Please have a look at the images and suggest a solution.
Thank you.
ss1.jpg
ss2.jpg
 

ncube

Well-Known Member
@ncube,
Thanks for your help in installing python.
I have installed python, it's working as expected. But facing problem in auto updating stockdata.csv from eod.txt.
Downloaded bhavcopy.csv file, saved as eod.txt. When trying to run the program to update, getting an error "ValueError: Usecols do not match columns, columns expected but not found: [5]"
Please have a look at the images and suggest a solution.
Thank you. View attachment 27297 View attachment 27298
@Vevensa_P , I use the free tool https://sourceforge.net/projects/nse-eod/ to download eod bhavcopy from NSE. You can use it to download the eod bhavcopy which is in text format. Just rename the file to eod.txt before running the cell in the notebook.

If you use any other methods to download the file, then you may need to change the processing logic in the function update_eod(). Can you share the content of the bhavcopy you have downloaded, I think it should work.
 
Last edited:
@Vevensa_P , I use the free tool https://sourceforge.net/projects/nse-eod/ to download eod bhavcopy from NSE. You can use it to download the eod bhavcopy which is in text format. Just rename the file to eod.txt before running the cell in the notebook.

If you use any other methods to download the file, then you may need to change the processing logic in the function update_eod(). Can you share the content of the bhavcopy you have downloaded, I think it should work.
Thank you @ncube . Got it. Problem solved.
 

nurav

Well-Known Member
http://prntscr.com/kgror7 Ncube Sir today this pair gave close to 20 points per pair in the ratio of 1long Hindzinc for every 2.5 shares short of NMDC. So for 100:250 shares profits of Rs.2000 and 500:1250 profits of Rs.10000. Only paper trade. Is it right ?
 

VJAY

Well-Known Member
Dear ncube,
Why am not getting play button in this scrip?

1533864822278.png
 

VJAY

Well-Known Member
why this pair chart looking ubnormal?

1533865119110.png
 

ncube

Well-Known Member
Dear ncube,
Why am not getting play button in this scrip?

View attachment 27324
@VJAY, Is this ok now?

why this pair chart looking ubnormal?

View attachment 27325
The prices of HDFCBANK & ICICIBANK are not at same scale, HDFC is about 7 times that of ICICI, hence while plotting this pair change the resolution ratio to 7:1 as follows: xres=700, yres=100, what this does is while plotting SS1 value is divided by xres=700 and SS2 value by yres=100

So that they both are in the same scale as the zScore.

plot_pairs(df,SS1,SS2,lb=20,xres=700,yres=100)

You can change these values and make the required adjustment for the pairs.