I’m trying to get the Entry and the Exit of a position in the same bar but I’m always getting the Exit at the Open of the next bar. I started trying with Daily bars and I was thinking this was due the fact to use Daily bars but when I also tried with 60min bars I got the exact same behavior, having the Exit at the Open of the next bar.
I tried with taking some extra ideas from the next thread:
https://ninjatrader.com/support/foru...it-on-same-bar
and I changed from ‘Calculate.OnPriceChange’ to ‘Calculate.OnEachTick’ in order to “To obtain intrabar actions”, but still getting the same result when for example I select daily bars, even when in the Strategy Analyzer are selected the settings “Break at EOD” and “Exit on session close”, and I thought that if using these settings with intrabar information and specifying “Exit on session close” then the result would have to be to get the Exit of the position at the Close, at least for the Daily bars.
Also, as I read in that thread, I also tried adding ‘IsFirstTickOfBar’ in the Entry and/or in the Exit condition, but in case of being needed in the Exit condition then I wouldn’t fully understand the reason to use ‘IsFirstTickOfBar’ for an event that happens at the end of the bar and not at beginning, because it would be to check the “first tick”, no?
Below you can see I’m using ‘(BarsSinceEntryExecution(0, @"Long", 0) == 0)’ with the idea that the Exit order can be placed in the same bar that the Entry order was placed too (‘… == 0’ equal to the current bar):
#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 StrategyTest : Strategy { private RSI RSI1; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your new custom Strategy here."; Name = "StrategyTest"; Calculate = Calculate.OnEachTick; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.ImmediatelySubmit; TimeInForce = TimeInForce.Day; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 1; // Disable this property for performance gains in Strategy Analyzer optimizations // See the Help Guide for additional information IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { } else if (State == State.DataLoaded) {[INDENT]RSI1 = RSI(Close, 14, 1);[/INDENT][INDENT]RSI1.Plots[0].Brush = Brushes.DodgerBlue;[/INDENT][INDENT]RSI1.Plots[1].Brush = Brushes.Goldenrod;[/INDENT][INDENT]AddChartIndicator(RSI1);[/INDENT] } } protected override void OnBarUpdate() {[INDENT]if (BarsInProgress != 0)[/INDENT][INDENT]return;[/INDENT][INDENT]if (CurrentBars[0] < 1)[/INDENT][INDENT]return;[/INDENT][INDENT] // Set 1 // if (IsFirstTickOfBar) // {[/INDENT][INDENT] if (RSI1.Default[0] > 70)[/INDENT][INDENT] {[/INDENT][INDENT=2]EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");[/INDENT][INDENT=2]BackBrushAll = Brushes.Green;[/INDENT][INDENT] } // }[/INDENT][INDENT] // Set 2 // if (IsFirstTickOfBar) // { if (BarsSinceEntryExecution(0, @"Long", 0) == 0) {[/INDENT][INDENT=2]ExitLong(Convert.ToInt32(DefaultQuantity), @"Exit", @"Long"); BackBrush = Brushes.Yellow;[/INDENT][INDENT] } // }[/INDENT] } } }
I need help to handle these kind of cases in a simplified way please, either for Daily and for intraday bars.
Thank you in advance!
Comment