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

SMA of double series returns current price of instrument instead of average of series

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

    SMA of double series returns current price of instrument instead of average of series

    Hello,

    I have a bizarre issue where SMA of a double series returns the current price of the instrument instead of the average of the series.

    I have tested that the data in the series is valid and correct, but SMA still returns the current price.

    Here is the SMA and the series it is supposed to be calculated on. It is in State.Dataloaded

    vb_barms = new Series<double>(this, MaximumBarsLookBack.TwoHundredFiftySix);
    vb_avgms = SMA(vb_barms, 20);

    This code in onbarupdate assigns the values to the vb_barms series and attempts to read the average etc. I've some debugging code in there too


    TimeSpan t = Time[0] - vb_barstart;
    vb_barms[1] = t.TotalMilliseconds; // store time in ms

    if (CurrentBar > 22000)
    {
    for (int i=CurrentBar; i>CurrentBar-20; i--)
    Print(vb_barms.GetValueAt(i));
    Print(vb_barms.Count+":"+t.TotalMilliseconds.ToStr ing()+", "+SMA(vb_barms,velocityBarsPeriod).GetValueAt( Curr entBar-1)+", "+vb_avgms.GetValueAt(CurrentBar-1)+", vb_barms[2]="+vb_barms[2]+", vb_barms[1]="+vb_barms[1]);
    }
    if (t.TotalMilliseconds * velocityBarsMultiplier < vb_avgms.GetValueAt(CurrentBar-1))
    VelocitySeries[1] = 1;
    }
    vb_barstart = Time[0];

    The debug loop will print something like this

    159195
    103410
    101668
    53496
    86547
    81215
    88328
    105559
    257980
    94449
    97711
    44969
    35602
    22660:121254, 15094.75, 15094.75, vb_barms[2]=150312, vb_barms[1]=121254

    All the numbers are from the vb_barms series. But, as you can see,

    vb_avgms.GetValueAt(CurrentBar-1)

    returns the price of the security.

    I could not figure out why, so I added SMA(vb_barms,velocityBarsPeriod).GetValueAt(Curren tBar-1), and clearly, you can see the numbers printed above from that vb_barms series, but that returns the current price of the security too! I can get a correct average if I manually loop through the numbers, add them up, divide by count, etc.

    Any ideas why SMA is doing this?

    Thanks.

    #2
    Hello pjsmith,

    I first recommend removing complexity and simplifying the prints with proper labels.

    Below is a link to a forum post on using prints.


    From the looks of it, vb_barms never has a value set on the current bar. Only the previous bar's value is ever set (and there is no check for CurrentBar to be greater than 1 so I would imagine this would cause an index error).

    This would mean that the SMA is trying to calculate with no value set on the current bar on its input series. If no value is set then a 0 is returned.

    Also, GetValueAt() uses an absolute barIndex value and not a barsAgo value. .GetValueAt(1) would be returning the value from the 2nd bar all the way to the left of the chart. .GetValueAt(CurrentBar) would return the value from the most recent bar. .GetValueAt(CurrentBar - 1) would return the value from the previous bar.


    Last, .GetValueAt() needs the actual series from the indicator (.Values[0]).

    Attached is the code, but with the custom series getting a value set on the current bar instead of the previous bar, and using the series for the parameter to .GetValueAt().
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3

      Hi,

      Thanks for the response and test code. I missed out on some critical info that is relevant. I apologise for this. The reason I noticed this is, I have just come across the same exact problem in a completely unrelated indicator. I didn't twig before, but with further troubleshooting, this is what is happening

      I have a double series series and an SMA of that double series. So, for example

      private Series<double> CDeltas;
      private SMA CDeltasSMA;

      in State.DataLoaded

      CDeltas = new Series<double>(this, MaximumBarsLookBack.Infinite);
      CDeltasSMA = SMA(CDeltas,20);

      Now, in OnBarUpdate, all is good.

      I've got this in OBU filtered with BarsInProgress == 0 (the indie is set to OnEachTick)

      if (CurrentBars[0] >= 0) CDeltas[0] = balance;

      And, this debug print statement below it

      Print("CurrentBar="+CurrentBar+", CDeltas="+CDeltas[0]+", CDeltasSMA[0]="+CDeltasSMA[0]);

      From that print statement, I get this (actual test data I just ran)


      CurrentBar=265, CDeltas=8396, CDeltasSMA[0]=5735.66666666667, CDeltasSMA[1]=5782
      CurrentBar=266, CDeltas=8655, CDeltasSMA[0]=5686.33333333333, CDeltasSMA[1]=5735.66666666667
      CurrentBar=267, CDeltas=8734, CDeltasSMA[0]=5799, CDeltasSMA[1]=5686.33333333333
      CurrentBar=268, CDeltas=8776, CDeltasSMA[0]=5839.33333333333, CDeltasSMA[1]=5799
      CurrentBar=269, CDeltas=8779, CDeltasSMA[0]=5854.33333333333, CDeltasSMA[1]=5839.33333333333
      CurrentBar=270, CDeltas=8774, CDeltasSMA[0]=5853.66666666667, CDeltasSMA[1]=5854.33333333333
      CurrentBar=271, CDeltas=8800, CDeltasSMA[0]=5860.66666666667, CDeltasSMA[1]=5853.66666666667
      CurrentBar=272, CDeltas=8809, CDeltasSMA[0]=5872.33333333333, CDeltasSMA[1]=5860.66666666667
      CurrentBar=273, CDeltas=8777, CDeltasSMA[0]=5864.66666666667, CDeltasSMA[1]=5872.33333333333
      CurrentBar=274, CDeltas=8748, CDeltasSMA[0]=5844.33333333333, CDeltasSMA[1]=5864.66666666667
      CurrentBar=275, CDeltas=8823, CDeltasSMA[0]=5859.66666666667, CDeltasSMA[1]=5844.33333333333

      So, you can see, the average is incrementing fine. The CDeltas series is fine. We are printing the currentbar number, the CDeltas series, and the current and prior value of the CDeltasSMA series. I printed the prior value to make sure historical values were available and showing correctly from OBU. All good.

      But, if access the same CDeltasSMA series via OnRender, using GetValue there is a problem

      Here, in the onrender loop, I have a debug print statement.

      Print("idx="+idx+", CDeltas.GetValueAt(idx)="+CDeltas.GetValueAt(idx)+ ", CDeltasSMA.GetValueAt(idx)"+CDeltasSMA.GetValueAt( idx));

      In the same test, this outputs this

      idx=265, CDeltas.GetValueAt(idx)=8655, CDeltasSMA.GetValueAt(idx)15416.25
      idx=266, CDeltas.GetValueAt(idx)=8734, CDeltasSMA.GetValueAt(idx)15422.75
      idx=267, CDeltas.GetValueAt(idx)=8776, CDeltasSMA.GetValueAt(idx)15424.75
      idx=268, CDeltas.GetValueAt(idx)=8779, CDeltasSMA.GetValueAt(idx)15427
      idx=269, CDeltas.GetValueAt(idx)=8774, CDeltasSMA.GetValueAt(idx)15425.5
      idx=270, CDeltas.GetValueAt(idx)=8800, CDeltasSMA.GetValueAt(idx)15424.75
      idx=271, CDeltas.GetValueAt(idx)=8809, CDeltasSMA.GetValueAt(idx)15425.5
      idx=272, CDeltas.GetValueAt(idx)=8777, CDeltasSMA.GetValueAt(idx)15425.5
      idx=273, CDeltas.GetValueAt(idx)=8748, CDeltasSMA.GetValueAt(idx)15421.75
      idx=274, CDeltas.GetValueAt(idx)=8823, CDeltasSMA.GetValueAt(idx)15423.75
      idx=275, CDeltas.GetValueAt(idx)=8823, CDeltasSMA.GetValueAt(idx)15428

      So, you can see the CDetlas.GetValueAt is returning the correct value (it matches with the output from OnBarUpdate

      The bizarre thing is, I get back the current instrument price (or the average of the current price series) for the SMA series CDeltasSMA when using CDeltasSMA.GetValueAt(idx) to retrieve the value, but in onbarupdate, as you can see from the debug prints, the series is fine.

      I thought I'd perhaps messed something up in prior code in the first post, so was trying to look at it in more detail, though I could not see nor understand the issue. Now, I have encountered the same issue in a totally unrelated codebase. - I am completely stumped!

      Can you comment on any issues you know of that might cause a problem accessing an SMA series from within OnRender using GetValueAt?

      I have built a cut down version of this as example code and have replicated the issue. I will email to you, for your attention. Thank you.




      Comment


        #4
        Hello,

        "Can you comment on any issues you know of that might cause a problem accessing an SMA series from within OnRender using GetValueAt?"

        This is asynchronous but I would not expect a run-time error as long as the value supplied to .GetValueAt is less than CurrentBar.

        Perhaps the order of events is not as you expect because the data thread and render thread are asynchronous?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Hello pjsmith,

          I have received a private email in our support address and will be conversing with you privately moving forward.
          Chelsea B.NinjaTrader Customer Service

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Jimmyk, 01-26-2018, 05:19 AM
          6 responses
          835 views
          0 likes
          Last Post emuns
          by emuns
           
          Started by jxs_xrj, 01-12-2020, 09:49 AM
          6 responses
          3,291 views
          1 like
          Last Post jgualdronc  
          Started by Touch-Ups, Today, 10:36 AM
          0 responses
          10 views
          0 likes
          Last Post Touch-Ups  
          Started by geddyisodin, 04-25-2024, 05:20 AM
          11 responses
          62 views
          0 likes
          Last Post halgo_boulder  
          Started by Option Whisperer, Today, 09:55 AM
          0 responses
          9 views
          0 likes
          Last Post Option Whisperer  
          Working...
          X