Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

NT's RSI implementation

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

    NT's RSI implementation

    Hello,

    wikipedia.org describes the implementation of the moving averages for the RSI as follows:
    The average U and D are calculated using an n-period exponential moving average (EMA) in the AIQ version (but with an equal-weighted moving average in Wilder's original version).
    What kind of moving average does NT use in its original code (marked as red)?
    Code:
    if ((CurrentBar + 1) == Period) 
    {
        // First averages 
        avgDown.Set(SMA(down, Period)[0]);
        avgUp.Set(SMA(up, Period)[0]);
    }  
    else 
    {
        // Rest of averages are smoothed
    [B][COLOR=Red]    avgDown.Set((avgDown[1] * (Period - 1) + down[0]) / Period);
        avgUp.Set((avgUp[1] * (Period - 1) + up[0]) / Period);[/COLOR][/B]
    }
    Regards
    Ralph

    #2
    Originally posted by Ralph View Post
    Hello,

    wikipedia.org describes the implementation of the moving averages for the RSI as follows:
    What kind of moving average does NT use in its original code (marked as red)?
    Code:
    if ((CurrentBar + 1) == Period) 
    {
        // First averages 
        avgDown.Set(SMA(down, Period)[0]);
        avgUp.Set(SMA(up, Period)[0]);
    }  
    else 
    {
        // Rest of averages are smoothed
    [B][COLOR=Red]    avgDown.Set((avgDown[1] * (Period - 1) + down[0]) / Period);
        avgUp.Set((avgUp[1] * (Period - 1) + up[0]) / Period);[/COLOR][/B]
    }
    Regards
    Ralph
    That is a very efficient method to calculate a SMA.

    Comment


      #3
      Originally posted by Ralph View Post
      wikipedia.org describes the implementation of the moving averages for the RSI as follows:
      The average U and D are calculated using an n-period exponential moving average (EMA) in the AIQ version (but with an equal-weighted moving average in Wilder's original version).
      This information is not correct. The Wikipedia article which you cite is either outdated or non-existant. Please check yourself:



      Welles Wilder did not have a PC to compute any moving averages. As he had to do it by hand, he appreciated that calculating an EMA is much easier than calculating a SMA. Therefore Wilder always used exponential moving averages. Please have a look at Wilder's book "New Concepts in Technical Trading Systems", pp. 63-70, and you will see this confirmed.


      Wilder's Smoothing

      However, Wilder's exponential moving averages use a different definition of the smoothing constant. Let us define

      indicator period = N
      smoothing constant = k

      Starting from here, Wilder uses a smoothing constant k = 1/N, which means that the current value of the moving average is calculated as

      current_value = (N-1)/N * previous_value + 1/N * current_close

      This is also the formula used by Wilder for the smoothing of the upcloses and RSI.


      Exponential Smoothing Redefined

      In an article published in Stocks & Commodities in March 1994, Jack Hutson suggested to use a smoothing constant of k = 2/(N+1) for exponential moving averages. He showed that this definition results in a comparable lag for a simple and exponential moving average with the period N. This new definition has since prevailed and is used by all modern charting applications.


      Wilder's Indicators Came First

      The book "New Concepts in Technical Trading Systems" was already published in 1978. Therefore all indicators described by Wilder in his book use the original formula for an exponential average with a smoothing constant k = 1/N.

      This explains that the Relative Strength Index (RSI) and the Average True Range (ATR) are calculated based on Wilder's smoothing formula.


      NinjaTrader implementation

      The NinjaTrader implementation follows the original method described by Wilder.
      Last edited by Harry; 12-01-2013, 11:39 AM.

      Comment


        #4
        Originally posted by koganam View Post
        That is a very efficient method to calculate a SMA.
        No, it is not. It is the formula for Wilder's smoothing and has nothing to do with a SMA.

        Comment


          #5
          My mistake!

          Originally posted by Harry View Post
          No, it is not. It is the formula for Wilder's smoothing and has nothing to do with a SMA.
          Mea culpa. I meant to write EMA, not SMA (this, without an addendum of "using a smoothing factor of 1/Period"). If I remember right, that indeed is the Wilder's Smoothing factor.

          The standard EMA, directly related to calculating the infinite series, would need a smoothing factor of 2/(Period +1).

          Thanks, Harry.
          Last edited by koganam; 11-30-2013, 10:46 PM.

          Comment


            #6
            The citation is located in the middle of the 'Calculation' section of the wikipedia RSI article.

            I would translate the NinjaTrader EMA formula for the RSI to:
            current_value = (N-1)/N * previous_value + 1/N * current_close

            Does that match one of the valid EMA implementations?

            Comment


              #7
              Originally posted by Ralph View Post
              The citation is located in the middle of the 'Calculation' section of the wikipedia RSI article.

              I would translate the NinjaTrader EMA formula for the RSI to:
              current_value = (N-1)/N * previous_value + 1/N * current_close

              Does that match one of the valid EMA implementations?
              The NinjaTrader implementation matches the original formula used by Wilder for calculating an exponential moving average.

              The information taken from Wikipedia (I have seen it now) is false.

              Comment


                #8
                1) current_value = N/(N-1) * previous_value + 1/N * current_close
                2) current_value = (N-1)/N * previous_value + 1/N * current_close

                Is 1) the formula you mentioned as "original formula used by Wilder"?
                2) is the formula used by NinjaTrader.
                These two formulas are different.

                Comment


                  #9
                  Originally posted by Ralph View Post
                  1) current_value = N/(N-1) * previous_value + 1/N * current_close
                  2) current_value = (N-1)/N * previous_value + 1/N * current_close

                  Is 1) the formula you mentioned as "original formula used by Wilder"?
                  2) is the formula used by NinjaTrader.
                  These two formulas are different.

                  The second formula is correct. The formula in my post was incorrect and I have edited and changed it to avoid any further confusion. An exponential moving average is calculated from the prior value of the moving average and the current close.

                  Code:
                  current_value_EMA =  k * current_close + (1- k) * prior_value_EMA with 0 < k < 1
                  The important point is that k and (1-k) add up to unity. This is true for both EMA formulae:

                  Wilder's Average:
                  Code:
                  current_value_EMA =  (1/N) * current_Close + (1-1/N) * prior_value_EMA
                  Standard definition of EMA:
                  Code:
                  current_value_EMA = (2/(N+1)) * current_Close + (1-2/(N+1)) * prior_value_EMA
                  For example, if you apply the RSI with a period of N = 14 (default setting), Wilder's smoothing will use a smoothing constant k = 1/14. The same result can be obtained by smoothing the up and down moves with an EMA(27), as the EMA(27) uses a smoothing constant of 2/(27 + 1) = 2/28 = 1/14.

                  Therefore an EMA(27) is identical to the WilderMA(14). AN EMA(26) would be identical to a WilderMA(13.5). Exponential moving averages can be calculated for non-integer periods, which is not the case for simple moving averages.

                  Comment


                    #10
                    Hello Ralph,

                    Thank you for your post.

                    You can find information and the calculations for the RSI at the following link: http://stockcharts.com/school/doku.p...ve_strength_in

                    Please let me know if I may be of further assistance.

                    Comment


                      #11
                      Thanks for the input guys.
                      For me, the RSI visualises the ratio between the average of up-bars and down-bars for a period of bars. Plotting an RSI based on ratios calculated with SMAs would be something I understand. Using EMAs (as it seems to be standard) confuses me because why should bars appearing at the beginning of the period have less importance than bars at the end?
                      If I would use the RSI for chart analysis I would implement an RSI based on SMAs. And perhaps that was the original intention of W. Wilder too.

                      Comment


                        #12
                        Originally posted by Ralph View Post
                        Thanks for the input guys.
                        For me, the RSI visualises the ratio between the average of up-bars and down-bars for a period of bars. Plotting an RSI based on ratios calculated with SMAs would be something I understand. Using EMAs (as it seems to be standard) confuses me because why should bars appearing at the beginning of the period have less importance than bars at the end?
                        If I would use the RSI for chart analysis I would implement an RSI based on SMAs. And perhaps that was the original intention of W. Wilder too.
                        Wilder also preferred exponential moving averages for other indicators, so certainly he did not intend to use the SMA. The SMA has an unpleasant property, as it changes its value, when old data drops out, whereas an EMA only reacts to new data coming in.

                        Below attached is a RSI, which uses a SMA for smoothing. In order to make it comparable to Wilder's RSI, the smoothing period for the SMA had to be adjusted. The modified RSI therefore uses an internal period for the SMA, which is calculated as

                        internal_period = 2 * RSI_period - 1

                        The default value for the internal period is 27, which is equivalent to Wilder's period of 14 used for the standard RSI. Indicator and chart attached below.
                        Attached Files

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by DJ888, 04-16-2024, 06:09 PM
                        6 responses
                        18 views
                        0 likes
                        Last Post DJ888
                        by DJ888
                         
                        Started by Jon17, Today, 04:33 PM
                        0 responses
                        1 view
                        0 likes
                        Last Post Jon17
                        by Jon17
                         
                        Started by Javierw.ok, Today, 04:12 PM
                        0 responses
                        6 views
                        0 likes
                        Last Post Javierw.ok  
                        Started by timmbbo, Today, 08:59 AM
                        2 responses
                        10 views
                        0 likes
                        Last Post bltdavid  
                        Started by alifarahani, Today, 09:40 AM
                        6 responses
                        41 views
                        0 likes
                        Last Post alifarahani  
                        Working...
                        X