MQL4 indicator not drawing lines
MQL4 indicator not drawing lines
Loading saved threads...
Samaara · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Samaara
Original post: https://stackoverflow.com/questions/76907017
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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.
Quote
Report
Sean Champ · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Sean Champ
Original post: https://stackoverflow.com/a/76973639
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
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_SECTION
line 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_LINE
style 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 value
EMPTY_VALUE
at that location. It may have been set to
EMPTY_VALUE
or 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 >= lowestLowVal
or
_high <= highestHighVal
? To have the indicator display something in these gaps, perhaps the
DRAW_SECTION
line style could be useful?
When developing indicators in MetaTrader, the
DRAW_SECTION
or
DRAW_LINE
style could be used with
SetIndexStyle()
(
ref
) during
OnInit()
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 value
EMPTY_VALUE
.
The value
EMPTY_VALUE
has 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_VALUE
e.g for an indicator using a
double[]
array named
buffer_data
to display data values,
buffer_data[99]=EMPTY_VALUE
would 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_data
array has been applied with
ArraySetAsSeries(buffer_data, true)
or alternately
false
.
For illustration, given the following:
ArraySetAsSeries(buffer_data, true);
buffer_data[0] = EMPTY_VALUE;
... 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_VALUE
there, if not a value too large for MetaTrader to display the value.
The second case can happen if an
EMPTY_VALUE
gets used in a calculation somehow. For calculations gone towards infinity, this might be accompanied with a label such as
nan
(not a number) in the indicator window.
Under the proverbial hood, a
double
typed
EMPTY_VALUE
is 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.
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Samaara Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/questions/76907017 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. 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.
Checking account access…