Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bug in CurrentDayOHL and GetSessionBar for Tick charts

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

    Bug in CurrentDayOHL and GetSessionBar for Tick charts

    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;

    #2
    Hi timwilsn, welcome to our support forums! Not sure I follow exactly, please test this little snippet on your tick charts -
    Code:
     
     
    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.

    Comment


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

      Comment


        #4
        timwilsn,

        Overriding the Plot() method is doable, but is beyond the level of support we can offer since it requires more advanced C# programming.
        Josh P.NinjaTrader Customer Service

        Comment


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

          Comment


            #6
            timwilsn,

            Which method do you have this code segment in?
            Josh P.NinjaTrader Customer Service

            Comment


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

              Comment


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

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                577 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                334 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X