Add Percent Rank indicator for calculating the percent rank of the provided input series over the specified period

This commit is contained in:
moshferatu 2024-08-13 13:51:07 -07:00
parent 82ed1266ec
commit b0491d0f36

122
indicators/PercentRank.cs Normal file
View File

@ -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<double> 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<double>();
}
}
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<double> 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<PercentRank>(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<double> 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<double> input , int percentRankPeriod)
{
return indicator.PercentRank(input, percentRankPeriod);
}
}
}
#endregion