2026-01-05 16:50:37 -03:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| SeriesArray.mqh |
|
|
|
|
|
//| |
|
2026-02-15 15:55:35 -03:00
|
|
|
//| KnitPkg for MetaTrader |
|
2026-01-05 16:50:37 -03:00
|
|
|
//| |
|
|
|
|
|
//| MIT License |
|
|
|
|
|
//| Copyright (c) 2025 Douglas Rechia |
|
|
|
|
|
//| |
|
|
|
|
|
//| Generic array wrapper implementing ITimeSeries<T> interface. |
|
|
|
|
|
//| Wraps arbitrary arrays with chronological ordering support. |
|
|
|
|
|
//| |
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
|
|
|
|
|
#include "TimeSeries.mqh"
|
|
|
|
|
|
2026-02-15 15:55:35 -03:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| 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[]);
|
|
|
|
|
};
|
|
|
|
|
|
2026-01-05 16:50:37 -03:00
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
//| SeriesArray — wraps arrays as ISeries<T>
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
template<typename T>
|
|
|
|
|
class TimeSeriesArray : public ITimeSeries<T>
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
T m_array[];
|
|
|
|
|
|
|
|
|
|
public:
|
2026-02-15 15:55:35 -03:00
|
|
|
// 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
|
2026-01-05 16:50:37 -03:00
|
|
|
// 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);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
//+------------------------------------------------------------------+
|