Close trades by time

Enrique Enguix  

Hello partners. I hope you are all doing great


I would like to add to a Expert Advisor time closures.

I mean, a few hours go by, things don't go well, and then it closes. Not by pips with a conventional SL, but by time.


Do any of you use time criteria in your expert advisors or discretionary strategy?

A little help in this regard would be very useful for me. What criteria do you apply?


Thank you very much in advance

William Roeder  
I've seen a few strategies with a fixed timed exit, like 210 bars or 24 hours. Close, regardless of profit.
Fernando Carreiro  
Enrique Enguix Vino: Hello partners. I hope you are all doing great I would like to add to a Expert Advisor time closures. I mean, a few hours go by, things don't go well, and then it closes. Not by pips with a conventional SL, but by time. Do any of you use time criteria in your expert advisors or discretionary strategy? A little help in this regard would be very useful for me. What criteria do you apply?

Yes, I have some strategies that benefit from using a time limit. I use two types, depending on the strategy's objective:

  1. Closing at a certain time, such as before the end of the day or before the start (or the end) of a certain session.
  2. Limiting the duration of a position so that it only stays open a certain number of hours or minutes and is then forced to close irrespective of gain.
In some strategies I use neither, in others only one of the above and in others I use both. It all depends on the strategy and the statistics I gathered.
Vladimir Karputov  
Enrique Enguix Vino :


Examples:

  • Close All at New Bar : Utility Expert Advisor: closes absolutely all positions at the beginning of the bar
  • Close after N : The idea of the utility Position lifetime is set through three independent parameters: ' After: Hour ', ' After: Minutes ', ' After: Seconds '.
  • Close after 24 hours : The idea of the utility The EA tracks the time elapsed since the position was opened and if 24 hours have passed, the position will be closed. The EA processes all positions - for any symbol and with any Magic number
Stephen Fabrico  
Yes, I absolutely include time passed as one of the exit criteria I use to exit a position. Essentially, I have a pre-designated price point to place my stop loss after n mins. If after n min, the profit of the position is lower than expected, then I close the position at the current price with a market order. Otherwise, if the profit of the position is greater than expected, I modify the stop loss of the position to that designated price point.
Antonio Simon Del Vecchio  
Enrique Enguix Vino:

Hello partners. I hope you are all doing great


I would like to add to a Expert Advisor time closures.

I mean, a few hours go by, things don't go well, and then it closes. Not by pips with a conventional SL, but by time.


Do any of you use time criteria in your expert advisors or discretionary strategy?

A little help in this regard would be very useful for me. What criteria do you apply?


Thank you very much in advance

Hello, 

I answered you in Spanish but I add it here in case it helps someone.

//+------------------------------------------------------------------+
//|                                                       Prueba.mq5 |
//|                                                Simón Del Vecchio |
//|                    https://www.mql5.com/es/users/simondelvecchio |
//+------------------------------------------------------------------+
#property copyright "Simón Del Vecchio"
#property link      "https://www.mql5.com/es/users/simondelvecchio"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Includes                                                         |
//+------------------------------------------------------------------+
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\AccountInfo.mqh>
//+------------------------------------------------------------------+
//| Objetos                                                          |
//+------------------------------------------------------------------+
CSymbolInfo Info;
CTrade Trade;
CPositionInfo Posicion;
CAccountInfo Cuenta;
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
input double Valor = 20;
//+------------------------------------------------------------------+
//| Variables globales                                               |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Función de inicialización del EA                                 |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   EstrategiaDeEntrada();
   EstrategiaDeSalida();
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void EstrategiaDeEntrada()
  {
   /*
   Código de la estrategia de entrada
   */
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void EstrategiaDeSalida()
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      Posicion.SelectByIndex(i);
      datetime Fecha1 = Posicion.Time();//Fecha de apertura de la posición seleccionada
      datetime Fecha2 = TimeCurrent();//Fecha y hora actual
      MqlDateTime str1, str2;//Estructuras para capturar Mes, día, hora,...
      TimeToStruct(Fecha1, str1);
      TimeToStruct(Fecha2, str2);
      //Por ejemplo, si el profit es menor a valor y ya han  pasado dos horas, cerrar la posición.
      if(Cuenta.Profit() < Valor && str2.hour - str1.hour > 2)
        {
         Trade.PositionClose(Posicion.Ticket(), -1);
        }
     }
  }
//+------------------------------------------------------------------+
Reason: