using System; using System.Collections.Generic; using System.Text; using System.Drawing; using WealthLab; using WealthLab.Indicators;namespace WealthLab.Strategies { public class ClearMethod : WealthScript { protected override void Execute() { Color clr = Color.Cyan; DataSeries swingLine = new DataSeries(Bars, "Clear Series"); DataSeries swingDir = new DataSeries(Bars, "Swing Direction"); // init to swing is up, lowest high double HH = High[0]; double LH = HH; double HL = Low[0]; double LL = HL; bool upSwing = true; swingDir[0] = -1; swingLine[0] = LL; for(int bar = 1; bar < Bars.Count; bar++) { double hi = High[bar]; double lo = Low[bar]; if (upSwing) { // if (hi > HH) HH = hi; // inconsequential if (lo > HL) HL = lo; if (hi < HL) { upSwing = false; //LL = lo; LH = hi; } } else { // if (lo < LL) LL = lo; //inconsequential if (hi < LH) LH = hi; if (lo > LH) { upSwing = true; // HH = hi; HL = lo; } } if (upSwing) { swingLine[bar] = HL; swingDir[bar] = -1; } else { swingLine[bar] = LH; swingDir[bar] = 0; } /* Plot the line manually to show crossovers and transitions */ if (swingDir[bar] != swingDir[bar-1]) { clr = (swingDir[bar] != 0) ? Color.Fuchsia : Color.Cyan; DrawLine(PricePane, bar - 1, swingLine[bar-1], bar, swingLine[bar-1], clr, LineStyle.Solid, 2); } else { clr = (swingDir[bar] != 0) ? Color.Cyan : Color.Fuchsia; DrawLine(PricePane, bar - 1, swingLine[bar-1], bar, swingLine[bar], clr, LineStyle.Solid, 2); } } /* Basic Trend Following "Clear Method" Strategy */ BuyAtMarket(1); for(int bar = 1; bar < Bars.Count; bar++) { Position p = LastPosition; if (swingDir[bar] != 0) // trend up { if (IsLastPositionActive && p.PositionType == PositionType.Short) { CoverAtMarket(bar + 1, p); BuyAtMarket(bar + 1); } } else { if (IsLastPositionActive && p.PositionType == PositionType.Long) { SellAtMarket(bar + 1, p); ShortAtMarket(bar + 1); } } } } } }