Download MetaTrader 5

First attempt to code mql5 migrating from mql4. Issues.

To add comments, please log in or register
kilian13
13
kilian13  

This is my first attempt to write a code in mql5 and there are a few issues I am facing.

 

I wanted to write a simple indicator that displays a red block if Pair1 bar is bullish and a green block if it is bearish.

Problem #1: the label and arrow settings are not applied to the 2nd pair buffer. it shows a little circle instead of a square.

#2 : my iClose and iOpen return wrong values.

 

I must have made some rudimentary mistakes but I am not able to find them myself. Can someone please help me out here. 

 

//+------------------------------------------------------------------+
//|                                                        Hedge.mq5 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_minimum 1
#property indicator_maximum 10
#property indicator_buffers 4
#property indicator_plots   2
//--- plot Label1
#property indicator_color1  clrLime,clrRed
#property indicator_type1   DRAW_COLOR_ARROW
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
// ---- plot Pair1
#property indicator_color2  clrLime,clrRed
#property indicator_type2   DRAW_COLOR_ARROW
#property indicator_style2  STYLE_SOLID
#property indicator_width2  2
//--- input parameters
input string   Pair1="EURUSD";
input string   Pair2="USDCHF";
//--- indicator buffers
double Pair1Buffer[],Pair2Buffer[];
double Pair1CBuffer[],Pair2CBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Pair1Buffer,INDICATOR_DATA);
   PlotIndexSetInteger(0,PLOT_ARROW,110);
   PlotIndexSetString(0,PLOT_LABEL,Pair1);
   
   SetIndexBuffer(1,Pair1CBuffer,INDICATOR_COLOR_INDEX);
   
   SetIndexBuffer(2,Pair2Buffer,INDICATOR_DATA);
   PlotIndexSetInteger(2,PLOT_ARROW,110);
   PlotIndexSetString(2,PLOT_LABEL,Pair2);
   
   SetIndexBuffer(3,Pair2CBuffer,INDICATOR_COLOR_INDEX);
   
   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 &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---   
      //--- auxiliary variables
   int i=0; 
      //--- set position for beginning
   if(i<prev_calculated) i=prev_calculated-1;
  
   //--- start calculations
   while(i<rates_total)
   {
      Pair1Buffer[i] = 2;
      Pair2Buffer[i] = 8;
      
      if( iClose(Pair1,0,i) >= iOpen(Pair1,0,i) )   { Pair1CBuffer[i] = 1; }
         else { Pair1CBuffer[i] = 0; }
      
      if( iClose(Pair2,0,i) > iOpen(Pair2,0,i) )   { Pair2CBuffer[i] = 1; }
         else { Pair2CBuffer[i] = 0; }
    
      i++;
   }
   
   //Try a fix shift.
   double temp[1];
   int shift =  5;
   CopyOpen(Pair1,PERIOD_CURRENT,shift,1,temp);
   Comment("Open: " + DoubleToString(temp[0],Digits()) + "\n" + "Close: " + DoubleToString(iClose(Pair1,0,shift),Digits()) + "\nShift: " + IntegerToString(shift,1));
      
         
   return(rates_total);
  }
//+------------------------------------------------------------------+


double iClose(string symbol,int period, int shift){
   double temp[1];
   if(CopyClose(symbol,period(period),shift,1,temp) == 1) return(temp[0]);
   	else	return(-1);
}

double iOpen(string symbol,int period, int shift){
   double temp[1];
   if(CopyOpen(symbol,period(period),shift,1,temp) == 1) return(temp[0]);
      else  return(-1);
}

double iHigh(string symbol,int period, int shift){
   double temp[1];
   if(CopyOpen(symbol,period(period),shift,1,temp) == 1) return(temp[0]);
      else  return(-1);
}

double iLow(string symbol,int period, int shift){
   double temp[1];
   if(CopyLow(symbol,period(period),shift,1,temp) == 1) return(temp[0]);
      else  return(-1);
}

long iVolume(string symbol,int period, int shift, bool realVolume = true){
   long temp[1];
   if(realVolume)
      if(CopyRealVolume(symbol,period(period),shift,1,temp) != -1) return(temp[0]);
         else  return(-1);
      else if (CopyTickVolume(symbol,period(period),shift,1,temp) != -1) return(temp[0]);
         else  return(-1);
    }

ENUM_TIMEFRAMES period(int tf = 0)
  {
   switch(tf)
     {
      case 0: return(PERIOD_CURRENT);
      case 1: return(PERIOD_M1);
      case 5: return(PERIOD_M5);
      case 15: return(PERIOD_M15);
      case 30: return(PERIOD_M30);
      case 60: return(PERIOD_H1);
      case 240: return(PERIOD_H4);
      case 1440: return(PERIOD_D1);
      case 10080: return(PERIOD_W1);
      case 43200: return(PERIOD_MN1);
      
      case 2: return(PERIOD_M2);
      case 3: return(PERIOD_M3);
      case 4: return(PERIOD_M4);      
      case 6: return(PERIOD_M6);
      case 10: return(PERIOD_M10);
      case 12: return(PERIOD_M12);
      case 16385: return(PERIOD_H1);
      case 16386: return(PERIOD_H2);
      case 16387: return(PERIOD_H3);
      case 16388: return(PERIOD_H4);
      case 16390: return(PERIOD_H6);
      case 16392: return(PERIOD_H8);
      case 16396: return(PERIOD_H12);
      case 16408: return(PERIOD_D1);
      case 32769: return(PERIOD_W1);
      case 49153: return(PERIOD_MN1);      
      default: return(PERIOD_CURRENT);
     }
  }
kilian13
13
kilian13  
Is this the wrong place for such requests?
phi nuts
2184
phi nuts  
kilian13:
Is this the wrong place for such requests?

No, just ... either lazy or busy ... being lazy to reply.

I haven't check the code, but you may want to assign the return of iClose, iHigh, iLOw, and especially iOpen function to a variable. That way if those function fails and give return value of -1, you don't have to process it.

Forget one thing  if do get -1 return value, find out what error is that. 

kilian13
13
kilian13  
the function already returns -1 if it fails. It hasn't done it yet but it just gives me back wrong values. 
phi nuts
2184
phi nuts  
kilian13:
the function already returns -1 if it fails. It hasn't done it yet but it just gives me back wrong values. 
Since you said that this is a migrating from mql4 code, can you attach (not insert) the ml4 codes ?
Migrating from MQL4 to MQL5
Migrating from MQL4 to MQL5
  • 2010.05.17
  • Sergey Pavlov
  • www.mql5.com
This article is a quick guide to MQL4 language functions, it will help you to migrate your programs from MQL4 to MQL5. For each MQL4 function (except trading functions) the description and MQL5 implementation are presented, it allows you to reduce the conversion time significantly. For convenience, the MQL4 functions are divided into groups, similar to MQL4 Reference.
kilian13
13
kilian13  

It is not a migrated code from mql4. I am just migrating from mt5 ;). But I can write it pretty quickly. It draws a red bar when the candle open < the close and vice versa of multiple currency pairs.

 

//+------------------------------------------------------------------+
//|                                                        HEDGE.mq4 |
//|                                              		     |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "xxx"
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 110

#property indicator_level1 0
#property indicator_buffers 8

extern string Pair1 = "EURUSD";
//extern bool Mirror1 = false;
extern string Pair2 = "USDCHF";
extern bool Mirror2 = true;
extern string Pair3 = "EURJPY";
//extern bool Mirror3 = false;
extern string Pair4 = "GBPUSD";
//extern bool Mirror4 = false;

double Pair1Up[],Pair1Down[],Pair2Up[],Pair2Down[],Pair3Up[],Pair3Down[],Pair4Up[],Pair4Down[];



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   int arrowCode = 110;
   SetIndexBuffer(0,Pair1Up);
   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,3,Lime);
   SetIndexArrow(0,110);
   SetIndexLabel(0,Pair1);
   
   SetIndexBuffer(1,Pair1Down);
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,3,Green);
   SetIndexArrow(1,110);
   SetIndexLabel(1,Pair1);
   
   SetIndexBuffer(2,Pair2Up);
   SetIndexStyle(2,DRAW_ARROW,STYLE_SOLID,3,DarkOrange);
   SetIndexArrow(2,110);
   SetIndexLabel(2,Pair2);
   
   SetIndexBuffer(3,Pair2Down);
   SetIndexStyle(3,DRAW_ARROW,STYLE_SOLID,3,OrangeRed);
   SetIndexArrow(3,110);
   SetIndexLabel(3,Pair2);
   
   SetIndexBuffer(4,Pair3Up);
   SetIndexStyle(4,DRAW_ARROW,STYLE_SOLID,3,DodgerBlue);
   SetIndexArrow(4,110);
   SetIndexLabel(4,Pair3);
   
   SetIndexBuffer(5,Pair3Down);
   SetIndexStyle(5,DRAW_ARROW,STYLE_SOLID,3,Blue);
   SetIndexArrow(5,110);
   SetIndexLabel(5,Pair3);
   
   SetIndexBuffer(6,Pair4Up);
   SetIndexStyle(6,DRAW_ARROW,STYLE_SOLID,3,Gold);
   SetIndexArrow(6,110);
   SetIndexLabel(6,Pair4);
   
   SetIndexBuffer(7,Pair4Down);
   SetIndexStyle(7,DRAW_ARROW,STYLE_SOLID,3,SaddleBrown);
   SetIndexArrow(7,110);
   SetIndexLabel(7,Pair4);
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  int    counted_bars=IndicatorCounted(),i,shift,Swing;
//---- TODO: add your code here
   i=(Bars-counted_bars)-1;
//----
   for(shift=i; shift>=0;shift--)
     {
     
     if(iClose(Pair1,0,shift) > iOpen(Pair1,0,shift)){Pair1Up[shift] = 5;  Pair1Down[shift] = EMPTY_VALUE;}
      else{Pair1Up[shift] = EMPTY_VALUE;  Pair1Down[shift] = 5;}
     
     if(iClose(Pair2,0,shift) > iOpen(Pair2,0,shift))
         {
            if(Mirror2) {Pair2Up[shift] = EMPTY_VALUE;  Pair2Down[shift] = 38;}
            else        {Pair2Up[shift] = 38;  Pair2Down[shift] = EMPTY_VALUE;}
            
         }
      else
         {
            if(Mirror2) {Pair2Up[shift] = 38;  Pair2Down[shift] = EMPTY_VALUE;}
            else        {Pair2Up[shift] = EMPTY_VALUE;  Pair2Down[shift] = 38;}
         }

     if(iClose(Pair3,0,shift) > iOpen(Pair3,0,shift)){Pair3Up[shift] = 71;  Pair3Down[shift] = EMPTY_VALUE;}
      else{Pair3Up[shift] = EMPTY_VALUE;  Pair3Down[shift] = 71;}
      
     if(iClose(Pair4,0,shift) > iOpen(Pair4,0,shift)){Pair4Up[shift] = 105;  Pair4Down[shift] = EMPTY_VALUE;}
      else{Pair4Up[shift] = EMPTY_VALUE;  Pair4Down[shift] = 105;}
      
            
     }
   return(0);
  }
//+------------------------------------------------------------------+ 

 

To add comments, please log in or register