Close+Delete+Delete_#

Author: www.mql5.com/ru/users/spoiltboy
Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
0 Views
0 Downloads
0 Favorites
Close+Delete+Delete_#
ÿþ//+------------------------------------------------------------------+

//|                                                 Close+Delete.mq4 |

//|                        Copyright 2019, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "www.mql5.com/ru/users/spoiltboy"

#property link      "https://www.mql5.com/ru/users/spoiltboy"

#property version   "1.01"

#property strict

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

//---

   Print("CloseOll()  ", CloseOll());

   Print("DeleteOll()  ", DeleteOll());

   Print("DeleteObj()  ", DeleteObj());

  }

//+------------------------------------------------------------------+

int CloseOll()

{

   int closed = 0;

   int last_closed = 0;

   int tries_count = 0;

   int count = 0;

   ResetLastError();

   while(!IsStopped())

   {

      bool have_orders = false;

      RefreshRates();

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

      {

         if(i==-1) return 0;

         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         {

            if (OrderSymbol() == Symbol() && (OrderType()==OP_BUY || OrderType()==OP_SELL))

            {

               have_orders=true;

               tries_count++;

               if(OrderType()==OP_BUY)

               {

                  if(OrderClose(OrderTicket(), OrderLots(), Bid, 100, Black))

                  {

                     tries_count=0;

                     closed++;

                  }

                  else //if(OrderClose

                  {

                     RefreshRates();

                     if(OrderClose(OrderTicket(), OrderLots(), Bid, 100, Black))

                     {

                        tries_count=0;

                        closed++;

                     }

                  }

               }

               if(OrderType()==OP_SELL){

                  if(OrderClose(OrderTicket(), OrderLots(), Ask, 100, Black))

                  {

                     tries_count=0;

                     closed++;

                  }

                  else //if(OrderClose

                  {

                     RefreshRates();

                     if(OrderClose(OrderTicket(), OrderLots(), Ask, 100, Black))

                     {

                        tries_count=0;

                        closed++;

                     }

                  }

               }

            }   

         }

      }

      if(!have_orders)

      {

         return closed;

      }

      if(last_closed == closed && tries_count>=3)

      {

         count++;

         if(count>=3)

         {

            Print("CloseOll() >H81:0 70:@KB8O ", GetLastError()); 

            return -1;

         }

      }

      if(last_closed != closed)

      {

         last_closed = closed;

      }

      if(tries_count>0)

      {

         Sleep(1000);

      }

   }   

   return closed;

}

int DeleteObj()

{

   int deleted = 0;

   int last_deleted = 0;

   int tries_count = 0;

   int count = 0;

   ResetLastError();

   while(!IsStopped())

   {

      bool have_obj = false;

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

      {

         if( StringFind(ObjectName(i), "#")>-1 )

         {  

            have_obj = true;

            tries_count++;

            if(ObjectDelete(ObjectName(i)))

            {

               tries_count=0;

               deleted++;

            }

         }      

      }

      if(!have_obj)

      {

         return deleted;

      }

      if(last_deleted == deleted && tries_count>=3)

      {

         count++;

         if(count>=3)

         {

            Print("DeleteObj() >H81:0 C40;5=8O ", GetLastError()); 

            return -1;

         }

      }

      if(last_deleted != deleted)

      {

         last_deleted = deleted;

      }

      if(tries_count>0)

      {

         Sleep(1000);

      }

   }

   return deleted;

}

int DeleteOll()

{  

   int deleted = 0;

   int last_deleted = 0;

   int tries_count = 0;

   int count = 0;

   ResetLastError();

   while(!IsStopped())

   {

      bool have_pos = false;

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

      {

         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))

         {

            if (OrderSymbol() == Symbol() && OrderType()!=OP_BUY && OrderType()!=OP_SELL)

            {

               have_pos = true;

               tries_count++;

               if(OrderDelete(OrderTicket(), Black))

               {

                  tries_count=0;

                  deleted++;

               }

            }   

         }

      }

      if(!have_pos)

      {

         return deleted;

      }

      if(last_deleted == deleted && tries_count>=3)

      {

         count++;

         if(count>=3)

         {

            Print("DeleteOll() >H81:0 C40;5=8O ", GetLastError()); 

            return -1;

         }

      }

      if(last_deleted != deleted)

      {

         last_deleted = deleted;

      }

      if(tries_count>0)

      {

         Sleep(1000);

      }

   }

   return deleted;

} 

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---