Array out of range mql4

Array out of range mql4

Manage alerts

Loading saved threads...

Michael · External communityPost link
External question — Stack Overflow Stack Exchange Author: Michael Original post: https://stackoverflow.com/questions/63067026 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I am trying to add indicator code, which works perfectly on its own, to an EA MQL4 source. While everything compiles just fine, the terminal tab shows the above error. Both errors occuring relate to tick[i+k] . double ticks[]; double tick[]; int init() { ArrayResize(ticks,TotalBars); ArraySetAsSeries(ticks,true); ArrayResize(tick,TotalBars); ArraySetAsSeries(tick,true); return(0); } int start() { for (i=TotalBars-1; i>=0; i--) { avg = tick[i]; for (k=1; k<AveragePeriod && tick[i+k]!=0; k++) avg += tick[i+k]; avg /= k; .... I want to prevent iCustom calls. What could be the issue? Thank you.
Quote
Report
Mahdi Ahmadifard · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Mahdi Ahmadifard Original post: https://stackoverflow.com/a/77773086 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. Copy and paste an indicator code directlt to an EA cause the problem, that is due to the the fact that in an indicator file arrays handle automatically. The bug code be fixed by some minor change in the code. For instance, you could check the size of the array tick then perform the below code or instead of using k<AveragePeriod, use k < MathMin(AveragePeriod - i , ArraySize(tick) - i ). That would solve the problem.
Quote
Report
Junaid Azhar · External communityPost link
External answer — Stack Overflow Stack Exchange Author: Junaid Azhar Original post: https://stackoverflow.com/a/77773144 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. In your loop, you're iterating from i = TotalBars - 1 down to i = 0 . However, when you use tick[i+k] inside the loop, it's possible that i + k exceeds the array size, causing the "out of range" error. int start() { int maxIndex = TotalBars - 1; // Adjust this if necessary for (int i = maxIndex; i >= 0; i--) { if (i + AveragePeriod >= TotalBars) { // Ensure i + k stays within array bounds continue; } double avg = tick[i]; int k; for (k = 1; k < AveragePeriod && i + k < TotalBars && tick[i + k] != 0; k++) { avg += tick[i + k]; } if (k > 0) { avg /= k; // ... } } return 0; }
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Junaid Azhar Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/77773144 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. In your loop, you're iterating from i = TotalBars - 1 down to i = 0 . However, when you use tick[i+k] inside the loop, it's possible that i + k exceeds the array size, causing the "out of range" error. int start() { int maxIndex = TotalBars - 1; // Adjust this if necessary for (int i = maxIndex; i >= 0; i--) { if (i + AveragePeriod >= TotalBars) { // Ensure i + k stays within array bounds continue; } double avg = tick[i]; int k; for (k = 1; k < AveragePeriod && i + k < TotalBars && tick[i + k] != 0; k++) { avg += tick[i + k]; } if (k > 0) { avg /= k; // ... } } return 0; }

Cancel quote

Checking account access…