From 82fec0811b8801636e615acc43060ca8144a1407 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Wed, 12 Jun 2024 05:23:12 -0700 Subject: [PATCH] An alternative Keltner Channel indicator to the one provided by NinjaTrader, utilizing ATR multiples --- indicators/KeltChannel.cs | 160 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 indicators/KeltChannel.cs diff --git a/indicators/KeltChannel.cs b/indicators/KeltChannel.cs new file mode 100644 index 0000000..b840d29 --- /dev/null +++ b/indicators/KeltChannel.cs @@ -0,0 +1,160 @@ +#region Using declarations +using NinjaTrader.Gui; +using NinjaTrader.Gui.Chart; +using System; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Windows.Media; +using System.Xml.Serialization; +#endregion + +//This namespace holds Indicators in this folder and is required. Do not change it. +namespace NinjaTrader.NinjaScript.Indicators +{ + public class KeltChannel : Indicator + { + private ATR atr; + private EMA ema; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Alternative to the built-in Keltner Channel, utilizes ATR to calculate the bands"; + Name = "Keltner Channel"; + Calculate = Calculate.OnPriceChange; + IsOverlay = true; + DisplayInDataBox = true; + DrawOnPricePanel = true; + PaintPriceMarkers = false; + ScaleJustification = ScaleJustification.Right; + IsSuspendedWhileInactive = true; + + EMAPeriod = 20; + ATRPeriod = 10; + Multiplier = 3.0; + + AddPlot(new Stroke(Brushes.Gray, DashStyleHelper.Dash, 3), PlotStyle.Line, "Middle Line"); + AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3), PlotStyle.Line, "Upper Line"); + AddPlot(new Stroke(Brushes.Yellow, DashStyleHelper.Solid, 3), PlotStyle.Line, "Lower Line"); + } + else if (State == State.DataLoaded) + { + ema = EMA(EMAPeriod); + atr = ATR(ATRPeriod); + } + else if (State == State.Historical) + { + SetZOrder(-1); // Display behind bars on chart. + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < Math.Max(EMAPeriod, ATRPeriod)) + return; + + double atrValue = atr[0]; + double middleLine = ema[0]; + + MiddleLine[0] = middleLine; + UpperLine[0] = middleLine + (Multiplier * atrValue); + LowerLine[0] = middleLine - (Multiplier * atrValue); + } + + public override string DisplayName + { + get { return Name; } + } + + [Browsable(false)] + [XmlIgnore] + public Series MiddleLine + { + get { return Values[0]; } + } + + [Browsable(false)] + [XmlIgnore] + public Series UpperLine + { + get { return Values[1]; } + } + + [Browsable(false)] + [XmlIgnore] + public Series LowerLine + { + get { return Values[2]; } + } + + [Range(1, int.MaxValue), NinjaScriptProperty] + [Display(Name = "EMA Period", Order = 1, GroupName = "Keltner Channel")] + public int EMAPeriod { get; set; } + + [Range(1, int.MaxValue), NinjaScriptProperty] + [Display(Name = "ATR Period", Order = 2, GroupName = "Keltner Channel")] + public int ATRPeriod { get; set; } + + [Range(0.1, double.MaxValue), NinjaScriptProperty] + [Display(Name = "Multiplier", Order = 3, GroupName = "Keltner Channel")] + public double Multiplier { get; set; } + } +} + +#region NinjaScript generated code. Neither change nor remove. + +namespace NinjaTrader.NinjaScript.Indicators +{ + public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase + { + private KeltChannel[] cacheKeltChannel; + public KeltChannel KeltChannel(int eMAPeriod, int aTRPeriod, double multiplier) + { + return KeltChannel(Input, eMAPeriod, aTRPeriod, multiplier); + } + + public KeltChannel KeltChannel(ISeries input, int eMAPeriod, int aTRPeriod, double multiplier) + { + if (cacheKeltChannel != null) + for (int idx = 0; idx < cacheKeltChannel.Length; idx++) + if (cacheKeltChannel[idx] != null && cacheKeltChannel[idx].EMAPeriod == eMAPeriod && cacheKeltChannel[idx].ATRPeriod == aTRPeriod && cacheKeltChannel[idx].Multiplier == multiplier && cacheKeltChannel[idx].EqualsInput(input)) + return cacheKeltChannel[idx]; + return CacheIndicator(new KeltChannel(){ EMAPeriod = eMAPeriod, ATRPeriod = aTRPeriod, Multiplier = multiplier }, input, ref cacheKeltChannel); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.KeltChannel KeltChannel(int eMAPeriod, int aTRPeriod, double multiplier) + { + return indicator.KeltChannel(Input, eMAPeriod, aTRPeriod, multiplier); + } + + public Indicators.KeltChannel KeltChannel(ISeries input , int eMAPeriod, int aTRPeriod, double multiplier) + { + return indicator.KeltChannel(input, eMAPeriod, aTRPeriod, multiplier); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.KeltChannel KeltChannel(int eMAPeriod, int aTRPeriod, double multiplier) + { + return indicator.KeltChannel(Input, eMAPeriod, aTRPeriod, multiplier); + } + + public Indicators.KeltChannel KeltChannel(ISeries input , int eMAPeriod, int aTRPeriod, double multiplier) + { + return indicator.KeltChannel(input, eMAPeriod, aTRPeriod, multiplier); + } + } +} + +#endregion