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) && stobar>85) || (llvDivbar < -20 && rocOilbar < -3) || (Closebar < llvbar-1 && correlation60bar < -0.4); bool cover = (CrossOver(bar, macd, macdSignal) && stobar<25 && (oilbar >= (1 + 4.0/100d) * llvOilbar) ) || (hhvDivbar>20 && rocOilbar>4.5 ) || (Closebar > hhvbar-1 && correlation60bar < -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 = (hhvDivbar > 20) && (divergencebar < divergencebar-1) && (rocbar>0) && (movbar > movbar-2) && (correlation20bar > -0.4); bool shrt = (llvDivbar < -20) && (divergencebar > divergencebar-1) && (rocbar<0) && (movbar < movbar-2) && (correlation20bar > -0.4); if( buy ) BuyAtMarket(bar+1); else if( shrt ) ShortAtMarket(bar+1); } } } } }