EA to close orders matching the same comment

Almo

Active Trader
Sep 20, 2015
13
0
37
43
Bahrain
Hello
i need Expert advisor to close all orders that matching the same comment
i have scripts but i need EA
Thank you
 

Almo

Active Trader
Sep 20, 2015
13
0
37
43
Bahrain
Why do you need an EA if you have a script? What would EA do? Just wait until an order with a matching comment appears and close it?
i need it because i am working with many pairs and using hedge , using EA more easy for my trading style
Thank you
 

hayseed

Master Trader
Jul 27, 2010
1,014
259
149
usa
if i want to use script i have to watch the market while if i use EA i do not need to watch market
//----

hey almo.... that part we get.....

but you must explain in far greater detail.... what triggers the order closure function....such as, is it a dollar value target, hour of the day, indicator crossing a signal line, is a total account profit or loss level..... are you under fifo rules.... is deleting pending orders involved.... and many more type things must be clearly laid out...... very clearly.....

otherwise we are just guessing..... guessing and coding will not end well......h
//-----

MQL4:
void CloseMagic(int MagicNumber,string comment)
  {
  RefreshRates();
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    if(OrderMagicNumber() != MagicNumber) continue;
    if(OrderComment()     != comment)     continue;
    RefreshRates();
    int type   = OrderType();
 
    bool result = false;
 
    switch(type)
    {
 
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;
 
 
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
 
    }
 
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }
    }
 
    }
 

Enivid

Administrator
Staff member
Nov 30, 2008
18,535
1,355
144
Odesa
www.earnforex.com
if i want to use script i have to watch the market while if i use EA i do not need to watch market
The problem is that if you set up an EA to close all orders with a given comment, the affected order will be closed immediately upon opening. So, the EA will be just preventing any trades with a given comment from actually staying for more than 1 second. I don't think that's what you need.
 

Almo

Active Trader
Sep 20, 2015
13
0
37
43
Bahrain
The problem is that if you set up an EA to close all orders with a given comment, the affected order will be closed immediately upon opening. So, the EA will be just preventing any trades with a given comment from actually staying for more than 1 second. I don't think that's what you need.
you are right but i will write the profit which i want to close all orders has the same comment
for example :
if i have three trades with comment #1 i will set to close them if the total profit become 300 $ as example
so in this case i will not need to stay online and waiting
 

Almo

Active Trader
Sep 20, 2015
13
0
37
43
Bahrain
//----

hey almo.... that part we get.....

but you must explain in far greater detail.... what triggers the order closure function....such as, is it a dollar value target, hour of the day, indicator crossing a signal line, is a total account profit or loss level..... are you under fifo rules.... is deleting pending orders involved.... and many more type things must be clearly laid out...... very clearly.....

otherwise we are just guessing..... guessing and coding will not end well......h
//-----

MQL4:
void CloseMagic(int MagicNumber,string comment)
  {
  RefreshRates();
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    OrderSelect(i, SELECT_BY_POS);
    if(OrderMagicNumber() != MagicNumber) continue;
    if(OrderComment()     != comment)     continue;
    RefreshRates();
    int type   = OrderType();
 
    bool result = false;
 
    switch(type)
    {
 
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
                          break;
 
 
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
 
    }
 
    if(result == false)
    {
      Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }
    }
 
    }
Hello
yes it is US Dollar target
no need to total account profit or loss level
no i am not in in fifo rules
not important the pending orders
The EA will close any orders with same comments and the user will set the profit value for each comment in the EA inputs
Thank you