You missed the point. The post you are referring to was about HOW to get the signal from an EA, you obviously can't have it by just getting the last 2 candles values, you need the third.
It's NOT possible to have an early signal because to detect a divergence you need a peak/trough and for that you need an additional candle which CLOSE. So when candle 0 just open, that means candle 1 is close, and it allow to confirm the peak/trough at candle 2. If someone want to modify the indicator to use unconfirmed peak/trough to have early signal, one can do it, but I will not help to that because it's a bad idea as you will get a lot of bad signals.
What could be done is to DRAW the signal on candle 1 or even candle 0, but that will change nothing, and I decided to draw the signal on the candle which actually show the peak/trough.
I have changed the code and you are right. You get false moves before the actual divergence. Thanks for your time on this.
You missed the point. The post you are referring to was about HOW to get the signal from an EA, you obviously can't have it by just getting the last 2 candles values, you need the third.
It's NOT possible to have an early signal because to detect a divergence you need a peak/trough and for that you need an additional candle which CLOSE. So when candle 0 just open, that means candle 1 is close, and it allow to confirm the peak/trough at candle 2. If someone want to modify the indicator to use unconfirmed peak/trough to have early signal, one can do it, but I will not help to that because it's a bad idea as you will get a lot of bad signals.
What could be done is to DRAW the signal on candle 1 or even candle 0, but that will change nothing, and I decided to draw the signal on the candle which actually show the peak/trough.
you might missed a point as well.
In the case of renko charts the current bar is (mostly) already closed so you would need one candle less. Or am I missing something?
Hi Alain, very good indicator, it works well even without adjusting the settings. It is good that it has got the Alerts, but the one thing that is missing for me is the ability to send push notifications to my phone. The same message which appears on the pop-up alert to have it send to my phone. Are you able to add this function to it? That would be very kind of you.
The signal is given at closing of a candle, 1 candle in the past (2 if you count open candle and not 3). It's normal and it's how it have to work. Divergence is based on research of extremum (top/bottom). If you have a way to detect an extremum on the current candle, I suggest you to learn programming and provide us, for free, this great innovation.
Hi Alain. Very good indicator. It makes correct signals.
BTW in part "//--- Bearish divergence is found" it was supposed to be "divergenceStyle" instead of "STYLE_SOLID" .
Indicator totaly not redraw. For my purpose I decide to make a little modification to make the last bar of indicator redrawable as well as divergence signal.
It will show divergence signal on previous bar, redrawable signal for sure. It will be helpfull on fast timeframes to use signal jast before close a bar.
I made changes:
- removed all part "//--- indicator updated only on new candle except total redraw"
- "start=prev_calculated-2;" was changed to "start=prev_calculated-1;"
- In part "//--- main loop of calculations" "for(int shift=start; shift<rates_total-2; shift++)" was changed to "for(int shift=start; shift<rates_total-1; shift++)"
Divergence signal now appears on the previous bar, but it freezes at the moment of appearance and not follow the indicator line on this bar.
I changed this part and solved it. May be it could be done better and simpler but it works. Arrows moved ahead 1 bar. Lines without arrows are uncomfirmed signals.
//--- indicator updated only on new candle except total redraw// static datetime lastCandleTime=0;// if(lastCandleTime==time[rates_total-1])// return(rates_total);// else// lastCandleTime=time[rates_total-1];//--- first calculation or number of bars was changedint start;
if(prev_calculated<=0)
{
start=slowEMA;
ArrayInitialize(bullishDivergence,EMPTY_VALUE); // divergence buffers must be initializedArrayInitialize(bearishDivergence,EMPTY_VALUE);
}
else
{
start=prev_calculated-1;
bullishDivergence[rates_total-1]=EMPTY_VALUE;
bearishDivergence[rates_total-1]=EMPTY_VALUE;
}
//--- data (macd buffers) count to copy int toCopy=rates_total-prev_calculated+(prev_calculated<=0 ? 0 : 1);
//--- not all data may be calculatedint calculated=BarsCalculated(macdHandle);
if(calculated<rates_total)
{
Print("Not all data of macdHandle is calculated (",calculated,"bars ). Error",GetLastError());
return(0);
}
//--- get Main MACD bufferif(IsStopped()) return(0); //Checking for stop flagif(CopyBuffer(macdHandle,MAIN_LINE,0,toCopy,macdBuffer)<=0)
{
Print("Getting MACD Main is failed! Error : ",GetLastError());
return(0);
}
//--- get Signal MACD bufferif(IsStopped()) return(0); //Checking for stop flagif(CopyBuffer(macdHandle,SIGNAL_LINE,0,toCopy,signalBuffer)<=0)
{
Print("Getting MACD Signal is failed! Error : ",GetLastError());
return(0);
}
//--- main loop of calculationsfor(int shift=start; shift<rates_total; shift++)
{
int currentExtremum,lastExtremum;
bool isBullishDivergence,isBearishDivergence;
string divergenceMsg;
ENUM_LINE_STYLE divergenceStyle=0;
//--- Catch Bullish Divergence
isBullishDivergence=false;
if(macdBuffer[shift-1]<=macdBuffer[shift-2] &&
macdBuffer[shift-1]<macdBuffer[shift-3] &&
macdBuffer[shift-1]<macdBuffer[shift])
//--- if current macd main is a bottom (lower than 2 previous and 1 next)
{
currentExtremum=shift;
lastExtremum=GetIndicatorLastTrough(shift);
//--- if(macdBuffer[currentExtremum-1]>macdBuffer[lastExtremum] &&
low[currentExtremum-1]<low[lastExtremum])
{
isBullishDivergence=true;
divergenceMsg="Classical bullish divergence on: ";
divergenceStyle=STYLE_SOLID;
}
//--- if(macdBuffer[currentExtremum-1]<macdBuffer[lastExtremum] &&
low[currentExtremum-1]>low[lastExtremum])
{
isBullishDivergence=true;
divergenceMsg="Reverse bullish divergence on: ";
divergenceStyle=STYLE_DOT;
}
//--- Bullish divergence is foundif(isBullishDivergence)
{
bullishDivergence[currentExtremum]=macdBuffer[currentExtremum]-ARROWS_DISPLACEMENT;
//---if(drawPriceTrendLines==true)
DrawTrendLine(TRENDLINE_MAIN,time[currentExtremum-1],time[lastExtremum],low[currentExtremum-1],low[lastExtremum],Green,divergenceStyle);
//---if(drawIndicatorTrendLines==true)
DrawTrendLine(TRENDLINE_INDICATOR,time[currentExtremum-1],time[lastExtremum],macdBuffer[currentExtremum-1],macdBuffer[lastExtremum],Green,divergenceStyle);
//---if(displayAlert==true && shift>=rates_total-3 && time[currentExtremum-1]!=lastAlertTime)
DisplayAlert(divergenceMsg,time[currentExtremum-1]);
}
}
//--- Catch Bearish Divergence
isBearishDivergence=false;
if(macdBuffer[shift-1]>=macdBuffer[shift-2] &&
macdBuffer[shift-1]>macdBuffer[shift-3] &&
macdBuffer[shift-1]>macdBuffer[shift])
//--- if current macd main is a top (higher than 2 previous and 1 next)
{
currentExtremum=shift;
lastExtremum=GetIndicatorLastPeak(shift);
//--- if(macdBuffer[currentExtremum-1]<macdBuffer[lastExtremum] &&
high[currentExtremum-1]>high[lastExtremum])
{
isBearishDivergence=true;
divergenceMsg="Classical bearish divergence on: ";
divergenceStyle=STYLE_SOLID;
}
if(macdBuffer[currentExtremum-1]>macdBuffer[lastExtremum] &&
high[currentExtremum-1]<high[lastExtremum])
{
isBearishDivergence=true;
divergenceMsg="Reverse bearish divergence on: ";
divergenceStyle=STYLE_DOT;
}
//--- Bearish divergence is foundif(isBearishDivergence)
{
bearishDivergence[currentExtremum]=macdBuffer[currentExtremum]+ARROWS_DISPLACEMENT;
//---if(drawPriceTrendLines==true)
DrawTrendLine(TRENDLINE_MAIN,time[currentExtremum-1],time[lastExtremum],high[currentExtremum-1],high[lastExtremum],Red,divergenceStyle);
//---if(drawIndicatorTrendLines==true)
DrawTrendLine(TRENDLINE_INDICATOR,time[currentExtremum-1],time[lastExtremum],macdBuffer[currentExtremum-1],macdBuffer[lastExtremum],Red,divergenceStyle);
//---if(displayAlert==true && shift>=rates_total-3 && time[currentExtremum-1]!=lastAlertTime)
DisplayAlert(divergenceMsg,time[currentExtremum-1]);
}
}
}
//--- return value of prev_calculated for next callreturn(rates_total);
}
//+------------------------------------------------------------------+//| Search last trough |//+------------------------------------------------------------------+int GetIndicatorLastTrough(int shift)
{
for(int i=shift-6; i>=2; i--)
{
if(signalBuffer[i] <= signalBuffer[i-1] && signalBuffer[i] <= signalBuffer[i-2] &&
signalBuffer[i] <= signalBuffer[i+1] && signalBuffer[i] <= signalBuffer[i+2])
{
for(int j=i; j>=2; j--)
{
if(macdBuffer[j] <= macdBuffer[j-1] && macdBuffer[j] < macdBuffer[j-2] &&
macdBuffer[j] <= macdBuffer[j+1] && macdBuffer[j] < macdBuffer[j+2])
return(j);
}
}
}
return(0);
}
//+------------------------------------------------------------------+//| Search last peak |//+------------------------------------------------------------------+int GetIndicatorLastPeak(int shift)
{
for(int i=shift-6; i>=2; i--)
{
if(signalBuffer[i] >= signalBuffer[i-1] && signalBuffer[i] >= signalBuffer[i-2] &&
signalBuffer[i] >= signalBuffer[i+1] && signalBuffer[i] >= signalBuffer[i+2])
{
for(int j=i; j>=2; j--)
{
if(macdBuffer[j] >= macdBuffer[j-1] && macdBuffer[j] > macdBuffer[j-2] &&
macdBuffer[j] >= macdBuffer[j+1] && macdBuffer[j] > macdBuffer[j+2])
return(j);
}
}
}
return(0);
}
//+------------------------------------------------------------------+//| ENUM_TRENDLINE_TYPE used by DrawTrendLine |//+------------------------------------------------------------------+
when divergence zero line below also price formation HL bottom ( price if not made again LL) histogram any other 5 30 60 240 tf 3 tf signed to fruitful
You missed the point. The post you are referring to was about HOW to get the signal from an EA, you obviously can't have it by just getting the last 2 candles values, you need the third.
It's NOT possible to have an early signal because to detect a divergence you need a peak/trough and for that you need an additional candle which CLOSE. So when candle 0 just open, that means candle 1 is close, and it allow to confirm the peak/trough at candle 2. If someone want to modify the indicator to use unconfirmed peak/trough to have early signal, one can do it, but I will not help to that because it's a bad idea as you will get a lot of bad signals.
What could be done is to DRAW the signal on candle 1 or even candle 0, but that will change nothing, and I decided to draw the signal on the candle which actually show the peak/trough.
You missed the point. The post you are referring to was about HOW to get the signal from an EA, you obviously can't have it by just getting the last 2 candles values, you need the third.
It's NOT possible to have an early signal because to detect a divergence you need a peak/trough and for that you need an additional candle which CLOSE. So when candle 0 just open, that means candle 1 is close, and it allow to confirm the peak/trough at candle 2. If someone want to modify the indicator to use unconfirmed peak/trough to have early signal, one can do it, but I will not help to that because it's a bad idea as you will get a lot of bad signals.
What could be done is to DRAW the signal on candle 1 or even candle 0, but that will change nothing, and I decided to draw the signal on the candle which actually show the peak/trough.
you might missed a point as well.
In the case of renko charts the current bar is (mostly) already closed so you would need one candle less. Or am I missing something?
Forum on trading, automated trading systems and testing trading strategies
macd divegence
ENEZA PETER MKIRAMWENI, 2021.09.12 12:26
hi help me with this indicator it display alert after three bar i want to display alert immediate after 1 bar
Forum on trading, automated trading systems and testing trading strategies
Indicators: MACD Divergence
Alain Verleyen, 2013.11.05 19:14
So friendly.
The signal is given at closing of a candle, 1 candle in the past (2 if you count open candle and not 3). It's normal and it's how it have to work. Divergence is based on research of extremum (top/bottom). If you have a way to detect an extremum on the current candle, I suggest you to learn programming and provide us, for free, this great innovation.
Thank you for your input.
I changed this part and solved it. May be it could be done better and simpler but it works. Arrows moved ahead 1 bar. Lines without arrows are uncomfirmed signals.
when divergence zero line below also price formation HL bottom ( price if not made again LL) histogram any other 5 30 60 240 tf 3 tf signed to fruitful
Hello Alain,
Thank you very much for this great indicator!
I am trying to use the Data Window values of this indicator inside an EA. (Please see attachment)
I use the same logic for all four indexes but I am able to get the correct values only for the last two (Main and Signal).
For the first two I see always a fix number
when, for the example attached, one should have a correct value and the other should be 0/blank
do you know why I have this problem?
Thank you