'Relative Strength' Function (RSI)


This section takes a closer look at the popular RSI oscillator, invented by Welles Wilder. We're not going to review the entire Tutorial code here, just the DSL code for the function itself. After looking at the plain vanilla implementation, the second part of this section will look at how we can optimize this function with lambdas.

 

from  Tutorial 202:

sref<double> RSI_DEMO(sref<double> ser, size_t period)

{

          auto chg = ser - ser(1);                          // creates a series of changes

          auto up_chg = IF(chg > 0, chg, CONST(0.0));       // positive changes only

          auto dn_chg = IF(chg < 0, ABS(chg), CONST(0.0));  // negative changes only

          auto avg_up = EMA_WILDER(up_chg, period);         // exp. average of positive changes (Wilder ver.)

          auto avg_dn = EMA_WILDER(dn_chg, period);         // exp. average of negative changes (Wilder ver.)

          auto rs = SAFE_DIVIDE(avg_up, avg_dn);            // divide, avoid division by zero

          auto rv = 100 - (100 / (1 + rs));                 // normalize the output

 

          rv.plot_as("RSI-DEMO", period                  // set plotting information

                  plot_type::line, color::pink, 3);

 

          rv.pane_h_line(color::white, 70, 2);              // paint horizontal lines

          rv.pane_h_line(color::white, 50, 2);

          rv.pane_h_line(color::white, 30, 2);

          rv.pane_y_min(10);                                // adjust plot range

          rv.pane_y_max(90);

          

     return rv;

}

 

 

 

 

 

 

Program Output: