for learning puprose and to apply on my other indicators; i tried in the below code to marking the chart when the signal from of overbought / oversold from simple RSI indicator using region highlight x as drawing.
It works but im sure it is not the right way, because it is repeating and drawing again and again the same thing when using onprice change until the last candle bar is not even visible..
is there a way to save and access the start point and end point of each signal so that the highlight region is only drawn once? or what is the best way in general to do the below?
advice appreciated
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.Peak
{
public class RSImarker : Indicator
{
int i = 0;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"RSI mean reversion indicator ";
Name = "RSImarker";
Calculate = Calculate.OnBarClose;
IsOverlay = false;
DisplayInDataBox = true;
DrawOnPricePanel = true;
DrawHorizontalGridLines = true;
DrawVerticalGridLines = true;
PaintPriceMarkers = true;
ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right;
//Disable this property if your indicator requires custom values that cumulate with each new market data event.
//See Help Guide for additional information.
IsSuspendedWhileInactive = true;
Legnth = 50;
Smooth = 25;
Overbought = 70;
Oversold = 30;
AddPlot(Brushes.Orange, "RSIgap");
AddPlot(Brushes.Firebrick, "OB");
AddPlot(Brushes.ForestGreen, "OS");
}
else if (State == State.Configure)
{
}
}
protected override void OnBarUpdate()
{
//Add your custom indicator logic here.
double rsi = RSI(Legnth,Smooth)[0];
int signalState = 0;
//signal painting on chart.
if (rsi > Overbought)
{ signalState = 1;
Draw.RegionHighlightX(this,"Buy"+i++,0,1,Brushes.T ransparent,Brushes.Firebrick,50);
}
else if (rsi < Oversold)
{ signalState = -1;
Draw.RegionHighlightX(this,"Buy"+i++,0,1,Brushes.T ransparent,Brushes.ForestGreen,50);
}
else
signalState = 0;
RSIgap[0] = rsi;
OB[0] = Overbought;
OS[0] = Oversold;
}
region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Legnth", Order=1, GroupName="Parameters")]
public int Legnth
{ get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name="Smooth", Order=2, GroupName="Parameters")]
public int Smooth
{ get; set; }
[NinjaScriptProperty]
[Range(0, double.MaxValue)]
[Display(Name="Overbought", Order=3, GroupName="Parameters")]
public double Overbought
{ get; set; }
[NinjaScriptProperty]
[Range(-1000, double.MaxValue)]
[Display(Name="Oversold", Order=4, GroupName="Parameters")]
public double Oversold
{ get; set; }
[Browsable(false)]
[XmlIgnore]
public Series<double> RSIgap
{
get { return Values[0]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> OB
{
get { return Values[1]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> OS
{
get { return Values[2]; }
}
[Browsable(false)]
[XmlIgnore]
public Series<double> signalState
{
get { return Values[3]; }
}
#endregion
}
}
region NinjaScript generated code. Neither change nor remove.
namespace NinjaTrader.NinjaScript.Indicators
{
public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
{
private Peak.RSImarker[] cacheRSImarker;
public Peak.RSImarker RSImarker(int legnth, int smooth, double overbought, double oversold)
{
return RSImarker(Input, legnth, smooth, overbought, oversold);
}
public Peak.RSImarker RSImarker(ISeries<double> input, int legnth, int smooth, double overbought, double oversold)
{
if (cacheRSImarker != null)
for (int idx = 0; idx < cacheRSImarker.Length; idx++)
if (cacheRSImarker[idx] != null && cacheRSImarker[idx].Legnth == legnth && cacheRSImarker[idx].Smooth == smooth && cacheRSImarker[idx].Overbought == overbought && cacheRSImarker[idx].Oversold == oversold && cacheRSImarker[idx].EqualsInput(input))
return cacheRSImarker[idx];
return CacheIndicator<Peak.RSImarker>(new Peak.RSImarker(){ Legnth = legnth, Smooth = smooth, Overbought = overbought, Oversold = oversold }, input, ref cacheRSImarker);
}
}
}
namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
{
public Indicators.Peak.RSImarker RSImarker(int legnth, int smooth, double overbought, double oversold)
{
return indicator.RSImarker(Input, legnth, smooth, overbought, oversold);
}
public Indicators.Peak.RSImarker RSImarker(ISeries<double> input , int legnth, int smooth, double overbought, double oversold)
{
return indicator.RSImarker(input, legnth, smooth, overbought, oversold);
}
}
}
namespace NinjaTrader.NinjaScript.Strategies
{
public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
{
public Indicators.Peak.RSImarker RSImarker(int legnth, int smooth, double overbought, double oversold)
{
return indicator.RSImarker(Input, legnth, smooth, overbought, oversold);
}
public Indicators.Peak.RSImarker RSImarker(ISeries<double> input , int legnth, int smooth, double overbought, double oversold)
{
return indicator.RSImarker(input, legnth, smooth, overbought, oversold);
}
}
}
#endregion
zz0.x06e5leyunzz

Comment