Check profit and close all orders automatically

 

Hello everyone, I am wanting to make an indicator or EA (without success) that check the profit and if you reach the profit you want it to close all the orders, I have a script that close manually and I want to automate it

//+------------------------------------------------------------------+
//|                                                     CloseAll.mq4 |
//|                                 Copyright © 2015, FlashTrade JFL |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2015, FlashTrade JFL"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   for(int x=OrdersTotal()-1;x>=0;x--)
     {
      if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderType()==OP_BUY || OrderType()==OP_SELL)
      if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE)){ Alert("fail deleting trade order"); }
     }
  }
//+------------------------------------------------------------------+

to return the profit value according to the documentation MQL4 uses AccountProfit()

I tried to make the following logic, but I'm not getting success, someone could help

   if(AccountProfit() > 10.00 || AccountProfit() < -10.00) {
       for(int x=OrdersTotal()-1;x>=0;x--){
          if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES)) continue;
          if(OrderType()==OP_BUY || OrderType()==OP_SELL)
          if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,CLR_NONE)){ Alert("fail deleting trade order"); }
            }
      }

Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
Descubra novos recursos para o MetaTrader 5 com a comunidade e os serviços MQL5
  • www.mql5.com
Tire dúvidas sobre análise técnica, fale com outros traders sobre sistemas de negociação e melhore suas habilidades de programação de estratégias em linguagem MQL5. Participe do fórum, compartilhe ideias com traders de todo o mundo e ajude com feedback os novatos — nossa comunidade está crescendo junto com você. Volume do Mercado Bom dia...
 
Marcos Luiz :

Hello everyone, I am wanting to make an indicator or EA (without success) that check the profit and if you reach the profit you want it to close the orders, I have a script that close manually and I want to automate it

to MQL4 uses AccountProfit ()

I tried to make the following logic, but I'm not getting any success, someone could help


What do you mean "without success?"
You could try OrderCloseTime () = 0 to confirm an open trade. 
Check the expert journal, or write test comments after each line, to see how far your code is getting?
 
Marcos Luiz:

Hello everyone, I am wanting to make an indicator or EA (without success) that check the profit and if you reach the profit you want it to close all the orders, I have a script that close manually and I want to automate it

to return the profit value according to the documentation MQL4 uses AccountProfit()

I tried to make the following logic, but I'm not getting success, someone could help




if(AccountProfit()>10.00 || AccountProfit()<-10.00) 
  {
   for(int x=OrdersTotal()-1;x>=0;x--)
     {
      if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES)) continue;
      switch(OrderType())
        {
         case OP_BUY    : if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,CLR_NONE)){ Alert("fail deleting trade order"); }; break;
         case OP_SELL   : if(!OrderClose(OrderTicket(),OrderLots(),Ask,3,CLR_NONE)){ Alert("fail deleting trade order"); }; break;
         default        : break;
        }
     }
  }
Reason: