Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SMA returns double?

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

    SMA returns double?

    Hi,

    I'm trying the following:

    doubleValue = SMA(mySeries, myLength);

    I'm getting the error:

    Cannot implicitily convert type 'NinjaTrader.Indicator.SMA' to 'double'

    Is this proper usage of SMA? If not, what is the correct way to get the SMA in a strategy?

    Thanks,

    Folls

    #2
    Code:
    doubleValue = SMA(mySeries, myLength)[COLOR=Red][0][/COLOR];
    You need to specify which bar you want to reference. This is done by the index value. 0 means current bar. 1 means previous bar. 2 means two bars ago. etc.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      thanks. now another related issue.

      Thanks that worked. It now compiles. I'm attempting to port some code I've used in TS for years. I'm running it in a strategy.

      Although it compiles in the strategy, it just hangs now. Do you see anything obvious to make this work?

      //compute cci for the spread
      //build the series needed for computation
      high = Highs[0][0] * symbol1Mult * symbol1Contracts - Highs[1][0] * symbol2Mult * symbol2Contracts;
      low = Lows[0][0] * symbol1Mult * symbol1Contracts - Lows[1][0] * symbol2Mult * symbol2Contracts;
      close = Closes[0][0] * symbol1Mult * symbol1Contracts - Closes[1][0] * symbol2Mult * symbol2Contracts;

      //set the dataseries
      typicalSpread.Set(high + low + close);

      if (CurrentBar <= CCILength)
      {
      cciSpread = 0;
      }
      else
      {
      mean = SMA(typicalSpread, CCILength)[0];

      avgDev = 0;

      for (int idx = CCILength - 1; idx >= 0; idx--)
      {
      avgDev = avgDev + Math.Abs(typicalSpread[idx] - mean);
      }
      avgDev = avgDev / CCILength;

      if (avgDev == 0)
      {
      cciSpread = 0;
      }
      else
      {
      cciSpread = (typicalSpread[0] - mean)/(0.015 * avgDev);
      }

      }

      Thanks!

      Folls

      Comment


        #4
        Please check the Log tab to see if the strategy produces any error messages.
        RayNinjaTrader Customer Service

        Comment


          #5
          There was an error.

          Error on calling 'OnBarUpdate' method for strategy 'Spread'. Index was out of range. Must be non-negative and less that the size of the collection. Parameter name: index.

          Although this is certainly a very descriptive error message, I can't figure out what might be wrong. Any ideas?

          Thanks again,

          Folls

          Comment


            #6
            It means that somewhere you are passing in an index value to an array where an object at that index value does not exist.

            Here are some helpful links


            RayNinjaTrader Customer Service

            Comment


              #7
              Thanks. I'll take a look.

              Comment


                #8
                The problem appears to be inside the for loop shown in a previous post. When I index down, it dies immediately with a value of 49. When I index up, it goes from 1 to 21 and dies at 21. This is repeatable.

                The only thing that uses the for loop index is "typcialSpread[idx]" where typicalSpread is a DataSeries I construct as shown in the code I posted.

                It appears that my typicalSpread series is not more than 20 deep. Why would this be? I execute typicalSpread.Set for every OnBarUpdate. (as shown in the code in a previous post.) I have multiple days of 3 minute bars in the chart.

                Does it matter how CalculateOnBarClose is set? This is set to false currently.

                Is the series built based on every bar? For example if there are 30 3 minute bars, shouldn't there be 30 elements in the series?

                Thanks,

                Folls

                Comment


                  #9
                  There are the same number of elements in a DataSeries as number of bars on a chart provided that you Set() the value. I know you say that you are setting a value for each bar but if you get an index out of range error then for sure you are not setting a value for each bar or you are passing in an index value that is greater than the number of bars in the chart.
                  RayNinjaTrader Customer Service

                  Comment


                    #10
                    Here is the code that is at the start of OnBarUpdate()

                    //build the series needed for computation
                    high = Highs[0][0] * symbol1Mult * symbol1Contracts - Highs[1][0] * symbol2Mult * symbol2Contracts;
                    low = Lows[0][0] * symbol1Mult * symbol1Contracts - Lows[1][0] * symbol2Mult * symbol2Contracts;
                    close = Closes[0][0] * symbol1Mult * symbol1Contracts - Closes[1][0] * symbol2Mult * symbol2Contracts;

                    //set the dataseries
                    typicalSpread.Set(high + low + close);

                    counter = counter + 1;
                    Print("Series.set = " + counter);
                    Print("Current bar = " + CurrentBar);

                    if (CurrentBar <= CCILength)
                    the for loop is executed here and dies because there is not enough data in the series.

                    This is the output from the Print statements:
                    Series.set = 1
                    Current bar = 20
                    Series.set = 2
                    Current bar = 62
                    idx = 49

                    So for some reason, the set is only getting called twice. Once for bar number 20 and once for bar number 62. I have multiple days of data on the chart.

                    The second symbol gets added with:
                    Add(addContract, BarsPeriod.Id, BarsPeriod.Value);

                    Any ideas?

                    Thanks again,

                    Folls

                    Comment


                      #11
                      Must be some bug in your code.

                      - Assuming CalculateOnBarClose == true
                      - Print(CurrentBar) should be sequential 20, 21, 22, 23 etc...
                      - If you add this as the first line of OnBarUpdate() you should see this

                      I know I am repetitive but I point you back to debugging. Strip down your code to 1 line, if it works, add another line until you come to the line that does not work as you expect.
                      RayNinjaTrader Customer Service

                      Comment


                        #12
                        CalculateOnBarClose is set to false.

                        I need to know values in the middle of the bar, make decisions, and take actions in the middle of the bar.

                        What is the correct way to build the DataSeries with CalculateOnBarClose set to false?

                        Thanks again,

                        Folls

                        Comment


                          #13
                          This setting is irrelevant for setting values in a DataSeries object.
                          RayNinjaTrader Customer Service

                          Comment


                            #14
                            Great. So if I have the following in OnBarUpdate(), it should build a good DataSeries?

                            //build the series needed for computation
                            high = Highs[0][0] * symbol1Mult * symbol1Contracts - Highs[1][0] * symbol2Mult * symbol2Contracts;
                            low = Lows[0][0] * symbol1Mult * symbol1Contracts - Lows[1][0] * symbol2Mult * symbol2Contracts;
                            close = Closes[0][0] * symbol1Mult * symbol1Contracts - Closes[1][0] * symbol2Mult * symbol2Contracts;

                            //set the dataseries
                            typicalSpread.Set(high + low + close);

                            I've spent over 10 hours trying to figure out my problem. Something isn't working. I don't think I'm doing anything complicated and I appear to be following the rules.

                            Thanks,

                            Folls

                            Comment


                              #15
                              What I can say is if you have the following:

                              OnBarUpdate()
                              {
                              yourDataSeries.Set(someValue);
                              }

                              It will always set a value on each bar regardless if CalculateOnBarClose is set to true or false.

                              I can not stress enough the requirement to start with a simple example as per this post. Confirm it works as you expect which it will. Then add the next layer of logic. Confirm that it works as you expect. And go through this itterative process until you reach a point where it does not work as expected. Then you know what is causing your problem.
                              RayNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              633 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              364 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              105 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              567 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              568 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X