From c054db534f060772cef840f5f63554dbad5cce90 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Mon, 3 Jun 2024 12:23:34 -0700 Subject: [PATCH] Add Rate of Change (RoC) indicator --- indicators/RateOfChange.cs | 130 +++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 indicators/RateOfChange.cs diff --git a/indicators/RateOfChange.cs b/indicators/RateOfChange.cs new file mode 100644 index 0000000..5c36100 --- /dev/null +++ b/indicators/RateOfChange.cs @@ -0,0 +1,130 @@ +#region Using declarations +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Input; +using System.Windows.Media; +using System.Xml.Serialization; +using NinjaTrader.Cbi; +using NinjaTrader.Gui; +using NinjaTrader.Gui.Chart; +using NinjaTrader.Gui.SuperDom; +using NinjaTrader.Gui.Tools; +using NinjaTrader.Data; +using NinjaTrader.NinjaScript; +using NinjaTrader.Core.FloatingPoint; +using NinjaTrader.NinjaScript.DrawingTools; +using NinjaTrader.Gui.PropertiesTest; +#endregion + +//This namespace holds Indicators in this folder and is required. Do not change it. +namespace NinjaTrader.NinjaScript.Indicators +{ + public class RateOfChange : Indicator + { + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @" Basic Rate of Change indicator"; + Name = "Rate of Change"; + Calculate = Calculate.OnBarClose; + IsOverlay = false; + DisplayInDataBox = true; + DrawOnPricePanel = true; + PaintPriceMarkers = true; + ScaleJustification = ScaleJustification.Right; + IsSuspendedWhileInactive = false; + + Period = 14; + + AddPlot(new Stroke(Brushes.Yellow, 3), PlotStyle.Line, "Rate of Change"); + AddLine(new Stroke(Brushes.Gray, 3), 0, "Origin"); + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < Period) + return; + + double inputBarsAgo = Input[Math.Min(CurrentBar, Period)]; + + if (inputBarsAgo == 0.0) + Value[0] = 0.0; + else + Value[0] = ((Input[0] - inputBarsAgo) / inputBarsAgo) * 100; + } + + public override string DisplayName + { + get { return Name; } + } + + [Range(1, int.MaxValue), NinjaScriptProperty] + [Display(Name = "Rate of Change Period", GroupName = "Rate of Change", Order = 1)] + public int Period { get; set; } + } +} + +#region NinjaScript generated code. Neither change nor remove. + +namespace NinjaTrader.NinjaScript.Indicators +{ + public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase + { + private RateOfChange[] cacheRateOfChange; + public RateOfChange RateOfChange(int period) + { + return RateOfChange(Input, period); + } + + public RateOfChange RateOfChange(ISeries input, int period) + { + if (cacheRateOfChange != null) + for (int idx = 0; idx < cacheRateOfChange.Length; idx++) + if (cacheRateOfChange[idx] != null && cacheRateOfChange[idx].Period == period && cacheRateOfChange[idx].EqualsInput(input)) + return cacheRateOfChange[idx]; + return CacheIndicator(new RateOfChange(){ Period = period }, input, ref cacheRateOfChange); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.RateOfChange RateOfChange(int period) + { + return indicator.RateOfChange(Input, period); + } + + public Indicators.RateOfChange RateOfChange(ISeries input , int period) + { + return indicator.RateOfChange(input, period); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.RateOfChange RateOfChange(int period) + { + return indicator.RateOfChange(Input, period); + } + + public Indicators.RateOfChange RateOfChange(ISeries input , int period) + { + return indicator.RateOfChange(input, period); + } + } +} + +#endregion