//+------------------------------------------------------------------+
//|                                              TickDataReader.mqh  |
//|                        The only wrapper around CopyTicksRange()  |
//+------------------------------------------------------------------+
#ifndef TICKDATAREADER_MQH
#define TICKDATAREADER_MQH

#include "OrderFlowTypes.mqh"

//+------------------------------------------------------------------+
//| Reads the ticks of one bar from the terminal tick history        |
//+------------------------------------------------------------------+
class CTickDataReader
  {
private:
   int               m_last_error;

   uint              ChooseFlags(const ENUM_OF_PRICE_SOURCE source) const;

public:
                     CTickDataReader(void);
                    ~CTickDataReader(void);

   bool              BuildRange(const datetime bar_open,
                                const datetime next_bar_open,
                                ulong &from_msc,
                                ulong &to_msc) const;
   int               ReadBarTicks(const string symbol,
                                  const datetime bar_open,
                                  const datetime next_bar_open,
                                  const ENUM_OF_PRICE_SOURCE source,
                                  MqlTick &ticks[]);
   int               LastError(void) const;
  };

//+------------------------------------------------------------------+
//| Constructor                                                      |
//+------------------------------------------------------------------+
CTickDataReader::CTickDataReader(void)
  {
   m_last_error=0;
  }

//+------------------------------------------------------------------+
//| Destructor                                                       |
//+------------------------------------------------------------------+
CTickDataReader::~CTickDataReader(void)
  {
  }

//+------------------------------------------------------------------+
//| Chooses the copy flags for the price source. Last price needs    |
//| trade ticks; bid price needs info (quote change) ticks.          |
//+------------------------------------------------------------------+
uint CTickDataReader::ChooseFlags(const ENUM_OF_PRICE_SOURCE source) const
  {
   if(source==OF_PRICE_LAST)
      return(COPY_TICKS_TRADE);
   if(source==OF_PRICE_BID)
      return(COPY_TICKS_INFO);
   return(COPY_TICKS_ALL);
  }

//+------------------------------------------------------------------+
//| Converts two bar open times into inclusive millisecond bounds.   |
//| The range is [bar_open*1000, next_bar_open*1000-1], so the end   |
//| of one bar and the start of the next never overlap and leave no  |
//| gap. Returns false when the range is empty or reversed.          |
//+------------------------------------------------------------------+
bool CTickDataReader::BuildRange(const datetime bar_open,
                                 const datetime next_bar_open,
                                 ulong &from_msc,
                                 ulong &to_msc) const
  {
   from_msc=0;
   to_msc=0;
   if(bar_open<=0 || next_bar_open<=bar_open)
      return(false);
   from_msc=(ulong)bar_open*1000;
   to_msc=(ulong)next_bar_open*1000-1;
   return(true);
  }

//+------------------------------------------------------------------+
//| Copies the ticks of one bar into the output array and returns    |
//| the tick count. Returns -1 on failure and records the error code |
//| in LastError(). A return of 0 means the call succeeded but found |
//| no ticks; the caller decides whether that is plausible. The      |
//| first request for a symbol can fail or return nothing while the  |
//| terminal synchronizes tick history, so callers must retry later. |
//+------------------------------------------------------------------+
int CTickDataReader::ReadBarTicks(const string symbol,
                                  const datetime bar_open,
                                  const datetime next_bar_open,
                                  const ENUM_OF_PRICE_SOURCE source,
                                  MqlTick &ticks[])
  {
   m_last_error=0;
   ::ArrayFree(ticks);
//--- millisecond boundaries
   ulong from_msc=0;
   ulong to_msc=0;
   if(!BuildRange(bar_open,next_bar_open,from_msc,to_msc))
     {
      m_last_error=ERR_INVALID_PARAMETER;
      return(-1);
     }
//--- request the ticks
   uint flags=ChooseFlags(source);
   ::ResetLastError();
   int copied=::CopyTicksRange(symbol,ticks,flags,from_msc,to_msc);
   if(copied<0)
     {
      m_last_error=::GetLastError();
      ::ArrayFree(ticks);
      return(-1);
     }
   return(copied);
  }

//+------------------------------------------------------------------+
//| Returns the error code recorded by the last ReadBarTicks() call  |
//+------------------------------------------------------------------+
int CTickDataReader::LastError(void) const
  {
   return(m_last_error);
  }

#endif // TICKDATAREADER_MQH
//+------------------------------------------------------------------+