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

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.
    BertrandNinjaTrader Customer Service

    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 burtoninlondon, Today, 12:38 AM
                0 responses
                10 views
                0 likes
                Last Post burtoninlondon  
                Started by AaronKoRn, Yesterday, 09:49 PM
                0 responses
                14 views
                0 likes
                Last Post AaronKoRn  
                Started by carnitron, Yesterday, 08:42 PM
                0 responses
                11 views
                0 likes
                Last Post carnitron  
                Started by strategist007, Yesterday, 07:51 PM
                0 responses
                14 views
                0 likes
                Last Post strategist007  
                Started by StockTrader88, 03-06-2021, 08:58 AM
                44 responses
                3,983 views
                3 likes
                Last Post jhudas88  
                Working...
                X