AlphaLab

StatArb

Pair trading is employed by professional traders to outperform the market. This script is a complete trading strategy where you can set your own parameters and the system will generate ready to trade signals. All you have to do is just execute profitable trades based on your own parameters.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?
study(title="Pair Trading. Grey area chart is normalized spread between two pairs", shorttitle="StatArb.Pair Trading. Grey area chart is normalized spread between two pairs", overlay=true)
tf_short = input(title="Timeframe",type=string,defval='15')
stoploss = input(title="StopLoss %",type=float,defval=10)
takeprofit = input(title="takeProfit %",type=float,defval=30)
avgLookback = input(title="Smoothing Lookback period in bars - short",type=integer,defval=10)
avgLookback_long = input(title="Smoothing Lookback period in bars - long" ,type=integer,defval=100)
stdDevMultiplier =input(title="Standard deviation multiplier",type=integer,defval=4)
sym1_short = input(title="Symbol1", type=symbol, defval="AUDUSD"), res1 = tf_short, source1 = close
sym2_short = input(title="Symbol2", type=symbol, defval="XAUUSD"), res2 = tf_short, source2 = close
sym_price1 = security(sym1_short, res1, source1) 
sym_price2 = security(sym2_short, res2, source2)
cum_changePcnt1_short = cum(1*(sym_price1- offset(sym_price1,1))/sym_price1)
cum_changePcnt2_short = cum(1*(sym_price2- offset(sym_price2,1))/sym_price2)
sym_priceNorm_1 = (sym_price1[0])*(1+cum_changePcnt1_short) 
sym_priceNorm_2 = (sym_price1[0])*(1+cum_changePcnt2_short)  

spread_norm = (sym_priceNorm_1-sym_priceNorm_2)
spread_short = ema(sym_priceNorm_1-sym_priceNorm_2 ,avgLookback) 
spread_long= ema(sym_priceNorm_1-sym_priceNorm_2  ,avgLookback_long)
spread_stdev_short = stdev(spread_long,avgLookback)
lower_band_entry = spread_long-spread_stdev_short*stdDevMultiplier
upper_band_entry = spread_long+spread_stdev_short*stdDevMultiplier
lower_band_exit = spread_long-spread_stdev_short*stdDevMultiplier*4
upper_band_exit = spread_long+spread_stdev_short*stdDevMultiplier*4

signalLine_Short = ((offset(spread_short,0) - offset(lower_band_entry,0)))>0 
               and ((offset(spread_short,1) - offset(lower_band_entry,1)))<0
signalLine_Long =  ((offset(spread_short,0) - offset(upper_band_entry,0))<0) 
               and ((offset(spread_short,1) - offset(upper_band_entry,1))>0)
 //SHORTS              
entry_Short = iff(signalLine_Short==0, na,spread_norm) 
stoploss_level_short   = entry_Short + abs(entry_Short *(stoploss/100))
takeprofit_level_short = entry_Short - abs(entry_Short *(takeprofit/100))
entry_Short_sl = iff((offset(spread_norm,0) > offset(stoploss_level_short,0)) 
                 and (offset(spread_norm,1) < offset(stoploss_level_short,1)),spread_norm,na )
entry_Short_tp = iff((offset(spread_norm,0) < offset(takeprofit_level_short,0)) 
                 and (offset(spread_norm,1) > offset(takeprofit_level_short,1)),spread_norm,na )
 //LONGS
entry_Long  = iff(signalLine_Long==0, na,sym_priceNorm_1-sym_priceNorm_2)  
stoploss_level_long   = entry_Long - abs(entry_Long *(stoploss/100))
takeprofit_level_long = entry_Long + abs(entry_Long *(takeprofit/100))
entry_Long_sl = iff((offset(spread_norm,0) < offset(stoploss_level_long,0)) 
                and (offset(spread_norm,1) > offset(stoploss_level_long,1)),spread_norm,na )
entry_Long_tp = iff((offset(spread_norm,0) > offset(takeprofit_level_long,0)) 
                and (offset(spread_norm,1) < offset(takeprofit_level_long,1)),spread_norm,na )
//
signalLine_Short_exit =  offset(spread_short,0) - offset(upper_band_exit,0)>0
                  and    offset(upper_band_exit,1) -    offset(spread_short,1)>0
signalLine_Long_exit  =  offset(spread_short,0)  - offset(lower_band_exit,0)>0 
                     and offset(lower_band_exit,1)  -    offset(spread_short,1)>0
                           
plot((spread_norm) ,style=area,color=black,transp =90, linewidth=1)  

plot(entry_Short,style=circles,color=red, trackprice=true,linewidth=4)
plot(entry_Long, style=circles,color=green, trackprice=true,linewidth=4) 
// exits                
plot(stoploss_level_short,style=linebr,color=red, trackprice=true,linewidth=1)
plot(takeprofit_level_short,style=cross,color=red, trackprice=true,linewidth=1)
plot(stoploss_level_long,style=linebr,color=green, trackprice=true,linewidth=1)
plot(takeprofit_level_long,style=cross,color=green, trackprice=true,linewidth=1)