I have a lot of open positions.How can I closeall at the same time? - page 2

 

There is a free EA I use before to close all position in one click, once you enable the EA it will start to close all open position, then I just disable the EA after the task.

 
//+------------------------------------------------------------------+
//|                                            1ClickCloseAllv01.mq4 |
//|                                Copyright 2016, Ozan Buyuksemerci |
//|                                             https://www.ozan.org |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Ozan Buyuksemerci (grandaevus)"
#property link      "https://ozan.org"
#property version   "1.00"
#property strict

//--- input parameters
input int MaxSlippage = 5;



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 
   
   ObjectCreate(0,"CloseButton",OBJ_BUTTON,0,0,0);
   ObjectSetInteger(0,"CloseButton",OBJPROP_XDISTANCE,25);
   ObjectSetInteger(0,"CloseButton",OBJPROP_YDISTANCE,100);
   ObjectSetInteger(0,"CloseButton",OBJPROP_XSIZE,100);
   ObjectSetInteger(0,"CloseButton",OBJPROP_YSIZE,50);

   ObjectSetString(0,"CloseButton",OBJPROP_TEXT,"Close All");
      
   
   ObjectSetInteger(0,"CloseButton",OBJPROP_COLOR, White);
   ObjectSetInteger(0,"CloseButton",OBJPROP_BGCOLOR, Red);
   ObjectSetInteger(0,"CloseButton",OBJPROP_BORDER_COLOR,Red);
   ObjectSetInteger(0,"CloseButton",OBJPROP_BORDER_TYPE,BORDER_FLAT);
   ObjectSetInteger(0,"CloseButton",OBJPROP_BACK,false);
   ObjectSetInteger(0,"CloseButton",OBJPROP_HIDDEN,true);
   ObjectSetInteger(0,"CloseButton",OBJPROP_STATE,false);
   ObjectSetInteger(0,"CloseButton",OBJPROP_FONTSIZE,12);

//---
   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

   ObjectDelete(0,"CloseButton");
   
  }
  
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      
  }
  
  
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---
            
   if(sparam== "CloseButton")
      {
      CloseAllOpenPositions(MaxSlippage); 
      ObjectSetInteger(0,"CloseButton",OBJPROP_STATE,false);    
      }
         
//---      
  }
//+------------------------------------------------------------------+


void CloseAllOpenPositions(int intMaxSlippage)
  {
   bool checkOrderClose = true;        
   int index = OrdersTotal()-1;
   
   while (index >=0 && OrderSelect (index,SELECT_BY_POS,MODE_TRADES)==true)
      {
      if (OrderType()==OP_BUY || OrderType()==OP_SELL)
         {         
         checkOrderClose = OrderClose (OrderTicket(), OrderLots(), OrderClosePrice(), intMaxSlippage, CLR_NONE); 
         
         if(checkOrderClose == false)
            {
            int errorCode = GetLastError();
            
            if (errorCode == 1 || errorCode == 2 || errorCode == 5 || errorCode == 6 || errorCode == 64 || errorCode == 65 || errorCode == 132 || errorCode == 133 || errorCode == 139) break;
            else continue;        
            }          
         }     
      
      index--;
      }   
   
   
  }

Ali Poormomen
:

Hi

I have a lot of open positions.How can I closeall at the same time?

If I use :

it will take a lot of time

And I do not have margin for buying and selling, unlike the direction.


Try this 

 

Forum on trading, automated trading systems and testing trading strategies

What is fastest way for closing all my positions?

fxsaber, 2017.07.23 08:51

#include <MT4Orders.mqh> // https://www.mql5.com/en/code/16006

bool CloseAllPositionsAsync( const bool AllSymbols = true, const int Slippage = 0 )
{
  bool Res = true;
  
  for (int i = OrdersTotal() - 1; i >= 0; i--)
    if (OrderSelect(i, SELECT_BY_POS) && (OrderType() <= OP_SELL) &&
        (AllSymbols ? true : (OrderSymbol() == Symbol())))
      Res &= OrderCloseAsync(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage);
      
  return(Res);
}


try this

 
Ali Poormomen:

thank you for your attention


But that also takes a long time.
There is a way to close everyone at a time?


Closing fast means an action from broker side EA just can send close all request only 

Reason: