144 lines
5.9 KiB
MQL5
144 lines
5.9 KiB
MQL5
//+------------------------------------------------------------------+
|
|
//| BarArray.mqh |
|
|
//| |
|
|
//| KnitPkg for MetaTrader |
|
|
//| |
|
|
//| MIT License |
|
|
//| Copyright (c) 2025 Douglas Rechia |
|
|
//| |
|
|
//| Array-based bar data provider. Wraps pre-loaded OHLCV arrays |
|
|
//| with automatic series ordering and type-safe accessors. |
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
|
|
#include "BarTimeSeries.mqh"
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| BarArray — bar data from arrays
|
|
//+------------------------------------------------------------------+
|
|
|
|
|
|
//+------------------------------------------------------------------+
|
|
//| |
|
|
//+------------------------------------------------------------------+
|
|
class BarArray : public IBar
|
|
{
|
|
private:
|
|
datetime m_timeArray[];
|
|
double m_openArray[];
|
|
double m_highArray[];
|
|
double m_lowArray[];
|
|
double m_closeArray[];
|
|
long m_volumeArray[];
|
|
long m_tickVolumeArray[];
|
|
|
|
ITimeSeries<datetime>* m_timeSeries;
|
|
ITimeSeries<double>* m_openSeries;
|
|
ITimeSeries<double>* m_highSeries;
|
|
ITimeSeries<double>* m_lowSeries;
|
|
ITimeSeries<double>* m_closeSeries;
|
|
ITimeSeries<long>* m_volumeSeries;
|
|
ITimeSeries<long>* m_tickVolumeSeries;
|
|
|
|
public:
|
|
// Constructor — copies data from source arrays
|
|
// asSeries: if true, newest bar at index 0; if false, oldest bar at index 0
|
|
// startIndex, endIndex: range within source arrays (-1 = full range)
|
|
BarArray(const datetime &time[],
|
|
const double &open[],
|
|
const double &high[],
|
|
const double &low[],
|
|
const double &close[],
|
|
const long &volume[],
|
|
const long &tickVolume[],
|
|
int startIndex=-1, int endIndex=-1, bool isSourceTimeseries=true)
|
|
{
|
|
m_timeSeries = new BarTimeSeries(GetPointer(this));
|
|
m_openSeries = new BarOpenTimeSeries(GetPointer(this));
|
|
m_highSeries = new BarHighTimeSeries(GetPointer(this));
|
|
m_lowSeries = new BarLowTimeSeries(GetPointer(this));
|
|
m_closeSeries = new BarCloseTimeSeries(GetPointer(this));
|
|
m_volumeSeries = new BarVolumeTimeSeries(GetPointer(this));
|
|
m_tickVolumeSeries = new BarTickVolumeTimeSeries(GetPointer(this));
|
|
|
|
if(startIndex < 0)
|
|
startIndex = 0;
|
|
if(endIndex < 0)
|
|
endIndex = ArraySize(time)-1;
|
|
int m = endIndex - startIndex + 1;
|
|
|
|
ArrayResize(m_timeArray, m);
|
|
ArrayResize(m_openArray, m);
|
|
ArrayResize(m_highArray, m);
|
|
ArrayResize(m_lowArray, m);
|
|
ArrayResize(m_closeArray, m);
|
|
ArrayResize(m_volumeArray, m);
|
|
ArrayResize(m_tickVolumeArray, m);
|
|
|
|
int i, j;
|
|
if(isSourceTimeseries)
|
|
{
|
|
j=m-1;
|
|
for(i = startIndex; i <= endIndex; i++)
|
|
{
|
|
m_timeArray[j] = time[i];
|
|
m_openArray[j] = open[i];
|
|
m_highArray[j] = high[i];
|
|
m_lowArray[j] = low[i];
|
|
m_closeArray[j] = close[i];
|
|
m_volumeArray[j] = volume[i];
|
|
m_tickVolumeArray[j] = tickVolume[i];
|
|
j--;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
j=0;
|
|
for(i = startIndex; i <= endIndex; i++)
|
|
{
|
|
m_timeArray[j] = time[i];
|
|
m_openArray[j] = open[i];
|
|
m_highArray[j] = high[i];
|
|
m_lowArray[j] = low[i];
|
|
m_closeArray[j] = close[i];
|
|
m_volumeArray[j] = volume[i];
|
|
m_tickVolumeArray[j] = tickVolume[i];
|
|
j++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Destructor
|
|
~BarArray()
|
|
{
|
|
delete m_timeSeries;
|
|
delete m_openSeries;
|
|
delete m_highSeries;
|
|
delete m_lowSeries;
|
|
delete m_closeSeries;
|
|
delete m_volumeSeries;
|
|
delete m_tickVolumeSeries;
|
|
}
|
|
|
|
// Individual bar accessors (shift = 0 is the current bar)
|
|
datetime Time(int shift = 0) { return m_timeArray[shift]; }
|
|
double Open(int shift = 0) { return m_openArray[shift]; }
|
|
double High(int shift = 0) { return m_highArray[shift]; }
|
|
double Low(int shift = 0) { return m_lowArray[shift]; }
|
|
double Close(int shift = 0) { return m_closeArray[shift]; }
|
|
long Volume(int shift = 0) { return m_volumeArray[shift]; }
|
|
long TickVolume(int shift = 0) { return m_tickVolumeArray[shift]; }
|
|
|
|
// Typed series accessors
|
|
ITimeSeries<datetime>* TimeSeries() { return m_timeSeries; }
|
|
ITimeSeries<double>* OpenSeries() { return m_openSeries; }
|
|
ITimeSeries<double>* HighSeries() { return m_highSeries; }
|
|
ITimeSeries<double>* LowSeries() { return m_lowSeries; }
|
|
ITimeSeries<double>* CloseSeries() { return m_closeSeries; }
|
|
ITimeSeries<long>* VolumeSeries() { return m_volumeSeries; }
|
|
ITimeSeries<long>* TickVolumeSeries() { return m_tickVolumeSeries; }
|
|
|
|
// Returns the total number of bars loaded
|
|
int Size() { return ArraySize(m_timeArray); }
|
|
};
|
|
//+------------------------------------------------------------------+
|