2024-07-24 19:41:35 +00:00
|
|
|
#region Using declarations
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.Gui;
|
|
|
|
using NinjaTrader.Gui.Chart;
|
|
|
|
using NinjaTrader.Data;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Windows.Media;
|
|
|
|
using System.Xml.Serialization;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Indicators
|
|
|
|
{
|
|
|
|
public class ATRRegime : Indicator
|
|
|
|
{
|
|
|
|
private const int PrimaryBars = 0;
|
|
|
|
private const int ADRBars = 1;
|
|
|
|
|
|
|
|
private ATR atr;
|
|
|
|
|
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
|
|
|
Description = @"";
|
|
|
|
Name = "ATR Regime";
|
|
|
|
Calculate = Calculate.OnBarClose;
|
|
|
|
IsOverlay = false;
|
|
|
|
DisplayInDataBox = true;
|
|
|
|
DrawOnPricePanel = true;
|
|
|
|
DrawHorizontalGridLines = true;
|
|
|
|
DrawVerticalGridLines = true;
|
|
|
|
PaintPriceMarkers = true;
|
|
|
|
ScaleJustification = ScaleJustification.Right;
|
|
|
|
IsSuspendedWhileInactive = true;
|
|
|
|
|
2024-07-25 18:04:45 +00:00
|
|
|
ATRPeriod = 5;
|
2024-07-24 19:41:35 +00:00
|
|
|
RankPeriod = 252; // Approximately 1 year in trading days.
|
|
|
|
|
|
|
|
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 },
|
|
|
|
RankPeriod * 2, Instrument.MasterInstrument.TradingHours.Name, Bars.IsResetOnNewTradingDay);
|
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
2024-07-25 18:04:45 +00:00
|
|
|
atr = ATR(BarsArray[ADRBars], ATRPeriod);
|
2024-07-24 19:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
|
|
|
if (PrimaryBars != BarsInProgress)
|
|
|
|
return;
|
|
|
|
|
2024-08-14 12:43:58 +00:00
|
|
|
if (CurrentBars[ADRBars] < RankPeriod)
|
2024-07-24 19:41:35 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
List<double> atrValues = new List<double>();
|
|
|
|
for (int i = RankPeriod; i > 0; i--)
|
|
|
|
atrValues.Add(atr[i]);
|
|
|
|
|
|
|
|
double currentAtrValue = atr[0];
|
|
|
|
int rank = atrValues.Count(x => x <= currentAtrValue);
|
|
|
|
PercentRank[0] = (double)rank / RankPeriod;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override string DisplayName
|
|
|
|
{
|
|
|
|
get { return Name; }
|
|
|
|
}
|
|
|
|
|
|
|
|
[Browsable(false)]
|
|
|
|
[XmlIgnore]
|
|
|
|
public Series<double> PercentRank {
|
|
|
|
get { return Values[0]; }
|
|
|
|
}
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
2024-08-14 12:41:14 +00:00
|
|
|
[Display(Name = "ATR Period", Description = "Period for the ATR calculation", GroupName = "ATR Regime", Order = 1)]
|
2024-07-25 18:04:45 +00:00
|
|
|
public int ATRPeriod { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
2024-08-14 12:41:14 +00:00
|
|
|
[Display(Name = "Rank Period", Description = "Period for the percent rank lookback", GroupName = "ATR Regime", Order = 2)]
|
2024-07-24 19:41:35 +00:00
|
|
|
public int RankPeriod { 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;
|
2024-07-25 18:04:45 +00:00
|
|
|
public ATRRegime ATRRegime(int aTRPeriod, int rankPeriod)
|
2024-07-24 19:41:35 +00:00
|
|
|
{
|
2024-07-25 18:04:45 +00:00
|
|
|
return ATRRegime(Input, aTRPeriod, rankPeriod);
|
2024-07-24 19:41:35 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 18:04:45 +00:00
|
|
|
public ATRRegime ATRRegime(ISeries<double> input, int aTRPeriod, int rankPeriod)
|
2024-07-24 19:41:35 +00:00
|
|
|
{
|
|
|
|
if (cacheATRRegime != null)
|
|
|
|
for (int idx = 0; idx < cacheATRRegime.Length; idx++)
|
2024-07-25 18:04:45 +00:00
|
|
|
if (cacheATRRegime[idx] != null && cacheATRRegime[idx].ATRPeriod == aTRPeriod && cacheATRRegime[idx].RankPeriod == rankPeriod && cacheATRRegime[idx].EqualsInput(input))
|
2024-07-24 19:41:35 +00:00
|
|
|
return cacheATRRegime[idx];
|
2024-07-25 18:04:45 +00:00
|
|
|
return CacheIndicator<ATRRegime>(new ATRRegime(){ ATRPeriod = aTRPeriod, RankPeriod = rankPeriod }, input, ref cacheATRRegime);
|
2024-07-24 19:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
|
|
|
|
{
|
|
|
|
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
|
|
|
|
{
|
2024-07-25 18:04:45 +00:00
|
|
|
public Indicators.ATRRegime ATRRegime(int aTRPeriod, int rankPeriod)
|
2024-07-24 19:41:35 +00:00
|
|
|
{
|
2024-07-25 18:04:45 +00:00
|
|
|
return indicator.ATRRegime(Input, aTRPeriod, rankPeriod);
|
2024-07-24 19:41:35 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 18:04:45 +00:00
|
|
|
public Indicators.ATRRegime ATRRegime(ISeries<double> input , int aTRPeriod, int rankPeriod)
|
2024-07-24 19:41:35 +00:00
|
|
|
{
|
2024-07-25 18:04:45 +00:00
|
|
|
return indicator.ATRRegime(input, aTRPeriod, rankPeriod);
|
2024-07-24 19:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
|
|
|
|
{
|
2024-07-25 18:04:45 +00:00
|
|
|
public Indicators.ATRRegime ATRRegime(int aTRPeriod, int rankPeriod)
|
2024-07-24 19:41:35 +00:00
|
|
|
{
|
2024-07-25 18:04:45 +00:00
|
|
|
return indicator.ATRRegime(Input, aTRPeriod, rankPeriod);
|
2024-07-24 19:41:35 +00:00
|
|
|
}
|
|
|
|
|
2024-07-25 18:04:45 +00:00
|
|
|
public Indicators.ATRRegime ATRRegime(ISeries<double> input , int aTRPeriod, int rankPeriod)
|
2024-07-24 19:41:35 +00:00
|
|
|
{
|
2024-07-25 18:04:45 +00:00
|
|
|
return indicator.ATRRegime(input, aTRPeriod, rankPeriod);
|
2024-07-24 19:41:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|