// ============================================================================================ // Vortex Engulfing Candle (pullback, 1.5R) - free NinjaTrader 8 example strategy from Vortex Futures Trade Lab // https://vortexfuturestradelab.com // // FOR EDUCATION ONLY. NOT FINANCIAL ADVICE. USE AT YOUR OWN RISK. // Trading futures carries a substantial risk of loss. Past or simulated results do not predict future // results. This strategy is a teaching example of a well-known idea; we do not claim it makes money. // Test it on a simulated account first. Many prop firms ban automated trading: check your firm's // rules before running any strategy on an evaluation or funded account. // // Times below use your chart's time zone. The defaults assume US Eastern time. // Rules (same as the simulation on the strategy page): a bullish engulfing bar is a green bar whose body covers the previous red bar's body; bearish is the mirror image. Buy a bullish engulfing bar that closes below the 20 EMA (a pullback in the up direction), sell short a bearish one that closes above it. The engulfing body must be at least 0.5 x ATR(14) and its range no more than 3 x ATR(14). Stop = the far end of the engulfing bar (at least 4 ticks), target = 1.5 x the stop distance. Flat by the end time. // ============================================================================================ #region Using declarations using System; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.NinjaScript.Indicators; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class VortexEngulfingCandle : Strategy { private EMA ema; private ATR atr; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Engulfing candle teaching example. Trades bullish and bearish engulfing bars against the 20 EMA, stop at the far end of the bar, 1.5R target, flat before the close. Education only."; Name = "VortexEngulfingCandle"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = false; IsFillLimitOnTouch = false; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlat; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 120; TradeFromTime = 93500; LastEntryTime = 153000; FlatByTime = 155500; EmaPeriod = 20; MinBodyAtr = 0.5; MaxRangeAtr = 3.0; TargetR = 1.5; AtrPeriod = 14; } else if (State == State.DataLoaded) { ema = EMA(EmaPeriod); atr = ATR(AtrPeriod); } } private bool IsLong { get { return Position.MarketPosition == MarketPosition.Long; } } private bool IsShort { get { return Position.MarketPosition == MarketPosition.Short; } } protected override void OnBarUpdate() { if (BarsInProgress != 0 || CurrentBar < BarsRequiredToTrade) return; int t = ToTime(Time[0]); if (t >= FlatByTime) { if (IsLong) ExitLong(); else if (IsShort) ExitShort(); return; } if (t < TradeFromTime) return; bool canEnter = t <= LastEntryTime; if (!canEnter || Position.MarketPosition != MarketPosition.Flat) return; double body = Math.Abs(Close[0] - Open[0]); if (body < MinBodyAtr * atr[0] || High[0] - Low[0] > MaxRangeAtr * atr[0]) return; bool bull = Close[1] < Open[1] && Close[0] > Open[0] && Open[0] <= Close[1] && Close[0] >= Open[1] && Close[0] < ema[0]; bool bear = Close[1] > Open[1] && Close[0] < Open[0] && Open[0] >= Close[1] && Close[0] <= Open[1] && Close[0] > ema[0]; if (bull) { int st = Math.Max(4, (int)Math.Round((Close[0] - Low[0]) / TickSize)); SetStopLoss(CalculationMode.Ticks, st); SetProfitTarget(CalculationMode.Ticks, (int)Math.Round(st * TargetR)); EnterLong(); } else if (bear) { int st = Math.Max(4, (int)Math.Round((High[0] - Close[0]) / TickSize)); SetStopLoss(CalculationMode.Ticks, st); SetProfitTarget(CalculationMode.Ticks, (int)Math.Round(st * TargetR)); EnterShort(); } } #region Properties [NinjaScriptProperty] [Range(0, 235959)] [Display(Name = "Trade from (HHmmss)", Description = "No trades before this bar-close time. Chart time zone.", Order = 1, GroupName = "Hours")] public int TradeFromTime { get; set; } [NinjaScriptProperty] [Range(0, 235959)] [Display(Name = "Last entry (HHmmss)", Order = 2, GroupName = "Hours")] public int LastEntryTime { get; set; } [NinjaScriptProperty] [Range(0, 235959)] [Display(Name = "Flat by (HHmmss)", Description = "Open positions are closed from this time. No overnight trades.", Order = 3, GroupName = "Hours")] public int FlatByTime { get; set; } [NinjaScriptProperty] [Range(2, 200)] [Display(Name = "Pullback EMA period", Order = 10, GroupName = "Parameters")] public int EmaPeriod { get; set; } [NinjaScriptProperty] [Range(0.1, 5)] [Display(Name = "Smallest body (x ATR)", Order = 11, GroupName = "Parameters")] public double MinBodyAtr { get; set; } [NinjaScriptProperty] [Range(0.5, 10)] [Display(Name = "Largest bar range (x ATR)", Order = 12, GroupName = "Parameters")] public double MaxRangeAtr { get; set; } [NinjaScriptProperty] [Range(0.25, 10)] [Display(Name = "Target (x stop distance)", Order = 13, GroupName = "Parameters")] public double TargetR { get; set; } [NinjaScriptProperty] [Range(1, 100)] [Display(Name = "ATR period", Order = 20, GroupName = "Parameters")] public int AtrPeriod { get; set; } #endregion } }