CopyBuffer in MT4/MQL4

 

Hello!

I am new to MT4 and currently learning about Expert Advisors.

I thought I'd start with this Guide (https://www.mql5.com/en/articles/100) to developing my own EA.

I only noticed later that the Guide offers an example using MQL5. Unfortunately my broker offers only MT4 :-/.


By now I have fixed almost all errors and already made some adjustments to the example.

I have two error messages left because MQL4 doesn't know the "CopyBuffer" and "IndicatorRelease" functions used in the Guide above :-/.


Here the Extract of what I have now (derived from the Guide above).

int OnInit()
  {
//--- Get handle for ADX indicator
   adxHandle=iADX(NULL,0,ADX_Period,PRICE_CLOSE,MODE_MAIN,0);
//--- Get the handle for Moving Average indicator
   maHandle=iMA(NULL,0,MA_Period,0,MODE_EMA,PRICE_CLOSE,0);
//--- What if handle returns Invalid Handle
   if(adxHandle<0 || maHandle<0)
     {
      Alert("Error Creating Handles for indicators - error: ",GetLastError(),"!");
      return(-1);
     }
...
  }


void OnDeinit(const int reason)
  {
//--- Release our indicator handles
   IndicatorRelease(adxHandle);
   IndicatorRelease(maHandle);
  }

void OnTick()
  {   
...
// the ADX values arrays
   ArraySetAsSeries(adxVal,true);
// the MA-8 values arrays
   ArraySetAsSeries(maVal,true);

   if(CopyBuffer(adxHandle,0,0,3,adxVal)<0 || CopyBuffer(adxHandle,1,0,3,plsDI)<0 || CopyBuffer(adxHandle,2,0,3,minDI)<0)
     {
      Alert("Error copying ADX indicator Buffers - error:",GetLastError(),"!");
      ResetLastError();
      return;
     }
   if(CopyBuffer(maHandle,0,0,3,maVal)<0)
     {
      Alert("Error copying Moving Average indicator buffer - error:",GetLastError());
      ResetLastError();
      return;
     }
...
  }


Can you please help me and explain to me what I need to change?

Thanks in advance!!

Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
Step-By-Step Guide to writing an Expert Advisor in MQL5 for Beginners
  • www.mql5.com
This article is aimed at beginners who wish to learn how to write simple Expert Advisors in the new MQL5 language. We will begin first by defining what we want our EA (Expert advisor) to do, and then move on to how we want the EA to do it. 1. Trading Strategy It will monitor a particular indicator, and when a certain condition is met (or...
 

Hi,

So you are coding in MQL5 language that is used in MT5 terminal,

You should learn MQL4 language for MT4 terminal,In MQL4 there are not these two functions,

You could get data directly from indicator without copying buffer via :

double idx_di_plus_value=iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_PLUSDI,0);
if(idx_di_plus_value<0)
     {
      Alert("DI+ is negative");
      return;
     }

Please read this documentation for more info:https://docs.mql4.com/indicators

Regards!

Technical Indicators - MQL4 Reference
Technical Indicators - MQL4 Reference
  • docs.mql4.com
For an Expert Advisor (or any other MQL4 program) to take up the value of any indicator, it is not necessary that this indicator is present in the chart. The requested indicator will be loaded and calculated in the thread of the module that has called it. Any indicator can be calculated on the data of not only current chart, but also on the...
 
Mehrdad Jeddi:

Hi,

So you are coding in MQL5 language that is used in MT5 terminal,

You should learn MQL4 language for MT4 terminal,In MQL4 there are not these two functions,

You could get data directly from indicator without copying buffer via :

Please read this documentation for more info:https://docs.mql4.com/indicators

Regards!

Thanks for your reply.

I am trying to find a way to get around the two functions.


What this does is copying the last three values (0,1,2) of the 0-stream of adxHandle to adxVal:

CopyBuffer(adxHandle,0,0,3,adxVal)


I found the ArrayCopy to be something like an equivalent for MQL4 but cannot figure out how to run it:

ArrayCopy(adxVal,adxHandle,0,0,3)

This is missing the statement which stream to use (I think :-D).


Does anybody have a hint for this problem?

 

So for using the ArrayCopy function you should spend the input variable as array,adxHandle variable is not valid in MQL4,it's just for MQL5,

In MQL4 you would use as it:

double idx_di_plus_value_1=iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_PLUSDI,1);
double idx_di_plus_value_2=iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_PLUSDI,2);
double idx_di_plus_value_3=iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_PLUSDI,3);


Or if you want to copy ADX index value into array,do as it:

double ADX_DI_Plus_Value[5];

for(int i=0;i<5;i++)
{
  ADX_DI_Plus_Value[i]=iADX(_Symbol,PERIOD_CURRENT,14,PRICE_CLOSE,MODE_PLUSDI,i);
}

for(i=0;i<5;i++)
{
 Print("ADX DI+ Shift:"+i+"==> value:"+ADX_DI_Plus_Value[i]);
}
 

Hi ! I would like an MQL4 code where I can read the indicator buffers which are just attached to the chart. It is possible ?

 
Andre Camara De Mattos - #: I would like an MQL4 code where I can read the indicator buffers which are just attached to the chart. It is possible ?

There are no “indicator buffers” attached to any chart. There are only indicators attached to charts.

Perhaps you should read the manual. Technical Indicators - MQL4 Reference
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

Your code can read any indicator, whether attached or not, from any chart.