Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
BarsSinceEntryExecution is broken
Collapse
X
-
BarsSinceEntryExecution is broken
All strategies containing an exit using BarsSinceEntryExecution are exiting 2 bars later than specified. Please advise.Tags: None
-
Hello, thanks for writing in. My test script works using this method. See my screenshot below. The condition becomes true on the 9:26:50 bar and the signal is processed on the first tick of the next bar. This may be what you are seeing. If so, reduce the value compared against BarsSinceEntryExecution, e.g. instead of 3 use 1 to exit on the first tick of the 2nd bar.
-
I want to exit after x bars. If (BarsSinceEntryExecution() == 20) {ExitLong()}
This has always worked, with bars in the trade showing as 20 and suddenly all of my scripts no longer work
Comment
-
Hi, have you used Prints to debug your strategy? Using Print should be the first thing you do to debug any issues you are having with your strategy code. What do the Prints say when you print out:
if(Postion.MarketPostion == MarketPosition.Long)
Print(BarsSinceEntryExecution());
My example works, so this means that BarsSinceEntryExecution() works in all cases, so if you are getting unexpected behavior, you need to debug your script.
Comment
-
Hi Chris,
I set the Bars to "if greater than 19, exit". The output window says 20, but the strategy analyzer "Trades" output says 22, and if I count the bars on the chart, There are 21. Lots of variety
David
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[0] < 5)
return;
// Set 1
if ((Close[0] > Close[5])
&& (Position.MarketPosition == MarketPosition.Flat))
{
EnterLong(Convert.ToInt32(DefaultQuantity), "");
}
// Set 2
if ((BarsSinceEntryExecution(0, "", 0) > BarsToHold)
&& (Position.MarketPosition == MarketPosition.Long))
{
ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
}
if(Position.MarketPosition == MarketPosition.Long)
Print(BarsSinceEntryExecution());
At most, the trades should be closing on the open of the 21st bar, not the open of the 22nd bar, which is what's happening.Last edited by trader3000a; 05-19-2023, 11:22 AM.
Comment
-
-
Comment
-
Hi Chris,
If i enter on the open of a bar, I'm 100% in the trade on that bar. Please explain how entering a trade on the open of a bar and being in that trade for a full day doesn't count as being in the trade? The entryexecution is on the open of that bar on the chart. I think I understand what you are saying, but it's definitely counterintuitive.
DavidLast edited by trader3000a; 05-19-2023, 03:20 PM.
Comment
-
Hi Chris,
The function should absolutely be re-named BarsAgoSinceEntryExecution or something more clever, since a full bar elapses on the entry bar AFTER the execution. The intent of the user is to use this function to address the number of bars IN trade. Please add this as a feature request?
thanks,
David
Comment
-
Hi Chris,
Understood. Please add this feature request for me: Please add a function "BarsInTrade". If I use 20 as an input variable, for instance, I could simply write:
if(BarsInTrade == 20) {ExitLong(); ExitShort();}
and I could be assured that from entry to exit, my trade will include 20 bars. Simple and intuitive.
Unless of course, you already have something like that, and were waiting to tell me because you love these long rants of mine...
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, 03-13-2026, 05:17 AM
|
0 responses
88 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
151 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
80 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
53 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
62 views
0 likes
|
Last Post
|

Comment