/* Seasonal Soybean Strategy with Chandelier exit */ using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using Community.Components;namespace WealthLab.Strategies { public class MyStrategy : WealthScript { StrategyParameter D1DXY; StrategyParameter D2SNL; StrategyParameter LRSDXYSELL; StrategyParameter LRSNLSELL; StrategyParameter chandelierPeriod; StrategyParameter chandelierCoeff; StrategyParameter chnlLength; StrategyParameter doPlot; public MyStrategy() { D1DXY = CreateParameter("DX LRSlope Period", 25, 10, 50, 1); D2SNL = CreateParameter("Snl LRSlope Period", 12, 5, 25, 1); LRSDXYSELL = CreateParameter("DX LRSlope", 0.3, 0.1, 0.8, 0.1); LRSNLSELL = CreateParameter("Snl LRSlope", -0.8, -1.2, -0.5, 0.1 ); chandelierPeriod = CreateParameter("Chandelier Period", 55, 8, 89, 1); chandelierCoeff = CreateParameter("Chandelier Coeff", 3, 1, 5, 0.5); chnlLength = CreateParameter("Channel Period", 4, 2, 20, 2); doPlot = CreateParameter("Plot All", 1, 0, 1, 1); } protected override void Execute() { // initialization for chandelier stop SeriesHelper obj = new SeriesHelper( this ); double chandelierStop = 0.0; int cPer = chandelierPeriod.ValueInt; PlotStops(); HideVolume(); int buyMonth = 9; int sellMonth = 6; // Access external data Bars dxy = GetExternalSymbol("DXY", true); Bars snl = GetExternalSymbol("SNL", true); // Create and plot indicators DataSeries channelHiPlus2 = (Highest.Series(Close, chnlLength.ValueInt) + 2d) >> 1; DataSeries channelLoMinus2 = (Lowest.Series(Close, chnlLength.ValueInt) - 2d) >> 1; DataSeries dxyLR = LinearReg.Series(dxy.Close, D1DXY.ValueInt); DataSeries LRSDXY = ROC.Series( dxyLR, D1DXY.ValueInt ) / D1DXY.ValueInt; LRSDXY.Description = "DX Avg ROC(" + D1DXY.ValueInt + ")"; DataSeries snlLR = LinearReg.Series(snl.Close, D2SNL.ValueInt); DataSeries LRSNL = ROC.Series(snlLR, D2SNL.ValueInt) / D2SNL.ValueInt; LRSNL.Description = "Seasonal Avg ROC(" + D2SNL.ValueInt + ")"; ChartPane slopePane = CreatePane(20, true, true); PlotSeries(slopePane, LRSDXY, Color.Black, LineStyle.Solid, 1); PlotSeries(slopePane, LRSNL, Color.Green, LineStyle.Histogram, 1); if( doPlot.ValueInt == 1 ) { PlotSeries(PricePane, channelHiPlus2, Color.Blue, LineStyle.Dashed, 1); PlotSeries(PricePane, channelLoMinus2, Color.Brown, LineStyle.Dashed, 1); } // Trading Strategy logic for(int bar = 3 * GetTradingLoopStartBar(1); bar < Bars.Count; bar++) { int month = Date[bar].Month; bool seasonalBuy = month < sellMonth || month > buyMonth; bool seasonalShort = month > sellMonth && month < buyMonth; if (IsLastPositionActive) { Position p = LastPosition; // Chandelier stop value using Period: 14, Coefficient: 3 chandelierStop = obj.ChandelierStop( Bars, bar, p, cPer, chandelierCoeff.Value ); ExitAtTrailingStop(bar+1, p, chandelierStop); } else if ( seasonalBuy && LRSDXY[bar] < LRSDXYSELL.Value && Close[bar] > channelHiPlus2[bar] && LRSNL[bar] > LRSNLSELL.ValueInt ) { RiskStopLevel = Close[bar] - 3d * ATR.Series(Bars, cPer)[bar]; BuyAtMarket(bar + 1); } else if (seasonalShort && LRSDXY[bar] > -LRSDXYSELL.Value && Close[bar] < channelLoMinus2[bar] && LRSNL[bar] < -LRSNLSELL.ValueInt ) { RiskStopLevel = Close[bar] + 3d * ATR.Series(Bars, cPer)[bar]; ShortAtMarket(bar + 1); } } } } }