mql4-bar/knitpkg/include/douglasrechia/bar/TimeSeriesArray.mqh

90 lines
3.3 KiB
MQL5

//+------------------------------------------------------------------+
//| SeriesArray.mqh |
//| |
//| KnitPkg for MetaTrader |
//| |
//| MIT License |
//| Copyright (c) 2025 Douglas Rechia |
//| |
//| Generic array wrapper implementing ITimeSeries<T> interface. |
//| Wraps arbitrary arrays with chronological ordering support. |
//| |
//+------------------------------------------------------------------+
#include "TimeSeries.mqh"
//+------------------------------------------------------------------+
//| ITimeSeriesArraySetup — interface for custom time series setup
//| Extend this interface to populate arrays in time series order
//| (index 0 = most recent element)
//+------------------------------------------------------------------+
template<typename T>
interface ITimeSeriesArraySetup
{
public:
// Populates array in time series order (index 0 = most recent)
void Setup(T &array[]);
};
//+------------------------------------------------------------------+
//| SeriesArray — wraps arrays as ISeries<T>
//+------------------------------------------------------------------+
template<typename T>
class TimeSeriesArray : public ITimeSeries<T>
{
private:
T m_array[];
public:
// Constructor — populates m_array via custom setup
// setup: object implementing ITimeSeriesArraySetup<T> that populates
// the array in time series order (index 0 = most recent element)
TimeSeriesArray(ITimeSeriesArraySetup<T> &setup)
{
setup.Setup(m_array);
}
// Constructor — copies data from source array
// array - source array with data to be copied into this time series.
// startIndex, endIndex: range within source array (-1 = full range)
// isSourceTimeseries: use true if source array is a time series
// (0 index contains the most recent data) or
// false otherwise
TimeSeriesArray(const T &array[], int startIndex=-1, int endIndex=-1, bool isSourceTimeseries=true)
{
if(startIndex < 0)
startIndex = 0;
if(endIndex < 0)
endIndex = ArraySize(array)-1;
int m = endIndex - startIndex + 1;
ArrayResize(m_array, m);
int i,j;
if(isSourceTimeseries)
{
j=0;
for(i = startIndex; i <= endIndex; i++)
m_array[j++] = array[i];
}
else
{
j=m-1;
for(i = startIndex; i <= endIndex; i++)
m_array[j--] = array[i];
}
}
// Returns element at the given index
virtual T ValueAtShift(int shift = 0)
{
return m_array[shift];
}
// Returns the total number of elements
int Size()
{
return ArraySize(m_array);
}
};
//+------------------------------------------------------------------+