Order Closed by time

 

Hi friends, I'm trying a function to close trades by time, but I'm not sure it's working 100% times, due the backtest results.

Somebody is looking bad code? Thanks in advantage


//+------------------------------------------------------------------+
//| Include                                                          |
//+------------------------------------------------------------------+
#include <Trade\SymbolInfo.mqh>
#include <Trade\PositionInfo.mqh>
#include <Trade\Trade.mqh>
#include <Trade\AccountInfo.mqh>
//+------------------------------------------------------------------+
//| Objects                                                          |
//+------------------------------------------------------------------+
CSymbolInfo Info;
CTrade Trade;
CPositionInfo Position;
CAccountInfo Account;

input bool Close_by_Time = true;                      //Allow Closing by Time
input double Time_to_Close = 48;                      //Time to Close (hours)


//Obviously in OnTick ----->     if(Close_by_Time)CloseTradesbyTime();

void CloseTradesbyTime()
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      Position.SelectByIndex(i);
      datetime Fecha1 = Position.Time();
      datetime Fecha2 = TimeCurrent();
      MqlDateTime str1, str2;
      TimeToStruct(Fecha1, str1);
      TimeToStruct(Fecha2, str2);

   
      if(str2.hour - str1.hour >= Time_to_Close)
        {
         Trade.PositionClose(Position.Ticket(), -1);
         Print ("Trade Closed by Max Time Allowed");
        }
     }
  }



Backtest info:


Testing trading strategies on real ticks
Testing trading strategies on real ticks
  • www.mql5.com
The article provides the results of testing a simple trading strategy in three modes: "1 minute OHLC", "Every tick" and "Every tick based on real ticks" using actual historical data.
 

      if(str2.hour - str1.hour >= Time_to_Close)
        {
         Trade.PositionClose(Position.Ticket(), -1);
         Print ("Trade Closed by Max Time Allowed");
        }

Surely hour can only be a number between 0 and 23? So the maximum difference will be 23.

input double Time_to_Close = 48;

It can never be 48.

 
Keith Watford #:

Surely hour can only be a number between 0 and 23? So the maximum difference will be 23.

It can never be 48.

You are completely correct, I did not notice this at first, thanks Keith.

But in the backtest I ran it with Value = 2 hours and got the image result, where two trades exceeded that limit (I ran it for every tick)

 
Enrique Enguix #:

You are completely correct, I did not notice this at first, thanks Keith.

But in the backtest I ran it with Value = 2 hours and got the image result, where two trades exceeded that limit (I ran it for every tick)

Sorry, I can't help you there.
I actually hate the MT5 tester compared to MT4 and have no idea what all the dots mean. I only use the tester to check that my EAs are executing correctly, so just look at the trade results and logs.

 

Same problem with code corrected


//+------------------------------------------------------------------+
void CloseTradesbyTime()
  {
   for(int i = PositionsTotal() - 1; i >= 0; i--)
     {
      Position.SelectByIndex(i);
      datetime Fecha1 = Position.Time();
      datetime Fecha2 = TimeCurrent();

      //Si ya han  pasado X horas, cerrar la posición.
      if((Fecha2 - 3600*Time_to_Close) >= Fecha1)
        {
         Trade.PositionClose(Position.Ticket(), -1);
         Print ("Trade Closed by Max Time Allowed");
        }
     }
  }



 
Keith Watford #:

Sorry, I can't help you there.
I actually hate the MT5 tester compared to MT4 and have no idea what all the dots mean. I only use the tester to check that my EAs are executing correctly, so just look at the trade results and logs.

Thank you also for your help. Unfortunately, it was only a couple of weeks ago that I decided to learn mql5, and I felt much more comfortable with mql4, both with code and terminal.


Thank you very much for your time, best regards

 
Finally, I am going to assume that it is a backtest error and that's it, I don't find sense that among thousands of operations one or two do not comply with the norm
 
Enrique Enguix #: Finally, I am going to assume that it is a backtest error and that's it, I don't find sense that among thousands of operations one or two do not comply with the norm

No, I don't believe it is a tester bug! Are you forgetting about weekends and trade session times?

Depending on the symbol you are testing, there are times that the Market is closed (off-hours, weekends, etc.) and during those times, you can't close the position until later on when the trade session is active again.

Also, if you are using OnTick, then remember that your code will only run when there are quotes and that may be later than you want to close the trades.

More importantly, in your code you are not checking if the close operation is successful or not. Did you check the Journal log for failed attempts?

You just assume that it will close. Always check, and report the condition to log so you can analyse the issue.

 
input double Time_to_Close = 48;

if((Fecha2 - 3600*Time_to_Close) >= Fecha1)

This closes after 48 real hours. Which means the first tick every Monday when the market reopens.

Don't you want 48 H1 candles, trading hours?

 
Clear!  I was thinking about the weekends but I wasn't thinking that the strategy tester was counting those hours!  You have helped me a lot.  I don't like having to resort so much to the forum, but the reality is that you are geniuses!
Reason: