using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators; using TASCIndicators;namespace WealthLab.Strategies { public class MyStrategy : WealthScript { //Pushed indicator StrategyParameter statements private StrategyParameter vmPeriod; private StrategyParameter atrFactor; public MyStrategy() { vmPeriod = CreateParameter("VM Period",14,5,100,10); atrFactor = CreateParameter("ATR Factor",4,1,5,0.5); } protected override void Execute() { int period = vmPeriod.ValueInt; int xBar = 0; // Crossing bar or stop bar bool bull = false; double triggerPrice = -1d; double stopPrice = -1d; DataSeries vmPlus = VMPlus.Series(Bars ,period); DataSeries vmMinus = VMMinus.Series(Bars ,period); DataSeries atr = atrFactor.Value * ATR.Series(Bars, 10); ChartPane paneVM = CreatePane(40,true,true); PlotSeries(paneVM, vmPlus, Color.Blue, LineStyle.Solid, 2); PlotSeries(paneVM, vmMinus, Color.Red, LineStyle.Solid, 2); PlotStops(); HideVolume(); for(int bar = period; bar < Bars.Count; bar++) { if (CrossOver(bar, vmPlus, vmMinus)) { SetBackgroundColor(bar, Color.LightBlue); bull = true; xBar = bar; } else if (CrossUnder(bar, vmPlus, vmMinus)) { SetBackgroundColor(bar, Color.LightPink); bull = false; xBar = bar; } if (IsLastPositionActive) { Position p = LastPosition; if (p.PositionType == PositionType.Long) { if (!bull) { SellAtStop(bar + 1, p, Low[xBar]); ShortAtStop(bar + 1, Low[xBar]); } else if (SellAtTrailingStop(bar + 1, p, Close[bar] - atr[bar], "T-Stop")) xBar = bar + 1; } else { if (bull) { CoverAtStop(bar + 1, p, High[xBar]); BuyAtStop(bar + 1, High[xBar]); } else if (CoverAtTrailingStop(bar + 1, p, Close[bar] + atr[bar], "T-Stop")) xBar = bar + 1; } } else if (xBar > period) // initial and re-entry { if (bull) BuyAtStop(bar + 1, High[xBar]); else ShortAtStop(bar + 1, Low[xBar]); } } } } }