From b0491d0f36525f5540153e1f417e8fb075f92381 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 13 Aug 2024 13:51:07 -0700 Subject: [PATCH] Add Percent Rank indicator for calculating the percent rank of the provided input series over the specified period --- indicators/PercentRank.cs | 122 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 indicators/PercentRank.cs diff --git a/indicators/PercentRank.cs b/indicators/PercentRank.cs new file mode 100644 index 0000000..3c921ee --- /dev/null +++ b/indicators/PercentRank.cs @@ -0,0 +1,122 @@ +#region Using declarations +using NinjaTrader.Gui; +using NinjaTrader.Gui.Chart; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Windows.Media; +#endregion + +namespace NinjaTrader.NinjaScript.Indicators +{ + public class PercentRank : Indicator + { + private List inputs; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Calculates the percent rank of the provided input series over the specified period"; + Name = "Percent Rank"; + Calculate = Calculate.OnBarClose; + IsOverlay = false; + DisplayInDataBox = true; + DrawOnPricePanel = true; + DrawHorizontalGridLines = true; + DrawVerticalGridLines = true; + PaintPriceMarkers = true; + ScaleJustification = ScaleJustification.Right; + IsSuspendedWhileInactive = true; + + PercentRankPeriod = 100; + + AddPlot(new Stroke(Brushes.Yellow, 3), PlotStyle.Line, "Percent Rank"); + } + else if (State == State.Configure) + { + inputs = new List(); + } + } + + protected override void OnBarUpdate() + { + inputs.Add(Input[0]); + + if (CurrentBar < PercentRankPeriod) + return; + + int rank = inputs.Count(input => input < Input[0]); + Value[0] = (double)rank / PercentRankPeriod * 100; + + if (inputs.Count > PercentRankPeriod) + inputs.RemoveAt(0); + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [Display(Name = "Percent Rank Period", GroupName = "Percent Rank", Order = 1)] + public int PercentRankPeriod { get; set; } + } +} + +#region NinjaScript generated code. Neither change nor remove. + +namespace NinjaTrader.NinjaScript.Indicators +{ + public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase + { + private PercentRank[] cachePercentRank; + public PercentRank PercentRank(int percentRankPeriod) + { + return PercentRank(Input, percentRankPeriod); + } + + public PercentRank PercentRank(ISeries input, int percentRankPeriod) + { + if (cachePercentRank != null) + for (int idx = 0; idx < cachePercentRank.Length; idx++) + if (cachePercentRank[idx] != null && cachePercentRank[idx].PercentRankPeriod == percentRankPeriod && cachePercentRank[idx].EqualsInput(input)) + return cachePercentRank[idx]; + return CacheIndicator(new PercentRank(){ PercentRankPeriod = percentRankPeriod }, input, ref cachePercentRank); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.PercentRank PercentRank(int percentRankPeriod) + { + return indicator.PercentRank(Input, percentRankPeriod); + } + + public Indicators.PercentRank PercentRank(ISeries input , int percentRankPeriod) + { + return indicator.PercentRank(input, percentRankPeriod); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.PercentRank PercentRank(int percentRankPeriod) + { + return indicator.PercentRank(Input, percentRankPeriod); + } + + public Indicators.PercentRank PercentRank(ISeries input , int percentRankPeriod) + { + return indicator.PercentRank(input, percentRankPeriod); + } + } +} + +#endregion