Sultonov differential indicator - page 41

 
Dmitry Fedoseev:

There will be a code in 5 mins. Keep in mind that RSI uses Wilder smoothing, which is the same as exponential smoothing but with a longer period, so there may be a noticeable mismatch.


What is there to prepare? We throw out the main buffer from RSI and put the existing two buffers in its place:

//+------------------------------------------------------------------+
//|                                                          RSI.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "https://www.mql4.com"
#property description "Relative Strength Index"
#property strict

#property indicator_separate_window
#property indicator_buffers    2
#property  indicator_color1     clrBlue
#property  indicator_color2     clrRed

//--- input parameters
input int InpRSIPeriod=14; // RSI Period
//--- buffers
double ExtPosBuffer[];
double ExtNegBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   SetIndexBuffer(0,ExtPosBuffer);
   SetIndexBuffer(1,ExtNegBuffer);
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexStyle(1,DRAW_LINE);
//--- name for DataWindow and indicator subwindow label
   short_name="RSI("+string(InpRSIPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- check for input
   if(InpRSIPeriod<2)
     {
      Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpRSIPeriod);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   int    i,pos;
   double diff;
//---
   if(Bars<=InpRSIPeriod || InpRSIPeriod<2)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtPosBuffer,false);
   ArraySetAsSeries(ExtNegBuffer,false);
   ArraySetAsSeries(close,false);
//--- preliminary calculations
   pos=prev_calculated-1;
   if(pos<=InpRSIPeriod)
     {
      //--- first RSIPeriod values of the indicator are not calculated
      ExtPosBuffer[0]=0.0;
      ExtNegBuffer[0]=0.0;
      double sump=0.0;
      double sumn=0.0;
      for(i=1; i<=InpRSIPeriod; i++)
        {
         ExtPosBuffer[i]=0.0;
         ExtNegBuffer[i]=0.0;
         diff=close[i]-close[i-1];
         if(diff>0)
            sump+=diff;
         else
            sumn-=diff;
        }
      //--- calculate first visible value
      ExtPosBuffer[InpRSIPeriod] = sump / InpRSIPeriod;
      ExtNegBuffer[InpRSIPeriod] = sumn / InpRSIPeriod;
      //--- prepare the position value for main calculation
      pos=InpRSIPeriod+1;
     }
//--- the main loop of calculations
   for(i=pos; i<rates_total && !IsStopped(); i++)
     {
      diff=close[i]-close[i-1];
      ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
      ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
     }
//---
   return(rates_total);
  }
//+------------------------------------------------------------------+
There is no smoothing there. And the calculation is different in that the strength of the bears includes the doji. That is the difference. The DA takes this into account.
 
Dmitry Fedoseev:

There will be a code in 5 mins. Keep in mind that RSI uses Wilder smoothing, which is the same as exponential but with a longer period, so there may be a noticeable mismatch.

Are you backing out now?
 

Attached is an indicator.

Parameters:

period - period in bars;

Components - component calculation option:

  • c_rsi - as for RSI
  • c_adx - as for ADX
  • c_x - as for RSI, but division not by period, but by actual number of increments.

Smooth - smooth method:

  • s_wilder - Wilder method
  • s_ema - exponential smoothing.
Keep in mind that RSI components are smoothed by Wilder, while ADX components are smoothed by the conventional exponential method (i.e. period 14 for RSI corresponds to period 27 for ADX).

Power - the way the final line is calculated:

  • f_off - not displayed to better see the components
  • f_rsi - as for RSI
  • f_adx - as ADX.

For information: final line of RSI is not smoothed, ADX is smoothed.

***

You can get RSI and ADX and various hybrids by this indicator through settings.

To get RSI:

  • Components - c_rsi
  • Smooth - s_wilder
  • Power - f_rsi;

To get ADX:

  • Components - c_adx
  • Smooth - s_ema
  • Power - f_adx

Power - enable/disable to keep components out of the way.

***

Template with two indicators as in the image from this post is also attached.

Files:
qwerty.mq5  6 kb
2ind.tpl  201 kb
 
Yousufkhodja Sultonov:
Are you backing up now?

Where's the back story? Five minutes to write a post is a lot?

 
Ihor Herasko:

...

There's no antialiasing in there....

What's that? -

ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
It's wilder smoothing. It's essentially identical to the exponential, but slower.
 
Dmitry Fedoseev:

Where's the back story? Is 5 minutes to write a post a lot?

There's no smoothing in the DA and it's not required. What do you say to that?
 
Yousufkhodja Sultonov:
There is no smoothing in DA and it is not required. What do you think about it?

It is possible not to smooth as in RSI, but the components themselves, it is a simple average. It is possible to refine my above universal indicator to enable/disable smoothing. But conceptually nothing changes.

Besides, you'd better show it before you speak. Why do you ask me so severely, and you blah blah blah

 
Dmitry Fedoseev:

Where's the back story? Is 5 minutes to write a post a lot?

What I meant was this: "Keep in mind that RSI uses Wilder smoothing, it's the same as exponential but with a larger period, there can be a noticeable mismatch from that. "

Any conventions with longer or shorter periods, exponential or regular smoothing - what's the point of all this? DA takes the Bull by the horns and the Bear by the paws and feet.

 
Dmitry Fedoseev:
It is possible not to smooth as in RSI, but the components themselves, it is a simple average. It is possible to refine my above universal indicator to enable/disable smoothing. But conceptually nothing changes.
Now of course you can.
 
Dmitry Fedoseev:
It is possible not to smooth as in RSI, but the components themselves, it is a simple average. You can refine my above universal indicator to enable/disable smoothing. But conceptually nothing changes.

You are trying to argue with formulas with someone who speaks the language of poetry.