Here is the code.
------------------------------------
namespace NinjaTrader.Indicator
{
/// <summary>
/// The RSI_mod (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
/// </summary>
[Description("The RSI_mod (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.")]
public class RSI_mod : Indicator
{
#region Variables
private DataSeries avgUp;
private DataSeries avgDown;
private DataSeries down;
private int period = 14;
private int smooth = 3;
private DataSeries up;
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.Green, "RSI_mod"));
Add(new Plot(Color.Orange, "Avg"));
Add(new Line(System.Drawing.Color.DarkViolet, 30, "Lower"));
Add(new Line(System.Drawing.Color.DarkViolet, 70, "Upper"));
avgUp = new DataSeries(this);
avgDown = new DataSeries(this);
down = new DataSeries(this);
up = new DataSeries(this);
PriceTypeSupported = true;
}
/// <summary>
/// Calculates the indicator value(s) at the current index.
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
{
down.Set(0);
up.Set(0);
return;
}
down.Set(Math.Max(Input[1] - Input[0], 0));
up.Set(Math.Max(Input[0] - Input[1], 0));
if ((CurrentBar + 1) < Period)
{
if ((CurrentBar + 1) == (Period - 1))
Avg.Set(50);
return;
}
if ((CurrentBar + 1) == Period)
{
// First averages
avgDown.Set(SMA(down, Period)[0]);
avgUp.Set(SMA(up, Period)[0]);
}
else
{
// Rest of averages are smoothed
avgDown.Set((avgDown[1] * (Period - 1) + down[0]) / Period);
avgUp.Set((avgUp[1] * (Period - 1) + up[0]) / Period);
}
double rs = avgUp[0] / (avgDown[0] == 0 ? 1 : avgDown[0]);
double rsi = 100 - (100 / (1 + rs));
double rsiAvg = (2.0 / (1 + Smooth)) * rsi + (1 - (2.0 / (1 + Smooth))) * Avg[1];
Avg.Set(rsiAvg);
Value.Set(rsi);
//**********************
if (rsi > 70)
{DrawTriangleUp("rsi up" + CurrentBar, false, 0, Low[0]-.50, Color.Green);}
if (rsi < 30)
{DrawTriangleDown("rsi down" + CurrentBar, false, 0, High[0]+0.40, Color.Red);}
}
Comment