Problem. How to close a position automatically on Friday

 
Hello everyone.
I have a problem with the mql5 code to close a market operation just before the closing of Friday.
In practice, I would like the open position to be closed 5 minutes before the Friday evening market closes.
Of course the Trading System should reconsider the different times between Forex, Indices, Commodities.
But I can't solve the problem.
Does anyone know how to do?
Thanks in advance
 
Alecxander: I have a problem with the mql5 code 
  1. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum

  2. Since Brokers use a variety of time zones, You have to have inputs to specify what you need.
 
William Roeder:
  1. You haven't stated a problem, you stated a want. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help

    Or pay someone. Top of every page is the link Freelance.
              Hiring to write script - General - MQL5 programming forum

  2. Since Brokers use a variety of time zones, You have to have inputs to specify what you need.

I will tell you the truth William Roeder.

Opening the reply messages and seeing that you write such a thing is truly starving. No help or advice is refused to anyone ... never. If I had asked for the whole code I could understand your reaction, but I only asked a question: it is likely that someone had the same problem and if I give some indication I will solve it myself.

I reply so because you did the same thing on another recent post. You didn't give anything just things that I already knew and that didn't explain how to solve. You probably live on this, ok I understand it. BUT THE NEXT TIME, YOU AVOID ENTERING IN DISCUSSIONS IF YOU DON'T WANT TO HELP.

Anyway, do me a courtesy; in the future, when you read a message posted by me, do not write anything, do not reply, because of your help = zero I don't know what to do with it.

 
Alecxander:
Hello everyone.
I have a problem with the mql5 code...

post your "code" if you need help.

 

Here is a demo Expert Advisor to close all positions, 5 minutes before Friday 10 pm GMT (Forex close time)

//+------------------------------------------------------------------+
//|                                            FridayCloseExpert.mq5 |
//|                                        Copyright © 2018, Amr Ali |
//|                             https://www.mql5.com/en/users/amrali |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2018, Amr Ali"
#property link      "https://www.mql5.com/en/users/amrali"
#property version   "1.000"
#property description "project description."
//--- Includes
#include <Trade\Trade.mqh>
#include <Trade\PositionInfo.mqh>
//---
CTrade         m_trade;      // trading object
CPositionInfo  m_position;   // position info object
//--- Input parameters                                                 |
input  bool    InpFridayCloseTrades   = false      ;  // FridayCloseTrades
input  string  InpFridayCloseTimeGMT  = "21:55"    ;  // FridayCloseTimeGMT
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- initialize common information
   m_trade.SetAsyncMode(true);
   m_trade.SetDeviationInPoints(INT_MAX);
   m_trade.SetMarginMode();
   m_trade.LogLevel(LOG_LEVEL_ERRORS);
//--- Initialization completed successfully
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(InpFridayCloseTrades && PositionsTotal() > 0 && FridayTimeIsActive())
     {
      CloseAllPositions();
     }
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool FridayTimeIsActive()
  {
   MqlDateTime dt;

   datetime gmtTime = TimeGMT(dt);
   datetime endTime = StringToTime(TimeToString(gmtTime,TIME_DATE) + " " + InpFridayCloseTimeGMT);

   if(dt.day_of_week==(ENUM_DAY_OF_WEEK)FRIDAY && gmtTime >= endTime)
     {
      return(true);
     }

   return(false);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void CloseAllPositions()
  {
//--- close market positions
   for(int i=PositionsTotal()-1; i>=0 && !IsStopped(); i--)
     {
      if(m_position.SelectByIndex(i))
        {
         //--- trading object
         m_trade.SetExpertMagicNumber(m_position.Magic());
         m_trade.SetTypeFillingBySymbol(m_position.Symbol());
         //--- close positions
         if(m_trade.PositionClose(m_position.Ticket()) && (m_trade.ResultRetcode()==TRADE_RETCODE_DONE || m_trade.ResultRetcode()==TRADE_RETCODE_PLACED))
            PrintFormat("Position #%I64u on %s to be closed",m_position.Ticket(),m_position.Symbol());
         else
            PrintFormat("> Error closing position #%I64u on %s (%s)",m_position.Ticket(),m_position.Symbol(),m_trade.ResultComment());
        }
      }
   }
//+------------------------------------------------------------------+
Files:
 
amrali:

Here is a demo Expert Advisor to close all positions, 5 minutes before Friday 10 pm GMT (Forex close time)

Thanks amrali, you have been very kind.