Close Delete On Friday

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

//|                                       Close Delete On Friday.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.116

*/

#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 bool     InpClosePositions    = true;        // 1. Close positions

input bool     InpDeletePending     = true;        // 2. Delete pending orders

input bool     InpCloseAllSymbols   = true;        // 3. Close all Symbols

input string   InpCloseOnlySymbols  = "USDJPY.m";  // 4. Close ONLY with this Symbol (if '3' -> 'false')

input bool     InpCloseAllMagics    = true;        // 5. Close all Magics

input ulong    InpCloseOnlyMagics   = 200;         // 6. Close ONLY with this Magic number (if '5' -> 'false')

input uchar    InpStartHour         = 10;          // 7. Friday Start Hour

input uchar    InpStartMinute       = 01;          // 8. Friday Start Minute

input ulong    InpDeviation         = 50;          // 9. Deviation, in points (1.00045-1.00055=10 points)

//---

bool     m_need_close_all           = false;       // close all positions

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(0);

   m_trade.SetMarginMode();

   m_trade.SetTypeFillingBySymbol(m_symbol.Name());

   m_trade.SetDeviationInPoints(InpDeviation);

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---



  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   if(m_need_close_all)

     {

      if(IsPositionExists())

        {

         CloseAllPositions();

         return;

        }

      else

         m_need_close_all=false;

     }

//---

   if(m_need_delete_all)

     {

      if(IsPendingOrdersExists())

        {

         DeleteAllPendingOrders();

         return;

        }

      else

         m_need_delete_all=false;

     }

//---

   if(TimeControlHourMinute())

     {

      m_need_close_all=InpClosePositions;

      m_need_delete_all=InpDeletePending;

     }

  }

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

//| 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.day_of_week==5)

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

         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

         if((InpCloseAllSymbols || (!InpCloseAllSymbols && m_order.Symbol()==InpCloseOnlySymbols)) &&

            (InpCloseAllMagics || (!InpCloseAllMagics && m_order.Magic()==InpCloseOnlyMagics)))

            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((InpCloseAllSymbols || (!InpCloseAllSymbols && m_order.Symbol()==InpCloseOnlySymbols)) &&

            (InpCloseAllMagics || (!InpCloseAllMagics && m_order.Magic()==InpCloseOnlyMagics)))

           {

            m_trade.OrderDelete(m_order.Ticket());

           }

  }

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

//| 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((InpCloseAllSymbols || (!InpCloseAllSymbols && m_position.Symbol()==InpCloseOnlySymbols)) &&

            (InpCloseAllMagics || (!InpCloseAllMagics && m_position.Magic()==InpCloseOnlyMagics)))

            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((InpCloseAllSymbols || (!InpCloseAllSymbols && m_position.Symbol()==InpCloseOnlySymbols)) &&

            (InpCloseAllMagics || (!InpCloseAllMagics && m_position.Magic()==InpCloseOnlyMagics)))

           {

            m_trade.PositionClose(m_position.Ticket());

           }

  }

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

Comments