Attached is part of an Expert Advisor I have been working on to test a trading strategy, but I’m having some trouble with MQL4’s limitations when dealing with arrays. What I want to so is use an array to store information regarding the positions I have open and to also redefine the values of some the elements. What I want to implement is a series of three functions that first, reconstructs the array of open orders. This would be done after every position opening or closing. The next sequential function is supposed to sort the open orders according to their respective opening prices. The last sequential function is to then redefine the stop loss and take profit values for the open trades. The redefined values for the stop losses and take profit values are to be based upon the opening price of the first order. I wanted the EA to execute the stop loss and take profit values silently. I am aware there are risks to setting up an EA this way and I wish to proceed with doing this.
double OrderStack[7][9]; // Order information array
if((OrderStack[TotalOrders + 1][1] < Bid) && (TotalOrders < 2)) // Opens new Sell position at more favorable price
{ //3
TicketNum = OrderSend(Symbol(),OP_SELL,LotSize2,Bid,3,0,0,CommentString,0,0,Red); //Open the order.
if(TicketNum<0)
{ //4
Print("Error Opening Trade: Error Code = ",GetLastError(),", Lotsize: ",Lots);
} //3
else
{ //4
if(OrderSelect(TicketNum,SELECT_BY_TICKET))
{ //5
Print("Sell Order Opened at Price: ",OrderOpenPrice());
OrderFlagS = 1; // Used by main EA function for comments
Terminal(); // Call to function that creates open order data array
} //4
} //3
} //2
} //1
} //0
//--------------------------------------------------------------------
// Terminal.mqh
//------------------------------------------------------------------------------ 1 --
// Order accounting function
// Global variables:
// OrderStack[7][9]
// The latest known orders array
// 1st index = order number
// [][0] not defined
// [][1] order open price (abs. price value)
// [][2] StopLoss of the order (abs. price value)
// [][3] TakeProfit of the order (abs. price value)
// [][4] order number
// [][5] order volume in lots (abs. price value)
// [][6] order type 0=B,1=S,2=BL,3=SL,4=BS,5=SS
// [][7] order magic number
// [][8] 0/1 comment availability
// Mas_Tip[6]
// Array of the amount of orders of all types
// [] order type: 0=B,1=S,2=BL,3=SL,4=BS,5=SS
//------------------------------------------------------------------------------ 2 --
double Terminal()
{ //1
int Mas_Tip[6];
int Qnt = 0;
int i;
// Orders counter
//------------------------------------------------------------------------------ 3 --
ArrayInitialize(OrderStack, 0.0); // Zeroize the array
ArrayInitialize(Mas_Tip, 0); // Zeroize the array
//------------------------------------------------------------------------------ 4 --
for(i = 0; i < OrdersTotal(); i++) // For market and pending orders
{ //2
if((OrderSelect(i,SELECT_BY_POS) == true) && (OrderSymbol() == Symbol()))
//If there is the next one.. and our currency pair
{ //3
//--------------------------------------------------------------------- 5 --
Qnt++; // Amount of orders
OrderStack[Qnt][1] = OrderOpenPrice(); // Order open price
OrderStack[Qnt][2] = OrderStopLoss(); // SL price
OrderStack[Qnt][3] = OrderTakeProfit(); // TP price
OrderStack[Qnt][4] = OrderTicket(); // Order number
OrderStack[Qnt][5] = OrderLots(); // Amount of lots
Mas_Tip[OrderType()]++; // Amount of orders of the type
OrderStack[Qnt][6] = OrderType(); // Order type
OrderStack[Qnt][7] = OrderMagicNumber(); // Magic number
if (OrderComment()=="")
{ //4
OrderStack[Qnt][8] = 0; // If there is no comment
} //3
else
{ //4
OrderStack[Qnt][8] = 1; // If there is a comment
} //3
} //2
//--------------------------------------------------------------------- 6 --
OrderStack[0][0] = Qnt; // Amount of orders
//------------------------------------------------------------------------------ 7 --
FillStack(OrderStack, Trend_Up); // Call to function that sorts open trade data based on trend direction and initial trade price
return(0);
} //1
} //0
//------------------------------------------------------------------------------ 8 --
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Existing Orders Sorting Function
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
double SortStack(double OrderStack[7][9], bool Trend_Up) // Function to sort the reconstructed order array in
// order based on initial order open price
{ //1
int i, TotalOrders;
TotalOrders = OrderStack[0][0];
if(Trend_Up == true)
{ //2
ArraySort(OrderStack[TotalOrders][1], TotalOrders,1,MODE_DECEND);
} //1
if(Trend_Up == false)
{ //2
ArraySort(OrderStack[TotalOrders][1], TotalOrders,1,MODE_ASCEND);
} //1
FillStack(OrderStack, Trend_Up); // Call to function that defines trade parameters for adverse
// market trades into order array
return(0);
} //0
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Record Adverse Market Trade Parameters Function
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
double FillStack(double OrderStack[7][9], bool Trend_Up) // Defines Adverse Market Trade Parameters
// based on initial order from Bollinger band piercing
{ //1
double PriceIn;
PriceIn = OrderStack[1][1];
{ //2
if(Trend_Up == true)
{ //3
OrderStack[1][2] = PriceIn - StopLossVal; // Column 2, Stop Loss Entry Column
OrderStack[1][3] = PriceIn + 0.0030; // Column 3, Take Profit Entry Column
OrderStack[2][1] = PriceIn - 0.0010; // Column 1, Average Down Market Order Buy Price
OrderStack[2][3] = PriceIn - 0.0005; // Column 3, Take Profit for Avg Down Market Entry
} //2
if(Trend_Up == false)
{ //3
OrderStack[1][2] = PriceIn + StopLossVal; // Column 2, Stop Loss Entry Column
OrderStack[1][3] = PriceIn - 0.0030; // Column 3, Take Profit Entry Column
OrderStack[2][1] = PriceIn + 0.0010; // Column 1, Average Down Market Order Buy Price
OrderStack[2][3] = PriceIn + 0.0005; // Column 3, Take Profit for Avg Down Market Entry
} //2
} //1
return(0);
} //0
The problem I am having with implementing this EA is that the compiler gives me error messages about “array item cannot be assigned” for the code in my FillStack() function. What is puzzling is that it look like that is exactly what is being done in the Terminal() function above my FillStack() function. Anyway, I’m hoping that one of the experienced programmers can give me a fix for this particular bug.
Its hard to believe that with all the similarities that MQL4 has with C++ that the ability to assign values to array elements like C++ would not be present in MQL4 also. Thank you for the help.
double OrderStack[7][9]; // Order information array
if((OrderStack[TotalOrders + 1][1] < Bid) && (TotalOrders < 2)) // Opens new Sell position at more favorable price
{ //3
TicketNum = OrderSend(Symbol(),OP_SELL,LotSize2,Bid,3,0,0,CommentString,0,0,Red); //Open the order.
if(TicketNum<0)
{ //4
Print("Error Opening Trade: Error Code = ",GetLastError(),", Lotsize: ",Lots);
} //3
else
{ //4
if(OrderSelect(TicketNum,SELECT_BY_TICKET))
{ //5
Print("Sell Order Opened at Price: ",OrderOpenPrice());
OrderFlagS = 1; // Used by main EA function for comments
Terminal(); // Call to function that creates open order data array
} //4
} //3
} //2
} //1
} //0
//--------------------------------------------------------------------
// Terminal.mqh
//------------------------------------------------------------------------------ 1 --
// Order accounting function
// Global variables:
// OrderStack[7][9]
// The latest known orders array
// 1st index = order number
// [][0] not defined
// [][1] order open price (abs. price value)
// [][2] StopLoss of the order (abs. price value)
// [][3] TakeProfit of the order (abs. price value)
// [][4] order number
// [][5] order volume in lots (abs. price value)
// [][6] order type 0=B,1=S,2=BL,3=SL,4=BS,5=SS
// [][7] order magic number
// [][8] 0/1 comment availability
// Mas_Tip[6]
// Array of the amount of orders of all types
// [] order type: 0=B,1=S,2=BL,3=SL,4=BS,5=SS
//------------------------------------------------------------------------------ 2 --
double Terminal()
{ //1
int Mas_Tip[6];
int Qnt = 0;
int i;
// Orders counter
//------------------------------------------------------------------------------ 3 --
ArrayInitialize(OrderStack, 0.0); // Zeroize the array
ArrayInitialize(Mas_Tip, 0); // Zeroize the array
//------------------------------------------------------------------------------ 4 --
for(i = 0; i < OrdersTotal(); i++) // For market and pending orders
{ //2
if((OrderSelect(i,SELECT_BY_POS) == true) && (OrderSymbol() == Symbol()))
//If there is the next one.. and our currency pair
{ //3
//--------------------------------------------------------------------- 5 --
Qnt++; // Amount of orders
OrderStack[Qnt][1] = OrderOpenPrice(); // Order open price
OrderStack[Qnt][2] = OrderStopLoss(); // SL price
OrderStack[Qnt][3] = OrderTakeProfit(); // TP price
OrderStack[Qnt][4] = OrderTicket(); // Order number
OrderStack[Qnt][5] = OrderLots(); // Amount of lots
Mas_Tip[OrderType()]++; // Amount of orders of the type
OrderStack[Qnt][6] = OrderType(); // Order type
OrderStack[Qnt][7] = OrderMagicNumber(); // Magic number
if (OrderComment()=="")
{ //4
OrderStack[Qnt][8] = 0; // If there is no comment
} //3
else
{ //4
OrderStack[Qnt][8] = 1; // If there is a comment
} //3
} //2
//--------------------------------------------------------------------- 6 --
OrderStack[0][0] = Qnt; // Amount of orders
//------------------------------------------------------------------------------ 7 --
FillStack(OrderStack, Trend_Up); // Call to function that sorts open trade data based on trend direction and initial trade price
return(0);
} //1
} //0
//------------------------------------------------------------------------------ 8 --
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Existing Orders Sorting Function
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
double SortStack(double OrderStack[7][9], bool Trend_Up) // Function to sort the reconstructed order array in
// order based on initial order open price
{ //1
int i, TotalOrders;
TotalOrders = OrderStack[0][0];
if(Trend_Up == true)
{ //2
ArraySort(OrderStack[TotalOrders][1], TotalOrders,1,MODE_DECEND);
} //1
if(Trend_Up == false)
{ //2
ArraySort(OrderStack[TotalOrders][1], TotalOrders,1,MODE_ASCEND);
} //1
FillStack(OrderStack, Trend_Up); // Call to function that defines trade parameters for adverse
// market trades into order array
return(0);
} //0
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Record Adverse Market Trade Parameters Function
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
double FillStack(double OrderStack[7][9], bool Trend_Up) // Defines Adverse Market Trade Parameters
// based on initial order from Bollinger band piercing
{ //1
double PriceIn;
PriceIn = OrderStack[1][1];
{ //2
if(Trend_Up == true)
{ //3
OrderStack[1][2] = PriceIn - StopLossVal; // Column 2, Stop Loss Entry Column
OrderStack[1][3] = PriceIn + 0.0030; // Column 3, Take Profit Entry Column
OrderStack[2][1] = PriceIn - 0.0010; // Column 1, Average Down Market Order Buy Price
OrderStack[2][3] = PriceIn - 0.0005; // Column 3, Take Profit for Avg Down Market Entry
} //2
if(Trend_Up == false)
{ //3
OrderStack[1][2] = PriceIn + StopLossVal; // Column 2, Stop Loss Entry Column
OrderStack[1][3] = PriceIn - 0.0030; // Column 3, Take Profit Entry Column
OrderStack[2][1] = PriceIn + 0.0010; // Column 1, Average Down Market Order Buy Price
OrderStack[2][3] = PriceIn + 0.0005; // Column 3, Take Profit for Avg Down Market Entry
} //2
} //1
return(0);
} //0
The problem I am having with implementing this EA is that the compiler gives me error messages about “array item cannot be assigned” for the code in my FillStack() function. What is puzzling is that it look like that is exactly what is being done in the Terminal() function above my FillStack() function. Anyway, I’m hoping that one of the experienced programmers can give me a fix for this particular bug.
Its hard to believe that with all the similarities that MQL4 has with C++ that the ability to assign values to array elements like C++ would not be present in MQL4 also. Thank you for the help.