iMACD parameters on MQL5

 

Hello everyone, I have been reading a little about the subject in the reference guide but I just don't understand how to transcribe a simple thing from MQL4 to MQL5.

In MQL4 I use the following code to read the indicator:

   double Main = iMACD (Symbol (), MACD_Timeframe, FastEMA_Period, SlowEMA_Period, Signal_Period, PRICE_CLOSE, MODE_MAIN, OperationMode);
   double Signal = iMACD (Symbol (), MACD_Timeframe, FastEMA_Period, SlowEMA_Period, Signal_Period, PRICE_CLOSE, MODE_SIGNAL, OperationMode);

Here I indicate the MAIN / SIGNAL mode, and the calculation period in OperationMode (0 = LiveBar, 1 = NewBar).

My problem is that I don't know how to apply these two parameters in MQL5 code, that is, MODE MAIN / SIGNAL and calculation period (closed bar or current bar).

I would appreciate it if you shed some light on the subject.

 

In MQL5 the way in which obtain indicator's values is changed.

You need to create an indicator handle (in the OnInit) and assign it to a an integer variable.

MACD_Handle   = iMACD(NULL,0,iMACDFast,iMACDSlow,iMACDSignal,iMACDPrice);

Then you need to copy its buffers value using the copy buffer function.

CopyBuffer(ind_handle,BufferNumber,-shift,AmountOfValues,OutArray)

Keep in mind that you also need to handle errors, like MACD_Handle==INVALID_HANDLE or Copybuffer<0.

On codebase you will find a lot of good examples.

 
Fabio Cavalloni:

In MQL5 changes the way in which obtain indicator's values.

You need to create an indicator handle (in the OnInit) and assign it to a an integer variable.

Then you need to copy its buffers value using the copy buffer function.

Keep in mind that you also need to handle errors, like MACD_Handle==INVALID_HANDLE or Copybuffer<0.

On codebase you will find a lot of good examples.

Not very clearly for me. I'm writing a script, not using OnInit.


//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(){
   for(int s=0;s<SymbolsTotal(false);s++){
      string SName=SymbolName(s,false);
      double Handle=iMACD(SName,MACD_Timeframe,FastEMA_Period,SlowEMA_Period,Signal_Period,PRICE_CLOSE);
      if(Main>0&&Main>Signal){}
      if(Main<0&&Main<Signal){}
      }
   }
//+------------------------------------------------------------------+
 

Handle need to be assigned to an integer variable, as I already said.

I never tried to code indicators in script in MT5, but I think that the only way is creating handle in OnStart and copying buffer also there because a script only has OnStart function.

 
Fabio Cavalloni:

Handle need to be assigned to an integer variable, as I already said.

I never tried to code indicators in script in MT5, but I think that the only way is creating handle in OnStart and copying buffer also there because a script only has OnStart function.

Ok, we have:

int MACD_Handle=iMACD(SName,MACD_Timeframe,FastEMA_Period,SlowEMA_Period,Signal_Period,PRICE_CLOSE);

So how do I get:

double MACD_Main=?
doublé MACD_Signal=?

Which its shift values.

 
double MACD_Main[], MACD_Signal[];
ArraySetAsSeries(MACD_Main,true);
ArraySetAsSeries(MACD_Signal,true);

CopyBuffer(MACD_Handle,0,-shift,AmountOfValues,MACD_Main);
CopyBuffer(MACD_Handle,1,-shift,AmountOfValues,MACD_Signal);
 
Fabio Cavalloni:

Thank you but not sure I understand this and why so difficult.

      int Handle=iMACD(SName,MACD_Timeframe,FastEMA_Period,SlowEMA_Period,Signal_Period,PRICE_CLOSE);
      double Main[],Signal[];
      ArraySetAsSeries(Main,true);
      ArraySetAsSeries(Signal,true);
      CopyBuffer(Handle,0,-OperationMode,AmountOfValues,Main);
      CopyBuffer(Handle,1,-OperationMode,AmountOfValues,Signal);
      if(Main>0&&Main>Signal){...}
      if(Main<0&&Main<Signal){...}
 

Now you need to pass the array index.

Since Main[] and Signal[] are array's you have to specify the index;

  if(Main[0]>0&&Main[0]>Signal[0]){...}
  if(Main[0]<0&&Main[0]<Signal[0]){...}
 
Marco vd Heijden:

Now you need to pass the array index.

Since Main[] and Signal[] are array's you have to specify the index;

Error 4302: cannot load indicator 'MACD', it appears to be a symbol select error. I'm trying to loop through all symbols for alert.

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart(){
   double Main[],Signal[];
   ArraySetAsSeries(Main,true);
   ArraySetAsSeries(Signal,true);
   for(int s=0;s<SymbolsTotal(false);s++){
      string SName=SymbolName(s,false);
      int MACD=iMACD(SName,MACD_Timeframe,FastEMA_Period,SlowEMA_Period,Signal_Period,PRICE_CLOSE);
      CopyBuffer(MACD,0,0,100,Main);CopyBuffer(MACD,0,0,100,Signal);
      if(Main[OperationMode]>MACD_Amplitude&&Main[OperationMode]<Signal[OperationMode]){
         Alert(SName," is ready to swing down.");
         }
      if(Main[OperationMode]<-MACD_Amplitude&&Main[OperationMode]>Signal[OperationMode]){
         Alert(SName," is ready to swing up.");
         }
      }
   }
 
False also incorporates the instruments that are disabled so you use true in both SymbolsTotal(1); and in SymbolName(pos,1); 
 
Marco vd Heijden:
False also incorporates the instruments that are disabled so you use true in both SymbolsTotal(1); and in SymbolName(pos,1); 

I know but should even work, it only loads correctly the current active window symbol.

Reason: