// ============================================================================================ // Vortex EMA Ribbon (8/13/21/34/55) - 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): buy when the five EMAs (8, 13, 21, 34, 55) line up in rising order (8 on top) after not being lined up on the previous bar; sell short when they line up in falling order. Exit when the 8 EMA crosses back through the 21 EMA. No reversal. Stop = 2 x ATR(14). 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 VortexEmaRibbon : Strategy { private EMA e1, e2, e3, e4, e5; private ATR atr; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"EMA ribbon trend teaching example. Enters when five EMAs align, exits when the 8 EMA crosses the 21 EMA, ATR stop, flat before the close. Education only."; Name = "VortexEmaRibbon"; 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; EmaPeriod1 = 8; EmaPeriod2 = 13; EmaPeriod3 = 21; EmaPeriod4 = 34; EmaPeriod5 = 55; AtrPeriod = 14; AtrStopMultiple = 2.0; } else if (State == State.DataLoaded) { e1 = EMA(EmaPeriod1); e2 = EMA(EmaPeriod2); e3 = EMA(EmaPeriod3); e4 = EMA(EmaPeriod4); e5 = EMA(EmaPeriod5); 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; bool asc = e1[0] > e2[0] && e2[0] > e3[0] && e3[0] > e4[0] && e4[0] > e5[0]; bool desc = e1[0] < e2[0] && e2[0] < e3[0] && e3[0] < e4[0] && e4[0] < e5[0]; bool ascPrev = e1[1] > e2[1] && e2[1] > e3[1] && e3[1] > e4[1] && e4[1] > e5[1]; bool descPrev = e1[1] < e2[1] && e2[1] < e3[1] && e3[1] < e4[1] && e4[1] < e5[1]; if (IsLong && e1[0] < e3[0]) ExitLong(); if (IsShort && e1[0] > e3[0]) ExitShort(); if (!canEnter || Position.MarketPosition != MarketPosition.Flat) return; int stopTicks = Math.Max(4, (int)Math.Round(atr[0] * AtrStopMultiple / TickSize)); if (asc && !ascPrev) { SetStopLoss(CalculationMode.Ticks, stopTicks); EnterLong(); } else if (desc && !descPrev) { SetStopLoss(CalculationMode.Ticks, stopTicks); 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(1, 200)] [Display(Name = "EMA 1 (fastest)", Order = 10, GroupName = "Parameters")] public int EmaPeriod1 { get; set; } [NinjaScriptProperty] [Range(1, 200)] [Display(Name = "EMA 2", Order = 11, GroupName = "Parameters")] public int EmaPeriod2 { get; set; } [NinjaScriptProperty] [Range(1, 200)] [Display(Name = "EMA 3 (exit line)", Order = 12, GroupName = "Parameters")] public int EmaPeriod3 { get; set; } [NinjaScriptProperty] [Range(1, 400)] [Display(Name = "EMA 4", Order = 13, GroupName = "Parameters")] public int EmaPeriod4 { get; set; } [NinjaScriptProperty] [Range(1, 400)] [Display(Name = "EMA 5 (slowest)", Order = 14, GroupName = "Parameters")] public int EmaPeriod5 { get; set; } [NinjaScriptProperty] [Range(1, 100)] [Display(Name = "ATR period", Order = 20, GroupName = "Parameters")] public int AtrPeriod { get; set; } [NinjaScriptProperty] [Range(0.5, 10)] [Display(Name = "ATR stop multiple", Order = 21, GroupName = "Parameters")] public double AtrStopMultiple { get; set; } #endregion } }