Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trying to calculate SMA of my custom variable

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

    Trying to calculate SMA of my custom variable

    Hi, I'm creating a variable in OnStateChange() and State == State.SetDefaults like this:

    myVariable = new Series<double>(this);

    In OnBarUpdate(), I set myVariable[0] to custom logic.

    I want to calculate the SMA of it and I'm trying to do that with this, also defined in state.SetDefaults:

    mySMA = SMA(myVariable, length);:

    But I'm getting the SMA of the close price, not the SMA of myVariable.

    I would greatly appreciate any help with this.

    Thanks!
    Norman


    #2
    Hello Norman,

    Welcome to the NinjaTrader forums!

    That would be unexpected, and I am not able to reproduce on my end.

    Attached is simple test script that just sets the series value to 5 to show that it is using this value for the SMA input series.

    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you very much for the quick reply, Chelsea!

      Yes, I was able to get your example to work. There must be something wrong with the data that I am creating.

      When I try to use Print() I don't see these statements in the output window. What do you think the issue with that could be? I'd like to use Print() to try to find what's wrong with my data.

      Norman

      Comment


        #4
        Hello Norman,

        Are you adding the prints at the top of OnBarUpdate() outside of any conditions?

        After adding the prints have you compiled the script, opened the NinjaScript Output window, before reloading the script on the chart?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I see it now! I was looking in the VS Studio output window, which is the wrong output window. Thank you.

          Comment


            #6
            Hello Norman,

            Below is a link to a support article on using prints to understand behavior.


            The NinjaScript Editor 401 video linked provides a visual demonstration.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Cool, thanks. Now that I have print working, below is my custom variable vByM that I'm passing to the SMA and that value of the SMA, which is returning NaN:

              vByM 3.57142857142857
              mySMA2[0]NaN
              vByM 1
              mySMA2[0]NaN
              vByM 0.125
              mySMA2[0]NaN

              I'm using the same pattern as in the example indicator you made but changed the length to 3. What do you think could be causing this?

              Comment


                #8
                Hello nleitman,

                NaN means not a number. As an example trying to divide by 0 would result in NaN.

                There's something about the values being supplied to the SMA that might be causing an issue.

                May I have the full output? (Right-click the output window select Save as)
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  I was using playback mode and rewound the playback which caused some other text to be printed. Otherwise, all the output is basically like what I posted above. See the attached file.
                  Attached Files

                  Comment


                    #10
                    Hello nleitman,

                    I was hoping to see a value on the series that would cause the NaN but I'm not seeing that.

                    May I have an export of the script to test on my end?

                    To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
                    1. Click Tools -> Export -> NinjaScript Add-on...
                    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
                    3. Click the 'Export' button
                    4. Enter the script name in the value for 'File name:'
                    5. Choose a save location -> click Save
                    6. Click OK to clear the export location message
                    By default your exported file will be in the following location:
                    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
                    Below is a link to the help guide on Exporting NinjaScripts.


                    Once exported, please attach the file as an attachment to your reply.​
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Chelsea,

                      Attached is my indicator. Thank you so much for looking into this.

                      Norman
                      Attached Files

                      Comment


                        #12
                        Hello nleitman,

                        Custom series cannot be instantiated and indicator calls cannot be made until State.DataLoaded. You need to move lines 59 to 65 from State.SetDefaults to State.DataLoaded.

                        Have a look at the example in post # 2.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Chelsea, I fixed that. Then I got this error:

                          Indicator 'FreedomOfMovement': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

                          so I added this statement to the beginning of OnBarUpdate()

                          if (CurrentBar == 0)
                          return;​

                          I also noticed that the very first value being passed to SMA is NaN so I added

                          if (double.IsNaN(vByM[0]))
                          return;

                          to fix that problem. But the output of SMA is still NaN:

                          vByM 0.1
                          mySMA2[0]NaN
                          vByM 0.1
                          mySMA2[0]NaN
                          vByM 10
                          mySMA2[0]NaN​

                          My updated indicator is attached. What do you suggest?

                          Thanks,
                          Norman​
                          Attached Files

                          Comment


                            #14
                            Hello nleitman,

                            Try directly assigning vByM[0] a value of 5 (instead of nVol / nMov) as a test.

                            Are you still getting that error?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              No, that worked:

                              vByM 5
                              mySMA2[0]2.5
                              vByM 5
                              mySMA2[0]3.33333333333333
                              vByM 5
                              mySMA2[0]5​

                              So it doesn't like nVol/nMov, but it's a real number so it should work.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              580 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              335 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              102 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              554 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              552 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X