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

Determining time stamp of the last bar on a chart

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

    Determining time stamp of the last bar on a chart

    When I want to catch the timestamp of the last bar in OnRender(), I have used

    Code:
    int  lastBarPainted  = ChartBars.ToIndex;
    DateTime lastBarPaintedTime  = BarsArray[0].GetTime(lastBarPainted);
    However, this code will throw an exception when the bar period of a multi-series chart is reduced, for example when you toggle a 30 min bar period to 5 min bar period.


    Here are the steps to reproduce the problem.

    (1) Take any indicator, add this custom plot and compile.

    Code:
    protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
    {
        if (ChartBars == null || ChartControl == null) 
            return;
        int    lastBarPainted  = ChartBars.ToIndex;
        DateTime lastBarPaintedTime  = BarsArray[0].GetTime(lastBarPainted);
        base.OnRender(chartControl, chartScale);
    }
    (2) Open a new default chart and add two data series such as ES 09-17 and YM 09-17 with an identical bar period such as 60 min. You should now see a 60 min chart with two panels holding bars objects.

    (3) Add the modified indicator to the upper panel.

    (4) Change the bar period from 60 min to 5 min.

    The indicator will now throw an exception when BarsArray[0].GetTime(lastBarPainted) is called. No exception will be thrown when the bar period is increased from 5 min back to 60 min. Error messages will only be produced when the bar period is reduced.

    #2
    Hello Harry,

    I've tested in an indicator but I am not able to reproduce any errors.
    Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.


    If you test this script are you able to produce errors?

    Are you using the latest version of NinjaTrader 8.0.7.1?
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      I've tested in an indicator but I am not able to reproduce any errors.https://www.screencast.com/t/wlWUBrFea If you test this script are you able to produce errors?
      I believe Harry mentioned to manually add "2" Data Series.. For instance ES in top panel and YM in bottom panel.. Apply your test indy to top panel ES.. then change the interval selecter to a lesser value..

      Looks like I've got a couple scripts that are/were effected by this as well.. In this particular case, I believe you could change it as below, and it will no longer produce that error..

      Code:
      DateTime lastBarPaintedTime  = BarsArray[0].GetTime(lastBarPainted);
      to

      Code:
      DateTime lastBarPaintedTime  = ChartBars.Bars.GetTime(lastBarPainted);

      But... This doesn't work for all cases.. I've got another case where I'm looping thru all the Plot Values[x] for various coordinates, and not real sure how to program around this one.. This produces same type of error..

      Loop for X
      Code:
      chartScale.[COLOR=#080808]GetYByValue[COLOR=#000000]([/COLOR][COLOR=#080808]Values[x][/COLOR][COLOR=#000000].[/COLOR][COLOR=#080808]GetValueAt[/COLOR][COLOR=#000000]([/COLOR][COLOR=#080808]ChartBars[/COLOR][COLOR=#000000].[/COLOR][COLOR=#080808]ToIndex[/COLOR][COLOR=#000000] -[/COLOR] [COLOR=#ff8c00]1[/COLOR][COLOR=#000000]))[/COLOR]
      [/COLOR]
      EndLoop




      -=Edge=-
      NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello Harry,

        I've tested in an indicator but I am not able to reproduce any errors.
        Free online storage and sharing with Screencast.com. 2 GB of storage and 2 GB of bandwidth per month for free. We won't compress, alter or take ownership of your content.


        If you test this script are you able to produce errors?

        Are you using the latest version of NinjaTrader 8.0.7.1?

        Hello Chelsea,

        Thank you for coming back.

        With a single bar series on your chart, there will be no error. If you repeat the experiment with a second bar series added, then you should see the error in the logs when you reduce the bar period.
        Attached Files

        Comment


          #5
          Hi =Edge=- and Harry,

          Thank you for pointing that out.

          This would be an attempt to supply the bar number of series 2 where the indicator is in the panel of, but would be attempting to get a bar from series 1, is this correct?

          Is there any check to make sure that bar exists first?

          I was able to reproduce and I'm looking further into the exact cause.

          I appreciate your patience.
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Originally posted by -=Edge=- View Post
            In this particular case, I believe you could change it as below, and it will no longer produce that error..

            Code:
            DateTime lastBarPaintedTime  = BarsArray[0].GetTime(lastBarPainted);
            to

            Code:
            DateTime lastBarPaintedTime  = ChartBars.Bars.GetTime(lastBarPainted);

            This actually fixes my problem. Thank you very much!

            However, it remains an interesting question, why BarsArray[0].GetTime(lastBarPainted) does not work on a multi bar series chart, when the bar period is reduced.

            - it works on simple charts
            - it works on multi bar series charts when the instrument is changed
            - it works on mulitbar series charts when the bar period is increased

            - but it does not work when a smaller bar period is selected

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hi =Edge=- and Harry,

              Thank you for pointing that out.

              This would be an attempt to supply the bar number of series 2 where the indicator is in the panel of, but would be attempting to get a bar from series 1, is this correct?

              Is there any check to make sure that bar exists first?

              I was able to reproduce and I'm looking further into the exact cause.

              I appreciate your patience.

              The second bar panel is not involved at all. It is simply needed to produce the error.

              The indicator is applied to the first panel. It tries to access the index of the last bar of BarsArray[0], which holds the chart bars of the first panel.

              Comment


                #8
                Hello,

                What I think I'm finding is that the dataloads before the script is reloaded with the new data. This causes the last update of OnRender to have the ChartBars.Index much higher than the BarsArray[0].Count.

                Print(string.Format("toindex: {0}, count: {1}", ChartBars.ToIndex, BarsArray[0].Count));

                Print this before the GetTime call to see what I mean.

                Adding a condition to check that the index is not higher than the count will prevent the error.

                Code:
                if (ChartBars.ToIndex > BarsArray[0].Count)
                {
                	base.OnRender(chartControl, chartScale);
                	return;
                }
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello,

                  What I think I'm finding is that the dataloads before the script is reloaded with the new data. This causes the last update of OnRender to have the ChartBars.Index much higher than the BarsArray[0].Count.

                  Print(string.Format("toindex: {0}, count: {1}", ChartBars.ToIndex, BarsArray[0].Count));

                  Print this before the GetTime call to see what I mean.

                  Adding a condition to check that the index is not higher than the count will prevent the error.

                  Code:
                  if (ChartBars.ToIndex > BarsArray[0].Count)
                  {
                      base.OnRender(chartControl, chartScale);
                      return;
                  }
                  That makes perfect sense, as it explains why the error only occurs, when the bar period is reduced. In that case the value of ChartsBars.ToIndex is increased, while BarsArray[0] still holds the old bars.

                  Your solution to the problem also prevents the error.

                  I am definitely happy with the two workarounds.


                  But shouldn't NInjaTrader check whether the new BarsArray[0] has been loaded, before OnRender() is being performed?

                  This still looks like a bug, which should be fixed.

                  Comment


                    #10
                    Originally posted by Harry View Post
                    I am definitely happy with the two workarounds. But shouldn't NInjaTrader check whether the new BarsArray[0] has been loaded, before OnRender() is being performed? This still looks like a bug, which should be fixed.
                    I'm happier with the first then the second.. heh.. but hey, that does work, so Thank You for that workaround ChelseaB...

                    Although while checking the barsarray count against the chartbars does prevent the error from happening, I'm kinda in agreement with FatTails here, This feels like something that maybe should be looked at.. As I'd rather not add un-necessary logic into every render pass if I don't have too..


                    -=Edge=-
                    NinjaTrader Ecosystem Vendor - High Tech Trading Analysis

                    Comment


                      #11
                      Harry, -=Edge=-,

                      Development has agreed this is a bug and has already provided a tracking ID, ID #NTEIGHT-11894.

                      Thank you for reporting this.

                      As new iterations of NinjaTrader 8 are released, please check the release notes for this ID. Once the ID appears in the release notes please (update and) test for the behavior once more.
                      Chelsea B.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by fx.practic, 10-15-2013, 12:53 AM
                      4 responses
                      5,403 views
                      0 likes
                      Last Post Bidder
                      by Bidder
                       
                      Started by Shai Samuel, 07-02-2022, 02:46 PM
                      4 responses
                      94 views
                      0 likes
                      Last Post Bidder
                      by Bidder
                       
                      Started by DJ888, Yesterday, 10:57 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post DJ888
                      by DJ888
                       
                      Started by MacDad, 02-25-2024, 11:48 PM
                      7 responses
                      158 views
                      0 likes
                      Last Post loganjarosz123  
                      Started by Belfortbucks, Yesterday, 09:29 PM
                      0 responses
                      8 views
                      0 likes
                      Last Post Belfortbucks  
                      Working...
                      X