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

indicator Time.IsValidDataPoint(0) Exception

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

    indicator Time.IsValidDataPoint(0) Exception

    Hello.

    I use in OnBarUpdate()

    Code:
    if (!Time.IsValidDataPoint(0))
    {
    return;
    }

    my setting in indicator OnStateChange() is

    Code:
    Calculate = Calculate.OnPriceChange;
    IsOverlay = true;
    IsSuspendedWhileInactive = false;
    IsAutoScale = false;
    BarsRequiredToPlot = 0;
    ShowTransparentPlotsInDataBox = true;
    DisplayInDataBox = false;
    If I use the indicator individually, it works.
    The indicator also works in a strategy as a backtest.

    When I want to optimize the indicator in a strategy, an exception occurs

    German
    Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.

    Error message translated into English
    The object reference was not set to an object instance.​​


    The line
    Code:
    Time.IsValidDataPoint(0)
    always causes an error.
    How do I get rid of the error?
    Last edited by 1001111; 12-24-2023, 08:37 AM.

    #2
    Originally posted by 1001111 View Post
    I use in OnBarUpdate()

    Code:
    if (!Time.IsValidDataPoint(0))
    {
    return;
    }
    Why do you have this code?
    What is its purpose?
    Have you ever inserted a 'Print' to see when
    the indicator actually does a return?

    Originally posted by 1001111 View Post
    ​The line
    Code:
    Time.IsValidDataPoint(0)
    always causes an error.
    How do I get rid of the error?
    Since I'm kinda questioning the purpose and usefulness
    of that code, what happens if you just remove it?

    My point is: Time should always have valid values in its
    data series, just like each of Open/Close/High/Low
    should always have valid values.

    I mean, these are internal data series, and the values
    should all be set by internal NinjaTrader code just before
    OnBarUpdate is called.

    Maybe it's just me, but my question is this:
    How can Time.IsValidDataPoint(0) ever be false?

    -=o=-

    Maybe you need a guard, like this,

    Code:
    if (CurrentBar < 0 || !Time.IsValidDataPoint(0))
    {
    return;
    }
    But still, I'm lost why you'd need the IsValidDataPoint check
    on the internal Time data series. IMHO, it serves no purpose.

    Just my 2˘.

    Last edited by bltdavid; 12-24-2023, 05:19 PM.

    Comment


      #3
      Hello 1001111,

      Thanks for your post.

      bltdavid has provided some good information on this topic.

      Why exactly are you checking if Time is set at a barsAgo value relative to the current bar?

      Time[0] would likely always have value values since Time refers to the time of a bar.

      If you remove this code from the script and run an optimization, do you see the error persist? Or, does the error stop appearing?
      Brandon H.NinjaTrader Customer Service

      Comment


        #4
        Hello.

        After
        Code:
        Time.IsValidDataPoint(0)
        ​
        , I just use
        Code:
        Time[0].
        I'll remove the first code and observe the error messages.
        (I probably won't be able to do it today)

        Thanks for the contributions.​

        Comment


          #5
          Originally posted by 1001111 View Post
          After
          Code:
          Time.IsValidDataPoint(0)
          ​
          , I just use
          Code:
          Time[0].
          I'll remove the first code and observe the error messages.
          Why would you do that?

          That change seems a bit pointless.

          What exactly are you wanting this code to achieve?

          Please describe what you want this code to do.

          My opinion is still the same: I think the original code
          is useless and serves no purpose.

          -=o=-

          Let's review.

          You've found this code to be benign in most of your
          use cases, but in one case you get an Exception, and
          you're wondering where that exception is coming
          from -- right?

          But I'm stuck on a more fundamental level.
          I'm wondering why that code even exists.
          What is its purpose?

          I'm suggesting the code is pointless, and since it
          is benign anyways, it can be completely removed,
          which, hey, that would solve the Exception issue.

          Makes sense?




          PS:
          I'll ask you again:

          Have you ever inserted a Print to see when
          the indicator actually does a return from that
          if statement?​


          This is a serious question -- have you done this?
          (I think the answer is 'no'.)

          Comment


            #6
            Originally posted by bltdavid View Post



            Have you ever inserted a Print to see when
            the indicator actually does a return from that
            if statement?​


            No

            I had the "Time.IsValidDataPoint(0)​"so that there would be no errors. But the errors still come.

            Comment


              #7
              The error still occurred. I logged the errors using Exception.

              CurrentBar was greater than 1 but the error occurred.

              State: Historical

              In the strategy I have

              Code:
              IsInstantiatedOnEachOptimizationIteration = false;
              change to

              Code:
              IsInstantiatedOnEachOptimizationIteration = true;



              After that, no more errors occurred.

              How can this be that despite a positive number of CurrentBar, Time[0] is not present?​

              Comment


                #8
                Hello 1001111,

                Thanks for your notes.

                The error message "The object reference was not set to an object instance.​​" means that something you are accessing was not created at the time you are accessing it. There is no specific thing I can advise as to the source of the error.

                To determine what the source of the error is you will need to debug your strategy. This is typically done with print statements added to your code to send a print output to the New>Ninjascript Output window. As you do not know where the error is, you would add a print statement every few lines of code and then observe the output window when you apply the strategy. The last print statement value shown in the output window would indicate the error occurs after that print statement, you can then add further print statements to zero in on the offending code.

                Here is a link to our tips on debugging: https://ninjatrader.com/support/help...script_cod.htm

                ​Below is a link to a forum post that demonstrates how to use prints to understand behavior.
                https://ninjatrader.com/support/foru...121#post791121
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

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