You appear to be defining dynamic arrays but not assigning a size to them.
A quick fix could be to simply assign the size at the start (although they would no longer be re-sizable then)
bars = MathMin(Bars - 2, Max_Bar); double pv[bars], s1[bars], r1[bars], s2[bars], r2[bars], s3[bars], r3[bars];
If that is not going to work, try re-sizing the arrays as soon as you compute bars
bars = MathMin(Bars - 2, Max_Bar); ArrayResize(pv ,bars); //repeat for each array
Also as you are using a reverse loop you may need to start at bars - 1 as pv[bars] would be out of range (would need to be pv[bars - 1]
for(int i = bars - 1; i >= 0; i--)
Thanks for the help
Resizing the array and then changing pv[i] to pv[bars - i] helped and that function now works without running into array out of range error
but im running into that issue again with a subsequent function that runs after the first one has finished drawing on the chart
if(PivotsOn1) { bars = MathMin(Bars - 2, Max_Bar); ArrayResize(N_pv ,bars); ArrayResize(N_s1,bars); ArrayResize(N_r1 ,bars); ArrayResize(N_s2,bars); ArrayResize(N_r2 ,bars); ArrayResize(N_s3,bars); ArrayResize(N_r3 ,bars); for(int i = bars; i >= 0; i--) { int currentDayOfWeek = TimeDayOfWeek(Time[i]); // if(currentDayOfWeek == 6 || currentDayOfWeek == 0) continue; int shift = iBarShift(Symbol(), N_TimeFrame, Time[i]) ; while(1) { int day1 = TimeDayOfWeek(iTime(Symbol(), N_TimeFrame, shift + 1)); if(N_TimeFrame == Daily && (day1 == Saturday || day1 == Sunday)) shift = shift + 1; else break; } double highc = iHigh(Symbol(), N_TimeFrame, shift + 1); double lowc = iLow(Symbol(), N_TimeFrame, shift + 1); double closec = iClose(Symbol(), N_TimeFrame, shift + 1); double pvc = (highc + lowc + closec) / 3; double s1c = 2 * pvc - highc; double r1c = 2 * pvc - lowc; double s2c = pvc - (highc - lowc); double r2c = pvc + (highc - lowc); double s3c = lowc - (2 * (highc - pvc)); double r3c = highc + (2 * (pvc - lowc)); N_pv[bars - i] = pvc; N_s1[bars - i] = s1c; N_r1[bars - i] = r1c; N_s2[bars - i] = s2c; N_r2[bars - i] = r2c; N_s3[bars - i] = s3c; N_r3[bars - i] = r3c; string addition = ""; if(N_TimeFrame == Weekly) { addition = DoubleToString(TimeDayOfWeek(iTime(Symbol(), PERIOD_CURRENT, i))); } if(N_TimeFrame == Monthly) { addition = DoubleToString(TimeDayOfYear(iTime(Symbol(), PERIOD_CURRENT, i))); } if(N_BeginningFound || (!N_BeginningFound && ((TimeHour(Time[i]) == 0) || (TimeHour(Time[i]) == 1)))) { N_BeginningFound = true; if(Text_Size > 0 && Show_Text && ((N_TimeFrame == Monthly && currentDayOfWeek == 1) || N_TimeFrame == Weekly || N_TimeFrame == Daily)) { string name = Symbol() + N_tf + TimeToString(iTime(Symbol(), N_TimeFrame, shift), TIME_DATE) + addition ; DrawText(TimeFrame1_0 + " Pivot ", name + " textPv", Time[i], pvc, " " + N_tf + "P", PColor, Text_Size); DrawText(TimeFrame1_0 + " Support1 ", name + " textS1", Time[i], s1c, " " + N_tf + "S1", SColor, Text_Size); DrawText(TimeFrame1_0 + " Resistance1 ", name + " textR1", Time[i], r1c, " " + N_tf + "R1", RColor, Text_Size); DrawText(TimeFrame1_0 + " Support2 ", name + " textS2", Time[i], s2c, " " + N_tf + "S2", SColor, Text_Size); DrawText(TimeFrame1_0 + " Resistance2 ", name + " textR2", Time[i], r2c, " " + N_tf + "R2", RColor, Text_Size); DrawText(TimeFrame1_0 + " Support3 ", name + " textS3", Time[i], s3c, " " + N_tf + "S3", SColor, Text_Size); DrawText(TimeFrame1_0 + " Resistance3 ", name + " textR3", Time[i], r3c, " " + N_tf + "R3", RColor, Text_Size); } if(N_prevShift != shift) { N_prevShift = shift; N_PourTime = Time[i]; N_S1ourTime = Time[i]; N_S2ourTime = Time[i]; N_S3ourTime = Time[i]; N_R1ourTime = Time[i]; N_R2ourTime = Time[i]; N_R3ourTime = Time[i]; /* string name1 = Symbol()+N_tf+TimeToString(iTime(Symbol(),N_TimeFrame,shift),TIME_DATE) ; DrawExtension(TimeFrame1_0 + " Pivot " ,name1+ " Pivot",Time[i],pvc,N_PourTime,pvc,"",PColor,Text_Size); DrawExtension(TimeFrame1_0 + " Support1 " ,name1+ " Support 1",Time[i],s1c,N_S1ourTime,s1c,"",SColor,Text_Size); DrawExtension(TimeFrame1_0 + " Resistance1 " ,name1+ " Resistance 1",Time[i],r1c,N_R1ourTime,r1c,"",RColor,Text_Size); DrawExtension(TimeFrame1_0 + " Support2 " ,name1+ " Support 2",Time[i],s2c,N_S2ourTime,s2c,"",SColor,Text_Size); DrawExtension(TimeFrame1_0 + " Resistance2 " ,name1+ " Resistance 2",Time[i],r2c,N_R2ourTime,r2c,"",RColor,Text_Size); DrawExtension(TimeFrame1_0 + " Support3 " ,name1+ " Support 3",Time[i],s3c,N_S3ourTime,s3c,"",SColor,Text_Size); DrawExtension(TimeFrame1_0 + " Resistance3 " ,name1+ " Resistance 3",Time[i],r3c,N_R3ourTime,r3c,"",RColor,Text_Size); */ } else { string name1 = Symbol() + N_tf + TimeToString(iTime(Symbol(), N_TimeFrame, shift), TIME_DATE) ; int k = 0; if(i > 0) k = i - 1; else k = i; if(LineType == Line) { DrawExtension(TimeFrame1_0 + " Pivot ", name1 + " Pivot", N_PourTime, pvc, Time[k], pvc, "", PColor, Text_Size); DrawExtension(TimeFrame1_0 + " Support1 ", name1 + " Support 1", N_S1ourTime, s1c, Time[k], s1c, "", SColor, Text_Size); DrawExtension(TimeFrame1_0 + " Resistance1 ", name1 + " Resistance 1", N_R1ourTime, r1c, Time[k], r1c, "", RColor, Text_Size); DrawExtension(TimeFrame1_0 + " Support2 ", name1 + " Support 2", N_S2ourTime, s2c, Time[k], s2c, "", SColor, Text_Size); DrawExtension(TimeFrame1_0 + " Resistance2 ", name1 + " Resistance 2", N_R2ourTime, r2c, Time[k], r2c, "", RColor, Text_Size); DrawExtension(TimeFrame1_0 + " Support3 ", name1 + " Support 3", N_S3ourTime, s3c, Time[k], s3c, "", SColor, Text_Size); DrawExtension(TimeFrame1_0 + " Resistance3 ", name1 + " Resistance 3", N_R3ourTime, r3c, Time[k], r3c, "", RColor, Text_Size); } } if(i == 0) { string name1 = Symbol() + N_tf; if(DrawExt) { int day = TimeDay(Time[0]); int dayofweek = TimeDayOfWeek(Time[0]); int month = TimeMonth(Time[0]); datetime t = 0; datetime tWeek = iTime(Symbol(), PERIOD_W1, 0); datetime tMonth = iTime(Symbol(), PERIOD_MN1, 0); if(N_TimeFrame == Daily) t = StrToTime(IntegerToString(TimeYear(TimeCurrent())) + "." + IntegerToString(TimeMonth(TimeCurrent())) + "." + IntegerToString(TimeDay(TimeCurrent())) + " " + "00:00:00") + (24 * 3600); else if(N_TimeFrame == Weekly) t = StrToTime(IntegerToString(TimeYear(TimeCurrent())) + "." + IntegerToString(TimeMonth(TimeCurrent())) + "." + IntegerToString(TimeDay(TimeCurrent())) + " " + "00:00:00") + (24 * 3600); else if(N_TimeFrame == Monthly) t = StrToTime(IntegerToString(TimeYear(tMonth)) + "." + IntegerToString(TimeMonth(tMonth)) + "." + IntegerToString(TimeDay(tMonth)) + " " + "00:00:00") + (30 * 24 * 3600); if(LineType == Line) { DrawExtension(TimeFrame1_0 + " Pivot ", name1 + " Pivot", Time[i], pvc, t, pvc, "", PColor, Text_Size); DrawExtension(TimeFrame1_0 + " Support1 ", name1 + " Support 1", Time[i], s1c, t, s1c, "", SColor, Text_Size); DrawExtension(TimeFrame1_0 + " Resistance1 ", name1 + " Resistance 1", Time[i], r1c, t, r1c, "", RColor, Text_Size); DrawExtension(TimeFrame1_0 + " Support2 ", name1 + " Support 2", Time[i], s2c, t, s2c, "", SColor, Text_Size); DrawExtension(TimeFrame1_0 + " Resistance2 ", name1 + " Resistance 2", Time[i], r2c, t, r2c, "", RColor, Text_Size); DrawExtension(TimeFrame1_0 + " Support3 ", name1 + " Support 3", Time[i], s3c, t, s3c, "", SColor, Text_Size); DrawExtension(TimeFrame1_0 + " Resistance3 ", name1 + " Resistance 3", Time[i], r3c, t, r3c, "", RColor, Text_Size); } } } } } }
Array out of range at N_bars ,
Compared to before the second function does run and draws on the chart it just when it reaches the current bar it then goes out of range ,
I did print the "bar-i" value to find it does go from 0 to till the max bars i have set but then after it goes out of range , guessing its trying to go beyond the max i set and i dont understand why , these two functions are practically the same but the first one reaches the max and stops while the second one doesnt
int OnInit() { prevShift = -1; N_prevShift = -1; EventSetTimer(5); bool IsOk = CheckTime(); if(IsOk && Bars > 100 && SessionsOn) { Initialize(); Initialized = true; } if(IsBacktesting) { Initialize(); Initialized = true; } SetIndexBuffer(0, pv); SetIndexBuffer(1, s1); SetIndexBuffer(2, r1); SetIndexBuffer(3, s2); SetIndexBuffer(4, r2); SetIndexBuffer(5, s3); SetIndexBuffer(6, r3); SetIndexBuffer(7, N_pv); SetIndexBuffer(8, N_s1); SetIndexBuffer(9, N_r1); SetIndexBuffer(10, N_s2); SetIndexBuffer(11, N_r2); SetIndexBuffer(12, N_s3); SetIndexBuffer(13, N_r3); if(PivotsOn || PivotsOn1) { tf = " D "; if(TimeFrame == Weekly) tf = " W "; else if(TimeFrame == Monthly) tf = " MN "; N_tf = " D "; if(N_TimeFrame == Weekly) N_tf = " W "; else if(N_TimeFrame == Monthly) N_tf = " MN "; TimeFrame_0 = " Daily "; if(TimeFrame == Weekly) TimeFrame_0 = " Weekly "; else if(TimeFrame == Monthly) TimeFrame_0 = " Monthly "; TimeFrame1_0 = " Daily "; if(N_TimeFrame == Weekly) TimeFrame1_0 = " Weekly "; else if(N_TimeFrame == Monthly) TimeFrame1_0 = " Monthly "; SetIndexLabel(0, "Pivot Point"); SetIndexLabel(1, "Support 1"); SetIndexLabel(2, "Resistance 1"); SetIndexLabel(3, "Support 2"); SetIndexLabel(4, "Resistance 2"); SetIndexLabel(5, "Support 3"); SetIndexLabel(6, "Resistance 3"); SetIndexLabel(7, "Pivot Point"); SetIndexLabel(8, "Support 1"); SetIndexLabel(9, "Resistance 1"); SetIndexLabel(10, "Support 2"); SetIndexLabel(11, "Resistance 2"); SetIndexLabel(12, "Support 3"); SetIndexLabel(13, "Resistance 3"); if(LineType == Line) { style = DRAW_NONE; style1 = STYLE_SOLID; size = Dot_Size; SetIndexStyle(0, style, style1, Dot_Size); SetIndexStyle(1, style, style1, Dot_Size); SetIndexStyle(2, style, style1, Dot_Size); SetIndexStyle(3, style, style1, Dot_Size); SetIndexStyle(4, style, style1, Dot_Size); SetIndexStyle(5, style, style1, Dot_Size); SetIndexStyle(6, style, style1, Dot_Size); SetIndexStyle(7, style, style1, Dot_Size); SetIndexStyle(8, style, style1, Dot_Size); SetIndexStyle(9, style, style1, Dot_Size); SetIndexStyle(10, style, style1, Dot_Size); SetIndexStyle(11, style, style1, Dot_Size); SetIndexStyle(12, style, style1, Dot_Size); SetIndexStyle(13, style, style1, Dot_Size); } else { SetIndexStyle(0, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(0, 159); SetIndexStyle(1, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(1, 159); SetIndexStyle(2, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(2, 159); SetIndexStyle(3, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(3, 159); SetIndexStyle(4, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(4, 159); SetIndexStyle(5, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(5, 159); SetIndexStyle(6, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(6, 159); SetIndexStyle(7, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(7, 159); SetIndexStyle(8, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(8, 159); SetIndexStyle(9, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(9, 159); SetIndexStyle(10, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(10, 159); SetIndexStyle(11, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(11, 159); SetIndexStyle(12, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(12, 159); SetIndexStyle(13, DRAW_ARROW, EMPTY, Dot_Size); SetIndexArrow(13, 159); style = DRAW_NONE; style1 = STYLE_DOT; size = Dot_Size; ChartRedraw(); } } lastTime = Time[0]; return(INIT_SUCCEEDED); }
I can't say without debugging the code - I suggest you set some breakpoints and step through the code to try and figure out why the error occurs later on.
I would start by stopping at this statement to see the size of pv before and after the call
SetIndexBuffer(0, pv);
I can't say without debugging the code - I suggest you set some breakpoints and step through the code to try and figure out why the error occurs later on.
I would start by stopping at this statement to see the size of pv before and after the call
Its zero both before and after but its supposed to be zero though right ? arent arrays in mt4 dynamically adjusted
or is that the difference in indicators and EA's ?
Its zero both before and after but its supposed to be zero though right ? arent arrays in mt4 dynamically adjusted
or is that the difference in indicators and EA's ?
Really I do not know (I use MQL5 and have not encountered that problem), but the fact it is 0 before and after seems to suggest that is the key issue as that would result in an out of range error if you try and access it.
I suggest looking at the documentation for that function - it may provide more clues
Really I do not know (I use MQL5 and have not encountered that problem), but the fact it is 0 before and after seems to suggest that is the key issue as that would result in an out of range error if you try and access it.
I suggest looking at the documentation for that function - it may provide more clues
idk as well , as what i find is that array buffers in mt4 are automatically resized and that i shouldn't have to resize my arrays . So there issue is how they are initialized or assigned ?
but i am not to sure at all .
double pv[], s1[], r1[], s2[], r2[], s3[], r3[]; string tf;
I do define them like this but i dont see why this would cause an issue and even when i do settheidex buffer in OnInit , i dont see the issue
SetIndexBuffer(0, pv); SetIndexBuffer(1, s1); SetIndexBuffer(2, r1); SetIndexBuffer(3, s2); SetIndexBuffer(4, r2); SetIndexBuffer(5, s3); SetIndexBuffer(6, r3); SetIndexBuffer(7, N_pv); SetIndexBuffer(8, N_s1); SetIndexBuffer(9, N_r1); SetIndexBuffer(10, N_s2); SetIndexBuffer(11, N_r2); SetIndexBuffer(12, N_s3); SetIndexBuffer(13, N_r3);
And the arrays are never touched again till OnTick where i try and pass data in but then it gives me an array out of range function
if(PivotsOn) { bars = MathMin(Bars - 2, Max_Bar); for(int i = bars; i >= 0; i--) { int currentDayOfWeek = TimeDayOfWeek(Time[i]); // if(currentDayOfWeek == 6 || currentDayOfWeek == 0) continue; if(currentDayOfWeek == Saturday || currentDayOfWeek == Sunday) continue; int shift = iBarShift(Symbol(), TimeFrame, Time[i]); while(1) { int day1 = TimeDayOfWeek(iTime(Symbol(), TimeFrame, shift + 1)); if(TimeFrame == Daily && (day1 == Sunday || day1 == Saturday)) shift = shift + 1; else break; } double highc = iHigh(Symbol(), TimeFrame, shift + 1); double lowc = iLow(Symbol(), TimeFrame, shift + 1); double closec = iClose(Symbol(), TimeFrame, shift + 1); double pvc = (highc + lowc + closec) / 3; double s1c = 2 * pvc - highc; double r1c = 2 * pvc - lowc; double s2c = pvc - (highc - lowc); double r2c = pvc + (highc - lowc); double s3c = lowc - (2 * (highc - pvc)); double r3c = highc + (2 * (pvc - lowc)); Print("Array size: ", ArraySize(pv), ", i: ", i); pv [i] = pvc; s1[i] = s1c; r1[i] = r1c; s2[i] = s2c; r2[i] = r2c; s3[i] = s3c; r3[i] = r3c;
printing i will just give me the max number of bars that i set and printing the size of the array gives me zero if i dont resize the array and that as well just sets it to the max number of bars i set , it doesnt stop the out of range error
idk as well , as what i find is that array buffers in mt4 are automatically resized and that i shouldn't have to resize my arrays . So there issue is how they are initialized or assigned ?
but i am not to sure at all .
I do define them like this but i dont see why this would cause an issue and even when i do settheidex buffer in OnInit , i dont see the issue
And the arrays are never touched again till OnTick where i try and pass data in but then it gives me an array out of range function
printing i will just give me the max number of bars that i set and printing the size of the array gives me zero if i dont resize the array and that as well just sets it to the max number of bars i set , it doesnt stop the out of range error
Until you figure out the reasons, I suggest putting if statements around the sections where the code is dumping to check the array sizes are > 0. At least it will stop abnormal terminations until you figure out the solution.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
double pv[], s1[], r1[], s2[], r2[], s3[], r3[];
then set here Oninit
then called in another function Ontick
This is the full function
And i did set the property indicators buffers
The error is at
Printing I gives 600 and printing the arraysize gives me 0
I am not sure how to fix the arraysize being 0