CombinedRSI, Code Review appreciated

 

Hello  MQL Community :-),


I'm pretty  new to MQL and just started diving into this whole stuff, actually im exercising a bit by implementing a custom Indicator based on the RSI.

For the beginning a very simple improvement will be coded, let the user select multiple timeframes, calculate an average based on the selection and then Display the average as combined RSI, nothing special as said just for training on MQL(im more a sysadmin guy in real-life, so i barely code something except a few shellscripts).


The whole thing works so far but im stepping into two Problems and i hope someone of you would be that nice to give me a hint


1. the Major Problem is that the Indicator doesnt calculate older Ticks like the normal RSI, it just displays the values since its added to a window, the ticks before are just a flat line(looks like the older ticks are calculated in the scope of the while loop and there seems to be a problem)

I have oriented by this tutorial but it doesnt seem to be actual, has the method start() instead of OnCalculate():

https://book.mql4.com/samples/icustom


2. I'd like to Display a second line with a single RSI based on actual Timeframe to compare the values easily, but I haven't archieved it to Display the line although it is done the same Way as the first one.



As said any Hints or Clues would be really nice from anyone more expierienced than me:-)


#property copyright "GNU/GPL"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict



#property indicator_separate_window
#property indicator_minimum 1
#property indicator_maximum 100
#property indicator_buffers    2 //only one line possible in separated window?
#property indicator_color1     DodgerBlue
#property indicator_color2     Red
#property indicator_level1     30.0
#property indicator_level2     70.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT

//input vars
input int RSIPeriod=14;
input bool m1=false;
input bool m5=true;
input bool m15=false;
input bool m30=false;
input bool h1=false;
input bool h4=false;
input bool d1=false;
input bool w1=false;
input bool mon1=false;

//buffers
double buffer0[];
double buffer1[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(){
   //line for combined rsi
   SetIndexBuffer(0,buffer0);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,3);
   //line for standard RSI ?
   SetIndexBuffer(1,buffer1);
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT,2);
   
   IndicatorShortName("CombinedRSI, Period="+RSIPeriod);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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 selectedTimeframes=0;

   //Array with booleans of selected timeframes
   bool timeframes[9];
   timeframes[0]=m1;
   timeframes[1]=m5;
   timeframes[2]=m15;
   timeframes[3]=m30;
   timeframes[4]=h1;
   timeframes[5]=h4;
   timeframes[6]=d1;
   timeframes[7]=w1;
   timeframes[8]=mon1;
   
   //Array with integers of ENUM_TIMEFRAMES
   int minutes[9];
   minutes[0]=1;
   minutes[1]=5;
   minutes[2]=15;
   minutes[3]=30;
   minutes[4]=60;
   minutes[5]=240;
   minutes[6]=1440;
   minutes[7]=10080;
   minutes[8]=43200;
   
   selectedTimeframes=countSelectedTimeframes(timeframes);
   
   //if there a no timeframes selected by the user set m1 as default
   if(selectedTimeframes<=0){
      timeframes[0]=true;
      selectedTimeframes=countSelectedTimeframes(timeframes);
   }   
   

   //start
   int i,countedBars;
   countedBars=IndicatorCounted();
   i=Bars-countedBars-1;
   while(i>=0){
      double sumOfRSI=0;
      //get the sum of all selected RSI
      for(int j=0;j<ArraySize(timeframes);j++){
         if(timeframes[j]==true){
            double temp=iRSI(NULL,minutes[j],RSIPeriod,PRICE_MEDIAN,i);
            sumOfRSI+=temp;
         }
      }
      //and divide by selectedTimeframes
      double avgRSI=sumOfRSI/selectedTimeframes;
      
      //assign the value to the buffer
      buffer0[i]=avgRSI;
      buffer1[i]=iRSI(NULL,0,RSIPeriod,PRICE_MEDIAN,i);
      i--;
   }



   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+


int countSelectedTimeframes(bool &array1[]){
   int counter=0;
   for(int i=0;i<ArraySize(array1);i++){
      if(array1[i]==true){
         counter++;
      }
      else{
         // do nothing
      }
   }
   return counter;
}
Creation of Custom Indicators - Simple Programs in MQL4 - MQL4 Tutorial
Creation of Custom Indicators - Simple Programs in MQL4 - MQL4 Tutorial
  • book.mql4.com
When creating a trading strategy a developer often faces the necessity to draw graphically in a security window a certain dependence calculated by a user (programmer). For this purpose MQL4 offers the possibility of creating custom indicators. Custom Indicator is an application program coded in MQL4; it is basically intended for graphical...
 
I know that it is not obvious, but topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I will move your topic to the MQL4 and Metatrader 4 section.
 
            double temp=iRSI(NULL,minutes[j],RSIPeriod,PRICE_MEDIAN,i);

You are mixing apples and oranges.

 

Hey Thanks for the quick replies;-)


@Keith Sorry my bad, i didn't know that the EA/Indicator Section is exclusively for mql5 only, won't happen again.


@William Hey Thanks, that explains why it can't calculate the ticks before starting :-)

 
DigitalAlchemist:

@Keith Sorry my bad, i didn't know that the EA/Indicator Section is exclusively for mql5 only, won't happen again.

Don't worry about it. The forum is badly organised and confusing.

MQL4 and MQL5 used to be 2 different websites and when they combined the 2 Metaquotes just gave the 1 section for MQL4 in the forum.

Reason: