//+------------------------------------------------------------------+
//|                                                SessionWindow.mqh |
//+------------------------------------------------------------------+
#ifndef SESSIONWINDOW_MQH
#define SESSIONWINDOW_MQH

//+------------------------------------------------------------------+
//| CSessionWindow                                                   |
//| Resolves a window (from/to) running from the trade server's      |
//| midnight to the current server time. This is a server calendar-  |
//| day boundary, not the instrument's actual trading session: it    |
//| has no knowledge of exchange hours, intraday breaks, weekend     |
//| closures, or the trading calendar. On an instrument whose real   |
//| session does not align to server midnight (overnight futures,    |
//| for example), this window will not match that session.           |
//+------------------------------------------------------------------+
class CSessionWindow
  {
private:
   string            m_symbol;

public:
                     CSessionWindow(void);
                    ~CSessionWindow(void);
   void              SetSymbol(const string symbol);
   datetime          TodayStart(void);
   bool              Resolve(datetime &from, datetime &to);
  };

//+------------------------------------------------------------------+
//| Constructor                                                      |
//| The constructor defaults to the chart symbol; SetSymbol lets the |
//| caller target a different instrument if needed.                  |
//+------------------------------------------------------------------+
CSessionWindow::CSessionWindow(void)
  {
//--- default to the symbol the EA is attached to
   m_symbol = ::_Symbol;
  }

//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CSessionWindow::~CSessionWindow(void)
  {
  }

//+------------------------------------------------------------------+
//| SetSymbol                                                        |
//| Overrides the symbol used when resolving the session window.     |
//+------------------------------------------------------------------+
void CSessionWindow::SetSymbol(const string symbol)
  {
//--- store the symbol for subsequent Resolve calls
   m_symbol = symbol;
  }

//+------------------------------------------------------------------+
//| TodayStart                                                       |
//| Returns the current day's session open time at midnight server   |
//| time. This is the anchor from which the session window is built. |
//+------------------------------------------------------------------+
datetime CSessionWindow::TodayStart(void)
  {
   MqlDateTime now_struct;
//--- decompose the current server time into its calendar fields
   ::TimeToStruct(::TimeCurrent(), now_struct);
//--- zero out the intraday fields to land on midnight
   now_struct.hour = 0;
   now_struct.min  = 0;
   now_struct.sec  = 0;
//--- recompose the struct back into a datetime value
   return(::StructToTime(now_struct));
  }

//+------------------------------------------------------------------+
//| Resolve                                                          |
//| Fills the from/to range spanning today's server midnight up to   |
//| the current server time. Returns false when the resulting window |
//| is empty or inverted (to <= from). This is not limited to the    |
//| exact instant of a midnight rollover: TimeCurrent() can also     |
//| produce an unusable or stale value when the market is closed     |
//| over a weekend or holiday, when the terminal has no fresh quotes |
//| yet, or immediately after startup before the first tick arrives. |
//| Callers should treat a false return as a routine, recurring      |
//| condition rather than a rare edge case.                          |
//+------------------------------------------------------------------+
bool CSessionWindow::Resolve(datetime &from, datetime &to)
  {
//--- the window starts at today's server midnight and runs to now
   from = TodayStart();
   to   = ::TimeCurrent();
//--- an empty or inverted window means nothing can be aggregated yet
   if(to <= from)
      return(false);
   return(true);
  }

#endif // SESSIONWINDOW_MQH
//+------------------------------------------------------------------+