140 lines
5.1 KiB
C#
140 lines
5.1 KiB
C#
#region Using declarations
|
|
using NinjaTrader.Cbi;
|
|
using NinjaTrader.Gui;
|
|
using NinjaTrader.Gui.Chart;
|
|
using NinjaTrader.Data;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Windows.Media;
|
|
#endregion
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
{
|
|
public class ATRRegime : Indicator
|
|
{
|
|
private const int PrimaryBars = 0;
|
|
private const int ATRBars = 1;
|
|
|
|
private ATR atr;
|
|
private PercentRank percentRank;
|
|
|
|
protected override void OnStateChange()
|
|
{
|
|
if (State == State.SetDefaults)
|
|
{
|
|
Description = @"Calculates the percentile of the ATR over the specified period";
|
|
Name = "ATR Regime";
|
|
Calculate = Calculate.OnBarClose;
|
|
IsOverlay = false;
|
|
DisplayInDataBox = true;
|
|
DrawOnPricePanel = true;
|
|
DrawHorizontalGridLines = true;
|
|
DrawVerticalGridLines = true;
|
|
PaintPriceMarkers = true;
|
|
ScaleJustification = ScaleJustification.Right;
|
|
IsSuspendedWhileInactive = true;
|
|
|
|
ATRPeriod = 5;
|
|
RankPeriod = 252; // Approximately 1 year in trading days.
|
|
DaysToLoad = RankPeriod * 2;
|
|
|
|
AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3), PlotStyle.Line, "ATR Regime");
|
|
}
|
|
else if (State == State.Configure)
|
|
{
|
|
AddDataSeries(Instrument.FullName, new BarsPeriod { BarsPeriodType = BarsPeriodType.Day, Value = 1 },
|
|
DaysToLoad, Instrument.MasterInstrument.TradingHours.Name, Bars.IsResetOnNewTradingDay);
|
|
}
|
|
else if (State == State.DataLoaded)
|
|
{
|
|
atr = ATR(BarsArray[ATRBars], ATRPeriod);
|
|
percentRank = PercentRank(atr, RankPeriod);
|
|
}
|
|
}
|
|
|
|
protected override void OnBarUpdate()
|
|
{
|
|
if (PrimaryBars != BarsInProgress || CurrentBars[ATRBars] < RankPeriod)
|
|
return;
|
|
|
|
Value[0] = percentRank[0];
|
|
}
|
|
|
|
public override string DisplayName
|
|
{
|
|
get { return Name; }
|
|
}
|
|
|
|
[NinjaScriptProperty]
|
|
[Range(1, int.MaxValue)]
|
|
[Display(Name = "ATR Period", Description = "Period for the ATR calculation", GroupName = "ATR Regime", Order = 1)]
|
|
public int ATRPeriod { get; set; }
|
|
|
|
[NinjaScriptProperty]
|
|
[Range(1, int.MaxValue)]
|
|
[Display(Name = "Rank Period", Description = "Period for the percent rank lookback", GroupName = "ATR Regime", Order = 2)]
|
|
public int RankPeriod { get; set; }
|
|
|
|
[NinjaScriptProperty]
|
|
[Range(1, int.MaxValue)]
|
|
[Display(Name = "Days to Load", Description = "Number of daily bars to load for calculating the regime", GroupName = "ATR Regime", Order = 3)]
|
|
public int DaysToLoad { get; set; }
|
|
}
|
|
}
|
|
|
|
#region NinjaScript generated code. Neither change nor remove.
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
{
|
|
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
|
|
{
|
|
private ATRRegime[] cacheATRRegime;
|
|
public ATRRegime ATRRegime(int aTRPeriod, int rankPeriod, int daysToLoad)
|
|
{
|
|
return ATRRegime(Input, aTRPeriod, rankPeriod, daysToLoad);
|
|
}
|
|
|
|
public ATRRegime ATRRegime(ISeries<double> input, int aTRPeriod, int rankPeriod, int daysToLoad)
|
|
{
|
|
if (cacheATRRegime != null)
|
|
for (int idx = 0; idx < cacheATRRegime.Length; idx++)
|
|
if (cacheATRRegime[idx] != null && cacheATRRegime[idx].ATRPeriod == aTRPeriod && cacheATRRegime[idx].RankPeriod == rankPeriod && cacheATRRegime[idx].DaysToLoad == daysToLoad && cacheATRRegime[idx].EqualsInput(input))
|
|
return cacheATRRegime[idx];
|
|
return CacheIndicator<ATRRegime>(new ATRRegime(){ ATRPeriod = aTRPeriod, RankPeriod = rankPeriod, DaysToLoad = daysToLoad }, input, ref cacheATRRegime);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
|
{
|
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
|
{
|
|
public Indicators.ATRRegime ATRRegime(int aTRPeriod, int rankPeriod, int daysToLoad)
|
|
{
|
|
return indicator.ATRRegime(Input, aTRPeriod, rankPeriod, daysToLoad);
|
|
}
|
|
|
|
public Indicators.ATRRegime ATRRegime(ISeries<double> input , int aTRPeriod, int rankPeriod, int daysToLoad)
|
|
{
|
|
return indicator.ATRRegime(input, aTRPeriod, rankPeriod, daysToLoad);
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
{
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
{
|
|
public Indicators.ATRRegime ATRRegime(int aTRPeriod, int rankPeriod, int daysToLoad)
|
|
{
|
|
return indicator.ATRRegime(Input, aTRPeriod, rankPeriod, daysToLoad);
|
|
}
|
|
|
|
public Indicators.ATRRegime ATRRegime(ISeries<double> input , int aTRPeriod, int rankPeriod, int daysToLoad)
|
|
{
|
|
return indicator.ATRRegime(input, aTRPeriod, rankPeriod, daysToLoad);
|
|
}
|
|
}
|
|
}
|
|
|
|
#endregion
|