Close Delete All a specific magic number

Author: Copyright © 2020, Vladimir Karputov
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
Close Delete All a specific magic number
ÿþ//+------------------------------------------------------------------+

//|                     Close Delete All a specific magic number.mq5 |

//|                              Copyright © 2020, Vladimir Karputov |

//|                     https://www.mql5.com/ru/market/product/43516 |

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

#property copyright "Copyright © 2020, Vladimir Karputov"

#property link      "https://www.mql5.com/ru/market/product/43516"

#property version   "1.000"

#property description "Profit is accounted for the specified 'Magic number'."

#property description "When the goal is reached, close all positions with the specified 'Magic number'"

#property description "   and delete all pending orders with the specified 'Magic number'"

/*

   barabashkakvn Trading engine 3.151

*/

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\OrderInfo.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

COrderInfo     m_order;                      // object of COrderInfo class

//--- input parameters

input group             "Trading settings"

input double   InpProfit            = 15;          // Profit target (in money deposit)

input group             "Additional features"

input ulong    InpMagic             = 200;         // Close and Delete is Magic number

//---

bool     m_need_close_all           = false;    // close all positions

bool     m_need_delete_all          = false;    // delete all pending orders

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   if(m_need_close_all)

     {

      if(IsPositionExists())

        {

         CloseAllPositions();

        }

      else

        {

         m_need_close_all=false;

        }

     }

   if(m_need_delete_all)

     {

      if(IsPendingOrdersExists())

        {

         DeleteAllPendingOrders();

        }

      else

         m_need_delete_all=false;

     }

   if(m_need_close_all || m_need_delete_all)

      return;

//---

   double profit=ProfitAllPositions();

   if(profit>=InpProfit)

     {

      m_need_close_all=true;

      m_need_delete_all=true;

     }

  }

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

//| Profit all positions                                             |

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

double ProfitAllPositions(void)

  {

   double profit=0.0;

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

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         if(m_position.Magic()==InpMagic)

            profit+=m_position.Commission()+m_position.Swap()+m_position.Profit();

//---

   return(profit);

  }

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

//| Is position exists                                               |

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

bool IsPositionExists(void)

  {

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

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         if(m_position.Magic()==InpMagic)

            return(true);

//---

   return(false);

  }

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

//| Close all positions                                              |

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

void CloseAllPositions(void)

  {

   for(int i=PositionsTotal()-1; i>=0; i--) // returns the number of current positions

      if(m_position.SelectByIndex(i)) // selects the position by index for further access to its properties

         if(m_position.Magic()==InpMagic)

            if(!m_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol

               Print(__FILE__," ",__FUNCTION__,", ERROR: "," PositionClose ",m_position.Ticket(),", ",m_trade.ResultRetcodeDescription());

  }

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

//| Is pending orders exists                                         |

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

bool IsPendingOrdersExists(void)

  {

   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders

      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties

         if(m_order.Magic()==InpMagic)

            return(true);

//---

   return(false);

  }

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

//| Delete all pending orders                                        |

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

void DeleteAllPendingOrders(void)

  {

   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders

      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties

         if(m_order.Magic()==InpMagic)

            if(!m_trade.OrderDelete(m_order.Ticket()))

               Print(__FILE__," ",__FUNCTION__,", ERROR: ","OrderDelete ",m_order.Ticket());

  }

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

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 ---