What's wrong with my code?

 

Dear all

I am new to MQL4 and have problem in writing my code (detail is shown below).

What I want it to do is, when the condition is fulfilled, an arrow should be shown on the specific bar of the chart.

#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Red
#property indicator_color3 Magenta
#property indicator_color4 Sienna

double up240[];
double up1440[];
double down240[];
double down1440[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0, DRAW_ARROW, EMPTY);
SetIndexArrow(0, 225);
SetIndexBuffer(0, up240);
SetIndexStyle(1, DRAW_ARROW, EMPTY);
SetIndexArrow(1, 226);
SetIndexBuffer(1, down240);
SetIndexStyle(2, DRAW_ARROW, EMPTY);
SetIndexArrow(2, 225);
SetIndexBuffer(2, up1440);
SetIndexStyle(3, DRAW_ARROW, EMPTY);
SetIndexArrow(3, 226);
SetIndexBuffer(3, down1440);

return(0);
}
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
int UpTrend, DownTrend;

double ima240_10_1 = iMA(NULL,240,10,0,MODE_EMA,PRICE_CLOSE,1);
double ima240_20_1 = iMA(NULL,240,20,0,MODE_SMA,PRICE_CLOSE,1);
double ima240_50_1 = iMA(NULL,240,50,0,MODE_SMA,PRICE_CLOSE,1);
double ima240_10_2 = iMA(NULL,240,10,0,MODE_EMA,PRICE_CLOSE,2);
double ima240_20_2 = iMA(NULL,240,20,0,MODE_SMA,PRICE_CLOSE,2);
double ima1440_10_1 = iMA(NULL,1440,10,0,MODE_EMA,PRICE_CLOSE,1);
double ima1440_20_1 = iMA(NULL,1440,20,0,MODE_SMA,PRICE_CLOSE,1);
double ima1440_10_2 = iMA(NULL,1440,10,0,MODE_EMA,PRICE_CLOSE,2);
double ima1440_20_2 = iMA(NULL,1440,20,0,MODE_SMA,PRICE_CLOSE,2);
double ima1440_50_1 = iMA(NULL,1440,50,0,MODE_SMA,PRICE_CLOSE,1);

//+----------------------------------------------------------------+
//| Block 1 |
//+----------------------------------------------------------------+

{
if( UpTrend < 2 && iClose(NULL,1440,1) > ima1440_10_1 && ima1440_10_1 > ima1440_20_1 &&
ima1440_10_1 > ima1440_50_1 )

UpTrend = 2;

else if( UpTrend < 1 && Close[1] > ima240_10_1 && ima240_10_1 > ima240_20_1 &&
ima240_10_1 > ima240_50_1 )

UpTrend = 1;

if( DownTrend < 2 && iClose(NULL,1440,1) < ima1440_10_1 && ima1440_10_1 < ima1440_20_1 &&
ima1440_10_1 < ima1440_50_1 )

DownTrend = 2;

else if( DownTrend < 1 && Close[1] < ima240_10_1 && ima240_10_1 < ima240_20_1 &&
ima240_10_1 < ima240_50_1 )

DownTrend = 1;

}

//+----------------------------------------------------------------+
//| Block 2 |
//+----------------------------------------------------------------+

{
if( UpTrend == 2 && ima1440_10_2 >= ima1440_20_2 && ima1440_10_1 < ima1440_20_1 )

UpTrend = 0;

if( UpTrend == 1 && ima240_10_2 >= ima240_20_2 && ima240_10_1 < ima240_20_1 )

UpTrend = 0;

if( DownTrend == 2 && ima1440_10_2 <= ima1440_20_2 && ima1440_10_1 > ima1440_20_1 )

DownTrend = 0;

if( DownTrend == 1 && ima240_10_2 <= ima240_20_2 && ima240_10_1 > ima240_20_1 )
DownTrend = 0;
}

//+----------------------------------------------------------------+
//| Block 3 |
//+----------------------------------------------------------------+

if(Bars <= 1500)
return(0);
int fixed_bars = IndicatorCounted();
//---- check for possible errors
if(fixed_bars < 0)
return(-1);
//---- last counted bar will be recounted
if(fixed_bars > 0)
fixed_bars--;
int limit = Bars - fixed_bars;
//----
for(int i = 0; i < limit; i++)

{
if (UpTrend == 1)

up240[i-1]=Low[i-1] - 0.0010;

if (DownTrend == 1)

down240[i-1]=High[i-1] + 0.0010;

if (UpTrend == 2)

up1440[i-1]=Low[i-1] - 0.0010;

if (DownTrend == 2)

down1440[i-1]=High[i-1] + 0.0010;
}
return(0);
}

However, when I attached it to a chart, arrows were put on each of the bars! I have no idea whether it is the error syntax or logic. Would anyone who is an expert here like to help me?

Many thanks!

tob2011

 

A) your setting Up/DownTrend ONCE ONLY per start() call, which is for CURRENT/LATEST bar

B) your using these CURRENT values to set OLD HISTORICAL bars.

C) you need to recalc Up/DownTrend INSIDE your [note: using SRC button above]

for(int i = 0; i < limit; i++)
{
   ...
}

loop, which BTW generates NEGATIVE indices in your code via [i-1] which is NOT GOOD