Price touched the object Delete All

Author: Copyright © 2021, Vladimir Karputov
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
Price touched the object Delete All
ÿþ//+------------------------------------------------------------------+

//|                          Price touched the object Delete All.mq5 |

//|                              Copyright © 2021, Vladimir Karputov |

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

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

#property copyright "Copyright © 2021, Vladimir Karputov"

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

#property version   "1.000"

#property description "If the Price is touched the object, all pending orders for this symbol will be deleted"

#property description "The object can be a line like this: 'Horizontal Line' or 'Trend Line'"

//---

#include <Trade\Trade.mqh>

#include <Trade\OrderInfo.mqh>

//---

CTrade         m_trade;                      // object of CTrade class

COrderInfo     m_order;                      // object of COrderInfo class

//--- input parameters

input string   InpObjectName  = "Line";      // Line Name

//---

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

     {

      if(IsPendingOrdersExists())

        {

         DeleteAllPendingOrders();

         return;

        }

      else

         m_need_delete_all=false;

     }

//---

   if(ObjectFind(ChartID(),InpObjectName)<0)

      return;

   double object_price=0.0;

   long object_type=ObjectGetInteger(ChartID(),InpObjectName,OBJPROP_TYPE);

   if(object_type==OBJ_HLINE || object_type==OBJ_TREND)

     {

      if(object_type==OBJ_HLINE) // OBJ_HLINE

        {

         object_price=ObjectGetDouble(ChartID(),InpObjectName,OBJPROP_PRICE);

        }

      else // OBJ_TREND

        {

         object_price=ObjectGetValueByTime(ChartID(),InpObjectName,TimeCurrent(),0);

        }

      if(object_price==0.0)

         return;

      MqlRates rates[];

      ArraySetAsSeries(rates,true);

      int start_pos=0,count=3;

      if(CopyRates(Symbol(),Period(),start_pos,count,rates)!=count)

         return;

      if(rates[0].low<object_price && rates[0].high>object_price)

         m_need_delete_all=true;

     }

//---

  }

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

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

            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.Symbol()==Symbol())

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

               Print(__FILE__," ",__FUNCTION__,", ERROR: ","CTrade.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 ---