Pending Delete EA

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

//|                                            Pending Delete EA.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"

/*

   barabashkakvn Trading engine 3.151

*/

#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 group             "Trading settings"

input bool     InpBuyLimit          = true;        // Delete Buy Limit

input bool     InpSellLimit         = true;        // Delete Sell Limit

input bool     InpBuyStop           = true;        // Delete Buy Stop

input bool     InpSellStop          = true;        // Delete Sell Stop

input group             "Time control"

input uchar    InpStartHour         = 9;           // Start Hour

input uchar    InpStartMinute       = 58;          // Start Minute

input group             "Additional features"

input ulong    InpMagic             = 681127880;   // Magic number

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//--- create timer

   EventSetTimer(3);

//---

   m_trade.SetExpertMagicNumber(InpMagic);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(Symbol());

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

//--- destroy timer

   EventKillTimer();

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

  }

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

//| Timer function                                                   |

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

void OnTimer()

  {

//---

   if(TimeControlHourMinute())

      DeleteAllPendingOrders();

  }

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

//| TradeTransaction function                                        |

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

void OnTradeTransaction(const MqlTradeTransaction &trans,

                        const MqlTradeRequest &request,

                        const MqlTradeResult &result)

  {

//---

  }

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

//| 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 && InpBuyLimit)

           {

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

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

            continue;

           }

         if(m_order.OrderType()==ORDER_TYPE_BUY_STOP && InpBuyStop)

           {

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

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

            continue;

           }

         if(m_order.OrderType()==ORDER_TYPE_SELL_LIMIT && InpSellLimit)

           {

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

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

            continue;

           }

         if(m_order.OrderType()==ORDER_TYPE_SELL_STOP && InpSellStop)

           {

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

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

            continue;

           }

        }

  }

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

//| TimeControl                                                      |

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

bool TimeControlHourMinute(void)

  {

   MqlDateTime STimeCurrent;

   datetime time_current=TimeCurrent();

   if(time_current==D'1970.01.01 00:00')

      return(false);

   TimeToStruct(time_current,STimeCurrent);

   if((STimeCurrent.hour*60*60+STimeCurrent.min*60>=InpStartHour*60*60+InpStartMinute*60) &&

      (STimeCurrent.hour*60*60+STimeCurrent.min*60<InpStartHour*60*60+InpStartMinute*60+60))

      return(true);

//---

   return(false);

  }

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

Comments