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 calling 'OnBarUpdate' on bar 439: Function does not accept ... NAN values.

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

    Error calling 'OnBarUpdate' on bar 439: Function does not accept ... NAN values.

    My strategy generates this error message on some futures instruments but not on others:


    Error on calling 'OnBarUpdate' method on bar 439: Function does not accept floating point Not-a-Number values.


    It works fine, error free, with NQ but if I switch to, for example, ES, CL, or GC it wont run and I get the above error (although the specific bar number may be different).

    Any ideas, generally, why this may be happening and how it can be fixed? It seems like there may be missing data or something of this nature but the error never happens with NQ.

    Thank you,

    Kerry

    #2
    Hello Kerry,

    The error is indicating that there is a line of code calling a method, and something supplied has a NaN value.

    What is the specific line of code causing the error?

    If you are unsure, add prints above each line to see which print is the last to appear before the error appears.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello Kerry,

      The error is indicating that there is a line of code calling a method, and something supplied has a NaN value.

      What is the specific line of code causing the error?

      If you are unsure, add prints above each line to see which print is the last to appear before the error appears.
      https://ninjatrader.com/support/foru...121#post791121
      Hi Chelsea,

      Thank you for your suggestion to use print statements to debug. So to be clear, as I wrote above, the strategy script works perfectly fine with NQ but if I switch to another instrument it won't even enable. It immediately shuts down after I click the enable box and the above mentioned error is displayed on the script output screen. I don't think that it will even get to a point where a print statement would execute. Can Visual Studio help in this situation? Otherwise, I may need use brute-force and comment out sections of the script until I isolate the problem. I still also don't understand why it's OK with NQ but switching to another instrument causes it to crash with the NAN error.

      Comment


        #4
        Originally posted by NinjaTrader_ChelseaB View Post
        Hello Kerry,

        The error is indicating that there is a line of code calling a method, and something supplied has a NaN value.

        What is the specific line of code causing the error?

        If you are unsure, add prints above each line to see which print is the last to appear before the error appears.
        https://ninjatrader.com/support/foru...121#post791121
        Chelsea,

        I did some additional investigating and found that the error is strongly dependent on the time scale that I choose. The shorter time scales (less than 5 minutes) seem to cause the error to occur more often on all instruments. When using longer time scales (above 5 minutes) I was able to get the strategy script to work on multiple instruments error free. So it seems to me that there is some sort of issue with loading data from the time series especially on the shorter time scales which results in the NAN error. What do you think? It ought to work on all time scales so long as the time series data isn't corrupted (or missing).

        Thanks again,

        Kerry

        Comment


          #5
          If you can't post any code to help anyone understand
          what you're doing, you need to get creative and add
          print statements that will help you see the problem.

          Chelsea has supplied the best advice available, given
          that you won't (or can't) reveal any code.

          Almost guaranteed, you have some kind of logic error,
          a bug that gets tickled only under certain conditions.

          Many expert C# programmers read these forums,
          many would gladly help you, even download and run
          your code -- but we're blind as wombats out here.

          No one knows what you're trying to do.

          Just my 2˘.

          Last edited by bltdavid; 11-28-2023, 04:08 AM.

          Comment


            #6
            Originally posted by bltdavid View Post
            If you can't post any code to help anyone understand
            what you're doing, you need to get creative and add
            print statements that will help you see the problem.

            Chelsea has supplied the best advice available, given
            that you won't (or can't) reveal any code.

            Almost guaranteed, you have some kind of logic error,
            a bug that gets tickled only under certain conditions.

            Many expert C# programmers read these forums,
            many would gladly help you, even download and run
            your code -- but we're blind as wombats out here.

            No one knows what you're trying to do.

            Just my 2˘.

            David,

            Thanks for your $0.02, much appreciated. I would have posted the code but the script is rather long and so I wanted to first try and narrow it down to the problematic lines and post those rather than have people waste their valuable time looking over lines and lines of code that were irrelevant to the NAN error that had been generated. That is why I was trying to get some general ideas as to what might be going wrong.

            In any case, I was able to resolve the problem, using a few print statements, and explain why it was dependent upon the chosen time scale. It turns out that it was a divide-by-zero issue that occurred when normalizing a probability calculation that I am carrying out in the script.

            For example, using a normalization parameter for the calculations such as math.abs(Close[0] - Close[1]):

            Then on longer time scales (say 4 minutes or longer), during normal trading hours, and assuming an actively traded instrument, there is a high likelihood that Close[0] is NOT EQUAL to Close[1] and so there will NOT be a divide-by-zero error generated since, in this example, math.abs(Close[0] - Close[1]) is unequal to zero.

            However the shorter the time scale (below 4 minutes) the higher the likelihood that Close[0] remains equal to Close[1] which would cause a divide-by-zero, resulting in the NAN error.

            On more lightly traded instruments, even on longer time scales, there is a good possibility that Close[0] remains equal to Close[1] which would cause a divide-by-zero resulting in a NAN error to occur with these instruments even in the longer time frames.

            So resolving the above noted error came down to checking if the particular normalization parameter, used in each instance, is equal to zero in order to avoid a divide-by-zero and the subsequent NAN error that caused the script to crash when it occurred.

            Regards,

            Kerry

            Comment


              #7
              Ah-hah!

              The power of the 'Print' strikes again.

              Simple advice, huge benefits.

              Want to know a professional software engineering secret?

              There are thousands of problems solved daily around the
              world (considering all languages) using variations of this
              technique.

              Glad you found your problem.

              Comment


                #8
                Originally posted by bltdavid View Post
                Ah-hah!

                The power of the 'Print' strikes again.

                Simple advice, huge benefits.

                Want to know a professional software engineering secret?

                There are thousands of problems solved daily around the
                world (considering all languages) using variations of this
                technique.

                Glad you found your problem.

                Yes, I know. I use print statements all the time when developing models in Mathematica (a.k.a. the Wolfram Language).

                Regards,

                Kerry

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by claxxical, 05-30-2017, 12:30 PM
                37 responses
                4,453 views
                0 likes
                Last Post Padan
                by Padan
                 
                Started by SugarDefwebsite, Today, 02:18 AM
                0 responses
                3 views
                0 likes
                Last Post SugarDefwebsite  
                Started by usglucofreeze, Today, 01:19 AM
                0 responses
                7 views
                0 likes
                Last Post usglucofreeze  
                Started by f.saeidi, Today, 01:12 AM
                0 responses
                13 views
                0 likes
                Last Post f.saeidi  
                Started by NinjaTrader_ChelseaB, 03-14-2017, 10:17 AM
                227 responses
                34,319 views
                7 likes
                Last Post rare312
                by rare312
                 
                Working...
                X