Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

VJAY

Well-Known Member
Dear ncube,
Is any chance to get these figures(marked red) from notebook?

Untitled.png
 

VJAY

Well-Known Member
I just ran a python script. But I didn't checked it. That's why I asked to cross check correlation in Excel before proceeding further.
I cannot upload the notebook here (this site doesn't allow the extension I believe)
But this is the code

Python:
import pandas as pd
df = pd.read_csv('stock_data.csv').iloc[:,1:]
from itertools import combinations
combin = combinations(df.columns, 2)
corr_map = df.corr().stack()
result = {x:corr_map[x] for x in combin}
pd.Series(result).sort_values().to_csv('pairs.csv')
You can copy paste it and run it a notebook.
The stock_data.csv file should be in the same folder where the notebook is
Dear ubermachine,
Need to paste in same notebook where allready pairtrading code have? Is it effects that orginal code?
 

VJAY

Well-Known Member
Do it in a separate notebook.
Its always safe
Please give the steps to make new notebook ...You are good command in Python....please start to teach us basic coding help and doing it in notebook in your thread when ever time permits you ...Python is new thing in traderji.....
 

20.sum1

Active Member
I just ran a python script. But I didn't checked it. That's why I asked to cross check correlation in Excel before proceeding further.
I cannot upload the notebook here (this site doesn't allow the extension I believe)
But this is the code

Python:
import pandas as pd
df = pd.read_csv('stock_data.csv').iloc[:,1:]
from itertools import combinations
combin = combinations(df.columns, 2)
corr_map = df.corr().stack()
result = {x:corr_map[x] for x in combin}
pd.Series(result).sort_values().to_csv('pairs.csv')
You can copy paste it and run it a notebook.
The stock_data.csv file should be in the same folder where the notebook is
Beautiful concise and powerful code
 

VJAY

Well-Known Member
Do it in a separate notebook.
Its always safe
My scrips file is in C drive...where Python installed ...in python folder where can I find notebook folder ?

Untitled.png
 

ncube

Well-Known Member
Dear ncube,
Need some clarity ...
1.When we enter trade using 30 min bar after trigger are we need to put SL @ abv/blw 30 bar?
2.So if 30 bar is big one and we expect move in our side we wait for next 15 bar and trade as per that bar ...here we use triggeres in 15 bar for both scrips?
3.If 30 bar close near lows/highs(big bar) we not jump into trade wait for 15 min bar
4.Please explain with example for bolded risk part...how can we calculate risk?
Thanks
@VJAY, the strategy rules I mentioned are the rules that I follow and comfortable with. You need not have to follow the exact rules, each trader has his own personality and the strategy rules should be in sync with his skills and psychological growth stage. I suggest you can select few pairs and start observing it and I am sure after few days you will come up with your own rules which will match with your personality. Day trading is more of skill and our emotional maturity to trade systematically. This will come only with experience and sufficient screen time there is no free lunch.

Coming to answer your queries on the strategy I follow, the details are as follows:

I track only a few of the eligible pairs regularly, Based on my observation,I have found that the stocks in those pairs on average moves about 3-5% each day, and when the zscore moves above +/-2 SD, the action starts and the pairs start to mean revert, usually 1 stock move more than the other, But it is difficult to predict which direction they will move. Hence I place opposite trades on the 2 stocks of the pair so that one of them get triggered. I dont force trade, once the trade is executed I manage the trade as follows:

1.When we enter trade using 30 min bar after trigger are we need to put SL @ abv/blw 30 bar?
>> If our trade goes through as per our analysis, only one of the trades is executed first and both stocks should move in the same direction. This give us a hint on the direction for the day. However if both are not moving in same direction and see that the trend is exhausted and reversing (Based on the price action of subsequent candles) I will wait to see if price pull back more than 50% of the 1st 30 min bar in opposite direction. This means there is some change in sentiment, hence higher chances that the price move will be stong in the reverse direction. In this case I will place a SL reverse order (Double the quantity of the stock traded) at the low of the 1st 30 min candle. You can refer to my ALBK-ANDHRABANK trade example that I shared in earlier post.
2.So if 30 bar is big one and we expect move in our side we wait for next 15 bar and trade as per that bar ...here we use triggeres in 15 bar for both scrips?
>> If the 30 min bar is nearer to the average daily movement of the stock, then I will skip trading it as there is no point trading it as risk-reward ratio will be small. However I sometimes do try to observe if the trend has exhausted. For example say both the stocks have moved up more than 3% in the 1st bar, then I will focus on the stock to be shorted on the 15 min candle. You can refer my TATAMTRDRV-TATAMOTORS trade shared earlier.
3.If 30 bar close near lows/highs(big bar) we not jump into trade wait for 15 min bar.
>> If the close is very near to the low of the 1st 30 min bar then, I will wait for a small pullback and then place the SL trade order. Else there is high chance that after hitting our SL the trend is reversed. I dont chase the price, I want it to come to me at the price that I choose and not the other way around.
4.Please explain with example for bolded risk part...how can we calculate risk?
>> Lets say the 1st 30 min candles of both the stocks are 3% long, then we will be placing a buy order at the top of the 1st stock and a sell order at the bottom of the other stock. In this case the risk is 3% as once the first trade is executed and the price reverses then by the time the 2nd stock sell order is executed we would lose 3% in the 1st stock.
 
Last edited:

ncube

Well-Known Member
Dear Ubermachine,
Thanks for the file..but data not correct when I check with 2...
I think most of pairs are not from one sector.......is diferent sector pairs do the same in trading?
Scanning 500 nifty stocks you can easily find more than 2000 pairs with high co-integration scores. I know its tempting to see so many options, but pair trading is not about trading so many pairs, but to understand few pairs and master its movement. It is humanely not possible to track so many pairs and its not required to make consistent profit.

I would suggested select few pairs that look interesting and have high co-integration significance values and monitor them regularly so that you understand the pattern in which the pairs moves. Also the excel sheet I have shared is just for your reference, stocks that you are not interested in you can delete it from the sheet, this will reduce the effort of daily updation.

For the few pairs that you have identified, do the following regularly:
1. What is the average movement the pairs trend each day.
2. Is it really moving in the same direction together, what happens if the movement is in opposite direction? does it sync by end of day
3. Does the pairs respect the zscore values, and what is the ideal zscore for the pairs to initiate the trades.
4. Finally you just need 1-2 pairs which you understand well to make money.
5. Pair trading is just a low risk way of trading stocks, and you can define your own trading and money management rules to trade it successfully.
 
Last edited: