From 8fb8117b9fdfe1601e9ecaaa805e81f4447bd04d Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 19 Mar 2024 05:45:02 -0700 Subject: [PATCH] Simple reversal indicator --- indicators/SimpleReversal.cs | 155 +++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 indicators/SimpleReversal.cs diff --git a/indicators/SimpleReversal.cs b/indicators/SimpleReversal.cs new file mode 100644 index 0000000..471f7b9 --- /dev/null +++ b/indicators/SimpleReversal.cs @@ -0,0 +1,155 @@ +#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 SimpleReversal : Indicator + { + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @""; + Name = "Simple Reversal"; + Calculate = Calculate.OnBarClose; + IsOverlay = true; + DisplayInDataBox = true; + DrawOnPricePanel = true; + ScaleJustification = ScaleJustification.Right; + IsSuspendedWhileInactive = true; + } + else if (State == State.Configure) + { + } + } + + protected override void OnBarUpdate() + { + if (CurrentBar < 2) + return; + + if (LongReversal()) + DrawTriangle(true); + else if (ShortReversal()) + DrawTriangle(false); + } + + private bool LongReversal() + { + return (Close[2] < Open[2]) && + (Low[1] < Low[2]) && + (Low[1] < Low[0]) && + (Close[0] > High[1]) && + (Close[0] > High[2]); + } + + private bool ShortReversal() + { + return (Close[2] > Open[2]) && + (High[1] > High[2]) && + (High[1] > High[0]) && + (Close[0] < Low[1]) && + (Close[0] < Low[2]); + } + + private void DrawTriangle(bool isLong) + { + var brush = isLong ? Brushes.LimeGreen : Brushes.Red; + var yValue = isLong ? Low[0] : High[0]; + + if (isLong) + { + var triangleUp = Draw.TriangleUp(this, "LongReversal@" + CurrentBar, true, 0, yValue, brush); + triangleUp.OutlineBrush = Brushes.Transparent; + } + else + { + var triangleDown = Draw.TriangleDown(this, "ShortReversal@" + CurrentBar, true, 0, yValue, brush); + triangleDown.OutlineBrush = Brushes.Transparent; + } + } + + 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 SimpleReversal[] cacheSimpleReversal; + public SimpleReversal SimpleReversal() + { + return SimpleReversal(Input); + } + + public SimpleReversal SimpleReversal(ISeries input) + { + if (cacheSimpleReversal != null) + for (int idx = 0; idx < cacheSimpleReversal.Length; idx++) + if (cacheSimpleReversal[idx] != null && cacheSimpleReversal[idx].EqualsInput(input)) + return cacheSimpleReversal[idx]; + return CacheIndicator(new SimpleReversal(), input, ref cacheSimpleReversal); + } + } +} + +namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns +{ + public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase + { + public Indicators.SimpleReversal SimpleReversal() + { + return indicator.SimpleReversal(Input); + } + + public Indicators.SimpleReversal SimpleReversal(ISeries input ) + { + return indicator.SimpleReversal(input); + } + } +} + +namespace NinjaTrader.NinjaScript.Strategies +{ + public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase + { + public Indicators.SimpleReversal SimpleReversal() + { + return indicator.SimpleReversal(Input); + } + + public Indicators.SimpleReversal SimpleReversal(ISeries input ) + { + return indicator.SimpleReversal(input); + } + } +} + +#endregion