From 014cb5b68f51e1a89dbd38444328e5ef7b8ca2f2 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Wed, 24 Jul 2024 12:41:35 -0700 Subject: [PATCH] Add ATR Regime indicator --- indicators/ATRRegime.cs | 145 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 indicators/ATRRegime.cs diff --git a/indicators/ATRRegime.cs b/indicators/ATRRegime.cs new file mode 100644 index 0000000..354b7af --- /dev/null +++ b/indicators/ATRRegime.cs @@ -0,0 +1,145 @@ +#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; + + 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) + { + atr = ATR(BarsArray[ADRBars], 5); + } + } + + protected override void OnBarUpdate() + { + + if (PrimaryBars != BarsInProgress) + return; + + if (CurrentBars[ADRBars] < 0 || CurrentBars[ADRBars] < RankPeriod) + return; + + List atrValues = new List(); + 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 PercentRank { + get { return Values[0]; } + } + + [NinjaScriptProperty] + [Range(1, int.MaxValue)] + [Display(Name = "Rank Period", Description = "Period for the percent rank lookback", Order = 1, GroupName = "ADR Regime")] + 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; + public ATRRegime ATRRegime(int rankPeriod) + { + return ATRRegime(Input, rankPeriod); + } + + public ATRRegime ATRRegime(ISeries input, int rankPeriod) + { + if (cacheATRRegime != null) + for (int idx = 0; idx < cacheATRRegime.Length; idx++) + if (cacheATRRegime[idx] != null && cacheATRRegime[idx].RankPeriod == rankPeriod && cacheATRRegime[idx].EqualsInput(input)) + return cacheATRRegime[idx]; + return CacheIndicator(new ATRRegime(){ RankPeriod = rankPeriod }, input, ref cacheATRRegime); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.ATRRegime ATRRegime(int rankPeriod) + { + return indicator.ATRRegime(Input, rankPeriod); + } + + public Indicators.ATRRegime ATRRegime(ISeries input , int rankPeriod) + { + return indicator.ATRRegime(input, rankPeriod); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.ATRRegime ATRRegime(int rankPeriod) + { + return indicator.ATRRegime(Input, rankPeriod); + } + + public Indicators.ATRRegime ATRRegime(ISeries input , int rankPeriod) + { + return indicator.ATRRegime(input, rankPeriod); + } + } +} + +#endregion