RSI - Multiple Levels and Color Changing depending on RSI current value

 

I have an indicator made in Pine Editor, for Trading View. and i want to make the same for MQL5 MT5. and i have a question and see if someone can aid me...




This is how it looks like in Pine Editor in the attachment,

as you can see when a strong movement appears and gets over level 70, it turns RED. if it goes past it into 80, it turns YELLOW.

If it goes below 30, it turns GREEN. If it's strong enough to go into 20, it turns CYAN. 


Now take a look, it doesn't stay green above 70, just when it rises. Once it's coming back it's blue again. This effect i really like. Same for under 30, once it goes back up it turns blue.

In Between 30-70, it's a regular blue. Sometimes when a really strong movement happens it turns into color even if it's inside 30-70 range.


I've already have added the lines 20, 30, 40, 50, 60, 70, 80 with their proper color to identify quickly what's going on.

But i want to duplicate this into MQL5 language. In PINE EDITOR, it looks like this the code to change color when RSI data is above/below certain value.


#######

if rsi >= 71 and rsi >= rsi[1]

    clr := #ff0000ff

if rsi >= 71 and rsi <= rsi[1]

    clr := #ff0000ff

if rsi <= 29 and rsi <= rsi[1]

    clr := #00ff00ff

if rsi <= 29 and rsi >= rsi[1]

    clr := #00ff00ff 

if rsi >= 78

    clr := #ffff00ff

if rsi <= 22

    clr := #00ffffff

    

plot(rsi, color=clr, linewidth = 2)

##########


Very simple code, MQL5 looks more complicated.

How can i do that? So far, the only way i've found to change color of RSI is like this


void OnInit()

  {

  //--- set color for each level (20, 30, 40) (60, 70, 80) and (50)

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrDarkGreen); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrLightGreen); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrDarkGreen); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,3,clrGray); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,4,clrDarkRed); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,5,clrRed); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,6,clrDarkRed);  

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,7,clrYellowGreen); 

   

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,5,STYLE_SOLID); 

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_SOLID); 

//--- 


https://www.thewaveacademy.net/wp-content/uploads/rsi2.jpg


These codes above successfully adds the horizontal lines as i want them to appear. Check Screenshot 2

But the RSI value doesn't change color depending on where it is.

The code i found made it blend/fade into multiple colors, i could only separate the code to make it green below 50, red above 50.


void setPlotColor(int plot)

  {

   PlotIndexSetInteger(plot,PLOT_COLOR_INDEXES,50); //Set number of colors



                                                    //Specify colors in loop



   for(int i=0;i<=24;i++)

     {

      PlotIndexSetInteger(plot,PLOT_LINE_COLOR,i,StringToColor("0,255,0"));

     }

   for(int i=0;i<=24;i++)

     {

      PlotIndexSetInteger(plot,PLOT_LINE_COLOR,i+25,StringToColor("255,0,0"));

     }

  }

This is the part responsible of adding color, and i have no idea why it's using i<=25, i+25, etc.

The logic is this:


IF RSI is above 70, turn red. If above 80, turn yellow.

IF RSI is below 30, turn green. if below 20, turn cyan.


Can someone help me code this? i know it's simple, but i'm new to this language.

Thanks!



FULL CODE BELOW ▼


//+------------------------------------------------------------------+

//|                                                          RSI.mq5 |

//|                        Copyright 2009, MetaQuotes Software Corp. |

//|                                              http://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright   "2009, MetaQuotes Software Corp."

#property link        "http://www.mql5.com"

#property description "Relative Strength Index"

//--- indicator settings

#property indicator_separate_window

#property indicator_minimum 0

#property indicator_maximum 100

#property indicator_level1 30

#property indicator_level2 70

/////////////////////////////////////////////////////////////////////

#property indicator_buffers 4 //The number of buffers has increased by 1

#property indicator_width1 2  //The line width has set to 4 pixels

/////////////////////////////////////////////////////////////////////

#property indicator_plots   1

/////////////////////////////////////////////////////////////////////

//Drawing style has been changed from DRAW_LINE to DRAW_COLOR_LINE

#property indicator_type1   DRAW_COLOR_LINE

/////////////////////////////////////////////////////////////////////

#property indicator_color1  DodgerBlue

//--- input parameters

input int InpPeriodRSI=14; // Period

//--- indicator buffers

double    ExtRSIBuffer[];

double    ExtPosBuffer[];

double    ExtNegBuffer[];

//--- global variable

int       ExtPeriodRSI;



//////////////////////////////////////////////////////////////////////

double buffer_color[]; //Declare an array for color indexes



//-- Modified

#property indicator_level1 20

#property indicator_level2 30

#property indicator_level3 40



#property indicator_level4 50



#property indicator_level5 60

#property indicator_level6 70

#property indicator_level7 80



//Added two functions

//+------------------------------------------------------------------+

//|    Set color for a graphic plot                                  |

//+------------------------------------------------------------------+

/*

*       The function specify the color for a graphic plot 

*       50 colors from Green to Blue are available.

*       The index of a graphic plot is passed to the function.

*/

void setPlotColor(int plot)

  {

   PlotIndexSetInteger(plot,PLOT_COLOR_INDEXES,50); //Set number of colors



                                                    //Specify colors in loop



   for(int i=0;i<=24;i++)

     {

      PlotIndexSetInteger(plot,PLOT_LINE_COLOR,i,StringToColor("0,255,0"));

     }

   for(int i=0;i<=24;i++)

     {

      PlotIndexSetInteger(plot,PLOT_LINE_COLOR,i+25,StringToColor("255,0,0"));

     }

  }

//+------------------------------------------------------------------+

//|  Get index of the color                                           |

//+------------------------------------------------------------------+

/*

*       The function returns the color index

*       The first parameter is the current value of the indicator

*       The second parameter is the minimal value of the indicator

*       The third parameter is the maximal value of the indicator

*/

int getPlotColor(double current,double min,double max)

  {

   return((int)NormalizeDouble((50/(max-min))*current,0));

  }

//////////////////////////////////////////////////////////////////////





//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

void OnInit()

  {

  //--- set color for each level (20, 30, 40) (60, 70, 80) and (50)

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,0,clrDarkGreen); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,1,clrLightGreen); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,2,clrDarkGreen); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,3,clrGray); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,4,clrDarkRed); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,5,clrRed); 

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,6,clrDarkRed);  

   IndicatorSetInteger(INDICATOR_LEVELCOLOR,7,clrYellowGreen); 

   

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,5,STYLE_SOLID); 

   IndicatorSetInteger(INDICATOR_LEVELSTYLE,1,STYLE_SOLID); 

//--- 



//--- check for input

   if(InpPeriodRSI<1)

     {

      ExtPeriodRSI=12;

      Print("Incorrect value for input variable InpPeriodRSI =",InpPeriodRSI,

            "Indicator will use value =",ExtPeriodRSI,"for calculations.");

     }

   else ExtPeriodRSI=InpPeriodRSI;

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtRSIBuffer,INDICATOR_DATA);

   

/////////////////////////////////////////////////////////////////////

//Assign the array with buffer of color indexes

        SetIndexBuffer(1,buffer_color,INDICATOR_COLOR_INDEX);

//The order of buffers is changed!

        SetIndexBuffer(2,ExtPosBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(3,ExtNegBuffer,INDICATOR_CALCULATIONS);

//Set colors

   setPlotColor(0);

/////////////////////////////////////////////////////////////////////



//--- set accuracy

   IndicatorSetInteger(INDICATOR_DIGITS,2);

//--- sets first bar from what index will be drawn

   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,ExtPeriodRSI);

//--- name for DataWindow and indicator subwindow label

   IndicatorSetString(INDICATOR_SHORTNAME,"RSI("+string(ExtPeriodRSI)+")");

//--- initialization done

  }

//+------------------------------------------------------------------+

//| Relative Strength Index                                          |

//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const int begin,

                const double &price[])

  {

   int    i;

   double diff;

//--- check for rates count

   if(rates_total<=ExtPeriodRSI)

      return(0);

//--- preliminary calculations

   int pos=prev_calculated-1;

   if(pos<=ExtPeriodRSI)

     {

      //--- first RSIPeriod values of the indicator are not calculated

      ExtRSIBuffer[0]=0.0;

      ExtPosBuffer[0]=0.0;

      ExtNegBuffer[0]=0.0;

      double SumP=0.0;

      double SumN=0.0;

      for(i=1;i<=ExtPeriodRSI;i++)

        {

         ExtRSIBuffer[i]=0.0;

         ExtPosBuffer[i]=0.0;

         ExtNegBuffer[i]=0.0;

         diff=price[i]-price[i-1];

         SumP+=(diff>0?diff:0);

         SumN+=(diff<0?-diff:0);

        }

      //--- calculate first visible value

      ExtPosBuffer[ExtPeriodRSI]=SumP/ExtPeriodRSI;

      ExtNegBuffer[ExtPeriodRSI]=SumN/ExtPeriodRSI;

      ExtRSIBuffer[ExtPeriodRSI]=100.0-(100.0/(1.0+ExtPosBuffer[ExtPeriodRSI]/ExtNegBuffer[ExtPeriodRSI]));

      //--- prepare the position value for main calculation

      pos=ExtPeriodRSI+1;

     }

//--- the main loop of calculations

   for(i=pos;i<rates_total;i++)

     {

      diff=price[i]-price[i-1];

      ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(ExtPeriodRSI-1)+(diff>0.0?diff:0.0))/ExtPeriodRSI;

      ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(ExtPeriodRSI-1)+(diff<0.0?-diff:0.0))/ExtPeriodRSI;

      ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);

/////////////////////////////////////////////////////////////////////

//Paint it

                buffer_color[i] = getPlotColor(ExtRSIBuffer[i],0,100);

/////////////////////////////////////////////////////////////////////

     }

//--- OnCalculate done. Return new prev_calculated.

   return(rates_total);

  }

//+------------------------------------------------------------------+
Reason: