RSISignal Indicator port to MQL5...

 

Hello All,


Whilst browsing for a useful indicator with sorce, I came across the RSISignal - indicator for MetaTrader 4. As I have my own wrapper for adding logic, I thought I'd port this combined indicator to MQL5. I have been making use of the plethora of open source indicators made available by generous & creative authors.

For this port I made extensive use of the excellent 'Migrating from MQL4 to MQL5', carefully following, modifying and adding based on this document. I know that I can make the execution more efficient, but I thought I'd just get it working first.

It now compiles, but displays sweet nothing. It seems that a little knowledge isn't enough, and I was hoping someone might take a look at what I have done and give me some hints/ideas as to what I have done wrong and what I might do to fix it please.

I'll share the port with the author once it's working as this is just derivative. Credit where credit is due.


Thank you in advance,


With my best regards, ESB.

RSISignal
RSISignal
  • 2018.04.16
  • www.mql5.com
This indicator is based on original RSI, but more features are added to general signals and watch the strength of the price movement. The main purpose is to general buy/sell signals using smoothed RSI and its MA.
Files:
RSISignal.zip  17 kb
 

Here's a starting point.

#property version   "1.00"
#property indicator_separate_window
#property indicator_maximum 100
#property indicator_minimum 0
#property indicator_buffers 7
#property indicator_plots   4
//--- plot OB
#property indicator_label1  "Overbought;Oversold"
#property indicator_type1   DRAW_FILLING
#property indicator_color1  clrGray
#property indicator_style1  STYLE_SOLID
//--- plot RSI1
#property indicator_label2  "RSI"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrDeepSkyBlue
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2
//--- plot MA1
#property indicator_label3  "MA of RSI;Centre"
#property indicator_type3   DRAW_COLOR_HISTOGRAM2
#property indicator_color3  clrGreen,clrLimeGreen,clrRed,clrFireBrick
#property indicator_style3  STYLE_SOLID
#property indicator_width3  5
//--- plot MA2
#property indicator_label4  "MA of MA"
#property indicator_type4   DRAW_LINE
#property indicator_color4  clrGold
#property indicator_style4  STYLE_SOLID
#property indicator_width4  2
//--- input parameters
input int      RSIPeriod=13;
input ENUM_APPLIED_PRICE RSIPrice=PRICE_CLOSE;
input int      MA1Period=10;
input int      MA1Shift=0;
input ENUM_MA_METHOD MA1Method=MODE_SMA;
input int      MA2Period=5;
input int      MA2Shift=0;
input ENUM_MA_METHOD MA2Method=MODE_SMA;
input int      OBLevel=70;
input int      OSLevel=30;
//--- indicator buffers
double         RSIBuffer[];
double         MA1Buffer[];
double         MA50Buffer[];
double         MA1Colors[];
double         MA2Buffer[];
double         OBBuffer[];
double         OSBuffer[];
//---
int handleRSI=INVALID_HANDLE;
int handleMA1=INVALID_HANDLE;
int handleMA2=INVALID_HANDLE;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit() {
//---
   if((handleRSI=iRSI(Symbol(),PERIOD_CURRENT,RSIPeriod,RSIPrice))==INVALID_HANDLE ||
      (handleMA1=iMA(Symbol(),PERIOD_CURRENT,MA1Period,MA1Shift,MA1Method,handleRSI))==INVALID_HANDLE ||
      (handleMA2=iMA(Symbol(),PERIOD_CURRENT,MA2Period,MA2Shift,MA2Method,handleMA1))==INVALID_HANDLE)
      return(INIT_PARAMETERS_INCORRECT);
//--- indicator buffers mapping
   SetIndexBuffer(0,OBBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,OSBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,RSIBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,MA1Buffer,INDICATOR_DATA);
   SetIndexBuffer(4,MA50Buffer,INDICATOR_DATA);
   SetIndexBuffer(5,MA1Colors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(6,MA2Buffer,INDICATOR_DATA);
//---
   IndicatorSetString(INDICATOR_SHORTNAME,StringFormat("RSI Signal(%d,%s %d,%s %d)",
                                          RSIPeriod,StringSubstr(EnumToString(MA1Method),5),MA1Period,
                                                    StringSubstr(EnumToString(MA2Method),5),MA2Period));
   IndicatorSetInteger(INDICATOR_DIGITS,1);
   IndicatorSetInteger(INDICATOR_LEVELS,3);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,0,OBLevel);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,1,50);
   IndicatorSetDouble(INDICATOR_LEVELVALUE,2,OSLevel);
//---
   PlotIndexSetInteger(0,PLOT_SHOW_DATA,false);
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int32_t rates_total,
                const int32_t prev_calculated,
                const int32_t begin,
                const double &price[]) {
//---
   int toCopy=(rates_total==prev_calculated)?1:rates_total-prev_calculated;
   if(CopyBuffer(handleRSI,0,0,toCopy,RSIBuffer)!=toCopy ||
      CopyBuffer(handleMA1,0,0,toCopy,MA1Buffer)!=toCopy ||
      CopyBuffer(handleMA2,0,0,toCopy,MA2Buffer)!=toCopy)
      return(0);
//---
   int start=(rates_total==prev_calculated)?rates_total-1:(!prev_calculated)?RSIPeriod+MA1Period+MA2Period:prev_calculated;
   for(int i=start;i<rates_total;i++) {
      OBBuffer[i]=OBLevel;
      OSBuffer[i]=OSLevel;
      double preMA=MA1Buffer[i-1];
      double curMA=MA1Buffer[i];
      MA1Colors[i]=(curMA>50 && curMA>preMA)?0:
                   (curMA>50 && curMA<preMA)?1:
                   (curMA<50 && curMA<preMA)?2:3;
      MA50Buffer[i]=50;
   }
//--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
 
Ernst Van Der Merwe #:

Here's a starting point.

Thanks @Ernst Van Der Merwe,

Yes, that's a lot tidier - I stuck to the original format and had cheated with the MQL4 port functions - which would be super inefficient (and lazy).

I'll take another look and thank you for taking the trouble.

With my best regards, ESB.

 
Ernst Van Der Merwe #:

Here's a starting point.

I didn't do your work justice in my reply! (it was late!) I compeletely missed the LEVELS bits & the colour mapping. That's very useful, thanks.