Announcement
Collapse
Looking for a User App or Add-On built by the NinjaTrader community?
Visit NinjaTrader EcoSystem and our free User App Share!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less
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.
Chris L.NinjaTrader Customer Service
-
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.
Chris L.NinjaTrader Customer Service
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, If we change any NinjaScript code like this it would be a very serious code-breaking change to the entire platform. All developers that ever used this method would need to change their code, so there is a low likelihood that this request would be fulfilled.Chris L.NinjaTrader Customer Service
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 cyberpete76, 03-27-2023, 12:29 AM
|
7 responses
265 views
1 like
|
Last Post
by slightly
Today, 01:47 AM
|
||
Started by renewsaltwater, Today, 01:15 AM
|
0 responses
2 views
0 likes
|
Last Post Today, 01:15 AM | ||
Started by slightly, Today, 12:49 AM
|
0 responses
3 views
0 likes
|
Last Post
by slightly
Today, 12:49 AM
|
||
Started by sdauteuil, 09-23-2021, 10:16 AM
|
4 responses
1,209 views
0 likes
|
Last Post Yesterday, 11:41 PM | ||
Started by agclub, 04-21-2024, 08:57 PM
|
5 responses
36 views
0 likes
|
Last Post
by agclub
Yesterday, 09:47 PM
|
Comment