incorrect custom indicator values in EA

 

Hi all.

I'm new to mql4 but I do my best to learn coding. I have created my first EA using custom indicator which I post below.

Indicator

//+------------------------------------------------------------------+

//| DT-ZigZag-ATR.mq4 |

//+------------------------------------------------------------------+



#property indicator_chart_window

#property indicator_buffers 2

#property indicator_color1 Red

#property indicator_color2 White

//---- parameters

extern int depth=12;

extern int perATR=12;

extern double k=2;

//---- buffers

double ExtMapBuffer1[];

double ExtMapBuffer2[];

//+------------------------------------------------------------------+

//| Custom indicator initialization function |

//+------------------------------------------------------------------+

int init()

{

//---- indicators

SetIndexStyle(0,DRAW_ARROW);

SetIndexBuffer(0,ExtMapBuffer1);

SetIndexArrow(0,159);

SetIndexStyle(1,DRAW_ARROW);

SetIndexBuffer(1,ExtMapBuffer2);

SetIndexArrow(1,159);

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custor indicator deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

//----

return(0);

}

//+------------------------------------------------------------------+

//| Custom indicator iteration function |

//+------------------------------------------------------------------+

int start()


{

int limit, bigshift;

int counted_bars=IndicatorCounted();

double zigzag1,atr1;

//----

// if (counted_bars<0) return(-1);

if (counted_bars>0) counted_bars--;

limit=Bars-counted_bars+100;

for (int i=limit; i>0; i--)

{

for (int cnt=i; cnt<(100+i); cnt++)

{

zigzag1=iCustom(NULL,0,"ZigZag",depth,5,3,0,cnt+1);

if ( zigzag1!=0 ) break;

}

atr1=iATR(NULL,0,perATR,i);

if ( iHigh(NULL,0,i+1)<=zigzag1 ) ExtMapBuffer2[i]=iLow(NULL,0,i)+k*atr1; else ExtMapBuffer2[i]=0.0;

if ( iLow(NULL,0,i+1)>=zigzag1 ) ExtMapBuffer1[i]=iHigh(NULL,0,i)-k*atr1; else ExtMapBuffer1[i]=0.0;

}

Comment ("zigzag1 = ",zigzag1," limit = ",limit );

//----

return(0);

}

//+------------------------------------------------------------------+

In my EA I refer to this indicator by following commands

double zigzagATRshort1 = iCustom(NULL,0,"DT-ZigZag-ATR",1,1);

double zigzagATRlong1 = iCustom(NULL,0,"DT-ZigZag-ATR",0,1); //index nr popawny 0 (drugi w kolejności)

Please note that EA works on full bars

Program should open long position if zigzagATRshort1 < zigzagATRlong1

Short position should be opened if zigzagATRshort1 > zigzagATRlong1

The problem is that when I run test it looks that both variables have different values comparing to DT-ZigZag-ATR.mq4 indicator attached to chart.

I completely do not know what causes the problem. It might be either something trival or perhaps I do not understand the logic of this indicator.

I would appreciate if enzone have anz suggestions

Thanks in advance

Wojciech

 

Please use this to post code . . . it makes it easier to read.

Your indicator has the following externs . . .

extern int depth=12;

extern int perATR=12;

extern double k=2;

when you call the Indicator using iCustom you have to pass ALL the externs . . .

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

for example . .

double zigzagATRshort1 = iCustom(NULL,0,"DT-ZigZag-ATR",  12,12,2  ,1,1);

 
  1. Use SRC
  2. double zigzagATRshort1 = iCustom(NULL,0,"DT-ZigZag-ATR",  12,12,2  ,1,1);
    In addition the indicator has two buffers
    #property indicator_color1 Red
    #property indicator_color2 White
    So I would use
    #define DTZZA_RED 0
    #define DTZZA_WHITE 1
    double zigzagATRshort1 = iCustom(NULL,0,"DT-ZigZag-ATR",  12,12,2  ,DTZZA_WHITE, 1);

  3. if (counted_bars>0) counted_bars--;
    limit=Bars-counted_bars+100;
    for (int i=limit; i>0; i--)
    {
    for (int cnt=i; cnt<(100+i); cnt++)
    {
    zigzag1=iCustom(NULL,0,"ZigZag",depth,5,3,0,cnt+1);
    :
    The first run, counted_bars is zero so you are calling iCustom(..., Bars+100+1) way beyond the arrays. The counted_bars decrement is unnecessary
    #define DRAWBEGIN 100
    if (counted_bars < LOOKBACK) counted_bars = DRAWBEGIN;// Don't look past H[Bars-1]
    limit=Bars-1-counted_bars;
    for (int i=limit; i>0; i--){
        for (int cnt=1; cnt<=DRAWBEGIN; cnt++){
            zigzag1=iCustom(NULL,0,"ZigZag",depth,5,3,0,cnt+i);
    :
 

RaptorUK, WHRoeder

1. Will use SRC in the future :-)

2. Many thanks for your suggestions. It solved my problem

Regards