Example: Simple Moving Average
Input parameters
#region Parameters[InputParameter("Period of Simple Moving Average", 0, 1, 999, 1, 1)]public int Period = 10;[InputParameter("Sources prices for MA", 1, variants: new object[]{ "Close", PriceType.Close, "Open", PriceType.Open, "High", PriceType.High, "Low", PriceType.Low, "Typical", PriceType.Typical, "Median", PriceType.Median, "Weighted", PriceType.Weighted})]public PriceType SourcePrice = PriceType.Close;#endregion ParametersIndicator's general info
public SMA() : base(){ Name = "Simple Moving Average Example"; Description = "Average price for the last N periods"; ShortName = "SMA (" + Period + ":" + SourcePrice.ToString() + ")"; AddLineSeries("SMA", Color.Red, 1, LineStyle.Solid); SeparateWindow = false;}Core logic
protected override void OnInit(){}protected override void OnUpdate(UpdateArgs args){ if (Count <= Period) return; double sum = 0.0; for (int i = 0; i < Period; i++) sum += GetPrice(SourcePrice, i); SetValue(sum / Period);}All source code
Last updated
Was this helpful?