Hello,
I have an indicator that show me 3 bar fractals. I'm drawing lines on OHLC of a candle and that's where it gives me some problems.
It works fine as long a i keep the number below i+4 in the line below - if i change it to i+5 i get the error "array out of range xxxx.mq4 (253,53)
Here is the code i'm working on
I know i have to use ArrayResize, but i cannot find out to put it in the code correctly so it removes the array out of range error.
This is how it looks like when the number is i+4
And when the number is changed to i+5 it looks like this
I have an indicator that show me 3 bar fractals. I'm drawing lines on OHLC of a candle and that's where it gives me some problems.
It works fine as long a i keep the number below i+4 in the line below - if i change it to i+5 i get the error "array out of range xxxx.mq4 (253,53)
Inserted Code
ObjectCreate(strObjectNameLow, OBJ_TREND, 0, Time[i-5], value, Time[i+5], value);
Here is the code i'm working on
Inserted Code
#property indicator_chart_window #property indicator_buffers 2 #include <WinUser32.mqh> extern int Fractal_Timeframe = 0; extern int Maxbar = 2000; extern color Up_Fractal_Color = Red; extern int Up_Fractal_Symbol = 108; extern color Down_Fractal_Color = Blue; extern int Down_Fractal_Symbol = 108; extern bool Extend_Line = false; extern bool Extend_Line_to_Background = true; extern bool Show_Validation_Candle = true; extern color Up_Fractal_Extend_Line_Color = Red; extern int Up_Fractal_Extend_Width = 0; extern int Up_Fractal_Extend_Style = 2; extern color Down_Fractal_Extend_Line_Color = Blue; extern int Down_Fractal_Extend_Width = 0; extern int Down_Fractal_Extend_Style = 2; extern double DistanceFromCandle = 25; double UpBuffer[], DoBuffer[], refchk, tempref, level, level_1, level_2; int barc; //+------------------------------------------------------------------+ //| INIT | //+------------------------------------------------------------------+ int init() { SetIndexBuffer(0,UpBuffer); SetIndexStyle(0,DRAW_ARROW, DRAW_ARROW, 0, Up_Fractal_Color); SetIndexArrow(0,Up_Fractal_Symbol); SetIndexBuffer(1,DoBuffer); SetIndexStyle(1,DRAW_ARROW, DRAW_ARROW, 0, Down_Fractal_Color); SetIndexArrow(1,Down_Fractal_Symbol); return(0); } //+------------------------------------------------------------------+ //| DEINIT | //+------------------------------------------------------------------+ int deinit() { for(int i = ObjectsTotal(); i >= 0; i--) { if(StringSubstr(ObjectName(i),0,12) == "MTF_Fractal_") { ObjectDelete(ObjectName(i)); } } for(int i = ObjectsTotal(); i >= 0; i--) { if(StringSubstr(ObjectName(i),0,12) == "Example line") { ObjectDelete(ObjectName(i)); } } Comment(""); return(0); } //+------------------------------------------------------------------+ //| START | //+------------------------------------------------------------------+ int start() { // Check if correctly timeframe is selected. Only show dots on D1 if((Period() == 1440)) { int c, dif; int i; tempref = iHigh(Symbol(), Fractal_Timeframe, 1) + iHigh(Symbol(), Fractal_Timeframe, 51) + iHigh(Symbol(), Fractal_Timeframe, 101); if(barc != Bars || IndicatorCounted() < 0 || tempref != refchk) { barc = Bars; refchk = tempref; } else return(0); deinit(); if(Fractal_Timeframe <= Period()) Fractal_Timeframe = Period(); dif = Fractal_Timeframe/Period(); if(Maxbar > Bars) Maxbar = Bars-10; int CountHigh = 1; int CountLow = 1; for(i = 0; i < Maxbar; i++) { if(iBarShift(NULL,Fractal_Timeframe,Time[i]) < 3) { UpBuffer[i] = 0; DoBuffer[i] = 0; continue; } // Add a dot above the candle that is the high fractal UpBuffer[i] = iFractals(NULL,Fractal_Timeframe,1,iBarShift(NULL,Fractal_Timeframe,Time[i])); if(CountHigh<2) { if(UpBuffer[i] > 0) { DrawFractalOHLCHigh(i, "M", High[i]); DrawFractalOHLCHigh(i, "N", Low[i]); DrawFractalOHLCHigh(i, "O", Open[i]); DrawFractalOHLCHigh(i, "P", Close[i]); CountHigh = CountHigh + 1; } } //level_1 = iFractals(NULL,Fractal_Timeframe,1,iBarShift(NULL,Fractal_Timeframe,Time[i])); //UpBuffer[i] = level_1+DistanceFromCandle*10*Point; // Add a dot below the candle that is the low fractal DoBuffer[i] = iFractals(NULL,Fractal_Timeframe,2,iBarShift(NULL,Fractal_Timeframe,Time[i])); if(CountLow<2) { if(DoBuffer[i] > 0) { DrawFractalOHLCLow(i, "Q", High[i]); DrawFractalOHLCLow(i, "R", Low[i]); DrawFractalOHLCLow(i, "S", Open[i]); DrawFractalOHLCLow(i, "T", Close[i]); CountLow = CountLow + 1; } } //level_2 = DoBuffer[i] = iFractals(NULL,Fractal_Timeframe,2,iBarShift(NULL,Fractal_Timeframe,Time[i])); //DoBuffer[i] = level_2-DistanceFromCandle*10*Point; } //if(Extend_Line) if(Show_Validation_Candle) { for(i = 0; i < 30; i++) { if(UpBuffer[i] > 0) { level = UpBuffer[i]; for(c = i; c > 0; c--) { if((Open[c] < level && Close[c] > level) || (Open[c] > level && Close[c] < level)) break; if(Open[c] <= level && Close[c] <= level && Open[c-1] >= level && Close[c-1] >= level) break; if(Open[c] >= level && Close[c] >= level && Open[c-1] <= level && Close[c-1] <= level) break; } if(Extend_Line) { DrawLine ("H", i, c, level, Extend_Line_to_Background, Up_Fractal_Extend_Line_Color, Up_Fractal_Extend_Width, Up_Fractal_Extend_Style); } //if(Show_Validation_Candle) UpBuffer[i-2*dif] = level; i += dif; } } } if(Show_Validation_Candle) { for(i = 0; i < 30; i++) { if(DoBuffer[i] > 0) { level = DoBuffer[i]; for(c = i; c > 0; c--) { if((Open[c] < level && Close[c] > level) || (Open[c] > level && Close[c] < level)) break; if(Open[c] <= level && Close[c] <= level && Open[c-1] >= level && Close[c-1] >= level) break; if(Open[c] >= level && Close[c] >= level && Open[c-1] <= level && Close[c-1] <= level) break; } if(Extend_Line) { DrawLine ("L", i, c, level, Extend_Line_to_Background, Down_Fractal_Extend_Line_Color, Down_Fractal_Extend_Width, Down_Fractal_Extend_Style); } //if(Show_Validation_Candle) DoBuffer[i-2*dif] = level; i += dif; } } } } else { Comment("Invalid timeframe selected. Change timeframe to D1."); } return(0); } //+------------------------------------------------------------------+ //| DRAW LINE | //+------------------------------------------------------------------+ void DrawLine (string dir, int i, int c, double lev, bool back, color col, int width, int style) { ObjectCreate("MTF_Fractal_"+dir+i,OBJ_TREND,0,0,0,0,0); ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_TIME1,iTime(Symbol(),Period(),i)); ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_PRICE1,lev); ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_TIME2,iTime(Symbol(),Period(),c)); ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_PRICE2,lev); ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_RAY,0); ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_BACK,back); ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_COLOR,col); ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_WIDTH,width); ObjectSet("MTF_Fractal_"+dir+i,OBJPROP_STYLE,style); } void DrawFractalOHLCHigh(int i, string dir, double value) { string strObjectNameHigh = "Example line"+dir+i; ObjectCreate(strObjectNameHigh, OBJ_TREND, 0, Time[i-10], value, Time[i+10], value); ObjectSet(strObjectNameHigh, OBJPROP_RAY, false); } void DrawFractalOHLCLow(int i, string dir, double value) { string strObjectNameLow = "Example line"+dir+i; [highlight=#FFDD00]ObjectCreate(strObjectNameLow, OBJ_TREND, 0, Time[i-5], value, Time[i+5], value);[/highlight] ObjectSet(strObjectNameLow, OBJPROP_RAY, false); }
I know i have to use ArrayResize, but i cannot find out to put it in the code correctly so it removes the array out of range error.
This is how it looks like when the number is i+4
And when the number is changed to i+5 it looks like this
Blindly following others will make you blind!