Pair Trading - Exploring The Low Risk Statistical Arbitrage Trading Concepts

travi

Well-Known Member
#91
@ncube ,

Is it not possible to employ this whole process in Amibroker ?
Theoretically Its very possible to implement in Amibroker, but the question is why re-invent the wheel? :)
Also if you do get around that, converting all statistical formulas into AFL, going forward that community is smaller and getting support is more difficult.
on the other hand, you can get a lot of .py snippets all over the web.

The best hybrid solution for this is to integrate both. There are various posts on how to run a Python COM server and use AB to call those functions.
This method in my view is robust and very practical.
Plus you can leverage the Quant part of Python and exceptionally good charting and other advantages of AB.
That way, atleast the data part is not a headache and you can continue using auto-updated/RTD data from AB itself.

Rajenderan from market calls among others have tested on this earlier. Search on this.

However, It will require a lot of time to expand this subject :D:D
 

cloudTrader

Well-Known Member
#92
Theoretically Its very possible to implement in Amibroker, but the question is why re-invent the wheel? :)
Also if you do get around that, converting all statistical formulas into AFL, going forward that community is smaller and getting support is more difficult.
on the other hand, you can get a lot of .py snippets all over the web.

The best hybrid solution for this is to integrate both. There are various posts on how to run a Python COM server and use AB to call those functions.
This method in my view is robust and very practical.
Plus you can leverage the Quant part of Python and exceptionally good charting and other advantages of AB.
That way, atleast the data part is not a headache and you can continue using auto-updated/RTD data from AB itself.

Rajenderan from market calls among others have tested on this earlier. Search on this.

However, It will require a lot of time to expand this subject :D:D
Thanks for your inputs regarding the various scenarios which could be employed using both Amibroker and Python. This explanation by you has certainly helped me. This is the first time I came across Python , all credit to ncube. Never ever even thought that I will install Python on my PC some day. :D
 

VJAY

Well-Known Member
#95
@VJAY , have you already started trading the pairs or is it just paper trade? I suggest for few days you just observe the pairs and do paper trading to get a feel of it. Its not a good practice to risk money on new strategies until you get some experience observing & journaling it.

Coming to the pairs ALBK-ANDHRABANK, as per the zScore, we need to look for shorting ANDHRABANK & Long ALBK. I checked the 30 min chart and see that the 1st 30 min candle is quite large for both the stocks, so one should take care while trading these as already the stocks have moved 3%, either aviod trading it or go to 15 min candle and trade the next candle.

Looks like for ANDHRABANK you traded it early, when the candle close is near the low, it is not good to places trade at low as there is high chance it will hit your SL. Day trading is all about you controling your trades not allowing market to dictate. Your thought process should be I will place the trade at a point where I think it will not be hit. If it hits then it should continue in the same direction taking my trade into profit. Remember you should never force a trade as you cannot force market to move as per your wish.

Following points to be noted:
1. If the 1st 30 min candle is larger avoid trading it unless you are sure of the direction. If the trade has already moved more than 3% there is less chance that it will give more profit.
2. Another risk of large candles is the hegde on the second candle will also kick start late, so your risk also increases accordingly, i.e the diffence between the 2 stock trades will be your risk. In this current case your risk will be about 3-4%, which on a capital of 25K will be around 750-1000. Ideally try to keep risk below 500.

But one good point is that your risk is limited and it will hover around 1000, however during the course of the day it may come down and you can try to close it at lesser loss.
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

Well-Known Member
#96
Hi,
I haven't grasped the entire thing so far.
Still, here is a list of correlations for all possible pairs sorted so that the best possible pairs could be identified.
This is based on the stock_data.csv file.
Hope it helps.
Just give a check before using it.
Download file
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?
 
Last edited:

UberMachine

Well-Known Member
#97
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?
Good point.
I take a different approach to pair trading.
The standard approach is to select companies with similar characteristics so that they go up and down together.
But on a more general note, correlation holds the key. If 2 pairs have had a high correlation in the past, irrespective of their sectors or similarities, they must work together. Think of a machine searching highly correlated pairs without any knowledge.
One big caveat is Correlation doesn't imply causation.
But since we are dealing with intraday timeframe, that may not hold true (I need to thoroughly test it before getting any conclusion).
If you are trading futures, I go with similar business.

The file is just a reference file for all possible combinations and the most correlated ones are similar (except for a few). You can check it or you can discard the ones with different sectors.
But check whether the correlation is correct before proceeding

Turning pairs trading into a intraday or a overnight strategy is pretty difficult but I would give it a try
 

VJAY

Well-Known Member
#98
Good point.
I take a different approach to pair trading.
The standard approach is to select companies with similar characteristics so that they go up and down together.
But on a more general note, correlation holds the key. If 2 pairs have had a high correlation in the past, irrespective of their sectors or similarities, they must work together. Think of a machine searching highly correlated pairs without any knowledge.
One big caveat is Correlation doesn't imply causation.
But since we are dealing with intraday timeframe, that may not hold true (I need to thoroughly test it before getting any conclusion).
If you are trading futures, I go with similar business.

The file is just a reference file for all possible combinations and the most correlated ones are similar (except for a few). You can check it or you can discard the ones with different sectors.
But check whether the correlation is correct before proceeding

Turning pairs trading into a intraday or a overnight strategy is pretty difficult but I would give it a try
Dear ubermachine,
How you getting this file?
 

UberMachine

Well-Known Member
#99
Dear ubermachine,
How you getting this file?
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