﻿//+------------------------------------------------------------------+
//|                                              TickCollector.mqh   |
//|                     Session Volume Profile Engine                |
//+------------------------------------------------------------------+
#ifndef TICK_COLLECTOR_MQH
#define TICK_COLLECTOR_MQH

//+------------------------------------------------------------------+
//| Tick Collector Class                                             |
//+------------------------------------------------------------------+
class CTickCollector
  {
private:
   MqlTick            m_ticks[];
   int                m_count;
   int                m_total_raw;

public:
                     CTickCollector(void);
                    ~CTickCollector(void);
   bool               Collect(const string symbol,
                              datetime session_start,
                              datetime session_end);
   int                GetCount(void)          const;
   double             GetPrice(int index)     const;
   long               GetVolume(int index)    const;
  };

//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CTickCollector::CTickCollector(void)
   : m_count(0),
     m_total_raw(0)
  {
  }

//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CTickCollector::~CTickCollector(void)
  {
   ArrayFree(m_ticks);
  }

//+------------------------------------------------------------------+
//| Collect and filter session ticks using CopyTicksRange            |
//+------------------------------------------------------------------+
bool CTickCollector::Collect(const string symbol,
                             datetime session_start,
                             datetime session_end)
  {
   m_count     = 0;
   m_total_raw = 0;
   ArrayFree(m_ticks);

//--- Convert datetime boundaries to millisecond timestamps
   ulong from_ms = (ulong)session_start * 1000;
   ulong to_ms   = (ulong)session_end   * 1000;

//--- Request all ticks within the session range
   int total = CopyTicksRange(symbol, m_ticks, COPY_TICKS_ALL, from_ms, to_ms);

   if(total <= 0)
     {
      PrintFormat("CopyTicksRange returned %d for %s [%s - %s]",
                  total,
                  symbol,
                  TimeToString(session_start),
                  TimeToString(session_end));
      return(false);
     }

   m_total_raw = total;

//--- Diagnostic: inspect first tick to understand broker volume fields
   if(total > 0)
     {
      PrintFormat("First tick — bid=%.5f  ask=%.5f  last=%.5f  volume=%d  volume_real=%.2f  flags=%d",
                  m_ticks[0].bid,
                  m_ticks[0].ask,
                  m_ticks[0].last,
                  m_ticks[0].volume,
                  m_ticks[0].volume_real,
                  m_ticks[0].flags);
     }

//--- Count valid ticks: any tick with a non-zero bid OR ask price
//--- This handles CFD brokers that report zero in volume and volume_real
   for(int i = 0; i < total; i++)
     {
      bool has_price = (m_ticks[i].bid > 0.0 || m_ticks[i].ask > 0.0);
      bool in_window = (m_ticks[i].time >= session_start &&
                        m_ticks[i].time <= session_end);

      if(has_price && in_window)
         m_count++;
     }

   if(m_count <= 0)
     {
      PrintFormat("No valid price ticks found — raw=%d  symbol=%s", total, symbol);
      return(false);
     }

   PrintFormat("CopyTicksRange: raw=%d  valid=%d  symbol=%s",
               m_total_raw, m_count, symbol);

   return(true);
  }

//+------------------------------------------------------------------+
//| Return number of valid session ticks                             |
//+------------------------------------------------------------------+
int CTickCollector::GetCount(void) const
  {
   return(m_count);
  }

//+------------------------------------------------------------------+
//| Return the most meaningful price for a specific tick             |
//+------------------------------------------------------------------+
double CTickCollector::GetPrice(int index) const
  {
   if(index < 0 || index >= ArraySize(m_ticks))
      return(0.0);

//--- Priority: last traded price → mid price (bid+ask)/2 → bid alone
   if(m_ticks[index].last > 0.0)
      return(m_ticks[index].last);

   if(m_ticks[index].bid > 0.0 && m_ticks[index].ask > 0.0)
      return((m_ticks[index].bid + m_ticks[index].ask) * 0.5);

   return(m_ticks[index].bid);
  }

//+------------------------------------------------------------------+
//| Return the best available volume weight for a specific tick      |
//+------------------------------------------------------------------+
long CTickCollector::GetVolume(int index) const
  {
   if(index < 0 || index >= ArraySize(m_ticks))
      return(0);

//--- Priority: integer volume → volume_real cast → count the tick as 1
   if(m_ticks[index].volume > 0)
      return(m_ticks[index].volume);

   if(m_ticks[index].volume_real > 0.0)
      return((long)MathRound(m_ticks[index].volume_real));

//--- Broker reports no volume at all: treat each tick as unit weight
   return(1);
  }

#endif // TICK_COLLECTOR_MQH
//+------------------------------------------------------------------+