using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class TASC201612 : WealthScript { StrategyParameter fastPeriod; StrategyParameter slowPeriod; StrategyParameter strategyMode; public TASC201612() { fastPeriod = CreateParameter("Fast Period", 4, 1, 100, 1); slowPeriod = CreateParameter("Slow Period", 40, 20, 300, 5); strategyMode = CreateParameter("Strategy Mode", 0, -1, 1, 1); } protected override void Execute() { // Enter your pair symbols here string stock1, stock2, longSymbol, shortSymbol; longSymbol = stock1 = Bars.Symbol; shortSymbol = stock2 = "SPY"; int MAPeriod = fastPeriod.ValueInt, MAPeriod2 = slowPeriod.ValueInt, mode = strategyMode.ValueInt; // Compute DataSeries DataSeries close1, close2, open1, open2, high1, high2, low1, low2; SetContext( stock1, true ); Bars bars1 = Bars; close1 = Close; open1 = Open; high1 = High; low1 = Low; SetContext( stock2, true ); Bars bars2 = Bars; close2 = Close; open2 = Open; high2 = High; low2 = Low; RestoreContext(); DataSeries Ratio = close1 / close2; Ratio.Description = "Ratio"; DataSeries RatioO = open1 / open2; RatioO.Description = "RatioOpen"; DataSeries RatioH = high1 / high2; RatioH.Description = "RatioHigh"; DataSeries RatioL = low1 / low2; RatioL.Description = "RatioLow"; DataSeries RatioSMA = SMA.Series( Ratio, MAPeriod ); RatioSMA.Description = "Ratio Fast SMA"; DataSeries RatioSMA2 = SMA.Series( Ratio, MAPeriod2 ); RatioSMA2.Description = "Ratio Slow SMA"; // Graphics ChartPane RatioPane = CreatePane( 6000, true, true ); ChartPane StocksPane = CreatePane( 2000, true, true ); PlotSymbol(StocksPane,bars1,Color.Green,Color.Red); PlotSymbol(StocksPane,bars2,Color.Green,Color.Red); HideVolume(); PlotSyntheticSymbol( RatioPane, "Price Ratio " + stock1 + "/" + stock2 + " as OHLC", RatioO, RatioH, RatioL, Ratio, new DataSeries("Bogus"), Color.Navy, Color.Salmon ); PlotSeries( RatioPane, RatioSMA, Color.Gray, WealthLab.LineStyle.Solid, 2 ); PlotSeries( RatioPane, RatioSMA2, Color.Salmon, WealthLab.LineStyle.Solid, 2 ); for(int bar = MAPeriod; bar < Bars.Count; bar++) { // Trend following strategy if( mode == 1 ) { if( IsLastPositionActive ) { if (CrossUnder(bar, RatioSMA, RatioSMA2 )) ExitAtClose(bar, Position.AllPositions); } else { if( CrossOver(bar, RatioSMA, RatioSMA2 ) ) { longSymbol = stock1; shortSymbol = stock2; SetContext( longSymbol, true ); BuyAtClose( bar, "TF Buy " + shortSymbol ); SetContext( shortSymbol, true ); ShortAtClose( bar, "TF Short " + longSymbol ); } } } else // Mean reverting strategy if( mode == -1 ) { if( IsLastPositionActive ) { if (Ratio[bar] > RatioSMA[bar]) ExitAtClose(bar, Position.AllPositions); } else { if (Ratio[bar] < RatioSMA[bar]) { longSymbol = stock1; shortSymbol = stock2; SetContext( longSymbol, true ); BuyAtClose( bar, "MR Buy " + shortSymbol ); SetContext( shortSymbol, true ); ShortAtClose( bar, "MR Short " + longSymbol ); } } } else // Trend pullback strategy if( mode == 0 ) { if( IsLastPositionActive ) { if( (RatioSMA[bar] < RatioSMA2[bar]) && (Ratio[bar] > RatioSMA[bar]) ) ExitAtClose(bar, Position.AllPositions); } else { if( (RatioSMA[bar] > RatioSMA2[bar]) && (Ratio[bar] < RatioSMA[bar]) ) { longSymbol = stock1; shortSymbol = stock2; SetContext( longSymbol, true ); BuyAtClose( bar, "PB Buy " + shortSymbol ); SetContext( shortSymbol, true ); ShortAtClose( bar, "PB Short " + longSymbol ); } } } RestoreContext(); } } } }