ninjatrader/indicators/percent-rank/PercentRank.cs

123 lines
3.6 KiB
C#

#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)
{
Name = "Percent Rank";
Description = @"Calculates the percent rank of the provided input series over the specified period";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = ScaleJustification.Right;
IsSuspendedWhileInactive = true;
Period = 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 < Period)
return;
int rank = inputs.Count(input => input < Input[0]);
Value[0] = (double)rank / Period * 100;
if (inputs.Count > Period)
inputs.RemoveAt(0);
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Period", GroupName = "Percent Rank", 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 PercentRank[] cachePercentRank;
public PercentRank PercentRank(int period)
{
return PercentRank(Input, period);
}
public PercentRank PercentRank(ISeries<double> input, int period)
{
if (cachePercentRank != null)
for (int idx = 0; idx < cachePercentRank.Length; idx++)
if (cachePercentRank[idx] != null && cachePercentRank[idx].Period == period && cachePercentRank[idx].EqualsInput(input))
return cachePercentRank[idx];
return CacheIndicator<PercentRank>(new PercentRank(){ Period = period }, input, ref cachePercentRank);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.PercentRank PercentRank(int period)
{
return indicator.PercentRank(Input, period);
}
public Indicators.PercentRank PercentRank(ISeries<double> input , int period)
{
return indicator.PercentRank(input, period);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.PercentRank PercentRank(int period)
{
return indicator.PercentRank(Input, period);
}
public Indicators.PercentRank PercentRank(ISeries<double> input , int period)
{
return indicator.PercentRank(input, period);
}
}
}
#endregion