This is the first time I've made an indicator in MQL4. What I want it to do is draw lines like ZigZag. When I ran my indicator on the chart, I got these lines blue plotted instead:

Here is my code in the OnCalculate function:
int Counted_bars=IndicatorCounted(); // Number of counted bars
int i=Bars-Counted_bars-1;
// Setup
if(rates_total<=DATA_LIMIT)
return(0);
// Loop for uncounted bars & for the 0th bar
while(i >= 0)
{
int shift1 = i+1;
int shift2 = i+2;
double ao1 = iAO(_Symbol, 0, shift1);
double ao2 = iAO(_Symbol, 0, shift2);
// mark the start of a new range
if(ao1 > 0 && ao2 < 0)
{
positiveRangeBar = BarIndex(shift1);
highestHighVal = high[shift1];
}
if(ao1 < 0 && ao2 > 0)
{
negativeRangeBar = BarIndex(shift1);
lowestLowVal = low[shift1];
}
// update the highest high/lowest low
if(ao1 > 0)
{
double _high = high[shift1];
if(_high > highestHighVal)
{
highestHighVal = _high;
currHigh[shift1] = highestHighVal;
}
}
if(ao1 < 0)
{
double _low = low[shift1];
if(_low < lowestLowVal)
{
lowestLowVal = _low;
currLow[shift1] = lowestLowVal;
}
}
i--;
}
I have no idea why this is happening.
From a visual review, it looks like the indicator may be using the line style,
DRAW_LINE[MQL4 Reference]. This is a common line style.Alternately, there is the
DRAW_SECTIONline style, which would have the MetaTrader terminal fill in any gap between non-empty* points in the indicator data. A line would be displayed in the visual model of the chart, connecting the nearest points of indicator data.For the coding part: When using the
DRAW_LINEstyle and given a line displaying a series of values from an indicator's data buffer in MetaTrader, if the line is not appearing at a location on the chart then the indicator's data buffer might have the valueEMPTY_VALUEat that location. It may have been set toEMPTY_VALUEor simply not initialized with a "non-empty" value.Towards understanding the logic of the indicator's data function, it looks like it might not be producing a value when
_low >= lowestLowValor_high <= highestHighVal? To have the indicator display something in these gaps, perhaps theDRAW_SECTIONline style could be useful?When developing indicators in MetaTrader, the
DRAW_SECTIONorDRAW_LINEstyle could be used withSetIndexStyle()(ref) duringOnInit()Example Code
Of course the MQL4 Reference offers examples throughout, and there are the example indicator files usually provided with the MetaTrader terminal.
If it may be of any interest as an additional point of reference towards implementing a zig zag indicator in MQL4, albeit in a programming language other than MQL4, there's a zig zag implementation in Python developed by jbn. There's a [Demo] available online.
Towards more complex applications in detecting a reversal in a market rate trend, John F. Ehlers writes about autocorrelation (Heat Map Graph) (Very Short Description, with book references).
Perhaps jbn's Python example might be more succinct? This approach does not rely on techniques in spectral analysis, digital signal processing, or applications of trigonometric functions, per se. John F. Ehlers' approach has a lot of depth though. Perhaps it's been discussed in some academic dissertations.
Footnotes
* With
DRAW_SECTION, a line would be drawn between non-empty points, i.e between points in the data array that have some value other than the valueEMPTY_VALUE.The value
EMPTY_VALUEhas a special meaning for indicator display in MetaTrader.In MQL4 or MQL5, an array must have some sort of value at each index. For a chart index where no data would be displayed, the indicator buffer at the index can be given the value,
EMPTY_VALUEe.g for an indicator using a
double[]array namedbuffer_datato display data values,buffer_data[99]=EMPTY_VALUEwould clear any data displayed at index 99 in that indicator.The exact position of this data index on the chart would depend on whether the
buffer_dataarray has been applied withArraySetAsSeries(buffer_data, true)or alternatelyfalse.For illustration, given the following:
... then the newest chart point in the indicator would have an empty value, and would typically not be displayed.
As far as I know, arrays are usually initialized in MQL4 as filled with
EMPTY_VALUE. For indicators, these values would not be displayed.Thus, if a gap appears in the indicator display, there might be an
EMPTY_VALUEthere, if not a value too large for MetaTrader to display the value.The second case can happen if an
EMPTY_VALUEgets used in a calculation somehow. For calculations gone towards infinity, this might be accompanied with a label such asnan(not a number) in the indicator window.Under the proverbial hood, a
doubletypedEMPTY_VALUEis a number, albeit a very large number that might not normally be produced from an indicator calculation. For purposes of indicator display in MetaTrader, it's interpreted as "No value" and thus may appear as a gap in the indicator's chart display.