From da65f9a38f01756cc3ea6289c8d153ecf5e61804 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Sun, 7 Jan 2024 06:05:41 -0800 Subject: [PATCH] Add indicator for coloring bars on chart according to whether price is above / below fast and slow moving averages --- indicators/MovingAverageTrend.cs | 195 +++++++++++++++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 indicators/MovingAverageTrend.cs diff --git a/indicators/MovingAverageTrend.cs b/indicators/MovingAverageTrend.cs new file mode 100644 index 0000000..7446e00 --- /dev/null +++ b/indicators/MovingAverageTrend.cs @@ -0,0 +1,195 @@ +#region Using declarations +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Input; +using System.Windows.Media; +using System.Xml.Serialization; +using NinjaTrader.Cbi; +using NinjaTrader.Gui; +using NinjaTrader.Gui.Chart; +using NinjaTrader.Gui.SuperDom; +using NinjaTrader.Gui.Tools; +using NinjaTrader.Data; +using NinjaTrader.NinjaScript; +using NinjaTrader.Core.FloatingPoint; +using NinjaTrader.NinjaScript.DrawingTools; +using NinjaTrader.Gui.PropertiesTest; +#endregion + +namespace NinjaTrader.NinjaScript.Indicators +{ + public class MovingAverageTrend : Indicator + { + private EMA fastMA; + private EMA slowMA; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Colors bars on chart according to price in relation to fast and slow moving averages"; + Name = "Moving Average Trend"; + Calculate = Calculate.OnPriceChange; + IsOverlay = true; + DisplayInDataBox = true; + DrawOnPricePanel = true; + ScaleJustification = ScaleJustification.Right; + IsSuspendedWhileInactive = true; + FastMAPeriod = 20; + SlowMAPeriod = 100; + UptrendColor = Brushes.LimeGreen; + DowntrendColor = Brushes.Red; + NeutralColor = Brushes.Gray; + } + else if (State == State.DataLoaded) + { + fastMA = EMA(FastMAPeriod); + slowMA = EMA(SlowMAPeriod); + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < SlowMAPeriod) + { + BarBrushes[0] = NeutralColor; + CandleOutlineBrushes[0] = NeutralColor; + return; + } + + if (IsFirstTickOfBar) + { + double currentClose = Close[1]; + if (currentClose > fastMA[1] && currentClose > slowMA[1]) + { + BarBrushes[0] = UptrendColor; + CandleOutlineBrushes[0] = UptrendColor; + } + else if (currentClose < fastMA[1] && currentClose < slowMA[1]) + { + BarBrushes[0] = DowntrendColor; + CandleOutlineBrushes[0] = DowntrendColor; + } + else + { + BarBrushes[0] = NeutralColor; + CandleOutlineBrushes[0] = NeutralColor; + } + } + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [Range(1, int.MaxValue)] + [Display(Name = "Fast MA Period", GroupName = "Moving Average Trend", Order = 1)] + public int FastMAPeriod { get; set; } + + [NinjaScriptProperty] + [Range(1, int.MaxValue)] + [Display(Name = "Slow MA Period", GroupName = "Moving Average Trend", Order = 2)] + public int SlowMAPeriod { get; set; } + + [NinjaScriptProperty] + [XmlIgnore] + [Display(Name = "Uptrend Color", GroupName = "Moving Average Trend", Order = 3)] + public Brush UptrendColor { get; set; } + + [Browsable(false)] + public string UptrendColorSerialization + { + get { return Serialize.BrushToString(UptrendColor); } + set { UptrendColor = Serialize.StringToBrush(value); } + } + + [NinjaScriptProperty] + [XmlIgnore] + [Display(Name = "Downtrend Color", GroupName = "Moving Average Trend", Order = 4)] + public Brush DowntrendColor { get; set; } + + [Browsable(false)] + public string DowntrendColorSerialization + { + get { return Serialize.BrushToString(DowntrendColor); } + set { DowntrendColor = Serialize.StringToBrush(value); } + } + + [NinjaScriptProperty] + [XmlIgnore] + [Display(Name = "Neutral Color", GroupName = "Moving Average Trend", Order = 5)] + public Brush NeutralColor { get; set; } + + [Browsable(false)] + public string NeutralColorSerialization + { + get { return Serialize.BrushToString(NeutralColor); } + set { NeutralColor = Serialize.StringToBrush(value); } + } + } +} + +#region NinjaScript generated code. Neither change nor remove. + +namespace NinjaTrader.NinjaScript.Indicators +{ + public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase + { + private MovingAverageTrend[] cacheMovingAverageTrend; + public MovingAverageTrend MovingAverageTrend(int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor) + { + return MovingAverageTrend(Input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor); + } + + public MovingAverageTrend MovingAverageTrend(ISeries input, int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor) + { + if (cacheMovingAverageTrend != null) + for (int idx = 0; idx < cacheMovingAverageTrend.Length; idx++) + if (cacheMovingAverageTrend[idx] != null && cacheMovingAverageTrend[idx].FastMAPeriod == fastMAPeriod && cacheMovingAverageTrend[idx].SlowMAPeriod == slowMAPeriod && cacheMovingAverageTrend[idx].UptrendColor == uptrendColor && cacheMovingAverageTrend[idx].DowntrendColor == downtrendColor && cacheMovingAverageTrend[idx].NeutralColor == neutralColor && cacheMovingAverageTrend[idx].EqualsInput(input)) + return cacheMovingAverageTrend[idx]; + return CacheIndicator(new MovingAverageTrend(){ FastMAPeriod = fastMAPeriod, SlowMAPeriod = slowMAPeriod, UptrendColor = uptrendColor, DowntrendColor = downtrendColor, NeutralColor = neutralColor }, input, ref cacheMovingAverageTrend); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.MovingAverageTrend MovingAverageTrend(int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor) + { + return indicator.MovingAverageTrend(Input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor); + } + + public Indicators.MovingAverageTrend MovingAverageTrend(ISeries input , int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor) + { + return indicator.MovingAverageTrend(input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.MovingAverageTrend MovingAverageTrend(int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor) + { + return indicator.MovingAverageTrend(Input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor); + } + + public Indicators.MovingAverageTrend MovingAverageTrend(ISeries input , int fastMAPeriod, int slowMAPeriod, Brush uptrendColor, Brush downtrendColor, Brush neutralColor) + { + return indicator.MovingAverageTrend(input, fastMAPeriod, slowMAPeriod, uptrendColor, downtrendColor, neutralColor); + } + } +} + +#endregion