Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EMA array seems to hold 1 value

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

    EMA array seems to hold 1 value

    These two statements are in OnBarUpdate. Currently all other code has been removed, as far as I can tell the EMA array only has a single value. Unless I am referencing it wrong but when I check the values array it seems to also indicate that there is only one value in that array.

    This works => Print("EMA " + EMA(Close,9)[0]);
    This doesn't => Print("EMA " + EMA(Close,9)[1]);​

    I checked a bunch of sample code and they say it should work EMA(9)[30] should show the 9 EMA value 30 bars ago but I get an error that states "You are accessing an index with a value that is invalid since its out of range".... anytime I reference any other index in the EMA other than the 0th position.
    Am I missing something? Is there a different way to get EMA values for past bars?

    Thank you in advance.

    #2
    Hello TimWhitbeck,

    You need to add a current bar check if you are looking at bars ago greater than the 0. On bar 0 in processing you only have 1 bar so if you use [1] that will produce an error.

    You can use a condition like this:
    Code:
    if(CuurrentBar < 1) return;
     Print("EMA " + EMA(Close,9)[1]);​

    Comment


      #3
      Jesse Sorry I am new to Ninja but not C#, so I think data context is where I am struggling. At strategy start I can look back 256 bars I think its the maximumbarslookback setting defined in the defaults. It seems from your answer though that for the EMA I am going to have an array that builds from when the strategy starts on for as long as it runs. So each stop and start of the strategy is going to reset that array. Is that right? If so is there a way to pass the EMA object the bars array I have and get it to populate back a number of bars so that it doesn't reset each time I stop the strategy? Or did I completely misunderstand how it works.

      Thanks again!

      Comment


        #4
        Hello TimWhitbeck,

        Yes each time you start and stop the strategy it rebuilds the data from bar 0. There is not a way to maintain the data between starting and stopping the strategy, it needs to recalculate each time you start it.

        The maximumbarslookback would not be related to this problem. If you get an error on the first bar of processing because you used 1 bars ago that would be expected if you didn't add a CurrentBar check.

        Please do the following:
        From the control center open the New -> Strategy builder
        In the conditions and actions screen create the following action under the do the following section:
        1. Click Add
        2. Click Misc -> Print
        3. Click Set inside the textbox
        4. Click Set inside the string0 textbox
        5. Select Indicators -> EMA
        6. Set the bars ago to 1
        7. Click OK until you get back to the conditions and actions screen

        At this point you can click View Code, that will show you the correct way to print the EMA of 1 bars ago along with the error checking that is required to do that.

        Comment


          #5
          Jesse I think you were misunderstanding my point. I really just want to get a grasp on what data I have access to and if I can utilize that to get what I am looking for. So I can get the high,low, open, close of a previous bar on the chart in the bar array utilizing the array index. So the strategy has access to data before it started. It does not however have access to the EMA data. So lets start there is that correct? Because I am currently printing out the high low open close of previous bars in the chart when I run the strategy and I am getting data with out a problem.

          Comment


            #6
            Hello TimWhitbeck,

            Jesse is correct here.

            Data starts processing from CurrentBar 0 each time the script is enabled. On the first bar, it is not possible to retrieve a previous bar's value.

            May I confirm you have added:

            if (CurrentBar < 1) return;

            to the top of OnBarUpdate(), and the error is still occurring?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Chelsea I can not @ you your name doesn't come up, unless you are Chelsea Chelsea? Anyways I understand the need for currentbar and checking to make sure the index I am using is valid that wasn't really what my question was. I was more asking about data. What I don't understand is in the same procedure I have a print statement giving me high[1] and low[1] and open[1] and close[1] and the values its gives me line up with the values of the previous candle. So It seems like even though its doing that it shouldn't? Because I can screenshot it and send it to you to prove it but I don't think that gets me anywhere.
              .
              That said though we can close this, it sounds like working or not the concept is that a strategy can only utilize the data that has occurred during its run so it can't look back in the chart and there is no way to import it so you have to let the strategy run to get enough data to be present to run any kind of calculations off of.

              Thanks!

              Comment


                #8
                I apologize but I should have said how much I appreciate y'alls help. I appreciate how quickly you have responded and your time on my questions! Jesse and Chelsea! Sorry still don't know your @

                Comment


                  #9
                  I have a print statement giving me high[1] and low[1] and open[1] and close[1] and the values its gives me line up with the values of the previous candle. So It seems like even though its doing that it shouldn't? Because I can screenshot it and send it to you to prove it but I don't think that gets me anywhere.
                  The [1] value of each series is the 1 BarsAgo from right now in processing. If we assume that you are on bar 5 of processing meaning you applied the script and OnBarUpdate has been called 5 times the [1] BarsAgo value of each series should be the 4th bar on the chart. If you are now on bar 6 of processing the [1] bars ago value will be the 5th bar on the chart. As the script processes bars the [1] barsago value is always going to trail by 1 bar.

                  it sounds like working or not the concept is that a strategy can only utilize the data that has occurred during its run so it can't look back in the chart
                  You start off by looking back in the chart. When you apply a script it starts at the leftmost bar of the chart which is bar 0. It processes all historical bars until entering realtime. Once its in realtime you can look back at the entire dataset if you wanted to. The error you originally posted about would be if you tried to look back at data before it had been processed, for example looking back 1 BarsAgo on bar 0 will produce that error.

                  and there is no way to import it so you have to let the strategy run to get enough data to be present to run any kind of calculations off of.
                  Because you start all the way back at the beginning of the chart your strategy would have enough data to run calculations. You need to specify when you load a chart how much historical data you want to load to accommodate how much data your calculations need. You then apply the script. Once applied it processes all of that historical data up until realtime where it can start working.






                  Comment


                    #10
                    Jesse Oh my gosh that was a great explanation, I think I finally understand it. Sorry I have been misunderstanding how the strategy scripts work. Thank you again for all your help and being so patient I greatly appreciate it.

                    Comment


                      #11
                      In your example of checking EMA(9)[30], if you tried to access that anytime before there are 31 bars on the chart, as when the indicator/strategy is starting up, you will get the error. In this case you would need something like this example:

                      if(CurrentBar < 30) //bars 0 - 29
                      return;
                      else
                      Print("Bar: " + CurrentBar + ", Value of EMA(9)[30] = " + EMA(9)[30]);

                      Edit: Sorry, you posted while I was composing. Glad you got it.
                      eDanny
                      NinjaTrader Ecosystem Vendor - Integrity Traders

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Today, 05:17 AM
                      0 responses
                      44 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      124 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      65 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      42 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      46 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X