HOW TO CLOSE all the remaining openorders of the same symbol when the first trade is closed ?

 

HOW TO CLOSE all the remaining openorders of the same symbol when the first trade is closed?

Here is the ordersend for the first trade with unique takeprofit and comment. OrderSend(Symbol(),0,b_lot,Ask,3,0,Ask+tp,"L"+1,magic,0,Blue);

i thought for a long time but could not find a way. best way i guess is to use the unique comment L1 with orderselect. the idea or logic is that if the trade with comment L1 is not found in the opentrades then all the other trades of the same symbol should be closed. trade one is closed because takeprofit is hit. the other trades cannot be assigned with the same takeprofit unfortunately. i could not write the code for how to do that. can anybody help me please. thanks

 

Not compiled or tested and no error checking

  
  string gvname="First"+Symbol()+(string)magic;
  
  int ticket = OrderSend(Symbol(),0,b_lot,Ask,3,0,Ask+tp,"L"+(string)1,magic,0,Blue);
  if(ticket>0)
     {
     GlobalVariableSet(gvname,ticket);
     GlobalVariablesFlush();     
     }
  //
  //
  //
  //
  if(OrderSelect((int)GlobalVariableGet(gvname),SELECT_BY_TICKET))
     {
     if(OrderCloseTime()!=0)
        {
        //CloseAllOrders
        }
     }
 
GumRai:

Not compiled or tested and no error checking

 



thanks gumrai, but its not working. ea opens too many trades at the same time
 

Other options ...

https://www.mql5.com/en/job

 
tsedawa:


thanks gumrai, but its not working. ea opens too many trades at the same time

Your query was

HOW TO CLOSE all the remaining openorders of the same symbol when the first trade is closed? 

 not

How to prevent the EA from opening too many orders at the same time. 

 
GumRai:

Your query was

HOW TO CLOSE all the remaining openorders of the same symbol when the first trade is closed? 

 not

How to prevent the EA from opening too many orders at the same time. 

 

 



i dont know why but when i remove your code ea works fine but when i put your code the ea sends multiple trades
 
tsedawa:


i dont know why but when i remove your code ea works fine but when i put your code the ea sends multiple trades


You didn't just copy and paste my example code straight into the EA did you?

It was an example. You need to combine it with your code. 

  string gvname="First"+Symbol()+(string)magic;
  
  //Your code to calculate whether to send and order and not to open a repeat order

  int ticket = Your OrderSend
  if(ticket>0)
     {
     GlobalVariableSet(gvname,ticket);
     GlobalVariablesFlush();     
     }
  //
  //
  //
  //
  if(OrderSelect((int)GlobalVariableGet(gvname),SELECT_BY_TICKET))
     {
     if(OrderCloseTime()!=0)
        {
        //Your code to CloseAllOrders
        }
     }