// ============================================================================================ // Vortex Inside Bar Breakout - 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): an inside bar is a bar whose high is below and low is above the previous (mother) bar. For the next 3 bars, a close above the mother bar's high buys and a close below its low sells short, if the mother bar's range is within 3 x ATR(14). Stop = the other end of the mother bar, target = the same distance (1R). 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 VortexInsideBarBreakout : Strategy { private ATR atr; private double motherHigh, motherLow; private int armedBars; private DateTime lastDate = DateTime.MinValue; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Inside-bar breakout teaching example. Trades a close beyond the mother bar within 3 bars, stop at the other end of the mother bar, 1R target, flat before the close. Education only."; Name = "VortexInsideBarBreakout"; 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; ValidBars = 3; MaxRiskAtr = 3.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 || 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 (Time[0].Date != lastDate) { lastDate = Time[0].Date; armedBars = 0; } if (armedBars > 0 && canEnter && Position.MarketPosition == MarketPosition.Flat) { double maxRisk = MaxRiskAtr * atr[0]; if (Close[0] > motherHigh && Close[0] - motherLow <= maxRisk) { int ticks = Math.Max(4, (int)Math.Round((Close[0] - motherLow) / TickSize)); SetStopLoss(CalculationMode.Ticks, ticks); SetProfitTarget(CalculationMode.Ticks, ticks); EnterLong(); armedBars = 0; } else if (Close[0] < motherLow && motherHigh - Close[0] <= maxRisk) { int ticks = Math.Max(4, (int)Math.Round((motherHigh - Close[0]) / TickSize)); SetStopLoss(CalculationMode.Ticks, ticks); SetProfitTarget(CalculationMode.Ticks, ticks); EnterShort(); armedBars = 0; } } if (armedBars > 0) armedBars--; if (High[0] < High[1] && Low[0] > Low[1]) { motherHigh = High[1]; motherLow = Low[1]; armedBars = ValidBars; } } #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(1, 20)] [Display(Name = "Bars the setup stays valid", Order = 10, GroupName = "Parameters")] public int ValidBars { get; set; } [NinjaScriptProperty] [Range(0.5, 10)] [Display(Name = "Skip if risk is more than (x ATR)", Order = 11, GroupName = "Parameters")] public double MaxRiskAtr { get; set; } [NinjaScriptProperty] [Range(1, 100)] [Display(Name = "ATR period", Order = 12, GroupName = "Parameters")] public int AtrPeriod { get; set; } #endregion } }