Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

RSI with WMA smoothing

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    RSI with WMA smoothing

    I would like to change the standard NT RSI smoothing from a simple to weighted MA. I have tried substituting "WMA" for "Average" in the code, but this will not compile, having named the indicator "RSIWMA". Here's the code, for which I would be grateful for any assistance :

    //
    // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
    // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
    //

    #region Using declarations
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.ComponentModel;
    using System.Xml.Serialization;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// The RSI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
    /// </summary>
    [Description("The RSI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.")]
    public class RSI : 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"));
    Add(new Plot(Color.Orange, "WMA"));

    Add(new Line(System.Drawing.Color.DarkViolet, 30, "Lower"));
    Add(new Line(System.Drawing.Color.YellowGreen, 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);
    }

    #region Properties
    /// <summary>
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Avg
    {
    get { return Values[1]; }
    }

    /// <summary>
    /// </summary>
    [Browsable(false)]
    [XmlIgnore()]
    public DataSeries Default
    {
    get { return Values[0]; }
    }

    /// <summary>
    /// </summary>
    [Description("Numbers of bars used for calculations")]
    [Category("Parameters")]
    public int Period
    {
    get { return period; }
    set { period = Math.Max(2, value); }
    }

    /// <summary>
    /// </summary>
    [Description("Number of bars for smoothing")]
    [Category("Parameters")]
    public int Smooth
    {
    get { return smooth; }
    set { smooth = Math.Max(1, value); }
    }
    #endregion
    }
    }

    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    public partial class Indicator : IndicatorBase
    {
    private RSI[] cacheRSI = null;

    private static RSI checkRSI = new RSI();

    /// <summary>
    /// The RSI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
    /// </summary>
    /// <returns></returns>
    public RSI RSI(int period, int smooth)
    {
    return RSI(Input, period, smooth);
    }

    /// <summary>
    /// The RSI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
    /// </summary>
    /// <returns></returns>
    public RSI RSI(Data.IDataSeries input, int period, int smooth)
    {
    checkRSI.Period = period;
    period = checkRSI.Period;
    checkRSI.Smooth = smooth;
    smooth = checkRSI.Smooth;

    if (cacheRSI != null)
    for (int idx = 0; idx < cacheRSI.Length; idx++)
    if (cacheRSI[idx].Period == period && cacheRSI[idx].Smooth == smooth && cacheRSI[idx].EqualsInput(input))
    return cacheRSI[idx];

    RSI indicator = new RSI();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    indicator.Input = input;
    indicator.Period = period;
    indicator.Smooth = smooth;
    indicator.SetUp();

    RSI[] tmp = new RSI[cacheRSI == null ? 1 : cacheRSI.Length + 1];
    if (cacheRSI != null)
    cacheRSI.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheRSI = tmp;
    Indicators.Add(indicator);

    return indicator;
    }

    }
    }

    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
    public partial class Column : ColumnBase
    {
    /// <summary>
    /// The RSI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.RSI RSI(int period, int smooth)
    {
    return _indicator.RSI(Input, period, smooth);
    }

    /// <summary>
    /// The RSI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
    /// </summary>
    /// <returns></returns>
    public Indicator.RSI RSI(Data.IDataSeries input, int period, int smooth)
    {
    return _indicator.RSI(input, period, smooth);
    }

    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// The RSI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.RSI RSI(int period, int smooth)
    {
    return _indicator.RSI(Input, period, smooth);
    }

    /// <summary>
    /// The RSI (Relative Strength Index) is a price-following oscillator that ranges between 0 and 100.
    /// </summary>
    /// <returns></returns>
    public Indicator.RSI RSI(Data.IDataSeries input, int period, int smooth)
    {
    if (InInitialize && input == null)
    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

    return _indicator.RSI(input, period, smooth);
    }

    }
    }
    #endregion

    #2
    Hi jtrade, it might be easier to create a new indicator for your idea - you could do something like this in the OnBarUpdate() -

    Code:
    [COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]double[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][COLOR=#000000] rsiWMA = WMA(RSI([/COLOR][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]20[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][COLOR=#000000], [/COLOR][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]1[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][COLOR=#000000]),[/COLOR][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]20[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][COLOR=#000000])[[/COLOR][/SIZE][/SIZE][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][SIZE=2][COLOR=#000000]];[/COLOR]
    [/SIZE][/SIZE][/COLOR]
    For our indicator coding tutorials, please check this link - http://www.ninjatrader-support.com/H...verview18.html

    Comment


      #3
      Thanks, Bertrand,

      I have inserted the code above, but when I compile I receive the error message "The namespace 'NinjaTrader.Indicator' already contains a definition for 'RSI'".... ???

      Comment


        #4
        You will need to create a new indicator for this to work properly, please check the attached RSIWMA out. Import with File > Utilities > Import NinjaScript.
        Attached Files

        Comment


          #5
          Bravo, Bertrand - and thanks !

          Comment


            #6
            You are welcome, great it helps!

            Comment


              #7
              Originally posted by NinjaTrader_Bertrand View Post
              You will need to create a new indicator for this to work properly, please check the attached RSIWMA out. Import with File > Utilities > Import NinjaScript.
              I downloaded your RSI WMA but it doesn't work with my NT8. Can you fix it to work with my NT8?

              Comment


                #8
                Hello Cocchu,

                This is a NinjaTrader 7 script. If you want to use it in NinjaTrader 8, you will need to convert the script into NT8's version of NinjaScript.

                Below are the relevant HG pages for how to access WMA, RSI, and AddPlot() in NT8 NinjaScript.





                Comment


                  #9
                  Originally posted by NinjaTrader_Gaby View Post
                  Hello Cocchu,

                  This is a NinjaTrader 7 script. If you want to use it in NinjaTrader 8, you will need to convert the script into NT8's version of NinjaScript.

                  Below are the relevant HG pages for how to access WMA, RSI, and AddPlot() in NT8 NinjaScript.





                  https://ninjatrader.com/support/help...t8/addplot.htm
                  Can you convert my RSI WMA file from Nt7 to Nt8. I don't know how to use programming language. Thank you very much.

                  Comment


                    #10
                    Hello Cocchu,

                    Unfortunately our support team does not provide conversion services. This thread will remain open for any members of the community who may wish to convert this on your behalf. You could also reach out to a NinjaScript consultant who provides conversion services (likely for a fee).

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    547 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    323 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    99 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    543 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    545 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X