Storing order parameters in an array and placing said orders as pending orders later on

 

Hello everyone!

This is my first post here so please don't roast me too hard if I'm going against any conventions here. I'll try my best.


I'm currently trying to place pending orders using MqlTradeRequest. Since I can't seem to put MqlTradeRequest objects in an array I created a struct mirroring the needed MqlTradeRequest parameters which I can store in an array.

The function DefineOrders() is called in OnInit(). I also tested to call it after initialization to no avail.

Error code is 4756 and Journal Message reads: "failed sell 0.01 USDCHF at 0.95000 (0.00000) sl: 2.00000 tp: 0.94500 [Invalid request]" (once for every iteration over the array)

Since the error message displays all the correct values I'm fairly certain that all values are passed correctly from the array to the request object.

The following code snippet is just meant as a proof of concept and the values are arbitrary.


void DefineOrders()
{
   struct OrderRequests
   {
       ENUM_TRADE_REQUEST_ACTIONS   action;
       ENUM_ORDER_TYPE              type;
       ENUM_ORDER_TYPE_FILLING      type_filling;
       string                       symbol;
       double                       volume;
       double                       price;
       double                       tp;
       double                       sl;
       ulong                        deviation;
       string                       comment;
   };
   OrderRequests OrderArray[5];
   double prices[5] = {0.970,0.965,0.960,0.955,0.950};
   MqlTradeRequest request;
   MqlTradeResult result;
   
   for(int i=0;i<=4;i++)   //fill array
   {
      OrderArray[i].action       = TRADE_ACTION_PENDING;
      OrderArray[i].type         = ORDER_TYPE_SELL;
      OrderArray[i].type_filling = ORDER_FILLING_FOK;
      OrderArray[i].symbol       = _Symbol;
      OrderArray[i].volume       = 0.01;
      OrderArray[i].tp           = 0.945;
      OrderArray[i].sl           = 2;
      OrderArray[i].price        = prices[i];
      OrderArray[i].deviation    = 20;
      OrderArray[i].comment      = "ordercomment";
   }
   
   for(int i=0;i<=4;i++)   //send orders with values from array
   {
      request.action       = OrderArray[i].action;
      request.type         = OrderArray[i].type;
      request.type_filling = OrderArray[i].type_filling;
      request.symbol       = OrderArray[i].symbol;
      request.volume       = OrderArray[i].volume;
      request.tp           = OrderArray[i].tp;
      request.sl           = OrderArray[i].sl;
      request.price        = OrderArray[i].price;
      request.deviation    = OrderArray[i].deviation;
      request.comment      = OrderArray[i].comment;
      
      if(!OrderSend(request,result)){Comment(GetLastError());}
   }
}

I feel like I'm overlooking something completely obvious and will bang my head against the wall once pointed out. Thanks for your time!

 
Your topic has been moved to the section: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
MQL5 forum: Expert Advisors and Automated Trading
  • www.mql5.com
How to create an Expert Advisor (a trading robot) for Forex trading