Close trades by time
Yes, I have some strategies that benefit from using a time limit. I use two types, depending on the strategy's objective:
- Closing at a certain time, such as before the end of the day or before the start (or the end) of a certain session.
- 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.
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
- www.mql5.com
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); } } } //+------------------------------------------------------------------+
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