View Full Version : Error with GetSessionBar
eqtrader
07-28-2010, 09:48 AM
I am running NinjaTrader 6.5.1000.16 and have an issue with GetSessionBar. I am using the IQ DTN data feed for real-time data.
In a 1-minute chart, when I run the following code to get the last sessions Daily Bar:
Print("before");
if ( Bars.GetSessionBar(1) == null )
Print("No data");
else
Print("Data Available");
Only the word "before" is printed, and neither subsequent print statements are printed. It seems that it is failing on Bars.GetSessionBar(1). My session in my 1-minute chart is set at 12:00 am, 12:00am.
Thoughts?
Thank you
NinjaTrader_RyanM
07-28-2010, 10:02 AM
Hello eqtrader,
Welcome to the NinjaTrader forums!
Are you seeing any error messages in the log tab of the control center when running this script?
eqtrader
07-28-2010, 10:05 AM
Yes...
It says that "Failed to call Initialize"..... "Bars" property cannot be accessed from within "Initialize" method.
I want to be able to analyze a set of daily bars one time before a strategy runs for that day...Is there a different method to call to do this?
Thanks
NinjaTrader_RyanM
07-28-2010, 10:13 AM
You would have to access this property from OnBarUpdate(), not the initialize method. If you want it to execute only once, you can access from the first bar in the series.
if (CurrentBar == 0)
{
//GetSessionBar logic here
}
eqtrader
07-28-2010, 11:59 AM
I tried a similiar thing with the below code placed inside OnBarUpdate()
However, I am receiving a null when I call Bars.GetSessionBar()....?
---------
if ( initialize == -1 )
{
if ( Bars.GetSessionBar(1) == null )
Print("No data");
else
{
Print( Bars.GetSessionBar(1).Open.ToString() );
Print( Bars.GetSessionBar(1).High.ToString() );
Print( Bars.GetSessionBar(1).Low.ToString() );
Print( Bars.GetSessionBar(1).Close.ToString() );
}
initialize = 1;
}
NinjaTrader_RyanM
07-28-2010, 01:18 PM
The data may not be available at the point you're trying to access it. Use the snippet below to find the CurrentBar value where this data is available.
Print("before");
if ( Bars.GetSessionBar(1) == null )
Print("No data");
else
Print("Data Available " + CurrentBar);
eqtrader
07-28-2010, 01:52 PM
According to the documentation:
// Print the prior session close
if (Bars.GetSessionBar(1) != null)
Print("The prior session's close is: " + Bars.GetSessionBar(1).Close.ToString());
This should get the prior sessions data. i.e. Yesterday's daily OHLC.
Why isn't this working?
NinjaTrader_RyanM
07-28-2010, 02:01 PM
I'm not sure what results you're getting with this. Please share the complete code snippet you're using here and the series you are running it against. (Instrument, interval, days loaded)
eqtrader
07-28-2010, 02:04 PM
In my 5-minute chart, I setup the parameters of format data series to go back 50-days. The earliest date I can see on the chart is 6/8/2010.
protected override void Initialize()
{
CalculateOnBarClose = true;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if ( initialize == -1 )
{
for (int i=0; i<20; ++i)
{
if ( Bars.GetSessionBar(i) == null )
{
Print("No data");
}
else
{
Print( Bars.GetSessionBar(i).ToString() );
Print( Bars.GetSessionBar(i).Time.ToString() );
Print( Bars.GetSessionBar(i).Open.ToString() );
Print( Bars.GetSessionBar(i).High.ToString() );
Print( Bars.GetSessionBar(i).Low.ToString() );
Print( Bars.GetSessionBar(i).Close.ToString() );
}
}
initialize = 1;
}
}
Thank you
NinjaTrader_RyanM
07-28-2010, 02:41 PM
I got some clarification on these methods, and the issue you're seeing is when trying to access these values before they exist. This is a convenience method to provide values you might not have access to through your data provider. They're meant to be accessed from the latest (most recent) session only.
My earlier suggestion to capture these values at the start of the strategy run may be leading you off-track. To get back on track:
These values are not available for every bar in the series. When you enclose the code in your initialize = -1 block, you're only allowing this to be processed on the first bar of the series, where this data is NOT available.