using System; using System.Collections.Generic; using System.Drawing; using WealthLab;namespace WealthLab.Strategies { public class ThreeBarPatternFutures : WealthScript { StrategyParameter _target; StrategyParameter _stoploss; StrategyParameter _modifiedShortTrigger; public ThreeBarPatternFutures() { _target = CreateParameter("% Target", 0.75, 0.5, 2.5, 0.05); _stoploss = CreateParameter("% Stop Loss", 0.75, 0.5, 2.5, 0.05); _modifiedShortTrigger = CreateParameter("Modified Short", 1, 0, 1, 1); } private void HighlightPattern(int bar, Color color, int patternBars) { double hi = High[bar]; double lo = Low[bar]; if (patternBars > 1) { int p = patternBars - 1; for (int n = bar - 1; n >= bar - p; n--) { hi = High[n] > hi ? High[n] : hi; lo = Low[n] < lo ? Low[n] : lo; } } int b1 = bar - patternBars; int b2 = Math.Min(bar + 1, Bars.Count - 1); DrawPolygon(PricePane, color, color, LineStyle.Invisible, 1, true, b1, hi, b2, hi, b2, lo, b1, lo); } protected override void Execute() { Position p = null; double target = 1e6; double stop = 0d; PlotStops(); HideVolume(); for(int bar = 3; bar < Bars.Count; bar++) { if (IsLastPositionActive) { p = LastPosition; if (!ExitAtStop(bar + 1, p, stop, "Stop Loss")) ExitAtLimit(bar + 1, p, target, "Profit Target"); } else { bool setup1L = Close[bar - 3] < Close[bar - 2]; bool setup1S = Close[bar - 3] > Close[bar - 2]; bool setup2 = High[bar - 2] > High[bar - 1] && Low[bar - 2] < Low[bar - 1]; bool setup3L = Close[bar - 1] < Close[bar]; bool setup3S = Close[bar - 1] > Close[bar]; if (_modifiedShortTrigger.ValueInt == 1) setup3S = Low[bar - 1] > Close[bar]; if (setup1L && setup2 && setup3L) { HighlightPattern(bar, Color.FromArgb(30, Color.Blue), 3); if (BuyAtMarket(bar + 1) != null) { p = LastPosition; target = p.EntryPrice * (1 + _target.Value/100d); stop = p.EntryPrice * (1 - _stoploss.Value/100d); } } else if (setup1S && setup2 && setup3S) { HighlightPattern(bar, Color.FromArgb(20, Color.Black), 3); if (ShortAtMarket(bar + 1) != null) { p = LastPosition; target = p.EntryPrice * (1 - _target.Value/100d); stop = p.EntryPrice * (1 + _stoploss.Value/100d); } } } } } } }