Purpose
I wish to execute a stock pairs trading strategy, where I need to track a specific list of 50 stock pairs simultaneously, in real-time while maintaining 1 year of historical data of the pairs in 1 minute candlestick format.
My method for creating Pair's Spread
Pairs are calculated using a simple rolling regression to find the hedge ratio and spread calculation use a simple y=mx formula where m is the hedge ratio, it is very crucial for me to at least update this hedge ratio every minute.
Goal
- Real-time updates with ≤ 200 MS latency.
- Full ticker-like behavior for custom pairs.
- Preloaded 1-year historical data with real-time backfilling (The 50 Spreads should backfill, update within a minute as soon as the tickers are backfilled contributing to the spreads).
- Custom symbols named ~Symbol1_Symbol2.
- Must use a registered data feed like IQ Feed or the IB plugin Feed.
Done So Far & Challenges Faced
Out of all the possible paths available in Amibroker, given my proficiency level I could come up with the following solution this is where I need specific help, as to is there a path that is more robust that I have missed
- I tried to achieve this through AddToComposite but it recalculates history repeatedly on every iteration, slowing everything down and my updation happens after 15 seconds rather than the desired 200 MS , I tried running it in indicator mode as well as repeated explorations but soon realized that I picked the wrong tool for my use case or probably have a lapse in my understanding.
- Static Variables don’t allow seamless ticker-like behavior. So, it’s a no go for me as most of the great seamless functionalities Amibroker provides on tickers won’t be so straightforward for me given my expertise using staticvariables.
- Also cant use formula files (via spread indicator) as pairs are required as tickers for complete functionality.
- I’m not proficient in C++ to create a custom plugin and want to build on the existing IB data feed or any other registered robust data feed that has handled the stock data properly, so I have a solid base without reinventing anything.
I apologise in advance if my problem statement is coming from lack of understanding of the basics as i am still getting familiar with Amibroker as its a really robust vast Software.
Below is my sample code that I implemented using AddtoComposite() to be run in scan mode repeatedly to keep the pairs updated.
y="PETRONET,IOC,"+
"TORNTPOWER,POWERGRID,"+
"TORNTPOWER,NTPC,"+
"TORNTPOWER,TATAPOWER,"+
"TORNTPOWER,IEX,"+
"POWERGRID,NTPC,"+
"POWERGRID,TATAPOWER,"+
"POWERGRID,IEX,"+
"NTPC,TATAPOWER,"+
"NTPC,IEX,"+
"TATAPOWER,IEX,"+
"GODREJPROP,DLF,"+
"GODREJPROP,OBEROIRLTY,"+
"DLF,OBEROIRLTY,"+
"TRENT,ABFRL,"+
"IDEA,INDUSTOWER,"+
"IDEA,TATACOMM,"+
"IDEA,BHARTIARTL,"+
"INDUSTOWER,TATACOMM,"+
"INDUSTOWER,BHARTIARTL,"+
"TATACOMM,BHARTIARTL";
for(i=0;i<StrLen(y);i+=2)
{
symbol_1 = StrExtract(y,i);
symbol_2 = StrExtract(y,i+1);
lookback_linearreg = 1500;
///////////////////////////////Symbol_import
//_N( Symbol_1= ParamStr("Symbol1", symbol_1) );
c1 = log(Foreign(symbol_1,"C"));
c2 = log(Foreign(symbol_2,"C"));
xy = c1*c2 ;
x2 = c1*c1 ;
y2 = c2*c2 ;
slope = 1/((Sum(xy,lookback_linearreg) - (Sum(c1 , lookback_linearreg)*Sum(c2,lookback_linearreg)))/
(Sum(x2, lookback_linearreg)-(Sum(c1,lookback_linearreg)*Sum(c1,lookback_linearreg))));
spread = (10+(c1 - (slope*c2)));
// calculating the spread
tickerspread = "~"+symbol_1+"_"+symbol_2;
AddToComposite(spread, tickerspread, "X", atcFlagDefaults);
AddToComposite(slope, tickerspread, "1", atcFlagDefaults);
Filter = 1 ;
}```