Close all trades if price is reached

 

Hi,

Do you know of any EA that closes all open trades and pending orders if a predetermined price is reached ?

Thanks.

 
mt4 or mt5?
 
Ahmet Metin Yilmaz:
mt4 or mt5?
At this moment I accept any of them...
 

this close at profit not a predefined price 

you can change its code or use in this way


https://www.mql5.com/en/code/10597

Close at Profit
Close at Profit
  • www.mql5.com
CloseAtProfit is an EA that I use only to close orders after defined profit, or loss, you can define if to close only trades on current symbol or to close all orders on all symbols, to close only opened trades or also delete pending orders. I am using it in live trading. I have closed with it more than 200 trades at once, without any problem...
 
Ahmet Metin Yilmaz:

this close at profit not a predefined price 

you can change its code or use in this way


https://www.mql5.com/en/code/10597

This might work! I have to do some testing, but thank you, it's a good bet.
 
FabioLinhares:
This might work! I have to do some testing, but thank you, it's a good bet.

youre welcome

 

I use MT5.  Hope this code is usefull.. 

#include <Trade\Trade.mqh> 

CTrade   Trd;


input  double TargetPrice = 0;   //fill with the price target. 


void  OnTick() {

    double v_price =  SymbolInfoDouble(_Symbol,SYMBOL_LAST);

    if ( v_price == TargetPrice ) {
    
      Trd.PositionClose(_Symbol);      
      CancelOrders();     
    }  
}

void CancelOrders() {
  
  CTrade mytrade;
  int o_total = OrdersTotal();

  for(int j=o_total-1; j>=0; j--) {

    ulong o_ticket = OrderGetTicket(j);
    string sym     = OrderGetString(ORDER_SYMBOL);

    if( o_ticket!=0 && sym == _Symbol ) {   // delete the pending order

      mytrade.OrderDelete(o_ticket);

      Print(_Symbol," Pending order ",o_ticket," deleted sucessfully!");
    }
  }
}
 
Fernando Lima:

I use MT5.  Hope this code is usefull.. 

I actually need to close all orders if 1 of 2 prices are reached. Can you add this to the code ?