// ============================================================================================ // Vortex Gap Fade - 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): at the close of the first 5-minute bar (09:35), if the 09:30 open gapped from the previous regular-session close by between 1 and 4 x ATR(14) and price is still beyond that close, trade back toward it: short a gap up, buy a gap down. Target = the previous close (the gap filled), stop = one gap size on the other side. One trade a day, 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 VortexGapFade : Strategy { private ATR atr; private double lastRthClose, prevClose; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Opening gap fade teaching example. Trades back toward yesterday's close after a moderate gap, stop one gap size away, flat before the close. Education only. Use the default CME US Index Futures ETH trading hours."; Name = "VortexGapFade"; 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; MinGapAtr = 1.0; MaxGapAtr = 4.0; AtrPeriod = 14; } else if (State == State.DataLoaded) { 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) return; int tt0 = ToTime(Time[0]); if (tt0 >= 93500 && tt0 <= 160000) { if (tt0 == 93500) prevClose = lastRthClose; lastRthClose = Close[0]; } if (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 (t != 93500 || prevClose <= 0 || Position.MarketPosition != MarketPosition.Flat) return; double gap = Open[0] - prevClose; double size = Math.Abs(gap); if (size < MinGapAtr * atr[0] || size > MaxGapAtr * atr[0]) return; int stopTicks = Math.Max(4, (int)Math.Round(size / TickSize)); if (gap > 0 && Close[0] - prevClose >= 4 * TickSize) { SetStopLoss(CalculationMode.Ticks, stopTicks); SetProfitTarget(CalculationMode.Ticks, (int)Math.Round((Close[0] - prevClose) / TickSize)); EnterShort(); } else if (gap < 0 && prevClose - Close[0] >= 4 * TickSize) { SetStopLoss(CalculationMode.Ticks, stopTicks); SetProfitTarget(CalculationMode.Ticks, (int)Math.Round((prevClose - Close[0]) / TickSize)); EnterLong(); } } #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(0.1, 10)] [Display(Name = "Smallest gap (x ATR)", Order = 10, GroupName = "Parameters")] public double MinGapAtr { get; set; } [NinjaScriptProperty] [Range(0.5, 20)] [Display(Name = "Largest gap (x ATR)", Order = 11, GroupName = "Parameters")] public double MaxGapAtr { get; set; } [NinjaScriptProperty] [Range(1, 100)] [Display(Name = "ATR period", Order = 12, GroupName = "Parameters")] public int AtrPeriod { get; set; } #endregion } }