Download MetaTrader 5

Problems to change some formulas from MQL4 to MQL5

To add comments, please log in or register
rodrigosm
362
rodrigosm  

Hi everybody,

I’m trying to change some formulas from MQL4 to MQL5, but I’m having some problems with that. It’s my first trying. Could you please check my formula and inform me where my mistake is?

It did not plot a line.

 

Thanks

 

#property copyright "Copyright 2011, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   2
#property indicator_type1   DRAW_LINE
#property indicator_color1  Yellow
#property indicator_type2   DRAW_LINE
#property indicator_color2  Red


//----Q_Agains_Good_Wishs indicator parameters ----//
//
extern int Per_Trend_Direct=21;                    //
extern int Per_Fast_Trend=3;                      // 
extern int Per_Slow_Trend=7;                      //
                                                   //      
//-------------------------------------------------//

//---- Buffers ------//
                     //
double Go_Long[];    //
double Go_Short[];   //
                     //
//-------------------//

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

//---- indicators

   SetIndexBuffer(0,Go_Long,INDICATOR_DATA);
   SetIndexBuffer(1,Go_Short,INDICATOR_DATA);
   
//---
   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Per_Trend_Direct);
   


//---
   return(0);
  }
//+------------------------------------------------------------------+
//| 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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
//---
//--- preparing calculation
   int limit;
   double Trend_Direction;
//   double Trend_Force;
   double Fast_Trend_Line;
   double Slow_Trend_Line;
   bool Trend_up;
   bool Trend_down;
//  bool Strengh_Ok;
   bool Long_position_ok;
   bool Short_position_ok;

//---- Data ckecking ----//

   if(rates_total<2*Per_Trend_Direct-2)
   {
    return(0);
   }
   if(prev_calculated==0)
   {
    limit=0;
   }      
   else 
   {
    limit=prev_calculated-1;
   }
     


//---- Indicator Calculus ----//

    for(int i=limit;i<rates_total;i++) // Count down for one pass
     {

      Trend_Direction=iMA(NULL,
                          0,
                          Per_Trend_Direct,
                          0,
                          MODE_EMA,
                          PRICE_CLOSE);

      Fast_Trend_Line=iMA(NULL,
                          0,
                          Per_Fast_Trend,
                          0,
                          MODE_EMA,
                          PRICE_CLOSE);

      Slow_Trend_Line=iMA(NULL,
                          0,
                          Per_Slow_Trend,
                          0,
                          MODE_EMA,
                          PRICE_CLOSE);

      //---- Flag to show the trend ----//

      if(Close[i]>Trend_Direction)
        {
         Trend_up=1;
        }
      else
        {
         Trend_up=0;
        }

      if(Close[i]<Trend_Direction)
        {
         Trend_down=1;
        }
      else
        {
         Trend_down=0;
        }
      //---- Flag to show position opportunity ----//   
      //Long//
      if(Close[i]<Fast_Trend_Line && Close[i]>Slow_Trend_Line)
        {
         Long_position_ok=1;
        }
      else
        {
         Long_position_ok=0;
        }
      //Short//
      if(Close[i]>Fast_Trend_Line && Close[i]<Slow_Trend_Line)
        {
         Short_position_ok=1;
        }
      else
        {
         Short_position_ok=0;
        }

      //---- How I wiil entry in the market? ----//
      //Long//
      if(Trend_up /*&& Strengh_Ok */ && Long_position_ok)
        {
         Go_Long[i]=limit;//1;
        }
      else
        {
         Go_Long[i]=limit;
        }
      //Short//  
      if(Trend_down /*&& Strengh_Ok */ && Short_position_ok)
        {
         Go_Short[i]=-1;
        }
      else
        {
         Go_Short[i]=0;
        }

     }



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

 Your indicator just shows a line...the array    Go_Short[]  may only containe 0 or -1 elements.....it´s impossible to show it in the window.What you want to plot...?

 

    

onewithzachy
2271
onewithzachy  
rodrigosm:

Hi everybody,

I’m trying to change some formulas from MQL4 to MQL5, but I’m having some problems with that. It’s my first trying. Could you please check my formula and inform me where my mistake is?

It did not plot a line.

Thanks

Hi rodrigosm,

Would you please show the MQL4 codes, otherwise you may get an error but running CI

:D 

Alexey Da
Moderator
6655
Alexey Da  

Firstly iMA function returns a handle. It isn't the same as indicator value. You should use CopyBuffer function to get indicator value.

Secondly it isn't good idea to create indicator handles in OnCalculate function. OnInit is more suitable.

Finally look at source code of custom indicator Alligator (it comes in standard delivery). It might be helpful.

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
Language Basics / Functions / Event Handling Functions - Documentation on MQL5
To add comments, please log in or register