molanis.com/forum - Tools for MetaTrader : The place to ask about the best expert advisor builder, expert advisor downloads, and expert advisor programming

AFTER YOU REGISTER SEND US AN EMAIL TO ACTIVATE YOUR ACCOUNT - Before posting:-1- Please read the user guide -2- Try the examples -3- Search in the forum
It is currently Fri May 17, 2024 4:45 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Importing custom indicator: a Divergence Indicator
PostPosted: Wed May 09, 2012 8:18 am 
Offline

Joined: Sun Mar 11, 2012 8:28 am
Posts: 11
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);
}
}


Top
 Profile  
 
 Post subject: Re: Importing custom indicator: a Divergence Indicator
PostPosted: Wed May 09, 2012 12:18 pm 
Offline

Joined: Tue Dec 22, 2009 12:22 am
Posts: 1761
I did not compile your indicator in mt4 but this code
SetIndexBuffer(0, bullishDivergence);
SetIndexBuffer(1, bearishDivergence);
seems to indicate that mode 0 is bullishDivergence and mode 1 is bearishDivergence


Top
 Profile  
 
 Post subject: Re: Importing custom indicator: a Divergence Indicator
PostPosted: Wed May 09, 2012 1:16 pm 
Offline

Joined: Sun Mar 11, 2012 8:28 am
Posts: 11
molanisfx wrote:
I did not compile your indicator in mt4 but this code
SetIndexBuffer(0, bullishDivergence);
SetIndexBuffer(1, bearishDivergence);
seems to indicate that mode 0 is bullishDivergence and mode 1 is bearishDivergence


Thank you!
How could I set a condition as "when bullishDivergence occurs do this"?

thank you


Top
 Profile  
 
 Post subject: Re: Importing custom indicator: a Divergence Indicator
PostPosted: Thu May 10, 2012 9:04 am 
Offline
Site Admin

Joined: Fri Oct 16, 2009 3:40 pm
Posts: 451
I think the indicator is delayed and probably it repaints. This means that the perfect signal that you see on the historical charts is painted one or two bars after the fact. First check how the signal is created live.
The other thing to find out is what is the value of the signal when there is no signal. This may sound odd - but each color is a signal. You need to understand what is the value of the indicator when there is a signal or when there are no signal.
The value when there is no signal could be zero or empty - it really depends on the preference of the person who wrote the indicator. Some use 0, others use empty. For you it means that if no signal is zero, your condition will be if indicator > 0 then do.. if no signal is empty, the signal will be if indicator < 10000 then do
Also, the shift selection is important here, you need to define but is better for you since the signal may be repainted, an it's delayed.. maybe you can get it with shift 2 or even shift 1.. you need to see it live (and not in the historical chart) and decide
To print out the signals value add this code:

change:
Code:
int start()
{
int countedBars = IndicatorCounted();
if(countedBars < 0)
countedBars = 0;
CalculateIndicator(countedBars);
//----
return(0);
}


to

Code:
int start()
{
int countedBars = IndicatorCounted();
if(countedBars < 0)
countedBars = 0;
CalculateIndicator(countedBars);
Print("Signal shift 0=", bullishDivergence[0]);
Print("Signal shift 1=", bullishDivergence[1]);
Print("Signal shift 2=", bullishDivergence[2]);
//----
return(0);
}


Top
 Profile  
 
 Post subject: Re: Importing custom indicator: a Divergence Indicator
PostPosted: Thu May 10, 2012 9:57 am 
Offline

Joined: Sun Mar 11, 2012 8:28 am
Posts: 11
Thank you very much for your reply.

I understand now. The only thing is that I've modified the code as suggested, it has compiled ok, but when a signal shows on a chart, it doesn't print anything,

Am I doing something wrong?

thank you again


Top
 Profile  
 
 Post subject: Re: Importing custom indicator: a Divergence Indicator
PostPosted: Thu May 10, 2012 10:38 am 
Offline

Joined: Tue Dec 22, 2009 12:22 am
Posts: 1761
It prints out in the logs tab - In mt4 go to the expert tab. You will see tge output there.
Try to create a strategy file .mol file after you identify the signal and post it here if you need help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by Molanis © 2009