[Backtesting] A question about the difference between "Control points ..." and "Open prices only ...)

To add comments, please log in or register
thomas2004
337
thomas2004  

Hi all,

My tes EA looks as follow. I just want to trade in the defined time interval since in different interval the spead is different.

//+------------------------------------------------------------------+
//|                                            TradingTimeFilter.mq4 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern bool UseTradingHours = true;
extern int OpenHour = 09;
extern int OpenMin = 15;
extern int CloseHour = 22;
extern int CloseMin = 45;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   Print("Current Time: ", TimeCurrent());
   if (UseTradingHours == true)
   {
      Print("UseTradingHours is TRUE!");
      if (TradingHours() == true)
         Print("TradingHours is TRUE.");
   }   
  }
//+------------------------------------------------------------------+

bool TradingHours()
{
   if(CloseHour>OpenHour) //within the day
   {
      if (OpenHour < TimeHour(TimeCurrent()) && TimeHour(TimeCurrent()) < CloseHour)
         {
         Comment("Open For Trading");
         return(true);
         }
      if (OpenHour == TimeHour(TimeCurrent()))
      {
         if(OpenMin<=TimeMinute(TimeCurrent()))
         {
         Comment("Open For Trading");
         return(true);
         }
         return(false);
      }
      
      if (CloseHour == TimeHour(TimeCurrent()))
      {
         if(CloseMin>=TimeMinute(TimeCurrent()))
         {
         Comment("Open For Trading");
         return(true);
         }
         return(false);
      }
      Comment("Closed");
      return(false);
   }
   if(OpenHour>CloseHour)  //Spanning two days
   {
      if (CloseHour < TimeHour(TimeCurrent()) && TimeHour(TimeCurrent()) < OpenHour)
         {
         Comment("Closed");
         return(false);
         }
      if (OpenHour == TimeHour(TimeCurrent()))
      {
         if(OpenMin<=TimeMinute(TimeCurrent()))
         {
         Comment("Open For Trading");
         return(true);
         }
         return(false);
      }
      if (CloseHour == TimeHour(TimeCurrent()))
      {
         if(CloseMin>=TimeMinute(TimeCurrent()))
         {
         Comment("Open For Trading");
         return(true);
         }
         return(false);
      }
      Comment("Open For Trading");
      return(true);
   }
   return(false);
}


By backtesting if I select the "Open prices only ...", I can see in the Journal following:

...

2018.04.18 11:05:29.357	DAX.,Daily: 383 tick events (1125 bars, 1507 bar states) processed in 0:00:00.015 (total time 0:00:00.015)
2018.04.18 11:05:29.357	2018.03.29 23:59:59  TradingTimeFilter DAX.,Daily: TradingHours is FALSE.
2018.04.18 11:05:29.357	2018.03.29 23:59:59  TradingTimeFilter DAX.,Daily: UseTradingHours is TRUE!
2018.04.18 11:05:29.357	2018.03.29 23:59:59  TradingTimeFilter DAX.,Daily: Current Time: 2018.03.29 23:59:59
2018.04.18 11:05:29.357	2018.03.29 00:00:00  TradingTimeFilter DAX.,Daily: TradingHours is FALSE.
2018.04.18 11:05:29.357	2018.03.29 00:00:00  TradingTimeFilter DAX.,Daily: UseTradingHours is TRUE!
2018.04.18 11:05:29.357	2018.03.29 00:00:00  TradingTimeFilter DAX.,Daily: Current Time: 2018.03.29 00:00:00
2018.04.18 11:05:29.357	2018.03.28 00:00:00  TradingTimeFilter DAX.,Daily: TradingHours is FALSE.


One can see the TradingHours() returns FALSE

But if I use the "Control points ..." I can see more as follow:

...

2018.04.18 11:02:43.254 2018.03.29 22:20:00  TradingTimeFilter DAX.,Daily: Current Time: 2018.03.29 22:20:00
2018.04.18 11:02:43.254 2018.03.29 21:53:20  TradingTimeFilter DAX.,Daily: TradingHours is TRUE.
2018.04.18 11:02:43.254 2018.03.29 21:53:20  TradingTimeFilter DAX.,Daily: UseTradingHours is TRUE!
2018.04.18 11:02:43.254 2018.03.29 21:53:20  TradingTimeFilter DAX.,Daily: Current Time: 2018.03.29 21:53:20
2018.04.18 11:02:43.254 2018.03.29 21:26:40  TradingTimeFilter DAX.,Daily: TradingHours is TRUE.
2018.04.18 11:02:43.254 2018.03.29 21:26:40  TradingTimeFilter DAX.,Daily: UseTradingHours is TRUE!
2018.04.18 11:02:43.254 2018.03.29 21:26:40  TradingTimeFilter DAX.,Daily: Current Time: 2018.03.29 21:26:40
2018.04.18 11:02:43.254 2018.03.29 21:00:00  TradingTimeFilter DAX.,Daily: TradingHours is TRUE.
2018.04.18 11:02:43.254 2018.03.29 21:00:00  TradingTimeFilter DAX.,Daily: UseTradingHours is TRUE!


One can see in this case the TradingHours() will return TRUE.


What could be the reason?

thomas2004
337
thomas2004  

I notice if I use the Daily period, I got this problem. If I use the H4 period or less, it will be OK. I guess maybe the daily OPEN price is open outside the time period I defined. But I do want to trade just inside the time period I define. How to solve?

whroeder1
17943
whroeder1  
thomas2004: I guess maybe the daily OPEN price is open outside the time period I defined. But I do want to trade just inside the time period I define. How to solve?
  1. It's not "the daily OPEN price" but daily bar time. The daily bar time is always midnight broker time.
  2. If you control trading time, stop using open or control points.
  3. Simplify your code
    bool TradingHours()
    {
       return in_range(OpenHour*3600 + OpenMin*60, CloseHour*3600 + CloseMin*60);
    }
    #define SECONDS uint
    bool        in_range(SECONDS beg, SECONDS end,
                            datetime when=0){ SECONDS  tod   = time(when);
       return beg < end ? beg <= tod && tod < end : !in_range(end, beg, when);
    }
    
              Find bar of the same time one day ago - Simple Trading Strategies - MQL4 and MetaTrader 4 - MQL4 programming forum
thomas2004
337
thomas2004  
whroeder1:
  1. It's not "the daily OPEN price" but daily bar time. The daily bar time is always midnight broker time.
  2. If you control trading time, stop using open or control points.
  3. Simplify your code          Find bar of the same time one day ago - Simple Trading Strategies - MQL4 and MetaTrader 4 - MQL4 programming forum

Thanks for the answer first.

>> If you control trading time, stop using open or control points.

I will do optimization backtesting. If I stop using the open only or control points and use the Every tick, it will take very very very long time. Any alternative? Maybe I ignore or comment out the control trading time, so I can use the open only or control pint to do the optimization backtesting. Then I choose a optimized parameter set and do the single backtesting by using the Every Tick. Is this OK?

To add comments, please log in or register