Hello guys;
I have an issue, I am trying to code an Arrow indicator for Moving Average crossover, but I always encounter an "Array out of Range Error" on any line with [i+2], and when i cooment out this lines, I later have the same "Array out of Range Error" on any line with [i+1] && [i+3]. I have been trying to rectify this issue for hours but have been going nowhere. This is very important to me as i am trying to port my self written Indicators from Mt4 to Mt5.
Below is the Error ScreenShot:
try:
for(int i=(int)MathMax(prev_calculated-1,3); i<rates_total; i++) {
try:
You should check the arraysize with the location you are either trying to write to or to read from because at some point it's none existent.
Now it tells you the problem is at line 176, cursor position 101, but we do not see line numbers so we can not check which line that is.
What part do you not understand ?
When you check if the array location exists, before reading it you can prevent trying to read a non existent location.
How can I check if location exists please? I tried checking for array size up to that error point and the size was what it should be.
Array MA[] contains rates_total elements indexed from 0 to rates_total-1.
Your loop index variable "i" iterates up to rates_total-1.
When i == rates_total-1, you try to read MA[i+1] and MA[i+2]
i+1 = rates_total-1+1 = rates_total
i+2 = rates_total-1+2 = rates_total+1
This causes "Array out of range error" because the last index in the MA[] array is rates_total-1
How can I check if location exists please? I tried checking for array size up to that error point and the size was what it should be.
If the position you are going to read is bigger then the size of the array....
You could do a simple if([i+2]<0) {i=0;}The true issue resides in the logic of your for loop.
for(int i=(int)MathMax(prev_calculated-1,1); i<rates_total; i++) {
So lets say for argument sake that rates_total = 1000. When prev_calculated also equals 1000, the loop starts off at index=999, which is exactly the last element in the array. Therefore, any attempt to increment the beginning index beyond 999 will result in an index out of range error. If you need to increment the index in the body of the for loop block then you will need to account for that in the loop expression.
Change that loop to this will do the trick.
for(int i=(int)MathMax(prev_calculated-1,1); i<rates_total && i+2<ArraySize(MA2); i++)

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello guys;
I have an issue, I am trying to code an Arrow indicator for Moving Average crossover, but I always encounter an "Array out of Range Error" on any line with [i+2], and when i cooment out this lines, I later have the same "Array out of Range Error" on any line with [i+1] && [i+3]. I have been trying to rectify this issue for hours but have been going nowhere. This is very important to me as i am trying to port my self written Indicators from Mt4 to Mt5.
Below is the Error ScreenShot:
Error Line: