#region Using declarations using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media; using System.Xml.Serialization; using NinjaTrader.Cbi; using NinjaTrader.Gui; using NinjaTrader.Gui.Chart; using NinjaTrader.Gui.SuperDom; using NinjaTrader.Gui.Tools; using NinjaTrader.Data; using NinjaTrader.NinjaScript; using NinjaTrader.Core.FloatingPoint; using NinjaTrader.NinjaScript.Indicators; using NinjaTrader.NinjaScript.DrawingTools; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class HekeinAshiTrail : Strategy { protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "HekeinAshiTrail"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlat; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 20; IsInstantiatedOnEachOptimizationIteration = true; } } protected override void OnBarUpdate() { if (BarsInProgress != 0 || CurrentBar < BarsRequiredToTrade) return; // Implement main strategy logic here if needed } } // MOVE THIS CLASS OUTSIDE OF OnBarUpdate() public class HeikinAshiTrailingStop : Strategy { private Series haClose; private Series haHigh; private Series haLow; private double trailingStop; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = "Trailing stop that follows the high/low of the previous Heikin Ashi candle after 10 ticks in profit."; Name = "HeikinAshiTrailingStop"; Calculate = Calculate.OnEachTick; IsOverlay = true; AddDataSeries(Data.BarsPeriodType.Tick, 1); } else if (State == State.Configure) { haClose = new Series(this); haHigh = new Series(this); haLow = new Series(this); } } protected override void OnBarUpdate() { if (BarsInProgress != 0 || CurrentBar < 1) return; // Calculate Heikin Ashi values haClose[0] = (Open[0] + High[0] + Low[0] + Close[0]) / 4; haHigh[0] = Math.Max(High[0], Math.Max(haClose[0], haClose[1])); haLow[0] = Math.Min(Low[0], Math.Min(haClose[0], haClose[1])); // Ensure a position exists if (Position.MarketPosition == MarketPosition.Long) { // If price has moved 10 ticks in profit, activate trailing stop if (Close[0] >= Position.AveragePrice + (10 * TickSize)) { trailingStop = haLow[1]; ExitLongStopMarket(trailingStop, "TrailingStopLong"); } } else if (Position.MarketPosition == MarketPosition.Short) { // If price has moved 10 ticks in profit, activate trailing stop if (Close[0] <= Position.AveragePrice - (10 * TickSize)) { trailingStop = haHigh[1]; ExitShortStopMarket(trailingStop, "TrailingStopShort"); } } } } }