need help with close all order!

 

Hi. I have a code of close all order and I want, after close all, EA will stop for a certain period of time. someone help me?



    

  


void CloseAllOrder()

{

int total = OrdersTotal();

for(int i=total-1;i>=0;i--)

{

if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

{

if(OrderSymbol() == Symbol())

  {

   int type = OrderType();

bool result = false;

switch(type)

{

//Close opened long positions

case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );

break;

//Close opened short positions

case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );

break;

//Close pending orders

case OP_BUYLIMIT :

case OP_BUYSTOP :

case OP_SELLLIMIT :

case OP_SELLSTOP : result = OrderDelete( OrderTicket() );

}

  }

}

}

}

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Requests to execute trade operations are formalized as orders. Each order has a variety of properties for reading. Information on them can be obtained using functions Position identifier that is set to an order as soon as it is executed. Each executed order results in a deal that opens or modifies an already existing position. The identifier of...
 
Please edit your post and
use the code button (Alt+S) when pasting code
 
Keith Watford:
Please edit your post and
use the code button (Alt+S) when pasting code
Thank you for the help, and can you answer my question?
 
hoangnam009:

Hi. I have a code of close all order and I want, after close all, EA will stop for a certain period of time. someone help me?   

  

Sleep()

 
//+------------------------------------------------------------------+
//|                                          Haskayafx Finance Company  |
//|                                   http://www.haskayayazilim.net  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005-2020, HaskayaFx Yazılım"
#property link      "http://www.haskayayazilim.net"
#property version   "1.0"
#property strict
input int  BeklemeZamani=5000;// Waiting Time Use 1,000 for 1 second
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CloseOrders();
   Sleep(BeklemeZamani);
   ExpertRemove();
  }
//+------------------------------------------------------------------+

int CloseOrders()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    bool rst=OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
       if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}



 
Mehmet Bastem:
Thanks for your help, is there any other option besides choosing ExpertRemove? eg EA paused for 3 hours.
 
hoangnam009:
Thanks for your help, is there any other option besides choosing ExpertRemove? eg EA paused for 3 hours.

For this you need to use GlobalVariableSet and GlobalVariableGet or be followed from the last closed order. (OrdersHistoryTotal ())

 
//+------------------------------------------------------------------+
//|                                          Haskayafx Finance Company  |
//|                                   http://www.haskayayazilim.net  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005-2020, HaskayaFx Yazılım"
#property link      "http://www.haskayayazilim.net"
#property version   "1.0"
#property strict
input int  DelayMinute=30;// Waiting Time Minutes
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   CloseOrders();
   Sleep(DelayMinute*60000);
  
   
  
  }
//+------------------------------------------------------------------+

int CloseOrders()
{
  int total = OrdersTotal();
  for(int i=total-1;i>=0;i--)
  {
    bool rst=OrderSelect(i, SELECT_BY_POS);
    int type   = OrderType();

    bool result = false;
    
    switch(type)
    {
      //Close opened long positions
      case OP_BUY       : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, CLR_NONE );
                          break;
      
      //Close opened short positions
      case OP_SELL      : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, CLR_NONE );
                          break;

      //Close pending orders
      case OP_BUYLIMIT  :
      case OP_BUYSTOP   :
      case OP_SELLLIMIT :
      case OP_SELLSTOP  : result = OrderDelete( OrderTicket() );
    }
    
    if(result == false)
    {
       if (GetLastError()>0) Alert("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
      Sleep(3000);
    }  
  }
  
  return(0);
}



 
Mehmet Bastem:
thank you very much!!!
Reason: