Hi everyone,
I got used to call macd indicator in MT4 in a very simple way and now I've got to switch in MT5 and things look deeply differents
So experts, I kindly request your help :
in MT4 calling macd and signal was quite easy for current or past period just by setting the right buffer and the right mode
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
And in MT5 I dont really figure out how it works,
I've checked the MACD_SAMPLE EA,
I dont see how it manages the signal MACD and the main MACD and more, how it manages the passed data for previous macd for instance.
this is a coding block from MACD_SAMPLE
if(BarsCalculated(m_handle_macd)<2 || BarsCalculated(m_handle_ema)<2)
return(false);
if(CopyBuffer(m_handle_macd,0,0,2,m_buff_MACD_main) !=2 ||
CopyBuffer(m_handle_macd,1,0,2,m_buff_MACD_signal)!=2 ||
CopyBuffer(m_handle_ema,0,0,2,m_buff_EMA) !=2)
return(false);
// m_indicators.Refresh();
//--- to simplify the coding and speed up access
//--- data are put into internal variables
m_macd_current =m_buff_MACD_main[0];
m_macd_previous =m_buff_MACD_main[1];
m_signal_current =m_buff_MACD_signal[0];
m_signal_previous=m_buff_MACD_signal[1];
it looks like the data are stored in an array but I don't figure out the way it's done, and how with this, the system makes difference between macd and signal,
can one of you light up my mind please : )
Please forgive my English : -))
This is how approach it.
I include a cool bit of code from the article https://www.mql5.com/en/articles/31
#include <GetIndicatorBuffers.mqh>
First you have to declare a couple arrays and a handle for the indicator
//---- arrays for indicators
double MACDMain[]; // array for MAIN_LINE of iMACD
double MACDSignal[]; // array for SIGNAL_LINE of iMACD
int iMACD_handle; // handle of the indicator iMACD
Then the handle is defined in the OnInit() function
int OnInit() { //--- creation of the indicator iMACD iMACD_handle=iMACD(Symbol(),Time_Frame,fast_ema_period, slow_ema_period,signal_period,PRICE_CLOSE); //--- report if there was an error in object creation if(iMACD_handle<0) { Print("The creation of iMACD has failed: Runtime error =",GetLastError()); //--- forced program termination return(-1); } }
Then inside your program you call for the buffers
if(!GetMACDBuffers(iMACD_handle,0,10,MACDMain,MACDSignal,true)) return;
So then you can assign the values
x=MACDMain[0]; y=MACDSignal[0];
Follow the article above and you should have no problem

- 2010.03.18
- Sergey Pavlov
- www.mql5.com
Hi John,
Thanks a lot for for your very clear explaination and the link you've added, I'll try it and report my results in here.
Have a good day,
balmala
Hi John,
I tried this simple EA to see and I got unexpected results.
What I expected was to have at each tick:
a new MACD and Signal calculation (current calculation) that are coming from index 0 ([0])of the ad hoc arrays
And to get these two unchanged values at the next stick from index 1 ([1]) this time as previous MACD and Signal.
that's not what I'm getting from the code Sample below,
#property copyright "Copyright 2017, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #include <Indicators\GetIndicatorBuffers.mqh> //---- arrays for indicators double MACDMain[]; // array for MAIN_LINE of iMACD double MACDSignal[]; // array for SIGNAL_LINE of iMACD // INDICATOR'S HANDLES int iMACD_handle; // handle of the indicator iMACD //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- //--- creation of the indicator iMACD iMACD_handle=iMACD(NULL,0,12,26,9,PRICE_CLOSE); //--- report if there was an error in object creation if(iMACD_handle<0) { Print("The creation of iMACD has failed: Runtime error =",GetLastError()); //--- forced program termination return(-1); } return(0); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- if(!GetMACDBuffers(iMACD_handle,0,10,MACDMain,MACDSignal,true)) return; double macd_c = MACDMain[0] ; // Current MACD double macd_p = MACDMain[1]; // Precious MACD double sign_c = MACDSignal[0]; // Current Signal double sign_p = MACDSignal[1]; // Previous Signal Alert("Current MACD : ",macd_c); Alert("Precious MACD : ",macd_p); Alert("Current Signal : ",sign_c); Alert("Previous Signal : ",sign_p); return; } //+------------------------------------------------------------------+
the MACDs and Signals have very strange values, and very different from the symbols values
Also, if the current calculations are performed and change at each stick
MACDMain[0] and MACDSignal[0]
, they are never stored as previous values at the next stick and they never change
MACDMain[1] and MACDSignal[1]
Can you (or someone passing by : )) take a look and help me?
Have a nice day.
Balmala
GetIndicatorBuffers -? What is it?
Use this code:
//+------------------------------------------------------------------+ //| Get value of buffers for the iMACD | //| the buffer numbers are the following: | //| 0 - MAIN_LINE, 1 - SIGNAL_LINE | //+------------------------------------------------------------------+ double iMACDGet(int handle_iMACD,const int buffer,const int index) { double MACD[1]; //--- reset error code ResetLastError(); //--- fill a part of the iMACDBuffer array with values from the indicator buffer that has 0 index if(CopyBuffer(handle_iMACD,buffer,index,1,MACD)<0) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the iMACD indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(0.0); } return(MACD[0]); }
GetIndicatorBuffers -? What is it?
Use this code:
Hi Vladimir,
thank you for your answer.
I tried your code sample, I'm still getting unexpected results
double iMACDGet(int handle_iMACD,const int buffer,const int index) { double MACD[1]; //--- reset error code ResetLastError(); //--- fill a part of the iMACDBuffer array with values from the indicator buffer that has 0 index if(CopyBuffer(handle_iMACD,buffer,index,1,MACD)<0) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the iMACD indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(0.0); } return(MACD[0]); } int OnInit() { //--- //--- creation of the indicator iMACD iMACD_handle=iMACD(NULL,0,12,26,9,PRICE_CLOSE); //--- report if there was an error in object creation if(iMACD_handle<0) { Print("The creation of iMACD has failed: Runtime error =",GetLastError()); //--- forced program termination return(-1); } return(0); //--- return(INIT_SUCCEEDED); } void OnTick() { //--- double macd_c = iMACDGet(iMACD_handle,0,0) ; // Current MACD double macd_p = iMACDGet(iMACD_handle,0,1); // Precious MACD double sign_c = iMACDGet(iMACD_handle,1,0); // Current Signal double sign_p = iMACDGet(iMACD_handle,1,1); // Previous Signal Alert("Current MACD : ",macd_c); Alert("Precious MACD : ",macd_p); Alert("Current Signal : ",sign_c); Alert("Previous Signal : ",sign_p); return; }
the current macd and signal look calculated at each tick, but never stored as previous the stick after, and beside that, I get values very close to zero that is not making sense for an average.
Can you take a look and tell me if something is wrong.
Buy the way, I don't understand how by a single imacd call the system can return a signal value and a macd value since it returns one value...
Can one explain what is behind ?
Thanks a lot
...
... but never stored as previous the stick after ...
Evidence is there? Symbol? Period? Histori datetime? Mode of generation of ticks?
Hi Vladimir,
Symbol : GBPCHF,
timeframe 5mn
I did not use the stragey tester but put my ea straight to the symbol chart, the ticks were from the system,
you can find attached the log file.
you will see that none of current macd nor signal is saved as previous later
Thank you for your help : )
Just a bunch of numbers. These figures are about anything not say :).
Show full code MQL5-program.
Here you go:
index "0"
index "1"
//+------------------------------------------------------------------+ //| iMACDGet.mq5 | //| Copyright 2017, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2017, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" //--- int handle_iMACD; // variable for storing the handle of the iMACD indicator //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- create handle of the indicator iMACD handle_iMACD=iMACD(Symbol(),Period(),12,26,9,PRICE_CLOSE); //--- if the handle is not created if(handle_iMACD==INVALID_HANDLE) { //--- tell about the failure and output the error code PrintFormat("Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d", Symbol(), EnumToString(Period()), GetLastError()); //--- the indicator is stopped early return(INIT_FAILED); } //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { //--- } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ void OnTick() { //--- 0 - MAIN_LINE, 1 - SIGNAL_LINE double main_0 = iMACDGet(MAIN_LINE,0); double signal_0 = iMACDGet(SIGNAL_LINE,0); double main_1 = iMACDGet(MAIN_LINE,1); double signal_1 = iMACDGet(SIGNAL_LINE,1); string text=" | "+"index 0 | "+"index 1 "+"\n" "main | "+DoubleToString(main_0,Digits()+1)+" | "+DoubleToString(main_1,Digits()+1)+"\n"+ "signal | "+DoubleToString(signal_0,Digits()+1)+" | "+DoubleToString(signal_1,Digits()+1); Comment(text); } //+------------------------------------------------------------------+ //| Get value of buffers for the iMACD | //| the buffer numbers are the following: | //| 0 - MAIN_LINE, 1 - SIGNAL_LINE | //+------------------------------------------------------------------+ double iMACDGet(const int buffer,const int index) { double MACD[1]; //--- reset error code ResetLastError(); //--- fill a part of the iMACDBuffer array with values from the indicator buffer that has 0 index if(CopyBuffer(handle_iMACD,buffer,index,1,MACD)<0) { //--- if the copying fails, tell the error code PrintFormat("Failed to copy data from the iMACD indicator, error code %d",GetLastError()); //--- quit with zero result - it means that the indicator is considered as not calculated return(0.0); } return(MACD[0]); } //+------------------------------------------------------------------+

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I got used to call macd indicator in MT4 in a very simple way and now I've got to switch in MT5 and things look deeply differents
So experts, I kindly request your help :
in MT4 calling macd and signal was quite easy for current or past period just by setting the right buffer and the right mode
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
And in MT5 I dont really figure out how it works,
I've checked the MACD_SAMPLE EA,
I dont see how it manages the signal MACD and the main MACD and more, how it manages the passed data for previous macd for instance.
this is a coding block from MACD_SAMPLE
if(BarsCalculated(m_handle_macd)<2 || BarsCalculated(m_handle_ema)<2)
return(false);
if(CopyBuffer(m_handle_macd,0,0,2,m_buff_MACD_main) !=2 ||
CopyBuffer(m_handle_macd,1,0,2,m_buff_MACD_signal)!=2 ||
CopyBuffer(m_handle_ema,0,0,2,m_buff_EMA) !=2)
return(false);
// m_indicators.Refresh();
//--- to simplify the coding and speed up access
//--- data are put into internal variables
m_macd_current =m_buff_MACD_main[0];
m_macd_previous =m_buff_MACD_main[1];
m_signal_current =m_buff_MACD_signal[0];
m_signal_previous=m_buff_MACD_signal[1];
it looks like the data are stored in an array but I don't figure out the way it's done, and how with this, the system makes difference between macd and signal,
can one of you light up my mind please : )
Please forgive my English : -))