Help with an indicator

 

Hello.


I've written a number of Expert Advisors, but writing an Indicator is different (apparently). I can't my indicator to produce output except for the last 4 (rightmost - most recent) bars. I'm sure I'm doing something stupid, I just can see it.

I've posted the code below. Will somebody please tell me why my main loop doesn't loop?

Once I get it working, I'll change the Indicator[i] = 0.5 to actually reflect what I'm want. For now, I'd be happy with a flat line that exists from the oldest to the newest bar on the graph.

BTW, I'm running this on a Day chart of NZDUSD if that matters for some reason using MetaTrader 4.0 Build 223.


Thanks for your help.

Daniel

------------------------------------------------

#property indicator_separate_window
#property indicator_minimum -5
#property indicator_maximum 5

#property indicator_buffers 1
#property indicator_color1 RoyalBlue

// allow the user to change the period of the indicator
extern int PeriodLength=5;

// variable to plot
double Indicator[];

// global vars
int Unchanged_bars = 0;

int init()
{
//---- indicator line
SetIndexStyle(0, DRAW_LINE);
SetIndexArrow(0, 120);
SetIndexBuffer(0, Indicator);
//name for indicator subwindow
IndicatorShortName("DanielsMA");
return(0);
} // end init()

int start()
{
int i, changed_bars;
double perc_upper, perc_lower, weight;

//---- check for possible errors
if (Bars <= PeriodLength)
return(0);
Unchanged_bars = IndicatorCounted();
if (Unchanged_bars < 0)
return(-1);
//---- last counted bar will be recounted
if (Unchanged_bars > 0)
Unchanged_bars--;
changed_bars = Bars - Unchanged_bars;

//---- main loop
weight = 2.0 / (PeriodLength + 1);
i = changed_bars;
while (i >= 0)
{
// % of candle above the close price
perc_upper = (High[i] - Close[i]) / (High[i] - Low[i]);
// % of candle below the close price
perc_lower = (Close[i] - Low[i]) / (High[i] - Low[i]);

if (i == Bars - Unchanged_bars)
Indicator[i+1] = 0.5;

Indicator[i] = 0.5;

i--;
} // end of while loop

return(0);
} // end start()

 

I bet your indicator fails with zero divide when it launches. Check out the log messages.

I beleive the problem is the same as in 'Can anybody help me? tks a lot. About error shows "zero divide" when uploading my indicator to EA'.

In the following ticks IndicatorCounted() returns a value like Bars - 1 or 2 and zero divide doesn't occur.

Although you're still lucky to have the indicator working on the most recent bars as it's quite possible to get one tick volume bar on a slow market where High[i] - Low[i] = 0.

 
Irtron:

I bet your indicator fails with zero divide when it launches. Check out the log messages.

I beleive the problem is the same as in 'Can anybody help me? tks a lot. About error shows "zero divide" when uploading my indicator to EA'.

In the following ticks IndicatorCounted() returns a value like Bars - 1 or 2 and zero divide doesn't occur.

Although you're still lucky to have the indicator working on the most recent bars as it's quite possible to get one tick volume bar on a slow market where High[i] - Low[i] = 0.

You hit the nail on the head, Irtron.


Thanks a bundle.

Daniel

 

why use a loop while you get it every tick,

//moving trend line
ObjectDelete("long_trend_line");
ObjectCreate("long_trend_line", OBJ_TREND, 0, Time[3], Close[3], Time[0], (High[0] + Low[0]) / 2);
ObjectSet("long_trend_line", OBJPROP_COLOR, Gold);
ObjectSet("long_trend_line",OBJPROP_STYLE,STYLE_SOLID);

if your not using this anywhere else in code, then uncomment this

//ObjectsRedraw();

Greets JB