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 = High0; double LH = HH; double HL = Low0; double LL = HL; bool upSwing = true; swingDir0 = -1; swingLine0 = LL; for(int bar = 1; bar < Bars.Count; bar++) { double hi = Highbar; double lo = Lowbar; 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) { swingLinebar = HL; swingDirbar = -1; } else { swingLinebar = LH; swingDirbar = 0; } /* Plot the line manually to show crossovers and transitions */ if (swingDirbar != swingDirbar-1) { clr = (swingDirbar != 0) ? Color.Fuchsia : Color.Cyan; DrawLine(PricePane, bar - 1, swingLinebar-1, bar, swingLinebar-1, clr, LineStyle.Solid, 2); } else { clr = (swingDirbar != 0) ? Color.Cyan : Color.Fuchsia; DrawLine(PricePane, bar - 1, swingLinebar-1, bar, swingLinebar, clr, LineStyle.Solid, 2); } } /* Basic Trend Following "Clear Method" Strategy */ BuyAtMarket(1); for(int bar = 1; bar < Bars.Count; bar++) { Position p = LastPosition; if (swingDirbar != 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); } } } } } }