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.
Comment