Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

"index with a value is out-of-range" for EMA on Market Replay

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

    "index with a value is out-of-range" for EMA on Market Replay

    Hi there,

    I'm doing something incredibly dumb, but I can't figure it out. It seems like a simple solution.
    I added a 5 period EMA to my script in the following manner (simplified for the sake of example):

    --------

    public class TrackingAandB : Strategy
    { double ema_5; }

    protected override void OnBarUpdate()
    { ema_5 = EMA(5)[1]; }

    -------

    The code compiles, but the following comes up in the Output field:
    "Strategy 'TrackingAandB': 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."

    I've tried using CurrentBar > 5 before allowing the script to read that code bit, but it still produces the above.
    I'm also attempting to test this script using a recorded session via Market Replay, using a 20 tick chart.

    Thanks in advance for any guidance you can give me.



    #2
    Hello Spiderbird,

    Is CurrentBar greater than 1 when attempting to use a barsAgo index of [1]?

    If so may we see the code to confirm this was done correctly?

    If you are getting an index error, then there is an invalid index..

    Below is a link to a forum post where this is discussed.
    https://ninjatrader.com/support/foru...13#post1048513

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello Spiderbird,

      Is CurrentBar greater than 1 when attempting to use a barsAgo index of [1]?

      If so may we see the code to confirm this was done correctly?

      If you are getting an index error, then there is an invalid index..

      Below is a link to a forum post where this is discussed.
      https://ninjatrader.com/support/foru...13#post1048513
      Hi Chelsea,

      Here you go. Code is attached. Let me know if you see anything off.
      Attached Files

      Comment


        #4
        Hello Spiderbird,

        To confirm, none of the prints are appearing in the output window, is this correct? (such as the print of a few dashes on line 126?)

        The output window only has the single error message and nothing else?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello Spiderbird,

          To confirm, none of the prints are appearing in the output window, is this correct? (such as the print of a few dashes on line 126?)

          The output window only has the single error message and nothing else?
          Hi Chelsea,

          The inputs are shown the minute i try to enable the strategy. However, after clicking the "Enabled" button and getting the pop-up window afterwards (and hitting OK), it immediately disables the strategy and shows the following error in the Output window.

          For reference, this is from a recorded session from 5/2 that starts at 8:30 AM CT. I waited until 8:49 (or about 20+ bars) before attempting to enable the strategy I included above.
          Any ideas?


          ---

          I'll also mention that the strategy is aligned with "Playback101" in the "Account Display Name" section of the 'Strategy' Tab. It's also focusing on the same instrument, and the 20 tick data series.
          Attached Files

          Comment


            #6
            Hello Spiderbird,

            Are you using the prints to figure out which line of code is the line of code generating the error?

            I am seeing that this script is printing values, meaning its likely getting past the line of code you have stated is the issue.

            If you comment out all logic in OnBarUpdate() except lines 121 and 122, are you still getting the error?
            Last edited by NinjaTrader_ChelseaB; 05-09-2019, 08:01 AM.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I figured it out, and it was pretty simple (and I was being dumb).
              It had nothing to do with the code I *thought* was causing the issue. It was with bidVolumeAvg and askVolumeAvg. Reference the following code:

              -----

              {

              // Add the total tick volume of the last 10 bars
              askVolume10 = askVolume9; bidVolume10 = bidVolume9;
              askVolume9 = askVolume8; bidVolume9 = bidVolume8;
              askVolume8 = askVolume7; bidVolume8 = bidVolume7;
              askVolume7 = askVolume6; bidVolume7 = bidVolume6;
              askVolume6 = askVolume5; bidVolume6 = bidVolume5;
              askVolume5 = askVolume4; bidVolume5 = bidVolume4;
              askVolume4 = askVolume3; bidVolume4 = bidVolume3;
              askVolume3 = askVolume2; bidVolume3 = bidVolume2;
              askVolume2 = askVolume; bidVolume2 = bidVolume;
              askVolumeTotal = askVolume10 + askVolume9 + askVolume8 + askVolume7 + askVolume6 + askVolume5 + askVolume4 + askVolume3 + askVolume2 + askVolume;
              bidVolumeTotal = bidVolume10 + bidVolume9 + bidVolume8 + bidVolume7 + bidVolume6 + bidVolume5 + bidVolume4 + bidVolume3 + bidVolume2 + bidVolume;

              askVolumeAvg = askVolumeTotal / 10;
              bidVolumeAvg = bidVolumeTotal / 10;

              if (askVolumeAvg >= bidVolumeAvg)
              { // print it on the ema line
              Draw.Text(this, Counter1.ToString(), true,
              "+" + askVolumeAvg.ToString(), 1,
              ema_5, 1, Brushes.Green,
              Arial_8pt, TextAlignment.Center,
              Brushes.Transparent, Brushes.Transparent, 0);
              }

              else if (askVolumeAvg < bidVolumeAvg)
              { // print it on the ema line
              Draw.Text(this, Counter1.ToString(), true,
              "-" + bidVolumeAvg.ToString(), 1,
              ema_5, 1, Brushes.Red,
              Arial_8pt, TextAlignment.Center,
              Brushes.Transparent, Brushes.Transparent, 0);
              }

              -----

              It was throwing back an error because neither bidVolumeAvg or askVolumeAvg could generate a value, since they were dependent on the previous 10 values of bidVolume to calculate. Here's the corrected code (with changes in red and bold below):

              -----

              {

              // Add the total tick volume of the last 10 bars
              askVolume10 = askVolume9; bidVolume10 = bidVolume9;
              askVolume9 = askVolume8; bidVolume9 = bidVolume8;
              askVolume8 = askVolume7; bidVolume8 = bidVolume7;
              askVolume7 = askVolume6; bidVolume7 = bidVolume6;
              askVolume6 = askVolume5; bidVolume6 = bidVolume5;
              askVolume5 = askVolume4; bidVolume5 = bidVolume4;
              askVolume4 = askVolume3; bidVolume4 = bidVolume3;
              askVolume3 = askVolume2; bidVolume3 = bidVolume2;
              askVolume2 = askVolume; bidVolume2 = bidVolume;
              askVolumeTotal = askVolume10 + askVolume9 + askVolume8 + askVolume7 + askVolume6 + askVolume5 + askVolume4 + askVolume3 + askVolume2 + askVolume;
              bidVolumeTotal = bidVolume10 + bidVolume9 + bidVolume8 + bidVolume7 + bidVolume6 + bidVolume5 + bidVolume4 + bidVolume3 + bidVolume2 + bidVolume;

              askVolumeAvg = askVolumeTotal / 10;
              bidVolumeAvg = bidVolumeTotal / 10;

              if (askVolumeAvg >= bidVolumeAvg && askVolume10 != 0)
              { // print it on the ema line
              Draw.Text(this, Counter1.ToString(), true,
              "+" + askVolumeAvg.ToString(), 1,
              ema_5, 1, Brushes.Green,
              Arial_8pt, TextAlignment.Center,
              Brushes.Transparent, Brushes.Transparent, 0);
              }

              else if (askVolumeAvg < bidVolumeAvg && bidVolume10 != 0)
              { // print it on the ema line
              Draw.Text(this, Counter1.ToString(), true,
              "-" + bidVolumeAvg.ToString(), 1,
              ema_5, 1, Brushes.Red,
              Arial_8pt, TextAlignment.Center,
              Brushes.Transparent, Brushes.Transparent, 0);
              }

              -----

              Thanks for your time and attention in helping trying to solve this Chelsea. I'm all set.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by junkone, Today, 11:37 AM
              0 responses
              1 view
              0 likes
              Last Post junkone
              by junkone
               
              Started by quantismo, 04-17-2024, 05:13 PM
              5 responses
              34 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by proptrade13, Today, 11:06 AM
              1 response
              6 views
              0 likes
              Last Post NinjaTrader_Clayton  
              Started by love2code2trade, 04-17-2024, 01:45 PM
              4 responses
              34 views
              0 likes
              Last Post love2code2trade  
              Started by cls71, Today, 04:45 AM
              2 responses
              10 views
              0 likes
              Last Post eDanny
              by eDanny
               
              Working...
              X