How To Implement Bulk Operations?

 

Hello.

I am an extreme newbie at this with no formal programming knowledge, so I apologize in advance.

I have created a very simple EA. The idea is to have two buttons that will make use of the 'Bulk Operations' commands for "Close All Positions" and "Delete All Orders". The reasoning for this is because the old methods that I have found are very slow and the Bulk Operations are very fast.

Unfortunately, I cannot understand how to implement these into my script. According to the MQL5 database, I should be using CExpert since it allows access to "CloseAll" and "DeleteOrders", which I assume is what I want for my buttons. Can someone guide me with this? I would really appreciate it. I have included images for reference. Thank you so much.

#include <Expert\Expert.mqh>

int OnInit(){

   createButton1("CLOSE","CLOSE",5,5,60,25);
   createButton2("CANCEL","CANCEL",5,32,60,25);
   ChartRedraw();
   
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){

   ObjectDelete(0,"CLOSE");
   ObjectDelete(0,"CANCEL");
   
}

void OnTick(){




         ObjectSetInteger(0,"CLOSE",OBJPROP_STATE,false);
         ObjectSetInteger(0,"CANCEL",OBJPROP_STATE,false);


}

bool createButton1(string objName, string text, int x, int y, int width, int height){
   if(ObjectCreate(0,objName,OBJ_BUTTON,0,0,0)){
      ObjectSetInteger(0,objName,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(0,objName,OBJPROP_YDISTANCE,y);
      ObjectSetInteger(0,objName,OBJPROP_XSIZE,width);
      ObjectSetInteger(0,objName,OBJPROP_YSIZE,height);
      ObjectSetInteger(0,objName,OBJPROP_CORNER,CORNER_LEFT_UPPER);
      ObjectSetString(0,objName,OBJPROP_TEXT,text);
      ObjectSetInteger(0,objName,OBJPROP_FONTSIZE,12);
      ObjectSetString(0,objName,OBJPROP_FONT,"Impact");
      ObjectSetInteger(0,objName,OBJPROP_COLOR,clrWhite);
      ObjectSetInteger(0,objName,OBJPROP_BGCOLOR,clrSlateGray);
      ObjectSetInteger(0,objName,OBJPROP_BORDER_COLOR,clrBlack);
      ObjectSetInteger(0,objName,OBJPROP_BACK,false);
      return(true);
   }
   return false;
   
}

bool createButton2(string objName, string text, int x, int y, int width, int height){
   if(ObjectCreate(0,objName,OBJ_BUTTON,0,0,0)){
      ObjectSetInteger(0,objName,OBJPROP_XDISTANCE,x);
      ObjectSetInteger(0,objName,OBJPROP_YDISTANCE,y);
      ObjectSetInteger(0,objName,OBJPROP_XSIZE,width);
      ObjectSetInteger(0,objName,OBJPROP_YSIZE,height);
      ObjectSetInteger(0,objName,OBJPROP_CORNER,CORNER_LEFT_UPPER);
      ObjectSetString(0,objName,OBJPROP_TEXT,text);
      ObjectSetInteger(0,objName,OBJPROP_FONTSIZE,12);
      ObjectSetString(0,objName,OBJPROP_FONT,"Impact");
      ObjectSetInteger(0,objName,OBJPROP_COLOR,clrWhite);
      ObjectSetInteger(0,objName,OBJPROP_BGCOLOR,clrDimGray);
      ObjectSetInteger(0,objName,OBJPROP_BORDER_COLOR,clrBlack);
      ObjectSetInteger(0,objName,OBJPROP_BACK,false);
      return(true);
   }
   return false;
}
Documentation on MQL5: Standard Library / Strategy Modules / Base classes for Expert Advisors / CExpert
Documentation on MQL5: Standard Library / Strategy Modules / Base classes for Expert Advisors / CExpert
  • www.mql5.com
CExpert - Base classes for Expert Advisors - Strategy Modules - Standard Library - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
1.png  36 kb
2.png  74 kb
3.png  46 kb
4.png  29 kb
5.png  41 kb
 

"Buik operations" are not available via MQL code. You can recreate an equivalente procedure using a loop and asynchronous mode.

Forum on trading, automated trading systems and testing trading strategies

Experts: Close All Trade at Once

Fernando Carreiro, 2022.10.28 03:25

No, not even MT5 can do it all at once. What it does is use asynchronous mode to make it faster and seem like it is a bulk operation all at once.

Forum on trading, automated trading systems and testing trading strategies

Do you trade or have you ever traded “Boom” and “Crash”?

Fernando Carreiro, 2022.10.06 12:05

Such functionality is usually dependant on the strategy and is interwoven in the code, so I don't have anything separate that I can share.

The documentation however, has sample code in the documentation for TRADE_ACTION_CLOSE_BY

And @fxsaber has publish a library in the CodeBase for asynchronous order processing...

"Asynchronous trading orders have a huge advantage - high speed when sent in bulk. However, the spreading of such orders is hampered by the inconvenience - order result data can only be seen in OnTradeTransaction making users to build an event model of their trading strategy if they want asynchrony. This is not always easy (EAs), and sometimes even impossible (scripts).

The library solves this problem. At any time, it provides access to all transactions in the trading terminal (full data of the appropriate OnTradeTransaction) since the application launch simplifying the process of making your programs asynchronous."

 
Fernando Carreiro #:

"Buik operations" are not available via MQL code. You can recreate an equivalente procedure using a loop and asynchronous mode.

Thank you for replying. I really appreciate it. I will take a look and see if I can figure out how to use the asynchronous method.

Do you happen to know why the bulk operations are not available?

Thanks.

 
NSAK3Y #: Thank you for replying. I really appreciate it. I will take a look and see if I can figure out how to use the asynchronous method. Do you happen to know why the bulk operations are not available? Thanks.

I suspect that the reason is because internally, that is exactly what it does—using asynchronous mode in a loop.

 
Fernando Carreiro #:

I suspect that the reason is because internally, that is exactly what it does—using asynchronous mode in a loop.

Thanks. I wonder about something though... I have an EA that I found online that uses the asynchronous method, but it is not open source, so I cannot say what else it does. However, and maybe this is just me, but when I use it, it does not seem to perform as well as the bulk operations within MT5. I suppose that there could be many reasons for this, though. Thanks, again.

 
NSAK3Y #: Thanks. I wonder about something though... I have an EA that I found online that uses the asynchronous method, but it is not open source, so I cannot say what else it does. However, and maybe this is just me, but when I use it, it does not seem to perform as well as the bulk operations within MT5. I suppose that there could be many reasons for this, though. Thanks, again.

If it's closed source, then how do you know if it uses asynchronous mode?

The answer is, it probably does NOT use asynchronous mode.

 
Because there is an interface with a boolean option for asynchronous mode.
 
NSAK3Y #: Because there is an interface with a boolean option for asynchronous mode.

Just because it supports asynchronous mode, does not mean that it is carrying out bulk operations. One does not imply the other.

 

Yes, I understand that. 

Thanks.

Reason: