From 9149fad5677cb24ee55d143420417ca36399ad85 Mon Sep 17 00:00:00 2001 From: moshferatu Date: Wed, 27 Dec 2023 11:11:52 -0800 Subject: [PATCH] Add indicator for coloring bars according to trend as defined as closing above / below swing highs / lows --- indicators/Trend.cs | 164 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 indicators/Trend.cs diff --git a/indicators/Trend.cs b/indicators/Trend.cs new file mode 100644 index 0000000..8a2ed28 --- /dev/null +++ b/indicators/Trend.cs @@ -0,0 +1,164 @@ +#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; +#endregion + +//This namespace holds Indicators in this folder and is required. Do not change it. +namespace NinjaTrader.NinjaScript.Indicators +{ + public class Trend : Indicator + { + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Colors bars based on closes above previous highs or below previous lows"; + Name = "Trend"; + Calculate = Calculate.OnPriceChange; + IsOverlay = true; + DisplayInDataBox = true; + DrawOnPricePanel = true; + DrawHorizontalGridLines = true; + DrawVerticalGridLines = true; + PaintPriceMarkers = true; + ScaleJustification = ScaleJustification.Right; + IsSuspendedWhileInactive = true; + UptrendColor = Brushes.LimeGreen; + DowntrendColor = Brushes.Red; + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < 2) + return; + + if (IsFirstTickOfBar) + { + double previousHigh = High[2]; + double previousLow = Low[2]; + double currentClose = Close[1]; + + if (currentClose > previousHigh) + { + BarBrushes[0] = UptrendColor; + CandleOutlineBrushes[0] = UptrendColor; + } + else if (currentClose < previousLow) + { + BarBrushes[0] = DowntrendColor; + CandleOutlineBrushes[0] = DowntrendColor; + } + else + { + BarBrushes[0] = BarBrushes[1]; + CandleOutlineBrushes[0] = CandleOutlineBrushes[1]; + } + } + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [XmlIgnore] + [Display(Name = "Uptrend Color", GroupName = "Trend", Order = 1)] + 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 = "Trend", Order = 2)] + public Brush DowntrendColor { get; set; } + + [Browsable(false)] + public string DowntrendColorSerialization + { + get { return Serialize.BrushToString(DowntrendColor); } + set { DowntrendColor = Serialize.StringToBrush(value); } + } + } +} + +#region NinjaScript generated code. Neither change nor remove. + +namespace NinjaTrader.NinjaScript.Indicators +{ + public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase + { + private Trend[] cacheTrend; + public Trend Trend(Brush uptrendColor, Brush downtrendColor) + { + return Trend(Input, uptrendColor, downtrendColor); + } + + public Trend Trend(ISeries input, Brush uptrendColor, Brush downtrendColor) + { + if (cacheTrend != null) + for (int idx = 0; idx < cacheTrend.Length; idx++) + if (cacheTrend[idx] != null && cacheTrend[idx].UptrendColor == uptrendColor && cacheTrend[idx].DowntrendColor == downtrendColor && cacheTrend[idx].EqualsInput(input)) + return cacheTrend[idx]; + return CacheIndicator(new Trend(){ UptrendColor = uptrendColor, DowntrendColor = downtrendColor }, input, ref cacheTrend); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.Trend Trend(Brush uptrendColor, Brush downtrendColor) + { + return indicator.Trend(Input, uptrendColor, downtrendColor); + } + + public Indicators.Trend Trend(ISeries input , Brush uptrendColor, Brush downtrendColor) + { + return indicator.Trend(input, uptrendColor, downtrendColor); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.Trend Trend(Brush uptrendColor, Brush downtrendColor) + { + return indicator.Trend(Input, uptrendColor, downtrendColor); + } + + public Indicators.Trend Trend(ISeries input , Brush uptrendColor, Brush downtrendColor) + { + return indicator.Trend(input, uptrendColor, downtrendColor); + } + } +} + +#endregion