From c1b76befe49561e1b1fea5623e5539aa92434a0f Mon Sep 17 00:00:00 2001 From: moshferatu Date: Fri, 19 Jul 2024 13:17:13 -0700 Subject: [PATCH] Initial commit of Chandelier Exit indicator --- indicators/ChandelierExit.cs | 143 +++++++++++++++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100644 indicators/ChandelierExit.cs diff --git a/indicators/ChandelierExit.cs b/indicators/ChandelierExit.cs new file mode 100644 index 0000000..7517cea --- /dev/null +++ b/indicators/ChandelierExit.cs @@ -0,0 +1,143 @@ +#region Using declarations +using NinjaTrader.Gui; +using NinjaTrader.Gui.Chart; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Windows.Media; +using System.Xml.Serialization; +#endregion + +namespace NinjaTrader.NinjaScript.Indicators +{ + [CategoryOrder("Chandelier Exit", 1)] + [CategoryOrder("Plots", 2)] + public class ChandelierExit : Indicator + { + private ATR atr; + private MAX highestHigh; + private MIN lowestLow; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Chandelier Exit Indicator"; + Name = "Chandelier Exit"; + Calculate = Calculate.OnBarClose; + IsOverlay = true; + DisplayInDataBox = true; + DrawOnPricePanel = true; + PaintPriceMarkers = true; + ScaleJustification = ScaleJustification.Right; + IsSuspendedWhileInactive = true; + + Period = 22; + ATRMultiplier = 3.0; + + AddPlot(new Stroke(Brushes.LimeGreen, DashStyleHelper.Solid, 3), PlotStyle.Line, "Long Exit"); + AddPlot(new Stroke(Brushes.Red, DashStyleHelper.Solid, 3), PlotStyle.Line, "Short Exit"); + } + else if (State == State.DataLoaded) + { + atr = ATR(Period); + highestHigh = MAX(High, Period); + lowestLow = MIN(Low, Period); + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < Period) + return; + + LongExit[0] = highestHigh[0] - atr[0] * ATRMultiplier; + ShortExit[0] = lowestLow[0] + atr[0] * ATRMultiplier; + } + + public override string DisplayName + { + get { return Name; } + } + + [Browsable(false)] + [XmlIgnore] + public Series LongExit + { + get { return Values[0]; } + } + + [Browsable(false)] + [XmlIgnore] + public Series ShortExit + { + get { return Values[1]; } + } + + [Range(1, int.MaxValue), NinjaScriptProperty] + [Display(Name = "Period", GroupName = "Chandelier Exit", Order = 1)] + public int Period + { get; set; } + + [Range(0.1, double.MaxValue), NinjaScriptProperty] + [Display(Name = "ATR Multiplier", GroupName = "Chandelier Exit", Order = 2)] + public double ATRMultiplier + { get; set; } + } +} + +#region NinjaScript generated code. Neither change nor remove. + +namespace NinjaTrader.NinjaScript.Indicators +{ + public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase + { + private ChandelierExit[] cacheChandelierExit; + public ChandelierExit ChandelierExit(int period, double aTRMultiplier) + { + return ChandelierExit(Input, period, aTRMultiplier); + } + + public ChandelierExit ChandelierExit(ISeries input, int period, double aTRMultiplier) + { + if (cacheChandelierExit != null) + for (int idx = 0; idx < cacheChandelierExit.Length; idx++) + if (cacheChandelierExit[idx] != null && cacheChandelierExit[idx].Period == period && cacheChandelierExit[idx].ATRMultiplier == aTRMultiplier && cacheChandelierExit[idx].EqualsInput(input)) + return cacheChandelierExit[idx]; + return CacheIndicator(new ChandelierExit(){ Period = period, ATRMultiplier = aTRMultiplier }, input, ref cacheChandelierExit); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.ChandelierExit ChandelierExit(int period, double aTRMultiplier) + { + return indicator.ChandelierExit(Input, period, aTRMultiplier); + } + + public Indicators.ChandelierExit ChandelierExit(ISeries input , int period, double aTRMultiplier) + { + return indicator.ChandelierExit(input, period, aTRMultiplier); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.ChandelierExit ChandelierExit(int period, double aTRMultiplier) + { + return indicator.ChandelierExit(Input, period, aTRMultiplier); + } + + public Indicators.ChandelierExit ChandelierExit(ISeries input , int period, double aTRMultiplier) + { + return indicator.ChandelierExit(input, period, aTRMultiplier); + } + } +} + +#endregion