using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Indicators;namespace WealthLab.Strategies { public class LoonieStrategy : WealthScript { private StrategyParameter paramPeriod; public LoonieStrategy() { paramPeriod = CreateParameter("days", 20, 2, 300, 1); } protected override void Execute() { int days = paramPeriod.ValueInt; StdDevCalculation s = StdDevCalculation.Sample; var oil = GetExternalSymbol("Light",true).Close; var bbCAD = 1 + ((Close - SMA.Series(Close,days) + 2*StdDev.Series(Close,days,s)) / (4*StdDev.Series(Close,days,s)+0.0001)); var bbOil = 1 + ((oil - SMA.Series(oil,days) + 2*StdDev.Series(oil,days,s)) / (4*StdDev.Series(oil,days,s)+0.0001)); var divergence =(bbOil-bbCAD)/bbCAD*100; divergence.Description = "Divergence"; var hhvDiv = Highest.Series(divergence,3); var llvDiv = Lowest.Series(divergence,3); var llv = Lowest.Series(Low,15); var hhv = Highest.Series(High,15); var roc = ROC.Series(Close,2); var mov = SMA.Series(oil,40); var correlation20 = Correlation.Series(Close,oil,20); var correlation60 = Correlation.Series(Close,oil,60); var rocOil = ROC.Series(oil,3); var llvOil = Lowest.Series(oil,4); var macd = MACD.Series(Close); var macdSignal = EMAModern.Series(macd,9); var sto = StochD.Series(Bars,30,3); ChartPane oilPane = CreatePane(50,true,true); HideVolume(); PlotSymbol(oilPane,GetExternalSymbol("Light",true),Color.Green,Color.Red); ChartPane divPane = CreatePane(50,true,true); LineStyle ld = LineStyle.Dashed; PlotSeries(divPane,divergence,Color.Black,LineStyle.Solid,1); DrawHorzLine(divPane,20,Color.Black,ld,1); DrawHorzLine(divPane,-20,Color.Black,ld,1); for(int bar = GetTradingLoopStartBar(60); bar < Bars.Count; bar++) { if (IsLastPositionActive) { bool sell = (CrossUnder(bar, macd, macdSignal) && sto[bar]>85) || (llvDiv[bar] < -20 && rocOil[bar] < -3) || (Close[bar] < llv[bar-1] && correlation60[bar] < -0.4); bool cover = (CrossOver(bar, macd, macdSignal) && sto[bar]<25 && (oil[bar] >= (1 + 4.0/100d) * llvOil[bar]) ) || (hhvDiv[bar]>20 && rocOil[bar]>4.5 ) || (Close[bar] > hhv[bar-1] && correlation60[bar] < -0.4); Position p = LastPosition; if( p.PositionType == PositionType.Long ) { if( sell ) SellAtMarket(bar+1, p); } else { if( cover ) CoverAtMarket(bar+1, p); } } else { bool buy = (hhvDiv[bar] > 20) && (divergence[bar] < divergence[bar-1]) && (roc[bar]>0) && (mov[bar] > mov[bar-2]) && (correlation20[bar] > -0.4); bool shrt = (llvDiv[bar] < -20) && (divergence[bar] > divergence[bar-1]) && (roc[bar]<0) && (mov[bar] < mov[bar-2]) && (correlation20[bar] > -0.4); if( buy ) BuyAtMarket(bar+1); else if( shrt ) ShortAtMarket(bar+1); } } } } }