Close and delete all at certain times

Author: Copyright © 2020, Vladimir Karputov
Orders Execution
Checks for the total of open orders
0 Views
0 Downloads
0 Favorites
Close and delete all at certain times
ÿþ//+------------------------------------------------------------------+

//|                        Close and delete all at certain times.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.001"

/*

   barabashkakvn Trading engine 3.148

*/

#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 datetime          InpTime     = D'1970.01.10 12:15'; // Beginning time (only HH:MM:SS)

//---

bool     m_need_close_all           = false;    // close all positions, delete all pending orders

datetime m_prev_close               = 0;        // "0" -> D'1970.01.01 00:00';

MqlDateTime m_STime;

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   m_prev_close=0;

   TimeToStruct(InpTime,m_STime);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

   datetime open_d1=iTime(Symbol(),PERIOD_D1,0);

   if(m_prev_close==open_d1)

      return;

//--- need close all

   if(m_need_close_all)

     {

      if(IsPositionExists())

        {

         CloseAllPositions();

         return;

        }

      if(IsPendingOrdersExists())

        {

         DeleteAllPendingOrders();

         return;

        }

      m_need_close_all=false;

      m_prev_close=open_d1;

      return;

     }

//---

   MqlDateTime STime;

   TimeToStruct(TimeCurrent(),STime);

   if(STime.hour>=m_STime.hour && STime.min>=m_STime.min && STime.sec>=m_STime.sec)

      m_need_close_all=true;

  }

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

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

         return(true);

//---

   return(false);

  }

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

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

  }

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

//| 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_trade.PositionClose(m_position.Ticket())) // close a position by the specified m_symbol

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

  }

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

//| 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_trade.OrderDelete(m_order.Ticket()))

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

  }

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

Comments