How to convert these to MQL5? IndicatorBuffers, High[], Low[]

To add comments, please log in or register
Yasir Kidil
81
Yasir Kidil  

I need some help. I did convert the most of codes from mql4 to mql5 but there are still some problems. 

Errors:

'IndicatorBuffers' - function not defined test.mq5 33 5
'High' - undeclared identifier test.mq5 113 18
'Low' - undeclared identifier test.mq5 113 50
'High' - undeclared identifier test.mq5 115 14
'Low' - undeclared identifier test.mq5 115 28

All codes:

//+------------------------------------------------------------------+
//|                                                         test.mq5 |
//|                                                             test |
//|                                                             test |
//|                                                             v1.0 |
//+------------------------------------------------------------------+
#property copyright   "test"
#property link        "test"
#property version     "1.0"
//#property strict

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 clrLime
#property indicator_color2 clrRed
#property indicator_level1 1
#property indicator_level2 -1
#property indicator_levelcolor clrDimGray
#property indicator_levelstyle STYLE_DOT

double Fisher[];
double Trigger[];
double Value[];

extern int period = 10;
int drawBegin = 8;

int OnInit()
  {
    if(period < 3) 
    period = 3;
    drawBegin = MathMax(period, drawBegin);
    IndicatorBuffers(3);
   
    SetIndexBuffer(0,Fisher);
    PlotIndexSetString(0,PLOT_LABEL,"Fisher");
    PlotIndexSetInteger(0,PLOT_DRAW_BEGIN, drawBegin);
    PlotIndexSetInteger(0,PLOT_LINE_STYLE, STYLE_SOLID);
    
    SetIndexBuffer(1,Trigger);
    PlotIndexSetString(1,PLOT_LABEL,"Trigger");
    PlotIndexSetInteger(1,PLOT_DRAW_BEGIN, drawBegin);
    PlotIndexSetInteger(1,PLOT_LINE_STYLE, STYLE_SOLID);
    
    SetIndexBuffer(2,Value);
    
    IndicatorSetString(INDICATOR_SHORTNAME,"FTI ("+IntegerToString(period)+")");

  return(INIT_SUCCEEDED);
  }

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[]) 
{
    if ( Bars(Symbol(),NULL) <= period )  
    return (0);
    
    int countedBars = prev_calculated;
    
    if (countedBars < 0) 
    return (-1);
    
    if (countedBars > 0) 
    countedBars--;
    
    int s, limit = MathMin(Bars(Symbol(),NULL) - countedBars - 1, Bars(Symbol(),NULL) - drawBegin);
    
    for (s = limit; s >= 0; s--) 
    {
        double price = P(s);
        double MaxH = price;
        double MinL = price;
        
        for (int i = 0; i < period; i++) 
        {
            price = P(s + i);
            if (price > MaxH) MaxH = price;
            if (price < MinL) MinL = price;
        } 
               
        Value[s] = 0.33 * 2.0 * ((P(s) - MinL) / (MaxH - MinL) - 0.5) + 0.67 * Value[s + 1];
        
        if (Value[s] > 0.99) 
        Value[s] = 0.999;
        
        if (Value[s] < -0.99) 
        Value[s] = -0.999;
        
        Fisher[s] = 0.5 * MathLog((1 + Value[s]) / (1 - Value[s])) + 0.5 * Fisher[s + 1];
        Trigger[s] = Fisher[s + 1];
        
        if (s > Bars(Symbol(),NULL) - drawBegin - 2) 
        {
            Fisher[s] = 0;
            Trigger[s] = 0;
        }
    }  
   return(rates_total);
  }

double P(int index) 
{
    if (index >= Bars(Symbol(),NULL)) 
    {
        return ((High[Bars(Symbol(),NULL) - 1] + Low[Bars(Symbol(),NULL) - 1]) / 2.0);
    }
    return ((High[index] + Low[index]) / 2.0);
}

Marco vd Heijden
Moderator
8844
Marco vd Heijden  
it's all lowercase high not High.
Yasir Kidil
81
Yasir Kidil  
Marco vd Heijden:
it's all lowercase high not High.

same errors

"double P" is a seperate function from the OnCalculate. I take the high and low errors because of that, I think. How can I solve?

Keith Watford
Moderator
15796
Keith Watford  
Marco vd Heijden:
it's all lowercase high not High.
Yasir Kidil:

same errors

"double P" is a seperate function from the OnCalculate. I take the high and low errors because of that, I think. How can I solve?

Marco is referring to

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[]) 

it is not High[], Low[], it is high[], low[].

They can only be used in OnCalculate() or passed by reference to a function.

Marco vd Heijden
Moderator
8844
Marco vd Heijden  

In that case you can try:

iHigh(Symbol(),PERIOD_CURRENT,bar_number);

It's the same as mql4.

fxsaber
11976
fxsaber  
MQL's OOP notes: Converting MetaTrader 4 indicators to MetaTrader 5
MQL's OOP notes: Converting MetaTrader 4 indicators to MetaTrader 5
  • 2017.10.11
  • www.mql5.com
It has been a long time since MetaTrader 5 was released, but MQL products for MetaTrader 4 do still prevail on mql5.com site (both in the codebase, and in the market), and in the Internet in general. This is why it's often a problem that an interesting indicator or expert adviser exists for MetaTrader 4 but is missing for MetaTrader 5. Today...
Yasir Kidil
81
Yasir Kidil  
Marco vd Heijden:

In that case you can try:

It's the same as mql4.

Thanks. I don't take any error or warning even with "#property strict", but there is no display in indicator window. I couldn't solve it.


//+------------------------------------------------------------------+
//|                                                         test.mq5 |
//|                                                             test |
//|                                                             test |
//|                                                             v1.0 |
//+------------------------------------------------------------------+
#property copyright   "test"
#property link        "test"
#property version     "1.0"
#property strict

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 clrLime
#property indicator_color2 clrRed
#property indicator_level1 1
#property indicator_level2 -1
#property indicator_levelcolor clrDimGray
#property indicator_levelstyle STYLE_DOT

double Fisher[];
double Trigger[];
double Value[];

extern int period = 10;
int drawBegin = 8;
int prev_calculated_global = 0;

int OnInit()
  {
    if(period < 3) 
    period = 3;
    drawBegin = MathMax(period, drawBegin);
   
    SetIndexBuffer(0,Fisher);
    PlotIndexSetString(0,PLOT_LABEL,"Fisher");
    PlotIndexSetInteger(0,PLOT_DRAW_BEGIN, drawBegin);
    PlotIndexSetInteger(0,PLOT_LINE_STYLE, STYLE_SOLID);
    
    SetIndexBuffer(1,Trigger);
    PlotIndexSetString(1,PLOT_LABEL,"Trigger");
    PlotIndexSetInteger(1,PLOT_DRAW_BEGIN, drawBegin);
    PlotIndexSetInteger(1,PLOT_LINE_STYLE, STYLE_SOLID);
    
    SetIndexBuffer(2,Value);
    
    IndicatorSetString(INDICATOR_SHORTNAME,"FTI ("+IntegerToString(period)+")");

  return(INIT_SUCCEEDED);
  }

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[]) 
{   
    prev_calculated_global = prev_calculated; 
    if ( Bars(Symbol(),NULL) <= period )  
    return (0);
    
    int countedBars = prev_calculated;
    
    if (countedBars < 0) 
    return (-1);
    
    if (countedBars > 0) 
    countedBars--;
    
    int s, limit = MathMin(Bars(Symbol(),NULL) - countedBars - 1, Bars(Symbol(),NULL) - drawBegin);
    
    for (s = limit; s >= 0; s--) 
    {
        double price = P(s);
        double MaxH = price;
        double MinL = price;
        
        for (int i = 0; i < period; i++) 
        {
            price = P(s + i);
            if (price > MaxH) MaxH = price;
            if (price < MinL) MinL = price;
        } 
               
        Value[s] = 0.33 * 2.0 * ((P(s) - MinL) / (MaxH - MinL) - 0.5) + 0.67 * Value[s + 1];
        
        if (Value[s] > 0.99) 
        Value[s] = 0.999;
        
        if (Value[s] < -0.99) 
        Value[s] = -0.999;
        
        Fisher[s] = 0.5 * MathLog((1 + Value[s]) / (1 - Value[s])) + 0.5 * Fisher[s + 1];
        Trigger[s] = Fisher[s + 1];
        
        if (s > Bars(Symbol(),NULL) - drawBegin - 2) 
        {
            Fisher[s] = 0;
            Trigger[s] = 0;
        }
    }  
   prev_calculated_global = rates_total;
   return(rates_total);
  }

double P(int index) 
{
    if (index >= Bars(Symbol(),NULL)) 
    {
        return ( (iHigh(Symbol(),PERIOD_CURRENT,Bars(Symbol(),NULL)-1) + iLow(Symbol(),PERIOD_CURRENT,Bars(Symbol(),NULL)-1)) / 2.0 );
    }
    return ( (iHigh(Symbol(),PERIOD_CURRENT,index) + iLow(Symbol(),PERIOD_CURRENT,index)) / 2.0 );
}
Yasir Kidil
81
Yasir Kidil  

thanks but it's very complicated for me :-/

Yasir Kidil
81
Yasir Kidil  
can anybody help me? :-\
Marco vd Heijden
Moderator
8844
Marco vd Heijden  

I have no idea what it should do but i would say drop the P and code it directly into the robot.

To add comments, please log in or register