Array out of range errors MT4
-
Started to get array errors after the hack incident. Out of array points to:
datetime new_value = time[0];and from
else if (mode=="balance") {size=(value/100)AccountBalance()/(LotSizeTickValue);}''
in the //-- error check
// "Once per bar" model
template<typename T1,typename T2,typename T3>
class MDL_OncePerBar: public BlockCalls
{
public: /* Input Parameters /
T1 Symbol;
T2 Period;
T3 PassMaxTimes;
/ Static Parameters */
string tokens[];
int passes[];
datetime old_values[];
datetime time[];
virtual void callback(int r) {return;}public: /* Constructor */ MDL_OncePerBar() { Symbol = (string)CurrentSymbol(); Period = (ENUM_TIMEFRAMES)CurrentTimeframe(); PassMaxTimes = (int)1; } public: /* The main method */ virtual void _execute_() { // this is static for speed reasons bool next = false; string token = Symbol + IntegerToString(Period); int index = ArraySearch(tokens, token); if (index == -1) { index = ArraySize(tokens); ArrayResize(tokens, index + 1); ArrayResize(old_values, index + 1); ArrayResize(passes, index + 1); tokens[index] = token; passes[index] = 0; old_values[index] = 0; } if (PassMaxTimes > 0) { CopyTime(Symbol, Period, 1, 1, time); datetime new_value = time[0]; if (new_value > old_values[index]) { passes[index]++; if (passes[index] >= PassMaxTimes) { old_values[index] = new_value; passes[index] = 0; } next = true; } } if (next) {_callback_(1);} else {_callback_(0);} }};
bool DeleteOrder(int ticket, color arrowcolor=clrNONE)
{
bool success=false;
if (!OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) {return(false);}while(true)
{
//-- wait if needed -----------------------------------------------
WaitTradeContextIfBusy();
//-- delete -------------------------------------------------------
success=OrderDelete(ticket,arrowcolor);
if (success==true) {
if (USE_VIRTUAL_STOPS) {
VirtualStopsDriver("clear",ticket);
}
OnTrade();
return(true);
}
//-- error check --------------------------------------------------
int erraction=CheckForTradingError(GetLastError(), "Deleting order #"+(string)ticket+" error");
switch(erraction)
{
case 0: break; // no error
case 1: continue; // overcomable error
case 2: break; // fatal error
}
break;
}
return(false);
}void DrawSpreadInfo()
{
static bool allow_draw = true;
if (allow_draw==false) {return;}
if (MQLInfoInteger(MQL_TESTER) && !MQLInfoInteger(MQL_VISUAL_MODE)) {allow_draw=false;} // Allowed to draw only once in testing modestatic bool passed = false;
static double max_spread = 0;
static double min_spread = EMPTY_VALUE;
static double avg_spread = 0;
static double avg_add = 0;
static double avg_cnt = 0;double custom_point = CustomPoint(Symbol());
double current_spread = 0;
if (custom_point > 0) {
current_spread = (SymbolInfoDouble(Symbol(),SYMBOL_ASK)-SymbolInfoDouble(Symbol(),SYMBOL_BID))/custom_point;
}
if (current_spread > max_spread) {max_spread = current_spread;}
if (current_spread < min_spread) {min_spread = current_spread;}avg_cnt++;
avg_add = avg_add + current_spread;
avg_spread = avg_add / avg_cnt;int x=0; int y=0;
string name;// create objects
if (passed == false)
{
passed=true;name="fxd_spread_current_label"; if (ObjectFind(0, name)==-1) { ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x+1); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y+1); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 18); ObjectSetInteger(0, name, OBJPROP_COLOR, clrDarkOrange); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetString(0, name, OBJPROP_TEXT, "Spread:"); } name="fxd_spread_max_label"; if (ObjectFind(0, name)==-1) { ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x+148); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y+17); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 7); ObjectSetInteger(0, name, OBJPROP_COLOR, clrOrangeRed); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetString(0, name, OBJPROP_TEXT, "max:"); } name="fxd_spread_avg_label"; if (ObjectFind(0, name)==-1) { ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x+148); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y+9); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 7); ObjectSetInteger(0, name, OBJPROP_COLOR, clrDarkOrange); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetString(0, name, OBJPROP_TEXT, "avg:"); } name="fxd_spread_min_label"; if (ObjectFind(0, name)==-1) { ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x+148); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y+1); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 7); ObjectSetInteger(0, name, OBJPROP_COLOR, clrGold); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetString(0, name, OBJPROP_TEXT, "min:"); } name="fxd_spread_current"; if (ObjectFind(0, name)==-1) { ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x+93); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y+1); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 18); ObjectSetInteger(0, name, OBJPROP_COLOR, clrDarkOrange); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetString(0, name, OBJPROP_TEXT, "0"); } name="fxd_spread_max"; if (ObjectFind(0, name)==-1) { ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x+173); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y+17); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 7); ObjectSetInteger(0, name, OBJPROP_COLOR, clrOrangeRed); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetString(0, name, OBJPROP_TEXT, "0"); } name="fxd_spread_avg"; if (ObjectFind(0, name)==-1) { ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x+173); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y+9); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 7); ObjectSetInteger(0, name, OBJPROP_COLOR, clrDarkOrange); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetString(0, name, OBJPROP_TEXT, "0"); } name="fxd_spread_min"; if (ObjectFind(0, name)==-1) { ObjectCreate(0, name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0, name, OBJPROP_XDISTANCE, x+173); ObjectSetInteger(0, name, OBJPROP_YDISTANCE, y+1); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0, name, OBJPROP_FONTSIZE, 7); ObjectSetInteger(0, name, OBJPROP_COLOR, clrGold); ObjectSetString(0, name, OBJPROP_FONT, "Arial"); ObjectSetString(0, name, OBJPROP_TEXT, "0"); }}
ObjectSetString(0, "fxd_spread_current", OBJPROP_TEXT, DoubleToStr(current_spread,2));
ObjectSetString(0, "fxd_spread_max", OBJPROP_TEXT, DoubleToStr(max_spread,2));
ObjectSetString(0, "fxd_spread_avg", OBJPROP_TEXT, DoubleToStr(avg_spread,2));
ObjectSetString(0, "fxd_spread_min", OBJPROP_TEXT, DoubleToStr(min_spread,2));
}string DrawStatus(string text="")
{
static string memory;
if (text=="") {
return(memory);
}static bool passed = false;
int x=210; int y=0;
string name;//-- draw the objects once
if (passed == false)
{
passed = true;
name="fxd_status_title";
ObjectCreate(0,name, OBJ_LABEL, 0, 0, 0);
ObjectSetInteger(0,name, OBJPROP_BACK, false);
ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER);
ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER);
ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
ObjectSetInteger(0,name, OBJPROP_XDISTANCE, x);
ObjectSetInteger(0,name, OBJPROP_YDISTANCE, y+17);
ObjectSetString(0,name, OBJPROP_TEXT, "Status");
ObjectSetString(0,name, OBJPROP_FONT, "Arial");
ObjectSetInteger(0,name, OBJPROP_FONTSIZE, 7);
ObjectSetInteger(0,name, OBJPROP_COLOR, clrGray);name="fxd_status_text"; ObjectCreate(0,name, OBJ_LABEL, 0, 0, 0); ObjectSetInteger(0,name, OBJPROP_BACK, false); ObjectSetInteger(0, name, OBJPROP_CORNER, CORNER_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER); ObjectSetInteger(0, name, OBJPROP_HIDDEN, true); ObjectSetInteger(0,name, OBJPROP_XDISTANCE, x+2); ObjectSetInteger(0,name, OBJPROP_YDISTANCE, y+1); ObjectSetString(0,name, OBJPROP_FONT, "Arial"); ObjectSetInteger(0,name, OBJPROP_FONTSIZE, 12); ObjectSetInteger(0,name, OBJPROP_COLOR, clrAqua);}
//-- update the text when needed
if (text != memory) {
memory=text;
ObjectSetString(0,"fxd_status_text", OBJPROP_TEXT, text);
}return(text);
}double DynamicLots(string symbol, string mode="balance", double value=0, double sl=0, string align="align", double RJFR_initial_lots=0)
{
double size=0;
double LotStep=MarketInfo(symbol,MODE_LOTSTEP);
double LotSize=MarketInfo(symbol,MODE_LOTSIZE);
double MinLots=MarketInfo(symbol,MODE_MINLOT);
double MaxLots=MarketInfo(symbol,MODE_MAXLOT);
double TickValue=MarketInfo(symbol,MODE_TICKVALUE);
double point=MarketInfo(symbol,MODE_POINT);
double ticksize=MarketInfo(symbol,MODE_TICKSIZE);
double margin_required=MarketInfo(symbol,MODE_MARGINREQUIRED);if (mode=="fixed" || mode=="lots") {size=value;}
else if (mode=="block-equity") {size=(value/100)*AccountEquity()/margin_required;}
else if (mode=="block-balance") {size=(value/100)*AccountBalance()/margin_required;}
else if (mode=="block-freemargin") {size=(value/100)*AccountFreeMargin()/margin_required;}
else if (mode=="equity") {size=(value/100)AccountEquity()/(LotSizeTickValue);}
else if (mode=="balance") {size=(value/100)AccountBalance()/(LotSizeTickValue);}
else if (mode=="freemargin") {size=(value/100)AccountFreeMargin()/(LotSizeTickValue);}
else if (mode=="equityRisk") {size=((value/100)AccountEquity())/(sl((TickValue/ticksize)*point)*PipValue(symbol));}
else if (mode=="balanceRisk") {size=((value/100)AccountBalance())/(sl((TickValue/ticksize)*point)*PipValue(symbol));}
else if (mode=="freemarginRisk") {size=((value/100)AccountFreeMargin())/(sl((TickValue/ticksize)*point)PipValue(symbol));}
else if (mode=="fixedRisk") {size=(value)/(sl((TickValue/ticksize)*point)*PipValue(symbol));}
else if (mode=="fixedRatio" || mode=="RJFR") {///// -
@afxts Can you please share a screenshot of the error.