Hello evryone,
I would like to ask your help with the following indicator (freely available). My aim is to "catch" the bullish and the bearish divergences, drawn by the indicator on the chart.
Could you please suggest which is the "mode" to use while importing and how to catch the divergence as soon as it has been drawn on a chart? Bullish divergence is drawn as a green line, while bearish as a red line.
Ty in advance for your help!
Here is the main portion of the EA:
#property indicator_separate_window #property indicator_buffers 4 #property indicator_color1 Green #property indicator_color2 Red #property indicator_color3 Magenta #property indicator_color4 Blue //---- #define arrowsDisplacement 0.0001 //---- input parameters extern string separator1 = "*** MACD Settings ***"; extern int fastEMA = 12; extern int slowEMA = 26; extern int signalSMA = 9; extern string separator2 = "*** Indicator Settings ***"; extern bool drawIndicatorTrendLines = true; extern bool drawPriceTrendLines = true; extern bool displayAlert = true; //---- buffers double bullishDivergence[]; double bearishDivergence[]; double macd[]; double signal[]; //---- static datetime lastAlertTime; static string indicatorName; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0, DRAW_ARROW); SetIndexStyle(1, DRAW_ARROW); SetIndexStyle(2, DRAW_LINE); SetIndexStyle(3, DRAW_LINE); //---- SetIndexBuffer(0, bullishDivergence); SetIndexBuffer(1, bearishDivergence); SetIndexBuffer(2, macd); SetIndexBuffer(3, signal); //---- SetIndexArrow(0, 233); SetIndexArrow(1, 234); //---- indicatorName = "FX5_MACD_Divergence_v1.1(" + fastEMA + ", " + slowEMA + ", " + signalSMA + ")"; SetIndexDrawBegin(3, signalSMA); IndicatorDigits(Digits + 2); IndicatorShortName(indicatorName);
return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { for(int i = ObjectsTotal() - 1; i >= 0; i--) { string label = ObjectName(i); if(StringSubstr(label, 0, 19) != "MACD_DivergenceLine") continue; ObjectDelete(label); } return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int countedBars = IndicatorCounted(); if(countedBars < 0) countedBars = 0; CalculateIndicator(countedBars); //---- return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CalculateIndicator(int countedBars) { for(int i = Bars - countedBars; i >= 0; i--) { CalculateMACD(i); CatchBullishDivergence(i + 2); CatchBearishDivergence(i + 2); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CalculateMACD(int i) { macd[i] = iMACD(NULL, 0, fastEMA, slowEMA, signalSMA, PRICE_CLOSE, MODE_MAIN, i); signal[i] = iMACD(NULL, 0, fastEMA, slowEMA, signalSMA, PRICE_CLOSE, MODE_SIGNAL, i); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CatchBullishDivergence(int shift) { if(IsIndicatorTrough(shift) == false) return; int currentTrough = shift; int lastTrough = GetIndicatorLastTrough(shift); //---- if(macd[currentTrough] > macd[lastTrough] && Low[currentTrough] < Low[lastTrough]) { bullishDivergence[currentTrough] = macd[currentTrough] - arrowsDisplacement; //---- if(drawPriceTrendLines == true) DrawPriceTrendLine(Time[currentTrough], Time[lastTrough], Low[currentTrough], Low[lastTrough], Green, STYLE_SOLID); //---- if(drawIndicatorTrendLines == true) DrawIndicatorTrendLine(Time[currentTrough], Time[lastTrough], macd[currentTrough], macd[lastTrough], Green, STYLE_SOLID); //---- if(displayAlert == true) DisplayAlert("Classical bullish divergence on: ", currentTrough); } //---- if(macd[currentTrough] < macd[lastTrough] && Low[currentTrough] > Low[lastTrough]) { bullishDivergence[currentTrough] = macd[currentTrough] - arrowsDisplacement; //---- if(drawPriceTrendLines == true) DrawPriceTrendLine(Time[currentTrough], Time[lastTrough], Low[currentTrough], Low[lastTrough], Green, STYLE_DOT); //---- if(drawIndicatorTrendLines == true) DrawIndicatorTrendLine(Time[currentTrough], Time[lastTrough], macd[currentTrough], macd[lastTrough], Green, STYLE_DOT); //---- if(displayAlert == true) DisplayAlert("Reverse bullish divergence on: ", currentTrough); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void CatchBearishDivergence(int shift) { if(IsIndicatorPeak(shift) == false) return; int currentPeak = shift; int lastPeak = GetIndicatorLastPeak(shift); //---- if(macd[currentPeak] < macd[lastPeak] && High[currentPeak] > High[lastPeak]) { bearishDivergence[currentPeak] = macd[currentPeak] + arrowsDisplacement; if(drawPriceTrendLines == true) DrawPriceTrendLine(Time[currentPeak], Time[lastPeak], High[currentPeak], High[lastPeak], Red, STYLE_SOLID); if(drawIndicatorTrendLines == true) DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak], macd[currentPeak], macd[lastPeak], Red, STYLE_SOLID);
if(displayAlert == true) DisplayAlert("Classical bearish divergence on: ", currentPeak); } if(macd[currentPeak] > macd[lastPeak] && High[currentPeak] < High[lastPeak]) { bearishDivergence[currentPeak] = macd[currentPeak] + arrowsDisplacement; //---- if(drawPriceTrendLines == true) DrawPriceTrendLine(Time[currentPeak], Time[lastPeak], High[currentPeak], High[lastPeak], Red, STYLE_DOT); //---- if(drawIndicatorTrendLines == true) DrawIndicatorTrendLine(Time[currentPeak], Time[lastPeak], macd[currentPeak], macd[lastPeak], Red, STYLE_DOT); //---- if(displayAlert == true) DisplayAlert("Reverse bearish divergence on: ", currentPeak); } }
|