Download MetaTrader 5

CopyBuffer MQL5 in MQL4

To add comments, please log in or register
Publish your article to let the traders see it in MetaTrader 5!
Gerardo Bustos
746
Gerardo Bustos 2015.02.22 03:47 

Hello Forum, good day.

 

I started out learning MQL5 a couple months ago and now need to translate an EA to MQL4 because the broker I will work with hasn't make the upgrade/migration to MQL5 and MT5 yet :(. Reading the docs, so far I've encountered several differences mainly on the way that orders are placed, and in this particular case, how to get/copy the data from a specified buffer of a certain indicator to a specific array (CopyBuffer). What I need is to copy the data from several indicators to arrays (Plus_DI[], MinusDI[]) and compare them (current +DI with -DI).


Here is what I need to change from MQL5 to MQL4: 

 

int OnInit() {
    ADX_Handle = iADX(NULL, 0, ADX_Period);
    ...
}

void OnTick() {
    ...

    CopyBuffer(ADX_Handle, 1, 1, 1, Plus_DI);

    CopyBuffer(ADX_Handle, 2, 1, 1, Minus_DI);

 

    if ( Plus_DI[0] > Minus_DI[0] ) {

        ... place order 

    } else {

        ... place order	

    }
    ...
}


I've looked at the MQL4 docs and found the next example: 

iADX(NULL,0,14,PRICE_HIGH,MODE_PLUSDI,0)

I don't know if this is the way to do it, but in this case I don't need the price because I'm already using iOpen() / iHigh() / iLow() / iClose() type of functions to get the price of the candles and compare them.

 

I would really appreciate the help of a more experienced programmer in MQL4. 

 

Regards and thank you in advance,

codeMolecules. 

Carl Schreiber
6588
Carl Schreiber 2015.02.22 09:22  

1) Have you read the MQL4 Docs? What does iADX(..) return? A handle?

2) Do you need in an EA the all the values of an indicator?

That's why an indicator has OnCalulate(const int rates_total, ..., const int& spread[]) while an EA has OnTick().

Keith Watford
Moderator
9620
Keith Watford 2015.02.22 11:27  
codeMolecules:

I've looked at the MQL4 docs and found the next example: 

iADX(NULL,0,14,PRICE_HIGH,MODE_PLUSDI,0)

 

I don't know if this is the way to do it, but in this case I don't need the price because I'm already using iOpen() / iHigh() / iLow() / iClose() type of functions to get the price of the candles and compare them.

I can appreciate your confusion. I am used to accessing indicator values in Mt4. I have not done a lot of work with MT5 and the way to retrieve indicator values seems totally alien to me. I can see that it would be the same for you having learnt to code in mq5 and trying to convert to mq4.

The PRICE_HIGH is the applied price for the indicator and is necessary otherwise you are not instructing the EA/Indicator to retrieve value for the indicator calculated on open, close high, low, median etc. 

Alain Verleyen
Moderator
30722
Alain Verleyen 2015.02.22 14:36  

With mql5 you proceed in 2 steps, first you get an handle to the indicator, then you copy the needed value(s) with CopyBuffer().

With mql4 you have only 1 step, but you can only get 1 value at a time. Also as stated by GumRai, there is 1 more parameter with iADX mql4 version.

int OnInit() 
  {
   ADX_Handle=iADX(NULL,0,ADX_Period);
   ...
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick() 
  {
   ...

   CopyBuffer(ADX_Handle,1,1,1,Plus_DI);
   double Plus_DI=iADX(NULL,0,ADX_Period,PRICE_CLOSE,1,1);

   CopyBuffer(ADX_Handle,2,1,1,Minus_DI);
   double Minus_DI=iADX(NULL,0,ADX_Period,PRICE_CLOSE,2,1);


   if(Plus_DI[0]>Minus_DI[0]) 
     {

      ... place order

        } else {

      ... place order

     }
   ...
  }
To add comments, please log in or register