Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

ncube

Well-Known Member
#11
Dear ncube,
Is there anyway to know the correlation between 2 scrips which we selected in excell without ADF?means looking anything otherthan ADF we select scrips for pairtrading?though maybe its not accurate like ADF but we can use.....
ADF tests are done to confirm whether the 2 stocks in the pair are stationary and co-integrating, which in plain english means how sure we can be that these 2 stocks together can mean revert. Co-relation is not same as co-integration, even if the stocks are highly co-related if they are not co-integrated they may not mean revert in future.

However for day trading it should not matter, you may select 2 highly co-related stocks and trade it as a pair since you will be closing the same by end of day. However knowing the stocks co-integrate gives us higher confidence to take and hold the trade.
 

VJAY

Well-Known Member
#12
You guys would already be having some strategies to day trade, you can use the same to place your trades, however you do not keep stop loss orders, instead you will place the opposite order on the second stock. Day trading is a skill, and there are multiple ways to execute your strategy, however I use these simple rules which I had explained in general chat thread:

Day Trading Strategy:
For day trading we can short stocks and use margins, so for the identified pairs fix same rupee value you want to trade for each stock, for example 25K per stock, so calculate the respective quantities of stocks that you can go long or short. With margin It will be about 10K for both the stocks combined together (50K).

Trading rules:
1. Wait for the 1st 15min or 30min candle to be formed.
2. Keep a SL MIS buy order at the high of the candle for the stock you want to go long
3. Keep a SL MIS sell order at the low of the candle for the stock you want to go short
4. Close the open trades before 3:15pm

Reasoning on why it will work:
1. If the stocks trend in our identified direction (usually the pair will move in same direction) only 1 of these trades will be triggered and will go into profit.
2. If the stocks trend is opposite to our identified direction both trades will not be triggered and no trade for us. (The price diversion will increase by end of day..so higher chances they will mean revert in subsequent days)
3. If the stocks trend in our identified direction and later change direction after the 1st trade was executed then the 1st trade will start going into loss, however the 2nd pair trade will get triggered and the position is now hedged. Usually in these cases by end of the day trend reverses (due to mean reversion force) giving small profit else during the course of the day they will come to a point where it can be close for break-even or small loss.

The reason why I select the same rupee value say 25K for each stock is that we are interested in the daily returns of each stock, since we know that one stock will more more than the other even if both the trades are executed in our favor we will have the difference in return as our profit.
For example, if stock1 return is 2% and stock2 return is 5%, then even if both got executed we will still end up with 3% profit. If only 1 is executed and if we are lucky we can end up with 5% profit.

I usually keep a target of 1% if both the trades are executed and about 3% if only 1 trade is executed, else I will allow it to run till the end of day as I am confident that my trades are hedged.

I think this much detail should be sufficient for day trading pairs and in subsequent 2 posts I will explain how to setup the python environment for pair trading and you guys will be set to go. Dont worry if you are not technically strong or have experience in coding, it is very easy to use python on Jupyter.
Keenly waiting for it ....this will remove my previous querry ...:) ...Thanks ncube for explaining pairtrading in simple way ...though please throw some insights about positional too as allready derivative trading timing will increasing soon ...It may help many who having big pockets....
 

ncube

Well-Known Member
#13
Installing and running Python & Jupyter Notebook:

Python is a very user friendly versatile programming language and easy to learn, dont worry if you are not familiar with coding, I plan to provide a readymade script which you can directly run in your python environment by just making few changes in the configuration. Lets first setup the python environment in your pc, please follow these steps:

1. Download the latest version of python exe from this URL: https://www.python.org/getit/. The latest version is 3.7.0, once downloaded, just click it to install and accept all the defaults.
1532619893318.png

2. Restart your PC
3. Now open a windows command prompt and run the following commands one by one to install few python libraries
>pip install pandas
>pip install matplotlib
>pip install statsmodels
1532619948563.png

4. Next we will install a browser based python interface called Jupyter as follows:
>pip install --upgrade pip
>pip install jupyter
1532619974756.png


Thats all there is to it, to check jupyter is properly installed run the following command in the windows command prompt
>jupyter notebook
1532619997019.png


If everything is properly installed it will open your browser and run the jupyter notebook as follows:
1532620091151.png


Download the jupyter notebook PairTrading.ipynb that I have attached in this post and place it in a folder.
Now click on the upload icon at the top right and upload the PairTrading.ipynb notebook, once uploaded it will be see in the list of files.
Also download the attached stockdata.csv file and place it in your c drive.

Running the PairTrading.ipynb notebook:
1. Click the file in the jupyter notebook interface it will open the notebook as follows:
1532620534256.png


2. Each cell in the notebook is a python executable code and you can run each cell interactively by clicking the play button. Start from the top and execute each cell (I have highlighted the play botton). Play with the options and you will be comfortable in no time.
1532620711138.png
 

ncube

Well-Known Member
#14
The output in the notebook for the cointegration significance and plot will look as follows:
1532620941368.png


I have shared both the pairtrading.ipynb notebook and sample stockdata.csv file which contains 2+yrs of data for NIFTY 500 stocks in the following path: https://drive.google.com/drive/folders/1a40Ih__otr99E1TAQe7WEjsTEsiBDKQQ?usp=sharing

So friends this should set you guys up to speed...play around and explore Pair Trading...Enjoy!!!..:)
 
Last edited:

cloudTrader

Well-Known Member
#15
The output in the notebook for the cointegration significance and plot will look as follows:
View attachment 26880

I am not able to upload PairTrading jupyter notebook and the stock data csv file here, it is giving me error that the file is unsupported extension and upload not supported. Can anyone please let me know how to upload the files? I even tried to zip it but still the same error.
You can upload those files somewhere else like Google Drive or Dropbox and give the direct link here
 

VJAY

Well-Known Member
#16
Dear ncube ,
Am stuck here :(
...Python by default not installing in programe files its installing in c drive but in another place ..unable to change the folder when installing window ..I tryed uninstall and reinstall...
Untitled.png
Untitled.png
 

ncube

Well-Known Member
#17
Dear ncube ,
Am stuck here :(
...Python by default not installing in programe files its installing in c drive but in another place ..unable to change the folder when installing window ..I tryed uninstall and reinstall...
View attachment 26885 View attachment 26886
Ok this is becoz windows in not able to find python in the default path. While installing python please select the option Add python 3.7 to PATH.
Once installation is done, better to restart the pc once so that this information is updated in the registery.
IMG_20180727_093744.jpg
 
Last edited:

cloudTrader

Well-Known Member
#18
Hey ncube,

Can you give a day trading example from your some recent trade ? Actually I have been following Pair Trading from quite sometime. I managed to get an article written by Mark Conway : Day Trading Stock Pairs. That method is based on Correlation , it has worked quite beautifully sometimes intraday but sometimes it fails miserably. So the perspectives you have suggested are very interesting and so an example of a real trade will surely be helpful to look broadly. The total decision making process you applied for placing the said trade.

Thanks.
 

ncube

Well-Known Member
#19
Hey ncube,

Can you give a day trading example from your some recent trade ? Actually I have been following Pair Trading from quite sometime. I managed to get an article written by Mark Conway : Day Trading Stock Pairs. That method is based on Correlation , it has worked quite beautifully sometimes intraday but sometimes it fails miserably. So the perspectives you have suggested are very interesting and so an example of a real trade will surely be helpful to look broadly. The total decision making process you applied for placing the said trade.

Thanks.
@cloudTrader , I do not day trade & prefer to take mostly positional trades, I developed my positional Pair Trading strategy just couple of months back and have day traded it as part of walk-forward testing of my strategy to fine tune and optimize it and found that it is equally effective for day trading. Since most of the members in this forum day trade I shared this concept here so that interested people can try it as I feel its a low risk fully hedged way of trading.

Few day back I posted the 3 pair trades that I took that day, you can check the following post in General Trading Chat for more detail. These 3 trades covered the typical scenarios that one can face when day trading the pairs. Once I am back home in the evening will try to post the screen shots with my thought process behind those trades. The strategy is the same as I explained in my earlier post, I waited for the 1st 30 min candle to be formed to initiate the trades in the direction that I had identified from the Pair Trading analysis the previous day, however I took few dynamic decisions on couple of trades to identify the exact trade point based on my analysis of the ongoing price action which I will explain in the screen shots.

@Riskyman, sorry I would not be able to comment on your system without more details, as there are many ways one can trade pairs. Maybe you can improve by trying to identify pairs with good stationarity and half life values, also if you take NIFTY 500 as your stock universe you can get more than 2000+ pairs daily some of which are not commonly tracked by many so higher chances of success. With so many options one can easily select pairs with higher significance value of 0.01(99%) instead of the typical 0.05(95%).

However I agree with you that having deep pockets definitely helps as one can take bigger positional trades & can also diversify for risk control. My intention was just to encourage people to explore new Ideas which are low risk and not get discouraged that it applies only to institutional players. Who knows they can come up with new ideas and share it in the forum here...:). Also I read few other threads where people were trying 1st candle break out strategies which are discretionary and I felt it is difficult to be consistent that way, instead if they trade that same method systematically with a clear statistical edge it will give better results.

Usually I dont day trade, however just to show that it works and give confidence to whoever is interested I took trades in these 3 pairs today: (Each Stocks worth 25K, total margin 18.5K on zerodha)
ALBK-ANDHRABANK --> Both trades got executed, hence one is negative and the other positive.
TATAMTRDRV-TATAMOTORS --> Only TATAMOTORS got executed
BANKBARODA-UNIONBANK --> Only UNIONBANK got executed.

View attachment 26809
 

VJAY

Well-Known Member
#20
Ok this is becoz windows in not able to find python in the default path. While installing python please select the option Add python 3.7 to PATH.
Once installation is done, better to restart the pc once so that this information is updated in the registery.
View attachment 26890
Will check it on sunday now...need to go out...thanks for the help