#region Using declarations using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.NinjaScript.Indicators; using System; using System.ComponentModel.DataAnnotations; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class PivotBot : Strategy { private PivotPoints pivots; private double pivotHigh; private double pivotLow; private OpeningRange openingRange; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Trades pivot point breakouts"; Name = "Pivot 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 = 20; IsInstantiatedOnEachOptimizationIteration = true; PivotWindowLength = 10; } else if (State == State.Configure) { // Data series required by the opening range indicator, must be loaded here. AddDataSeries(BarsPeriodType.Minute, 1); AddDataSeries(Instrument.FullName, BarsPeriod, "US Equities RTH"); } else if (State == State.DataLoaded) { pivots = PivotPoints(PivotWindowLength); openingRange = OpeningRange(5, OpeningRangeBarType.Minutes); } } protected override void OnBarUpdate() { if (BarsInProgress != 0) return; if (CurrentBar < BarsRequiredToTrade) return; DateTime now = Time[0]; if (now <= openingRange.GetOpeningRangeEndTime(now)) return; PivotPoint pivot = pivots.Pivot[0]; if (pivot != null) { if (pivot.IsPivotHigh) pivotHigh = pivot.Level; else if (pivot.IsPivotLow) pivotLow = pivot.Level; } if (Close[0] > pivotHigh) { /*if (Position.MarketPosition == MarketPosition.Short) ExitShort();*/ if (openingRange.ORH[0] > 0.0 && Close[0] > openingRange.ORH[0]) EnterLong(); } else if (Close[0] < pivotLow) { if (Position.MarketPosition == MarketPosition.Long) ExitLong(); /*if (openingRange.ORL[0] > 0.0 && Close[0] < openingRange.ORL[0]) EnterShort();*/ } } public override string DisplayName { get { return Name; } } [NinjaScriptProperty] [Display(Name = "Pivot Window Length", GroupName = "Pivot Bot", Order = 1)] public int PivotWindowLength { get; set; } } }