I try to write an indicator which shows the direction of Highs and Lows. It should return 1 for higher values and -1 for lower ones.
In the case of equal highs or equal lows it should neither return 0 (zero), nor EMPTY_VALUEs, but instead return the previous direction (the direction when the highs or lows were unequal). Which means that the only acceptable values are 1 and -1.
I thought it would be quite straightforward, but for unknown reasons the indicator seems to return erratic results when High[i] == High[i+1] or Low[i] == Low[i+1].
For example consider the following sequence of XAUUSD prices (from older to newer) and the corresponding values returned by this indicator (where HiDir represents the direction of Highs and LoDir the direction of Lows.):
Bar 1 : High = 1961.15 Low = 1960.73 ………………………………………..
Bar 2 : High = 1960.76 Low = 1960.58 HiDir = -1 (correct) LoDir = -1 (correct)
Bar 3 : High = 1960.73 Low = 1960.58 HiDir = -1 (correct) LoDir = 1 (wrong, it should be -1) @ 2023.06.09 23:24 (5 minute chart)
static int Hi_PreviousDir, Lo_PreviousDir;
for(int i=1; i<limit ;i++) { H0 = high[i-1]; H1 = high[i]; L0 = low[i-1]; L1 = low[i]; if (H0 != H1) { if (H0>H1) {HiDir[i] = -1;} //last direction of highs if (H0<H1) {HiDir[i] = 1;} //last direction of highs } else HiDir[i] = HiDir[i-1]; if (L0 != L1) { if (L0>L1) {LoDir[i] = -1;} //last direction of lows if (L0<L1) {LoDir[i] = 1;} //last direction of lows } else LoDir[i] = LoDir[i-1]; }
Correct me if I am wrong, but unless otherwise defined we work with Timeseries. And with Timeseries i+1 always means a bar back, while i-1 means a bar forward (future).
In my approach I use Timeseries. So, i-1 must be wrong as it references a future value.
If I was to use arrays then i-1 would make sense to me.
If the above are all wrong, I fear I have not understood many things regarding MQL4, which is quite probable I regret to say.
Correct me if I am wrong, but unless otherwise defined we work with Timeseries. And with Timeseries i+1 always means a bar back, while i-1 means a bar forward (future).
In my approach I use Timeseries. So, i-1 must be wrong as it references a future value.
If I was to use arrays then i-1 would make sense to me.
If the above are all wrong, I fear I have not understood many things regarding MQL4, which is quite probable I regret to say.
Your are building your indicator buffers in the wrong direction :
for(int i=0; i<limit-1 ;i++)You should go from past to present, not the reverse.
Your "previous" is from the future.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I try to write an indicator which shows the direction of Highs and Lows. It should return 1 for higher values and -1 for lower ones.
My code is this:In the case of equal highs or equal lows it should neither return 0 (zero), nor EMPTY_VALUEs, but instead return the previous direction (the direction when the highs or lows were unequal). Which means that the only acceptable values are 1 and -1.
I thought it would be quite straightforward, but for unknown reasons the indicator seems to return erratic results when High[i] == High[i+1] or Low[i] == Low[i+1].
For example consider the following sequence of XAUUSD prices (from older to newer) and the corresponding values returned by this indicator (where HiDir represents the direction of Highs and LoDir the direction of Lows.):
Bar 1 : High = 1961.15 Low = 1960.73 ………………………………………..
Bar 2 : High = 1960.76 Low = 1960.58 HiDir = -1 (correct) LoDir = -1 (correct)
Bar 3 : High = 1960.73 Low = 1960.58 HiDir = -1 (correct) LoDir = 1 (wrong, it should be -1) @ 2023.06.09 23:24 (5 minute chart)