View Full Version : Bug in CurrentDayOHL and GetSessionBar for Tick charts
timwilsn
04-14-2009, 12:25 PM
The following code works just fine when used on minute charts and volume charts whether 24-hr or day-session. For tick charts, it works only when the chart is a 24-hr chart. When tick charts are set to day-session hours, the CurrentDayOHL() and GetSessionBar() methods return NULL no matter what the parameters.
int avgDays = 5;
double avgRange = 0;
if (CurrentDayOHL().CurrentHigh[0] != null && CurrentDayOHL().CurrentLow[0] != null) {
avgRange += CurrentDayOHL().CurrentHigh[0] - CurrentDayOHL().CurrentLow[0];
}
else avgDays =-1;
if (Bars.GetSessionBar(1).High != null && Bars.GetSessionBar(1).Low != null) {
avgRange += (Bars.GetSessionBar(1).High - Bars.GetSessionBar(1).Low);
}
else avgDays =- 1;
NinjaTrader_Bertrand
04-14-2009, 12:57 PM
Hi timwilsn, welcome to our support forums! Not sure I follow exactly, please test this little snippet on your tick charts -
protected override void OnBarUpdate()
{
double avgRange = CurrentDayOHL().CurrentHigh[0] - CurrentDayOHL().CurrentLow[0];
Plot0.Set(avgRange);
}
It works for me regardless of the set session times.
timwilsn
04-14-2009, 01:23 PM
Wouldn't you know it, after I created a clean workspace and new chart to continue testing, the problem went away. The old workspace was acting strangely, in that the bar coloring (different code) would sometimes quit working on the chart's right edge and then correct itself a few bars later.
The price code is now working normally. Mine was running in the Plot() function, as I use it to print some target price text on the chart. Use of these in the Plot() function should be OK, correct?
NinjaTrader_Josh
04-14-2009, 01:38 PM
timwilsn,
Overriding the Plot() method is doable, but is beyond the level of support we can offer since it requires more advanced C# programming.
timwilsn
04-14-2009, 01:47 PM
Spoke too soon. It occurs in the call to Bars.GetSessionBar(). I get the following "Exception - System.NullReferenceException: Object reference not set to an instance of an object" in my try/catch.
The call fails as there isn't enough tick data to go 5 days back. The code is supposed to self-correct by subtracting one for the divisor for missing days (the else clause). However, it appears that I can't even make the call to GetSessionBar to check for a null return, as the code stops functioning altogether unless a try/catch is present.
My question: how do I correctly handle this case of not enough tick data present? Should I bracket each GetSessionBar with a try/catch rather than the check for null return?
The current nonworking code:
if (Bars.GetSessionBar(4).High != null && Bars.GetSessionBar(4).Low != null)
avgRange += (Bars.GetSessionBar(4).High - Bars.GetSessionBar(4).Low);
else avgDays =- 1;
NinjaTrader_Josh
04-14-2009, 02:13 PM
timwilsn,
Which method do you have this code segment in?
Ralph
04-14-2009, 02:14 PM
timwilsn,
in your code you request and use the session bar object within a single command. That's the reason it crashes. If you couldn't work around it an other way, you should insert a test before application:
IBar ibar = Bars.GetSessionBar(4);
if (ibar != null)
{
avgRange += ibar.High;
}
Regards
Ralph
timwilsn
04-14-2009, 03:16 PM
A copy/paste bug, the worst kind. :-) I copied the calculation line beneath to make the compare statement and blindly added the "!= null" to test the High/Low members (which of course is not the object pointer but the member value).
Thanks for the help Ralph, Josh, and Bertrand.