Help Needed: My indicator print the cloud in the wrong place
Hello Friends!
I tried to convert an indicator from MQL4 to MQL5. the indicator named: Swing Cloud and I don't know what is wrong with MQL5 code.
In uptrend the cloud should be below the price and in downtrend the cloud should be above the price.
Here is on MT4 it print the cloud in the correct position.
But the cloud printed on MT5 is not in the correct position.
here is the code
Traders and coders are working for free:
- if it is interesting for them personally, or
- if it is interesting for many members on this forum.
Freelance section of the forum should be used in most of the cases.
- 2026.02.10
- www.mql5.com
1. Count up.
2. Make atrValues[] a buffer and only copy what is necessary.
// Calculate start position int start=(rates_total==prev_calculated)?rates_total-1:prev_calculated; if(prev_calculated == 0) { // First calculation // Initialize buffers with EMPTY_VALUE ArrayInitialize(buff_Stop, EMPTY_VALUE); ArrayInitialize(buff_Swing, EMPTY_VALUE); ArrayInitialize(buff_CloudUp, EMPTY_VALUE); ArrayInitialize(buff_CloudDown, EMPTY_VALUE); ArrayInitialize(buff_LowerBand, EMPTY_VALUE); ArrayInitialize(buff_UpperBand, EMPTY_VALUE); ArrayInitialize(buff_Trend, EMPTY_VALUE); // Initialize trend from the first bar buff_Trend[start++] = 0.0; } // Copy ATR values //double atrValues[]; int toCopy=(rates_total==prev_calculated)?1:rates_total-prev_calculated; if(CopyBuffer(atrHandle, 0, 0, toCopy, atr_Values) <= 0) { Print("Failed to copy ATR values"); return 0; } // Main calculation loop for(int i = start; i < rates_total; i++) { double atr = atr_Values[i]; // Skip if ATR is not valid if(atr <= 0 || atr == EMPTY_VALUE) { buff_Stop[i] = buff_Stop[i-1]; buff_Swing[i] = buff_Swing[i-1];
Hello Friends!
I tried to convert an indicator from MQL4 to MQL5. the indicator named: Swing Cloud and I don't know what is wrong with MQL5 code.
In uptrend the cloud should be below the price and in downtrend the cloud should be above the price.
Here is on MT4 it print the cloud in the correct position.
But the cloud printed on MT5 is not in the correct position.
here is the code
The loop is written MT4 style, i goes from rates_total-1 down to 0 and treats [i+1] as the previous bar. In MQL5 the OnCalculate arrays, your buffers and the CopyBuffer result are not series by default, so index 0 is the oldest bar and the whole thing runs backwards in time. Easiest fix is to flip them all at the top of OnCalculate:
ArraySetAsSeries(high, true); ArraySetAsSeries(low, true); ArraySetAsSeries(close, true); ArraySetAsSeries(atrValues, true); // before CopyBuffer ArraySetAsSeries(buff_Stop, true); ArraySetAsSeries(buff_Swing, true); ArraySetAsSeries(buff_CloudUp, true); ArraySetAsSeries(buff_CloudDown, true); ArraySetAsSeries(buff_LowerBand, true); ArraySetAsSeries(buff_UpperBand, true); ArraySetAsSeries(buff_Trend, true);
Also change the non-first-run start to rates_total - prev_calculated, otherwise with series indexing you recalculate almost the whole history on every tick.
Hello Friends!
I tried to convert an indicator from MQL4 to MQL5. the indicator named: Swing Cloud and I don't know what is wrong with MQL5 code.
In uptrend the cloud should be below the price and in downtrend the cloud should be above the price.
Here is on MT4 it print the cloud in the correct position.
But the cloud printed on MT5 is not in the correct position.
here is the code