I would like to design a strategy which stops and reverses based on the Parabolic SAR (orange dots in attached screenshot).
I am not sure how to achieve a position reversal in the same bar. The strategy "buys to cover" and "sells" in the correct bar, but opens the next position a bar later.
Can anyone please help? Bear in mind, I use NinjaTrader 8, and my C# skills still leave much to be desired.
Thanks in advance,
Cyberjoe
namespace NinjaTrader.NinjaScript.Strategies { public class StopAndReverse20160919 : Strategy { private ParabolicSAR ParabolicSAR1; double expectedParabolicSARValue; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Explore various indicators..."; Name = "StopAndReverse20160919"; 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; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { ParabolicSAR1 = ParabolicSAR(0.02, 0.2, 0.02); AddChartIndicator(ParabolicSAR1); } else if (State == State.DataLoaded) { ClearOutputWindow(); } } protected override void OnBarUpdate() { if (CurrentBars[0] < 1) return; // Pick initial long/short position if (Position.MarketPosition == MarketPosition.Flat) { if (Low[0] > ParabolicSAR1[0]) { EnterLong(); } else if (High[0] < ParabolicSAR1[0]) { EnterShort(); } } // Update parabolic SAR for more accurate reversal during upcoming bar if (BarsSinceEntryExecution() == 0) { expectedParabolicSARValue = ParabolicSAR1[0]; } else if (BarsSinceEntryExecution() > 0) { expectedParabolicSARValue = ParabolicSAR1[0] - (ParabolicSAR1[1] - ParabolicSAR1[0]); } // Long position reversal if (Position.MarketPosition == MarketPosition.Long) { BackBrushAll = Brushes.PaleGreen; ExitLongStopMarket(this.expectedParabolicSARValue); } // Short position reversal if (Position.MarketPosition == MarketPosition.Short) { BackBrushAll = Brushes.Pink; ExitShortStopMarket(this.expectedParabolicSARValue); } Print(string.Format("{0}; {1}; {2}; {3}; {4}; {5}; {6}", Time[0], Close[0], High[0], Low[0], ParabolicSAR1[0], ParabolicSAR1[1], this.expectedParabolicSARValue)); } } }
Comment