#Pay10©: Pairs Trading With Context #Following can help find cointegrated underlyings #Resources: www.aistockcharts.com #Resources: www.pairtradinglab.com #Hint: Use cointegration math models to find constant spreads in two symbols. The system here provides context regarding implied volatility, correlations, as well ash showing entries and exits on the trades. Use Implied Volatility adjusting to help with trade sizing. Use Correlations to add more meaning to chosen co-integrated pair. When comparing multiple trade opportunities use the "Ratio Context" & "Spread Range Context" labels to see wehre the best opportunity is. Ratio Context: the higher the absolute value percentage the farther the current ratio has moved past the mean. Spread Range Context: the higher the absolute value percentage the more profit potential the pair has built in when it returns to the mean. Trade entry is when the ratio moves outside the standard deviation bands, put on a reversal trade. Trade exit is when the ratio returns to the average ratio mid-line. input Lookback_Average = 0.75; input Lookback_Standard_Deviation = 1.25; input Lookback_Context = 1.50; input Average_Plot_Type = averageType.SIMPLE; input StandardDeviations = 2.00; input TradeContextPercent = .35; input InverseTradeContextPercent = .15; #input A = "/ES"; #hint A: FUTURES = /ES to /YM, /ES to /NQ, /YM to /NQ, /ES to /RTY, /NQ to /RTY, /ES to /ZB, /ES to /GC, /ES to /BTC, /GC to /BTC, TUT=/ZT to /ZN, TUF=/ZT to /ZF, FYT=ZF to /ZN, FOB=/ZF to /ZB, NOB=/ZN to /ZB, BOB=/ZB to /UB, TUB=/ZT to /UB, /GC to /SI, /ZC to /ZW, /ZC to /ZS, /ZW to /ZS, /6E to /6J, /6E to /6B, /6C to /6A, /6E to /6C, /6J to /6A, /CL to /NG || STOCKS & OPTIONS = SPY to QQQ, SPY to DIA, QQQ to IWM, CORN to SOYB, FXE to FXA, FXE to FXC, FXA to FXC, GOOG to GOOGL, TLT to IEF, XOM to SPY, KO to PEP, TSLA to THO, BA to UTX, FOX to FOXA, XOM to CVX, WBA to WMT, LOW to HD, KO to MCD, FDX to UPS, CSCO to INTC, V to MA, JNUG to NUGT, EEM to EWW, EEM to EWM, EEM to EWZ, GLD to TLT, XLV to UNH, QQQ to XLK, USO to XLE, SMH to TSM, BAC to XLF, WFC to XLF, XOM to XOP, KO to WMT, XLRE to IYR, GDX to GDXJ, input TickSizeA = 1.00; input TickValueA = 1.00; input LotSizeA = 1; #input B = "/NQ"; #hint B: FUTURES = /ES to /YM, /ES to /NQ, /YM to /NQ, /ES to /RTY, /NQ to /RTY, /ES to /ZB, /ES to /GC, /ES to /BTC, /GC to /BTC, TUT=/ZT to /ZN, TUF=/ZT to /ZF, FYT=ZF to /ZN, FOB=/ZF to /ZB, NOB=/ZN to /ZB, BOB=/ZB to /UB, TUB=/ZT to /UB, /GC to /SI, /ZC to /ZW, /ZC to /ZS, /ZW to /ZS, /6E to /6J, /6E to /6B, /6C to /6A, /6E to /6C, /6J to /6A, /CL to /NG || STOCKS & OPTIONS = SPY to QQQ, SPY to DIA, QQQ to IWM, CORN to SOYB, FXE to FXA, FXE to FXC, FXA to FXC, GOOG to GOOGL, TLT to IEF, XOM to SPY, KO to PEP, TSLA to THO, BA to UTX, FOX to FOXA, XOM to CVX, WBA to WMT, LOW to HD, KO to MCD, FDX to UPS, CSCO to INTC, V to MA, JNUG to NUGT, EEM to EWW, EEM to EWM, EEM to EWZ, GLD to TLT, XLV to UNH, QQQ to XLK, USO to XLE, SMH to TSM, BAC to XLF, WFC to XLF, XOM to XOP, KO to WMT, XLRE to IYR, GDX to GDXJ, input TickSizeB = 1.00; input TickValueB = 1.00; input LotSizeB = 1; #### plots ########################################################################### def spread = close; plot Avg_Ratio = MovingAverage(Average_Plot_Type, spread, (100*Lookback_Average)); def Spread_Percent_Off = AbsValue((spread - Avg_Ratio) / Avg_Ratio); def SpreadStDev = StDev(spread, (100*Lookback_Standard_Deviation)); plot SD_Upper_Band = Avg_Ratio + (StandardDeviations * SpreadStDev); plot SD_Lower_Band = Avg_Ratio - (StandardDeviations * SpreadStDev); def Spread_Range = SD_Upper_Band - SD_Lower_Band; def Spread_Range_Average = MovingAverage(AverageType.SIMPLE, Spread_Range, (100*Lookback_Context)); def Spread_Range_Percentage = (Spread_Range - Spread_Range_Average) / Spread_Range_Average; #### Labels & Plot Styles ############################################################## assignBackgroundColor( if spread > SD_Upper_Band then COLOR.plum else if spread < SD_Lower_Band then COLOR.blue else if spread > Avg_Ratio and spread < SD_Upper_Band then COLOR.DARK_RED else if spread < Avg_Ratio and spread > SD_Lower_Band then COLOR.DARK_GREEN else COLOR.black); AssignPriceColor( if spread > spread[1] then color.WHITE else if spread < spread[1] then color.BLACK else Color.GRAY); AddLabel (yes, "Ratio Context: " + AsPercent(Spread_Percent_Off), Color.WHITE); AddLabel (yes, "Spread Range Context: " + AsPercent(Spread_Range_Percentage), Color.WHITE); AddLabel (yes, "Potential Profit: " + absValue(close - Avg_Ratio), Color.WHITE); SD_Upper_Band.AssignValueColor(if Spread_Range_Percentage > TradeContextPercent then Color.DARK_GREEN else if Spread_Range_Percentage > 0 then Color.ORANGE else if Spread_Range_Percentage > -(InverseTradeContextPercent) then Color.BLUE else Color.DARK_RED); #SD_Upper_Band.AssignValueColor(if Spread SD_Lower_Band.AssignValueColor(if Spread_Range_Percentage > TradeContextPercent then Color.DARK_GREEN else if Spread_Range_Percentage > 0 then Color.ORANGE else if Spread_Range_Percentage > -(InverseTradeContextPercent) then Color.BLUE else Color.DARK_RED); Avg_Ratio.AssignValueColor(if Avg_Ratio > Spread then CreateColor(204, 255, 255) else CreateColor(000, 153, 153)); Avg_Ratio.SetLineWeight(2); AddCloud(Spread, SD_Upper_Band, Color.LIGHT_RED, Color.BLACK); AddCloud(Spread, SD_LOWER_Band, Color.BLACK, Color.LIGHT_GREEN); ADDLABEL(1, asdollars(close(getSymbolPart(1))) + " " + getSymbolPart(1) + "x" + lotSizeA + " | " + asdollars(close(getSymbolPart(2))) + " " + getSymbolPart(2) + "x" + lotSizeB,color.CYAN); addlabel(1, "Correlation: " + aspercent(correlation(close(getSymbolPart(1)), close(getSymbolPart(2)), 500)), color.CYAN); AddLabel(1, "Notional Ratio: " + ((((close(getSymbolPart(1)) / TickSizeA) * TickValueA) * LotSizeA) / (((close(getSymbolPart(2)) / TickSizeB) * TickValueB) * LotSizeB)), Color.MAGENTA); AddLabel(1, "IV Adj Notional Ratio: " + ((imp_volatility(getSymbolPart(1)) * (((close(getSymbolPart(1)) / TickSizeA) * TickValueA) * LotSizeA)) / (imp_volatility(getSymbolPart(2)) * (((close(getSymbolPart(2)) / TickSizeB) * TickValueB) * LotSizeB))), Color.MAGENTA); #### Order Entries and Exits ######################################################### #######LONGS####### AddOrder(OrderType.BUY_TO_OPEN, close <= SD_Lower_Band and close[1] >= SD_Lower_Band[1], tickcolor = Color.CYAN, arrowcolor = Color.GREEN); AddOrder(OrderType.SELL_TO_CLOSE, close[1] <= Avg_Ratio[1] and close >= Avg_Ratio, tickcolor = Color.GREEN, arrowcolor = Color.GREEN); AddOrder(OrderType.SELL_TO_CLOSE, Avg_Ratio[1] >= EntryPrice() and Avg_Ratio <= EntryPrice(), tickcolor = Color.RED, arrowcolor = Color.GREEN); #######SHORTS####### AddOrder(OrderType.SELL_TO_OPEN, close >= SD_Upper_Band and close[1] <= SD_Upper_Band[1], tickcolor = Color.CYAN, arrowcolor = Color.RED); AddOrder(OrderType.BUY_TO_CLOSE, close[1] >= Avg_Ratio[1] and close <= Avg_Ratio, tickcolor = Color.GREEN, arrowcolor = Color.RED); AddOrder(OrderType.BUY_TO_CLOSE, Avg_Ratio[1] <= EntryPrice() and Avg_Ratio >= EntryPrice(), tickcolor = Color.RED, arrowcolor = Color.RED); ########################################################################################