//+------------------------------------------------------------------+
//|                                     DailyAggregatorTypes.mqh     |
//|                     Minimal data holders for the aggregator      |
//+------------------------------------------------------------------+
#ifndef DAILYAGGREGATORTYPES_MQH
#define DAILYAGGREGATORTYPES_MQH

//+------------------------------------------------------------------+
//| CM1BarSample                                                     |
//| A minimal, immutable reduction of one M1 bar: just the fields the|
//| rest of the pipeline actually needs.                             |
//+------------------------------------------------------------------+
class CM1BarSample
  {
private:
   datetime          m_time;
   double            m_open;
   double            m_high;
   double            m_low;
   double            m_close;
   long              m_tick_volume;

public:
                     CM1BarSample(void);
                     CM1BarSample(const datetime time,
                const double open,
                const double high,
                const double low,
                const double close,
                const long tick_volume);

   datetime          GetTime(void) const;
   double            GetOpen(void) const;
   double            GetHigh(void) const;
   double            GetLow(void) const;
   double            GetClose(void) const;
   long              GetTickVolume(void) const;
  };

//+------------------------------------------------------------------+
//| Default constructor.                                             |
//+------------------------------------------------------------------+
CM1BarSample::CM1BarSample(void)
  {
//--- Returns a sample with every field zeroed, useful only as an
//--- array placeholder before real data is assigned.
   m_time        = 0;
   m_open        = 0.0;
   m_high        = 0.0;
   m_low         = 0.0;
   m_close       = 0.0;
   m_tick_volume = 0;
  }

//+------------------------------------------------------------------+
//| Parameterized constructor.                                       |
//| Assumptions: the caller supplies values taken directly from one  |
//|              MqlRates element returned by CopyRates().           |
//| Returns: a fully populated, read-only sample.                    |
//+------------------------------------------------------------------+
CM1BarSample::CM1BarSample(const datetime time,
                           const double open,
                           const double high,
                           const double low,
                           const double close,
                           const long tick_volume)
  {
   m_time        = time;
   m_open        = open;
   m_high        = high;
   m_low         = low;
   m_close       = close;
   m_tick_volume = tick_volume;
  }

//+------------------------------------------------------------------+
//| CM1BarSample::GetTime                                            |
//| Returns: the bar's open time.                                    |
//+------------------------------------------------------------------+
datetime CM1BarSample::GetTime(void) const
  {
   return(m_time);
  }

//+------------------------------------------------------------------+
//| CM1BarSample::GetOpen                                            |
//| Returns: the bar's open price.                                   |
//+------------------------------------------------------------------+
double CM1BarSample::GetOpen(void) const
  {
   return(m_open);
  }

//+------------------------------------------------------------------+
//| CM1BarSample::GetHigh                                            |
//| Returns: the bar's high price.                                   |
//+------------------------------------------------------------------+
double CM1BarSample::GetHigh(void) const
  {
   return(m_high);
  }

//+------------------------------------------------------------------+
//| CM1BarSample::GetLow                                             |
//| Returns: the bar's low price.                                    |
//+------------------------------------------------------------------+
double CM1BarSample::GetLow(void) const
  {
   return(m_low);
  }

//+------------------------------------------------------------------+
//| CM1BarSample::GetClose                                           |
//| Returns: the bar's close price.                                  |
//+------------------------------------------------------------------+
double CM1BarSample::GetClose(void) const
  {
   return(m_close);
  }

//+------------------------------------------------------------------+
//| CM1BarSample::GetTickVolume                                      |
//| Returns: the bar's tick volume.                                  |
//+------------------------------------------------------------------+
long CM1BarSample::GetTickVolume(void) const
  {
   return(m_tick_volume);
  }

//+------------------------------------------------------------------+
//| CDailyBar                                                        |
//| One aggregated synthetic daily bar, plus the session day key it  |
//| was grouped under and a count of contributing M1 bars.           |
//+------------------------------------------------------------------+
class CDailyBar
  {
private:
   datetime          m_day_key;
   datetime          m_open_time;
   double            m_open;
   double            m_high;
   double            m_low;
   double            m_close;
   long              m_volume;
   int               m_sample_count;

public:
                     CDailyBar(void);

   void              InitializeFromFirstSample(const datetime day_key,const CM1BarSample &first_sample);
   void              ApplySubsequentSample(const CM1BarSample &sample);

   datetime          GetDayKey(void) const;
   datetime          GetOpenTime(void) const;
   double            GetOpen(void) const;
   double            GetHigh(void) const;
   double            GetLow(void) const;
   double            GetClose(void) const;
   long              GetVolume(void) const;
   int               GetSampleCount(void) const;
  };

//+------------------------------------------------------------------+
//| CDailyBar::CDailyBar                                             |
//| Default constructor.                                             |
//| Assumptions: none.                                               |
//| Returns: an empty daily bar with zeroed fields; not valid for use|
//|          until InitializeFromFirstSample() has been called.      |
//+------------------------------------------------------------------+
CDailyBar::CDailyBar(void)
  {
   m_day_key      = 0;
   m_open_time    = 0;
   m_open         = 0.0;
   m_high         = 0.0;
   m_low          = 0.0;
   m_close        = 0.0;
   m_volume       = 0;
   m_sample_count = 0;
  }

//+------------------------------------------------------------------+
//| CDailyBar::InitializeFromFirstSample                             |
//| Sets every field of this daily bar from the single M1 sample that|
//| starts a new session day group.                                  |
//| Assumptions: this is the first sample seen for this day_key.     |
//| Returns: nothing. After this call, open, high, low, and close all|
//|          equal first_sample's own values, and sample_count is 1, |
//|          which is the correct outcome for a day that never       |
//|          receives a second M1 bar.                               |
//+------------------------------------------------------------------+
void CDailyBar::InitializeFromFirstSample(const datetime day_key,const CM1BarSample &first_sample)
  {
   m_day_key      = day_key;
   m_open_time    = first_sample.GetTime();
   m_open         = first_sample.GetOpen();
   m_high         = first_sample.GetHigh();
   m_low          = first_sample.GetLow();
   m_close        = first_sample.GetClose();
   m_volume       = first_sample.GetTickVolume();
   m_sample_count = 1;
  }

//+------------------------------------------------------------------+
//| CDailyBar::ApplySubsequentSample                                 |
//| Folds one more M1 sample into an already-initialized daily bar.  |
//| Assumptions: InitializeFromFirstSample() has already been called,|
//|              and sample is chronologically after every sample    |
//|              already applied, per CopyRates()'s ascending order  |
//|              guarantee.                                          |
//| Returns: nothing. High becomes the running maximum, low the      |
//|          running minimum, close becomes this sample's close, and |
//|          volume and sample_count accumulate.                     |
//+------------------------------------------------------------------+
void CDailyBar::ApplySubsequentSample(const CM1BarSample &sample)
  {
   m_high    = MathMax(m_high,sample.GetHigh());
   m_low     = MathMin(m_low,sample.GetLow());
   m_close   = sample.GetClose();
   m_volume += sample.GetTickVolume();
   m_sample_count++;
  }

//+------------------------------------------------------------------+
//| CDailyBar::GetDayKey                                             |
//| Returns: the session day key this bar was grouped under.         |
//+------------------------------------------------------------------+
datetime CDailyBar::GetDayKey(void) const
  {
   return(m_day_key);
  }

//+------------------------------------------------------------------+
//| CDailyBar::GetOpenTime                                           |
//| Returns: the actual timestamp of the first contributing M1 bar,  |
//|          which is what gets written to the custom symbol.        |
//+------------------------------------------------------------------+
datetime CDailyBar::GetOpenTime(void) const
  {
   return(m_open_time);
  }

//+------------------------------------------------------------------+
//| CDailyBar::GetOpen                                               |
//| Returns: the daily open price.                                   |
//+------------------------------------------------------------------+
double CDailyBar::GetOpen(void) const
  {
   return(m_open);
  }

//+------------------------------------------------------------------+
//| CDailyBar::GetHigh                                               |
//| Returns: the daily high price.                                   |
//+------------------------------------------------------------------+
double CDailyBar::GetHigh(void) const
  {
   return(m_high);
  }

//+------------------------------------------------------------------+
//| CDailyBar::GetLow                                                |
//| Returns: the daily low price.                                    |
//+------------------------------------------------------------------+
double CDailyBar::GetLow(void) const
  {
   return(m_low);
  }

//+------------------------------------------------------------------+
//| CDailyBar::GetClose                                              |
//| Returns: the daily close price.                                  |
//+------------------------------------------------------------------+
double CDailyBar::GetClose(void) const
  {
   return(m_close);
  }

//+------------------------------------------------------------------+
//| CDailyBar::GetVolume                                             |
//| Returns: the summed tick volume across all contributing M1 bars. |
//+------------------------------------------------------------------+
long CDailyBar::GetVolume(void) const
  {
   return(m_volume);
  }

//+------------------------------------------------------------------+
//| CDailyBar::GetSampleCount                                        |
//| Returns: how many M1 bars contributed to this daily bar.         |
//+------------------------------------------------------------------+
int CDailyBar::GetSampleCount(void) const
  {
   return(m_sample_count);
  }

#endif // DAILYAGGREGATORTYPES_MQH
//+------------------------------------------------------------------+