From c1e52bced302604e88785d2d86694abc67d2d5eb Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 30 Jul 2024 20:40:16 -0700 Subject: [PATCH] Add Kaufman Efficiency Ratio indicator --- indicators/EfficiencyRatio.cs | 127 ++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 indicators/EfficiencyRatio.cs diff --git a/indicators/EfficiencyRatio.cs b/indicators/EfficiencyRatio.cs new file mode 100644 index 0000000..1b5a068 --- /dev/null +++ b/indicators/EfficiencyRatio.cs @@ -0,0 +1,127 @@ +#region Using declarations +using NinjaTrader.Gui; +using NinjaTrader.Gui.Chart; +using System; +using System.ComponentModel.DataAnnotations; +using System.Windows.Media; +#endregion + +namespace NinjaTrader.NinjaScript.Indicators +{ + public class EfficiencyRatio : Indicator + { + private SUM sumOfPriceChanges; + + private Series priceChanges; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Kaufman Efficiency Ratio"; + Name = "Efficiency Ratio"; + Calculate = Calculate.OnBarClose; + IsOverlay = false; + DisplayInDataBox = true; + DrawOnPricePanel = true; + DrawHorizontalGridLines = true; + DrawVerticalGridLines = true; + PaintPriceMarkers = true; + ScaleJustification = ScaleJustification.Right; + IsSuspendedWhileInactive = true; + + Period = 10; + + AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 2), PlotStyle.Line, "Efficiency Ratio"); + } + else if (State == State.Configure) + { + } + else if (State == State.DataLoaded) + { + priceChanges = new Series(this, MaximumBarsLookBack.Infinite); + + sumOfPriceChanges = SUM(priceChanges, Period); + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < 1) + return; + + priceChanges[0] = Math.Abs(Close[0] - Close[1]); + + if (CurrentBar < Period) + Value[0] = 0.0; + else + Value[0] = Math.Abs(Close[0] - Close[Period]) / sumOfPriceChanges[0]; + } + + public override string DisplayName + { + get { return Name; } + } + + [Range(1, int.MaxValue), NinjaScriptProperty] + [Display(Name = "Period", GroupName = "Efficiency Ratio", 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 EfficiencyRatio[] cacheEfficiencyRatio; + public EfficiencyRatio EfficiencyRatio(int period) + { + return EfficiencyRatio(Input, period); + } + + public EfficiencyRatio EfficiencyRatio(ISeries input, int period) + { + if (cacheEfficiencyRatio != null) + for (int idx = 0; idx < cacheEfficiencyRatio.Length; idx++) + if (cacheEfficiencyRatio[idx] != null && cacheEfficiencyRatio[idx].Period == period && cacheEfficiencyRatio[idx].EqualsInput(input)) + return cacheEfficiencyRatio[idx]; + return CacheIndicator(new EfficiencyRatio(){ Period = period }, input, ref cacheEfficiencyRatio); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.EfficiencyRatio EfficiencyRatio(int period) + { + return indicator.EfficiencyRatio(Input, period); + } + + public Indicators.EfficiencyRatio EfficiencyRatio(ISeries input , int period) + { + return indicator.EfficiencyRatio(input, period); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.EfficiencyRatio EfficiencyRatio(int period) + { + return indicator.EfficiencyRatio(Input, period); + } + + public Indicators.EfficiencyRatio EfficiencyRatio(ISeries input , int period) + { + return indicator.EfficiencyRatio(input, period); + } + } +} + +#endregion