From f5fbda72975eae30e80dc211c7abfc0c9ebdc037 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Fri, 5 Jul 2024 07:52:31 -0700 Subject: [PATCH] Add Nasdaq cumulative 52 week new highs new lows indicator --- indicators/NasdaqCumulativeNewHighsNewLows.cs | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 indicators/NasdaqCumulativeNewHighsNewLows.cs diff --git a/indicators/NasdaqCumulativeNewHighsNewLows.cs b/indicators/NasdaqCumulativeNewHighsNewLows.cs new file mode 100644 index 0000000..502ba30 --- /dev/null +++ b/indicators/NasdaqCumulativeNewHighsNewLows.cs @@ -0,0 +1,136 @@ +#region Using declarations +using NinjaTrader.Gui; +using NinjaTrader.Gui.Chart; +using NinjaTrader.Data; +using System.Windows.Media; +#endregion + +namespace NinjaTrader.NinjaScript.Indicators +{ + public class NasdaqCumulativeNewHighsNewLows : Indicator + { + private const int NewHighsBars = 1; + private const int NewLowsBars = 2; + + private double cumulativeNewHighsLows = 0.0; + + private Series newHighsLows; + + private SMA mean; + private StdDev stdev; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Nasdaq Cumulative New Highs New Lows"; + Name = "Nasdaq Cumulative New Highs New Lows"; + Calculate = Calculate.OnBarClose; + IsOverlay = false; + PaintPriceMarkers = true; + ScaleJustification = ScaleJustification.Right; + IsSuspendedWhileInactive = true; + + AddPlot(new Stroke(Brushes.Yellow, 3), PlotStyle.Line, "Z-Score"); + } + else if (State == State.Configure) + { + AddDataSeries("FIQH", BarsPeriodType.Day, 1); + AddDataSeries("FIQL", BarsPeriodType.Day, 1); + } + else if (State == State.DataLoaded) + { + newHighsLows = new Series(this); + + mean = SMA(newHighsLows, 252); + stdev = StdDev(newHighsLows, 252); + } + } + + protected override void OnBarUpdate() + { + if (BarsInProgress != 0) + return; + + int newHighsBar = BarsArray[NewHighsBars].GetBar(Time[0]); + double newHighs = BarsArray[NewHighsBars].GetClose(newHighsBar); + + int newLowsBar = BarsArray[NewLowsBars].GetBar(Time[0]); + double newLows = BarsArray[NewLowsBars].GetClose(newLowsBar); + + cumulativeNewHighsLows = cumulativeNewHighsLows + (newHighs - newLows); + newHighsLows[0] = cumulativeNewHighsLows; + + double zScore = (cumulativeNewHighsLows - mean[0]) / stdev[0]; + Value[0] = zScore; + + if (zScore > 0) + BackBrush = Brushes.LimeGreen; + else + BackBrush = Brushes.Red; + } + + public override string DisplayName + { + get { return Name; } + } + } +} + + +#region NinjaScript generated code. Neither change nor remove. + +namespace NinjaTrader.NinjaScript.Indicators +{ + public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase + { + private NasdaqCumulativeNewHighsNewLows[] cacheNasdaqCumulativeNewHighsNewLows; + public NasdaqCumulativeNewHighsNewLows NasdaqCumulativeNewHighsNewLows() + { + return NasdaqCumulativeNewHighsNewLows(Input); + } + + public NasdaqCumulativeNewHighsNewLows NasdaqCumulativeNewHighsNewLows(ISeries input) + { + if (cacheNasdaqCumulativeNewHighsNewLows != null) + for (int idx = 0; idx < cacheNasdaqCumulativeNewHighsNewLows.Length; idx++) + if (cacheNasdaqCumulativeNewHighsNewLows[idx] != null && cacheNasdaqCumulativeNewHighsNewLows[idx].EqualsInput(input)) + return cacheNasdaqCumulativeNewHighsNewLows[idx]; + return CacheIndicator(new NasdaqCumulativeNewHighsNewLows(), input, ref cacheNasdaqCumulativeNewHighsNewLows); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.NasdaqCumulativeNewHighsNewLows NasdaqCumulativeNewHighsNewLows() + { + return indicator.NasdaqCumulativeNewHighsNewLows(Input); + } + + public Indicators.NasdaqCumulativeNewHighsNewLows NasdaqCumulativeNewHighsNewLows(ISeries input ) + { + return indicator.NasdaqCumulativeNewHighsNewLows(input); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.NasdaqCumulativeNewHighsNewLows NasdaqCumulativeNewHighsNewLows() + { + return indicator.NasdaqCumulativeNewHighsNewLows(Input); + } + + public Indicators.NasdaqCumulativeNewHighsNewLows NasdaqCumulativeNewHighsNewLows(ISeries input ) + { + return indicator.NasdaqCumulativeNewHighsNewLows(input); + } + } +} + +#endregion