// ============================================================================================ // Vortex Ichimoku Cloud Breakout (9/26/52) - 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): Tenkan = midpoint of the last 9 bars, Kijun = midpoint of the last 26, cloud = Senkou A (average of Tenkan and Kijun) and Senkou B (midpoint of the last 52), both plotted 26 bars ahead. Buy when the close breaks above the top of the cloud while Tenkan is above Kijun; sell short on a close below the bottom of the cloud while Tenkan is below Kijun. Exit when the close crosses back through Kijun. 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 VortexIchimokuCloud : Strategy { private MAX hi9, hi26, hi52; private MIN lo9, lo26, lo52; private ATR atr; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Ichimoku cloud breakout teaching example. Trades a close through the cloud when Tenkan agrees, exits at the Kijun line, ATR stop, flat before the close. Education only."; Name = "VortexIchimokuCloud"; 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; TenkanPeriod = 9; KijunPeriod = 26; SenkouPeriod = 52; AtrPeriod = 14; AtrStopMultiple = 2.0; } else if (State == State.DataLoaded) { hi9 = MAX(High, TenkanPeriod); lo9 = MIN(Low, TenkanPeriod); hi26 = MAX(High, KijunPeriod); lo26 = MIN(Low, KijunPeriod); hi52 = MAX(High, SenkouPeriod); lo52 = MIN(Low, SenkouPeriod); 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; int d = KijunPeriod; double tk = (hi9[0] + lo9[0]) / 2.0; double kj = (hi26[0] + lo26[0]) / 2.0; double saNow = ((hi9[d] + lo9[d]) / 2.0 + (hi26[d] + lo26[d]) / 2.0) / 2.0; double sbNow = (hi52[d] + lo52[d]) / 2.0; double saPrev = ((hi9[d + 1] + lo9[d + 1]) / 2.0 + (hi26[d + 1] + lo26[d + 1]) / 2.0) / 2.0; double sbPrev = (hi52[d + 1] + lo52[d + 1]) / 2.0; double topNow = Math.Max(saNow, sbNow); double botNow = Math.Min(saNow, sbNow); double topPrev = Math.Max(saPrev, sbPrev); double botPrev = Math.Min(saPrev, sbPrev); if (IsLong && Close[0] < kj) ExitLong(); if (IsShort && Close[0] > kj) ExitShort(); if (!canEnter || Position.MarketPosition != MarketPosition.Flat) return; int stopTicks = Math.Max(4, (int)Math.Round(atr[0] * AtrStopMultiple / TickSize)); if (Close[0] > topNow && Close[1] <= topPrev && tk > kj) { SetStopLoss(CalculationMode.Ticks, stopTicks); EnterLong(); } else if (Close[0] < botNow && Close[1] >= botPrev && tk < kj) { 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(2, 100)] [Display(Name = "Tenkan period", Order = 10, GroupName = "Parameters")] public int TenkanPeriod { get; set; } [NinjaScriptProperty] [Range(2, 200)] [Display(Name = "Kijun period (also the cloud shift)", Order = 11, GroupName = "Parameters")] public int KijunPeriod { get; set; } [NinjaScriptProperty] [Range(2, 400)] [Display(Name = "Senkou B period", Order = 12, GroupName = "Parameters")] public int SenkouPeriod { 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 } }