Delete pending orders rules

Author: Copyright © 2022, Vladimir Karputov
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
Delete pending orders rules
ÿþ//+------------------------------------------------------------------+

//|                                  Delete pending orders rules.mq5 |

//|                              Copyright © 2022, Vladimir Karputov |

//|                      https://www.mql5.com/en/users/barabashkakvn |

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

#property copyright "Copyright © 2022, Vladimir Karputov"

#property link      "https://www.mql5.com/en/users/barabashkakvn"

#property version   "1.001"

#property description "Delete All pending orders (all Symbols and all Magic) when a position appears"

/*

   barabashkakvn Trading engine 4.010

*/

#include <Trade\PositionInfo.mqh>

#include <Trade\Trade.mqh>

#include <Trade\SymbolInfo.mqh>

#include <Trade\OrderInfo.mqh>

//---

CPositionInfo  m_position;                   // object of CPositionInfo class

CTrade         m_trade;                      // object of CTrade class

CSymbolInfo    m_symbol;                     // object of CSymbolInfo class

COrderInfo     m_order;                      // object of COrderInfo class

//--- input parameters

input group             "Trading settings"

input bool                 InpCatchInDeals      = true;        // Catch 'IN' Deals

input bool                 InpCatchOutDeals     = true;        // Catch 'OUT' Deals

input group             "Additional features"

input bool                 InpPrintLog          = true;        // Print log

input ulong                InpMagic             = 1959386;     // Magic number

//---

bool     m_need_delete_all          = false;       // delete all pending orders

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   if(!m_symbol.Name(Symbol())) // sets symbol name

     {

      Print(__FILE__," ",__FUNCTION__,", ERROR: CSymbolInfo.Name");

      return(INIT_FAILED);

     }

//---

   m_trade.SetExpertMagicNumber(InpMagic);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   if(m_need_delete_all)

     {

      if(IsPendingOrdersExists())

        {

         DeleteAllPendingOrders();

         return;

        }

      else

         m_need_delete_all=false;

     }

//---

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//--- get transaction type as enumeration value

   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;

//--- if transaction is result of addition of the transaction in history

   if(type==TRADE_TRANSACTION_DEAL_ADD)

     {

      long     deal_ticket       =0;

      long     deal_order        =0;

      long     deal_time         =0;

      long     deal_time_msc     =0;

      long     deal_type         =-1;

      long     deal_entry        =-1;

      long     deal_magic        =0;

      long     deal_reason       =-1;

      long     deal_position_id  =0;

      double   deal_volume       =0.0;

      double   deal_price        =0.0;

      double   deal_commission   =0.0;

      double   deal_swap         =0.0;

      double   deal_profit       =0.0;

      string   deal_symbol       ="";

      string   deal_comment      ="";

      string   deal_external_id  ="";

      if(HistoryDealSelect(trans.deal))

        {

         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);

         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);

         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);

         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);

         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);

         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);

         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);

         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);

         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);

         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);

         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);

         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);

         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);

         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);

         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);

        }

      else

         return;

      ENUM_DEAL_ENTRY enum_deal_entry=(ENUM_DEAL_ENTRY)deal_entry;

      if(deal_type==DEAL_TYPE_BUY || deal_type==DEAL_TYPE_SELL)

        {

         if((deal_entry==DEAL_ENTRY_IN && InpCatchInDeals) || ((deal_entry==DEAL_ENTRY_INOUT || deal_entry==DEAL_ENTRY_OUT) && InpCatchOutDeals))

            m_need_delete_all=true;

        }

     }

  }

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

//| 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.OrderType()==ORDER_TYPE_BUY_LIMIT)

           {

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

               if(InpPrintLog)

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

            continue;

           }

         if(m_order.OrderType()==ORDER_TYPE_BUY_STOP)

           {

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

               if(InpPrintLog)

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

            continue;

           }

         if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT)

           {

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

               if(InpPrintLog)

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

            continue;

           }

         if(m_order.OrderType()==ORDER_TYPE_SELL_STOP)

           {

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

               if(InpPrintLog)

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

            continue;

           }

        }

  }

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

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

         return(true);

//---

   return(false);

  }

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

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