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!
See more
See less

Partner 728x90

Collapse

Error with GetSessionBar

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Error with GetSessionBar

    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




    #2
    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?
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      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

      Comment


        #4
        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
        }
        Ryan M.NinjaTrader Customer Service

        Comment


          #5
          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;

          }

          Comment


            #6
            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);
            Ryan M.NinjaTrader Customer Service

            Comment


              #7
              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?

              Comment


                #8
                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)
                Ryan M.NinjaTrader Customer Service

                Comment


                  #9
                  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.

                  protectedoverridevoid Initialize()
                  {
                  CalculateOnBarClose = true;


                  }
                  ///<summary>
                  /// Called on each bar update event (incoming tick)
                  ///</summary>
                  protectedoverridevoid 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

                  Comment


                    #10
                    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.
                    Ryan M.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Touch-Ups, Today, 10:36 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post Touch-Ups  
                    Started by geddyisodin, 04-25-2024, 05:20 AM
                    8 responses
                    61 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by jxs_xrj, 01-12-2020, 09:49 AM
                    4 responses
                    3,289 views
                    1 like
                    Last Post jgualdronc  
                    Started by Option Whisperer, Today, 09:55 AM
                    0 responses
                    5 views
                    0 likes
                    Last Post Option Whisperer  
                    Started by halgo_boulder, 04-20-2024, 08:44 AM
                    2 responses
                    24 views
                    0 likes
                    Last Post halgo_boulder  
                    Working...
                    X