public class TestIndicator : Indicator { #region Variables private string[] symbols = { "A", "AA", "BAC", }; DataSeries sumHistory; private PeriodType periodType = PeriodType.Minute; private int periodValue = 3; #endregion protected override void Initialize() { int i; // Add each symbol with the calculation period we're told, since there's no way to get Ninja****ter's. for (i = 0; i < symbols.Length; i++) { Add(symbols[i], periodType, periodValue); } sumHistory = new DataSeries(this); BarsRequired = 30; } long calcbar = 0; double sumprice = 0; protected override void OnBarUpdate() { //Thank you, NinjaTrader. if (CurrentBars[0] < BarsRequired || CurrentBars[BarsInProgress] < BarsRequired) return; // At bar change, reset. if (CurrentBars[0] != calcbar) { Print(""); Print("New bar: " + CurrentBar); calcbar = CurrentBars[0]; sumprice = 0; } // For example, sum the closing prices and take exponential average of that sum. Print("Summing price for: " + BarsInProgress + " (at " + CurrentBars[0] + "): " + Close[0]); sumprice += Close[0]; // Update the history for the current bar. sumHistory.Set(sumprice); // This is documented as calling OnBarUpdate again, and so should update the // value that EMA returns for this bar. EMA(sumHistory, 3).Update(); // For clarity, lets see what we're taking the exponential average over, and the return value. Print(Math.Round(sumHistory[0], 2) + "; " + Math.Round(sumHistory[1], 2) + "; " + Math.Round(sumHistory[2], 2) + "; average: " + EMA(sumHistory, 3)[0]); } }
Output:
New bar: 30 Summing price for: 0 (at 30): 1392 1392; 0; 0; average: 696 Summing price for: 2 (at 30): 9.69 1401.69; 0; 0; average: 696 Summing price for: 3 (at 30): 8.07 1409.76; 0; 0; average: 696 New bar: 31 Summing price for: 0 (at 31): 1392.75 1392.75; 1409.76; 0; average: 1044.375 Summing price for: 2 (at 31): 9.69 1402.44; 1409.76; 0; average: 1044.375 Summing price for: 3 (at 31): 8.09 1410.53; 1409.76; 0; average: 1044.375 ....
Comment