rookie coder in peril: divergence and iCustom EA problem

 

Hi guys!

Once again I'm humbly asking for help- this problem should be relatively easy to solve for more experienced coders.


So... I've coded a divergence indicator- the "red line" is a difference between EMA for the current bar and EMA "6 bars ago", the green line just smoothed red line for the last 60 bars- time periods are irrelevant for this problem -here's the code:


#property copyright "TZ"
#property link ""
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Green
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,ExtMapBuffer2);
string short_name = "EMA div heading";
IndicatorShortName(short_name);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//---- check for possible errors
if (counted_bars<0) return(-1);
//---- last counted bar will be recounted
if (counted_bars>0) counted_bars--;
int pos=Bars-counted_bars;
//---- main calculation loop
while(pos>=0)
{
double x,y,z;
x= iMA(NULL,5, 30, 0, MODE_EMA, 0, pos)- iMA(NULL,5, 50, 0, MODE_EMA, 0, pos);
y= iMA(NULL,5, 30, 0, MODE_EMA, 0, pos+6)- iMA(NULL,5,50, 0, MODE_EMA, 0, pos+6);
z=(x-y);
ExtMapBuffer1[pos]=z;
ExtMapBuffer2[pos]=iMAOnArray(ExtMapBuffer1,0,60,0, MODE_EMA,pos);
pos--;
}
//----
return(0);
}
//+---------------------------------


Using iCustom I've made a condition in an EA:


IF current value for the "red line">A(exern var) AND "red line" is above "green line" THEN open a buy order

I've coded it like this (parts of the EA code):

.

.

//variable definition:

double DH= iCustom(NULL, 0, "EMA div",0,0);
double DHx= iCustom(NULL, 0, "EMA div",1,0);

.

.

//buy condition:

if(DH>A && DH>DHx)

{

OrderSend(...)

.

.


of course the external "A" variable is also defined


THE PROBLEM:

When sending order, in some cases, the EA seems to ignore the "red above green" condition. Also, when ploting the indicator, metadrader sometimes doesen't draw the green line.

I would be very grateful if someone could correct the code- maybe I'm doing a simple mistake and can't figure it out.

It either sth wrong with basic code in the indicator or iCustom ;)


Thanks a lot for your time, I appreciate any help!

Cheers,

Tom