Trying to translate from an indicator to an EA... Please help

 

This is a challenge for me as I am a newbee but I am sure most anyone with a little experience could re rewrite this indicator into the EA.... please help!!! This is an RSI divergence... I'd like to make entries where the arrows appear..

#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 int signalSMA = 9;
extern bool drawIndicatorTrendLines = true;
extern bool drawPriceTrendLines = true;
extern bool displayAlert = true;
//---- buffers
double bullishDivergence[];
double bearishDivergence[];
double RSI[];
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, RSI);
SetIndexBuffer(3, signal);
//----
SetIndexArrow(0, 233);
SetIndexArrow(1, 234);
//----
indicatorName = "RSI_Divergence";
SetIndexDrawBegin(3, signalSMA);
IndicatorDigits(Digits + 2);
IndicatorShortName(indicatorName);

SetLevelValue(0,50);
SetLevelStyle(STYLE_DOT,1,DimGray);

return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
for(int i = ObjectsTotal() - 1; i >= 0; i--)
{
string label = ObjectName(i);
if(StringSubstr(label, 0, 18) != "RSI_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--)
{
CalculateRSI(i);
CatchBullishDivergence(i + 2);
CatchBearishDivergence(i + 2);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CalculateRSI(int i)
{
RSI[i] = iRSI(Symbol(),0,6,PRICE_CLOSE,i);

signal[i] = iRSI(Symbol(),0,6,PRICE_CLOSE,i);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void CatchBullishDivergence(int shift)
{
if(IsIndicatorTrough(shift) == false)
return;
int currentTrough = shift;
int lastTrough = GetIndicatorLastTrough(shift);
//----
if(RSI[currentTrough] > RSI[lastTrough] &&
Low[currentTrough] < Low[lastTrough])
{
bullishDivergence[currentTrough] = RSI[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],
RSI[currentTrough],
RSI[lastTrough],
Green, STYLE_SOLID);
//----
if(displayAlert == true)
DisplayAlert("Classical bullish divergence on: ",
currentTrough);
}
//----
if(RSI[currentTrough] < RSI[lastTrough] &&
Low[currentTrough] > Low[lastTrough])
{
bullishDivergence[currentTrough] = RSI[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],
RSI[currentTrough],
RSI[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(RSI[currentPeak] < RSI[lastPeak] &&
High[currentPeak] > High[lastPeak])
{
bearishDivergence[currentPeak] = RSI[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],
RSI[currentPeak],
RSI[lastPeak], Red, STYLE_SOLID);

if(displayAlert == true)
DisplayAlert("Classical bearish divergence on: ",
currentPeak);
}
if(RSI[currentPeak] > RSI[lastPeak] &&
High[currentPeak] < High[lastPeak])
{
bearishDivergence[currentPeak] = RSI[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],
RSI[currentPeak],
RSI[lastPeak], Red, STYLE_DOT);
//----
if(displayAlert == true)
DisplayAlert("Reverse bearish divergence on: ",
currentPeak);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsIndicatorPeak(int shift)
{
if(RSI[shift] >= RSI[shift+1] && RSI[shift] > RSI[shift+2] &&
RSI[shift] > RSI[shift-1])
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool IsIndicatorTrough(int shift)
{
if(RSI[shift] <= RSI[shift+1] && RSI[shift] < RSI[shift+2] &&
RSI[shift] < RSI[shift-1])
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetIndicatorLastPeak(int shift)
{
for(int i = shift + 5; i < Bars; i++)
{
if(signal[i] >= signal[i+1] && signal[i] >= signal[i+2] &&
signal[i] >= signal[i-1] && signal[i] >= signal[i-2])
{
for(int j = i; j < Bars; j++)
{
if(RSI[j] >= RSI[j+1] && RSI[j] > RSI[j+2] &&
RSI[j] >= RSI[j-1] && RSI[j] > RSI[j-2])
return(j);
}
}
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int GetIndicatorLastTrough(int shift)
{
for(int i = shift + 5; i < Bars; i++)
{
if(signal[i] <= signal[i+1] && signal[i] <= signal[i+2] &&
signal[i] <= signal[i-1] && signal[i] <= signal[i-2])
{
for (int j = i; j < Bars; j++)
{
if(RSI[j] <= RSI[j+1] && RSI[j] < RSI[j+2] &&
RSI[j] <= RSI[j-1] && RSI[j] < RSI[j-2])
return(j);
}
}
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DisplayAlert(string message, int shift)
{
if(shift <= 2 && Time[shift] != lastAlertTime)
{
lastAlertTime = Time[shift];
Alert(message, Symbol(), ", ", Period(), " minutes chart");
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DrawPriceTrendLine(datetime x1, datetime x2, double y1,
double y2, color lineColor, double style)
{
string label = "RSI_DivergenceLine" + DoubleToStr(x1, 0);
ObjectDelete(label);
ObjectCreate(label, OBJ_TREND, 0, x1, y1, x2, y2, 0, 0);
ObjectSet(label, OBJPROP_RAY, 0);
ObjectSet(label, OBJPROP_COLOR, lineColor);
ObjectSet(label, OBJPROP_STYLE, style);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void DrawIndicatorTrendLine(datetime x1, datetime x2, double y1,
double y2, color lineColor, double style)
{
int indicatorWindow = WindowFind(indicatorName);
if(indicatorWindow < 0)
return;
string label = "RSI_DivergenceLine_" + DoubleToStr(x1, 0);
ObjectDelete(label);
ObjectCreate(label, OBJ_TREND, indicatorWindow, x1, y1, x2, y2,
0, 0);
ObjectSet(label, OBJPROP_RAY, 0);
ObjectSet(label, OBJPROP_COLOR, lineColor);
ObjectSet(label, OBJPROP_STYLE, style);
}
//+------------------------------------------------------------------+



 

Normally, the EA would use iCustom to query the Indicator for its values...

 
phy:

Normally, the EA would use iCustom to query the Indicator for its values...

Phy, you're back!!! Nice to see you posting again :))
 

Use SRC or attach large files.

 

Phy, can you please give an example?


phy:

Normally, the EA would use iCustom to query the Indicator for its values...

 
adavinci:

Phy, can you please give an example?



iCustom() allows you to pull the results of any indicator into an ea so you don't have to convert the indicator code. Simply create an EA and assign a variable using icustom to get the indicator result...

https://docs.mql4.com/indicators/iCustom

hope that helps

V

 

Viffer,

Thank you but I am afraid I am not that advanced yet to that... will you please simplify it for me with an example? (i.e. an existing EA that does that would be helpful)


Viffer:

iCustom() allows you to pull the results of any indicator into an ea so you don't have to convert the indicator code. Simply create an EA and assign a variable using icustom to get the indicator result...

https://docs.mql4.com/indicators/iCustom

hope that helps

V


 
adavinci:

Viffer,

Thank you but I am afraid I am not that advanced yet to that... will you please simplify it for me with an example? (i.e. an existing EA that does that would be helpful)



Have a read of this and hopefully that will explain it...

https://book.mql4.com/samples/shared

V

Reason: