Closing Multiple Trades at once - page 3

 
fxsaber:


Result


I changed Point to Tick Size. So instead of using PriceToInteger I'll have to use ceil()

void OnStart()
{
  double Price1 = 1.23456;
  double Price2 = 1.23457;
  double point = 1e-5;
  
  _P(Price1);
  _P(Price2);
  
  _P((int)ceil(fabs((Price1 - Price2) / point)));
  _P(fabs(PriceToInteger(Price1, point) - PriceToInteger(Price2, point)));
}
void OnStart(), Line = 28: Price1 = 1.23456
void OnStart(), Line = 29: Price2 = 1.23457
void OnStart(), Line = 31: (int)ceil(fabs((Price1-Price2)/point)) = 1
void OnStart(), Line = 32: fabs(PriceToInteger(Price1,point)-PriceToInteger(Price2,point)) = 1

Updated script attached

Files:
 
nicholishen:

I changed Point to Tick Size. So instead of using PriceToInteger I'll have to use ceil()

void OnStart(), Line = 28: Price1 = 1.23456
void OnStart(), Line = 29: Price2 = 1.23473
void OnStart(), Line = 31: (int)ceil(fabs(Price1-Price2)/point) = 18
void OnStart(), Line = 32: fabs(PriceToInteger(Price1,point)-PriceToInteger(Price2,point)) = 17
 
fxsaber:
int test = (int)NormalizeDouble(fabs((Price1 - Price2) / tick_size),0);
 
isaacarsenal:

I know the OrderCloseBy() function which close pairs of orders. But it cannot close all orders at once.

Is there any function which closes all trades at once?

Maybe this can help.
void Close_AllOrders()
  {
/*-----------------------------------------------------------------------------------------------*/
//Close Orders according to FIFO Rule
   for(i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         if(OrderSymbol()==Symbol())
           {
            if(OrderType()==OP_BUY)
              {
               Close_Result=OrderClose(OrderTicket(),OrderLots(),Bid,0,clrNONE);
               if(Close_Result) i--;
              }
            if(OrderType()==OP_SELL)
              {
               Close_Result=OrderClose(OrderTicket(),OrderLots(),Ask,0,clrNONE);
               if(Close_Result) i--;
              }
            if(OrderType()==OP_SELLLIMIT ||
               OrderType()==OP_SELLSTOP ||
               OrderType()==OP_BUYLIMIT ||
               OrderType()==OP_BUYSTOP)
              {
               Close_Result=OrderDelete(OrderTicket()clrNONE);
               if(Close_Result) i--;
              }
           }
     }
/*-----------------------------------------------------------------------------------------------*/
  }
 
Donald Gibson:
Maybe this can help.

The following example does the job recursively..


TODO:

You can pass the OrderType() you like to the CMD param.. The OrderType() can be: OP_BUY, OP_SELL, or any etc..

You can pass SameLot as true/false..

You can add extra lines for GetInfoInteger to calc pendings, if that to be in use..

You can, too, adjust what TM or TradeMode is required to be checked..

You can pass Pair and ID as paramz to all functions, if leaving them fixed ain't enough..

Finally, call functions the way you like..


Change example:

GetInfoInteger(string Pair, int ID, int InfoType=NULL)

instead of:

GetInfoInteger(int InfoType=NULL)

and so on for the rest of functions to let them work for many pairs the way you like the iteration..



Main Example:

//--------------------------------------------------------------------

string    Pair = "EURUSD";
int         ID = 123;
int   Slippage = 3;
int SleepInter = 100;


//+------------------------------------------------------------------+
//| GetInfoInteger                                                   |
//+------------------------------------------------------------------+
int GetInfoInteger(int InfoType=NULL)
  {
//---
   int totDeals=OrdersTotal(); if(totDeals<=0){return(0);}
//---
   int Result=0;
//---
   for(int i=0; i<=totDeals; i++)
     {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){continue;}
      if(OrderSymbol()!=Pair || OrderMagicNumber()!=ID || OrderType()>OP_SELL){continue;}
      //---
      switch(InfoType)
        {
         case 1:
         if(OrderType()==OP_BUY){Result++;} break;
         case 2:
         if(OrderType()==OP_SELL){Result++;} break;
         default:
            Result++; break;
        }
     }
//---
   return(Result);
//---
  }
//+------------------------------------------------------------------+
//| expert MilkTheCow function                                       |
//+------------------------------------------------------------------+
void MilkTheCow(bool SameLot=true)
  {
   if((bool)MarketInfo(Pair,MODE_CLOSEBY_ALLOWED)==false){return;}
   int TM=(int)SymbolInfoInteger(Pair,SYMBOL_TRADE_MODE);
   int tries=0,Cmd=-1,ticket=-1,i=0,ii=0,iii=0,liOrdsTot=GetInfoInteger();
   double lots=0.0;
   for(i=0; i<=liOrdsTot; i++) //loop is OK.. index is fine.. you tested it..
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Pair && OrderMagicNumber()==ID)
           {
            ticket=OrderTicket();
            lots=OrderLots();
            Cmd=OrderType();
            if(Cmd==OP_SELL)
              {
               for(ii=0; ii<=liOrdsTot; ii++) //loop is OK.. index is fine.. you tested it..
                 {
                  if(OrderSelect(ii,SELECT_BY_POS,MODE_TRADES))
                    {
                     if(OrderSymbol()==Pair && OrderMagicNumber()==ID)
                       {
                        if(OrderType()==OP_BUY && ((OrderLots()==lots && SameLot) || SameLot==false) && (TM==SYMBOL_TRADE_MODE_FULL || TM==SYMBOL_TRADE_MODE_CLOSEONLY))
                          {
                           tries=0; while(!OrderCloseBy(OrderTicket(),ticket) && tries<3){tries++; RefreshRates(); Sleep(SleepInter);}
                           liOrdsTot=GetInfoInteger(); i=0; ii=0; iii=0;
                           break;
                          }
                       }
                    }
                 }// for ii
              }
            if(Cmd==OP_BUY)
              {
               for(iii=0; iii<=liOrdsTot; iii++) //loop is OK.. index is fine.. you tested it..
                 {
                  if(OrderSelect(iii,SELECT_BY_POS,MODE_TRADES))
                    {
                     if(OrderSymbol()==Pair && OrderMagicNumber()==ID)
                       {
                        if(OrderType()==OP_SELL && ((OrderLots()==lots && SameLot) || SameLot==false) && (TM==SYMBOL_TRADE_MODE_FULL || TM==SYMBOL_TRADE_MODE_CLOSEONLY))
                          {
                           tries=0; while(!OrderCloseBy(OrderTicket(),ticket) && tries<3){tries++; RefreshRates(); Sleep(SleepInter);}
                           liOrdsTot=GetInfoInteger(); i=0; ii=0; iii=0;
                           break;
                          }
                       }
                    }
                 }// for iii
              }
           }
        }
     }// for i
  }
//+------------------------------------------------------------------+
//| expert MilkTheCows function                                      |
//+------------------------------------------------------------------+
void MilkTheCows(int CMD)
  {
   int tries=0,Cmd=-1,i=0,liOrdsTot=GetInfoInteger();
   int TM=(int)SymbolInfoInteger(Pair,SYMBOL_TRADE_MODE);
   double price=0.0;
   double ASK = MarketInfo(Pair,MODE_ASK);
   double BID = MarketInfo(Pair,MODE_BID);
   for(i=0; i<=liOrdsTot; i++) //loop is OK.. index is fine.. you tested it..
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Pair && OrderMagicNumber()==ID && (TM==SYMBOL_TRADE_MODE_FULL || TM==SYMBOL_TRADE_MODE_CLOSEONLY))
           {
            Cmd = OrderType();
            ASK = MarketInfo(Pair,MODE_ASK);
            BID = MarketInfo(Pair,MODE_BID);
            if(Cmd==CMD && CMD<=OP_SELL){price=(Cmd==OP_SELL) ? ASK : BID; tries=0; while(!OrderClose(OrderTicket(),OrderLots(),price,Slippage) && tries<3){tries++; RefreshRates(); Sleep(SleepInter); price=(Cmd==OP_SELL) ? MarketInfo(Pair,MODE_ASK) : MarketInfo(Pair,MODE_BID);} Sleep(SleepInter);}
           }
        }
     }
  }
//+------------------------------------------------------------------+
//| expert SleepTheCows function                                     |
//+------------------------------------------------------------------+
void SleepTheCows(int CMD)
  {
   int tries=0,Cmd=-1,i=0,liOrdsTot=GetInfoInteger();
   int TM=(int)SymbolInfoInteger(Pair,SYMBOL_TRADE_MODE);
   for(i=0; i<=liOrdsTot; i++) //loop is OK.. index is fine.. you tested it..
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         if(OrderSymbol()==Pair && OrderMagicNumber()==ID && (TM==SYMBOL_TRADE_MODE_FULL || TM==SYMBOL_TRADE_MODE_CLOSEONLY))
           {
            Cmd = OrderType();
            if(Cmd==CMD){tries=0; while(!OrderDelete(OrderTicket()) && tries<3){tries++; RefreshRates(); Sleep(SleepInter);} Sleep(SleepInter);}
           }
        }
     }
  }



Thanks :)

Have a nice day!

Reason: