#region Using declarations using NinjaTrader.Cbi; using NinjaTrader.Data; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class ThreeDayTradeBot : Strategy { private const int PrimaryBars = 0; private const int MinuteBars = 1; private const string LongEntry = "3-Day Trade Long"; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Based on the 3-Day Trade, taken from chapter 24 of Kaufman Constructs Trading Systems"; Name = "3-Day Trade Bot"; 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 = 3; IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { AddDataSeries(BarsPeriodType.Minute, 1); } } protected override void OnBarUpdate() { if (CurrentBars[PrimaryBars] < BarsRequiredToTrade || CurrentBars[MinuteBars] < BarsRequiredToTrade) return; if (BarsInProgress == PrimaryBars && BarsSinceEntryExecution(PrimaryBars, LongEntry, 0) == 1) ExitLong(); if (BarsInProgress != MinuteBars) return; if (Closes[PrimaryBars][0] < Opens[PrimaryBars][0] && Closes[PrimaryBars][1] < Opens[PrimaryBars][1]) { if (Bars.IsFirstBarOfSession && Opens[MinuteBars][0] < Closes[PrimaryBars][0]) EnterLong(LongEntry); else if (Bars.IsLastBarOfSession && Closes[MinuteBars][0] < Closes[PrimaryBars][0]) EnterLong(LongEntry); } } public override string DisplayName { get { return Name; } } } }