#Code request , close all trades when trade gets closed.

 

Hi

 

I'm looking for a code which closes all open orders ( long and short ) for the current pair, when one of the orders hits TP or gets manually closed

 

Right now i'm struggling with the spread which causes not all orders to close due to the difference, and to me adding that code could be a solution.

 

can anyone help me out? providing the code?

 

thnx 

 
alexbiesheuvel:

Hi

 

I'm looking for a code which closes all open orders ( long and short ) for the current pair, when one of the orders hits TP or gets manually closed

 

Right now i'm struggling with the spread which causes not all orders to close due to the difference, and to me adding that code could be a solution.

 

can anyone help me out? providing the code?

 

thnx 

https://www.mql5.com/en/search#!keyword=close%20all%20trades&module=mql5_module_forum
 

I've been over that multiple times, and i see alot of scripts passing by that closes trades when:

SL is hit

TP is hit

Time is achieved etc etc.

It all doesn't do what i need...

 

It needs to be a script that counts the open trades on a pair on that chart, and closes all long and short trades when the nr of the trades is below the nr of trades 2 seconds ago.The nr of trades will increase,

 

in basic:

 

10:30:00 nr of trades on pair EURGBP = 6 closeall = no

10:30:01 nr of trades on Pair EURGBP = 7 closeall = no 

 10:30:02 nr of trade on pair EUR GBP = 6 close all = yes

 

I hope you understand, as i am not a die hard programmer :P

 

hope anyone can help me out.

 

thnx 

 
alexbiesheuvel:

I've been over that multiple times, and i see alot of scripts passing by that closes trades when:

SL is hit

TP is hit

Time is achieved etc etc.

It all doesn't do what i need...

 

It needs to be a script that counts the open trades on a pair on that chart, and closes all long and short trades when the nr of the trades is below the nr of trades 2 seconds ago.The nr of trades will increase,

 

in basic:

 

10:30:00 nr of trades on pair EURGBP = 6 closeall = no

10:30:01 nr of trades on Pair EURGBP = 7 closeall = no 

 10:30:02 nr of trade on pair EUR GBP = 6 close all = yes

 

I hope you understand, as i am not a die hard programmer :P

 

hope anyone can help me out.

 

thnx 

Sounds like a part time job.

Maybe you can try freelance https://www.mql5.com/en/job

Freelance service at MQL5.com
Freelance service at MQL5.com
  • www.mql5.com
Orders for the development of automated trading programs
 

was hoping anyone can help me out over here.

 

toch bedankt :P 

 
//+------------------------------------------------------------------+
//|                                                    tictactoe.mq4 |
//|       Copyright 2016,Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016,Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int i,New,Old;
bool result;
double profit;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetMillisecondTimer(1000);

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

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--- tic
   New=0;
   for(i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==Symbol())
           {
            New++;
           }
        }
     }
//--- tac     
   if(New<Old)
     {
      for(i=0;i<OrdersTotal();i++)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol())
              {
               switch(OrderType())
                 {
                  case OP_BUY:
                     result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);
                     if(result==false)
                       {
                        Alert("Order ",OrderTicket()," failed to close. Error:",GetLastError());
                       }
                     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());
                       }
                     break;
                 }
              }
           }
        }
     }
//--- toe     
   Old=0;profit=0;
   for(i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==Symbol())
           {
            profit=profit+OrderProfit();
            Old++;
           }
        }
     }
    Comment(Symbol()+" "+IntegerToString(Old)+" Orders, Profit: ",DoubleToString(profit,2));
  }
//+------------------------------------------------------------------+
 

wow.. that was fast.... i'll check it out.

 

thnx 

 
Marco vd Heijden:
      for(i=0;i<OrdersTotal();i++)

You always have to count down or you will have some surprises.

 
Alain Verleyen:

You always have to count down or you will have some surprises.

I know.

//+------------------------------------------------------------------+
//|                                                    tictactoe.mq4 |
//|       Copyright 2016,Marco vd Heijden, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016,Marco vd Heijden, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int i,New,Old;
bool result;
double profit;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetMillisecondTimer(1000);

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

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//--- tic
   New=0;
   for(i=OrdersTotal();i>0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==Symbol())
           {
            New++;
           }
        }
     }
//--- tac     
   if(New<Old)
     {
      for(i=OrdersTotal();i>0;i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
           {
            if(OrderSymbol()==Symbol())
              {
               switch(OrderType())
                 {
                  case OP_BUY:
                     result=OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);
                     if(result==false)
                       {
                        Alert("Order ",OrderTicket()," failed to close. Error:",GetLastError());
                       }
                     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());
                       }
                     break;
                 }
              }
           }
        }
     }
//--- toe     
   Old=0;profit=0;
   for(i=OrdersTotal();i>0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
        {
         if(OrderSymbol()==Symbol())
           {
            profit=profit+OrderProfit();
            Old++;
           }
        }
     }
    Comment(Symbol()+" "+IntegerToString(Old)+" Orders, Profit: ",DoubleToString(profit,2));
  }
//+------------------------------------------------------------------+
 
Marco vd Heijden:

I know.

      for(i=OrdersTotal()-1;i>=0;i--)
 

OrdersTotal

Returns the number of market and pending orders.

int  OrdersTotal();

Returned value

Total amount of market and pending orders.

Example:

  int handle=FileOpen("OrdersReport.csv",FILE_WRITE|FILE_CSV,"\t");
  if(handle<0) return(0);
  // write header
  FileWrite(handle,"#","open price","open time","symbol","lots");
  int total=OrdersTotal();
  // write open orders
  for(int pos=0;pos<total;pos++)
    {
     if(OrderSelect(pos,SELECT_BY_POS)==falsecontinue;
     FileWrite(handle,OrderTicket(),OrderOpenPrice(),OrderOpenTime(),OrderSymbol(),OrderLots());
    }
  FileClose(handle);


Even in the reference ++ is used in many examples.

Normally i do not call the function OrdersTotal() in the for loop this was just a quick sketch to give Mr. Biesheuvel some inspiration.

I know from experience that it does not work because there is the risk not all orders get closed.

Reason: