Any help would be appreciated.!
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Historical state or CurrentBar issue - indicator run amok
Collapse
X
-
Historical state or CurrentBar issue - indicator run amok
I'm trying ot create an indicator that looks at the latest 4 bars of a chart and send me and send me an email if a particular condition occurs.I set it to use Calculate.OnBarClose but when I apply it to a chart and switch the IsVisible to on, it sends a flood on email and crashes NT. My guess is it is processing more bars than it should. I've tried changing the value of "CurrentBar", tried using "IsFirstTickOfBar", all to no avail. I'm attaching a snippet of some of the relevant code. I believe the resolution will involve State === historical but I'm not sure where it would go and the how it is worded correctly.
Any help would be appreciated.!Tags: None
-
Hello sobertradingpartner,
Thank you for your post.
I also suspect that it is attempting to send a flood of emails when it runs through the historical data upon starting the indicator.
I recommend adding logic to either prevent the indicator from processing in historical (you can add this at the top of OnBarUpdate()):
Or adding a check in the condition that would cause the email to be sent that makes sure the indicator is running in real-time.Code:if (State == State.Historical) return;
Code:if (State == State.Realtime) // send email
Please let me know if you have any further questions.
- Likes 1
-
Are you sure?Originally posted by NinjaTrader_Gaby View PostI also suspect that it is attempting to send a flood of emails when it runs through the historical data upon starting the indicator.
The SendMail documentation says,
"This method can only be called once the State has reached State.Realtime.
Calls to this method in any other State will be silently ignored..."
So, according to the help guide, calls to SendMail in State.Historical should
not be causing the problem.
Just my 2˘.
Comment
-
That worked perfectly, thank you! Incidentally, I ended up using both - added "Historical" just after OnBarUpdate and "Realtime" to the logic that sends the email. Does it matter what the "CurrentBar" settings is?Originally posted by NinjaTrader_Gaby View PostHello sobertradingpartner,
Thank you for your post.
I also suspect that it is attempting to send a flood of emails when it runs through the historical data upon starting the indicator.
I recommend adding logic to either prevent the indicator from processing in historical (you can add this at the top of OnBarUpdate()):
Or adding a check in the condition that would cause the email to be sent that makes sure the indicator is running in real-time.Code:if (State == State.Historical) return;
Code:if (State == State.Realtime) // send email
Please let me know if you have any further questions.
Comment
-
First off, let's consider this code,Originally posted by sobertradingpartner View PostI'm trying ot create an indicator that looks at the latest 4 bars of a chart and send me and send me an email if a particular condition occurs.I set it to use Calculate.OnBarClose but when I apply it to a chart and switch the IsVisible to on, it sends a flood on email and crashes NT. My guess is it is processing more bars than it should. I've tried changing the value of "CurrentBar", tried using "IsFirstTickOfBar", all to no avail. I'm attaching a snippet of some of the relevant code. I believe the resolution will involve State === historical but I'm not sure where it would go and the how it is worded correctly.
Any help would be appreciated.!
Why the negative number? That's wrong.Code:if (CurrentBar < -2) return;
I see the highest BarsAgo index used is 3,
so that guard code should actually be,
Do you understand why?Code:if (CurrentBar < 3) return;
-=o=-
Try changing your code to this,
IsSuspendedWhileInactive = false;
See if that makes a difference.
-=o=-
You'll need to debug your code.
Instead of calling SendMail, use Print instead,
then isolate why that Print is being called so
much using other Print statements.
//SendMail(....);
Print("Bar=" + CurrentBar + " Calling SendMail now");
If you need more help than that, we'd probably
need to see more code, esp the code that is
calling SendMail.
Last edited by bltdavid; 02-16-2024, 09:53 AM.
- Likes 1
Comment
-
Thank you. I've read, but I guess not correctly understood the CurrentBar usage. Some said -1 some said 1. The use case examples weren't very helpful. I appreciate your clarifying that. My point was to make sure all the bars loaded, then run off the last four bars (0-3). As that part of code is working now it was as I suspected, it was running off the historical data.Originally posted by bltdavid View Post
First off, let's consider this code,
Why the negative number? That's wrong.Code:if (CurrentBar < -2) return;
I see the highest BarsAgo index used is 3,
so that guard code should actually be,
Do you understand why?Code:if (CurrentBar < 3) return;
-=o=-
Try changing your code to this,
IsSuspendedWhileInactive = false;
See if that makes a difference.
-=o=-
You'll need to debug your code.
Instead of calling SendMail, use Print instead,
then isolate why that Print is being called so
much using other Print statements.
//SendMail(....);
Print("Bar=" + CurrentBar + " Calling SendMail now");
If you need more help than that, we'd probably
need to see more code, esp the code that is
calling SendMail.
Last edited by sobertradingpartner; 02-16-2024, 10:20 AM.
Comment
-
But, SendMail is supposed to do nothing when called during State.Historical.Originally posted by sobertradingpartner View PostAS that part of code is working now it was as I suspected, it was running off the historical data.
You should confirm that, with something like this,
SendMail(..., "Testing SendMail",
"Bar=" + CurrentBar + " State=" + State + " Calling SendMail");
Are you using the SendMail API?
Comment
-
Comment
-
I'm using an added "SendMailChart" function as shown in the attachment.Originally posted by bltdavid View Post
But, SendMail is supposed to do nothing when called during State.Historical.
You should confirm that, with something like this,
SendMail(..., "Testing SendMail",
"Bar=" + CurrentBar + " State=" + State + " Calling SendMail");
Are you using the SendMail API?
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
580 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
335 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
102 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
554 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
552 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment