//+------------------------------------------------------------------+ //| BarMqlRates.mqh | //| | //| KnitPkg for MetaTrader | //| | //| MIT License | //| Copyright (c) 2025 Douglas Rechia | //| | //| Live bar data provider from broker. Fetches and maintains | //| current OHLCV data with automatic series ordering. | //| | //+------------------------------------------------------------------+ #include "BarTimeSeries.mqh" //+------------------------------------------------------------------+ //| BarMqlRates — live bar data from broker //+------------------------------------------------------------------+ //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class BarMqlRates : public IBar { private: string m_symbol; ENUM_TIMEFRAMES m_tf; MqlRates m_rates[]; ITimeSeries* m_timeSeries; ITimeSeries* m_openSeries; ITimeSeries* m_highSeries; ITimeSeries* m_lowSeries; ITimeSeries* m_closeSeries; ITimeSeries* m_volumeSeries; ITimeSeries* m_tickVolumeSeries; public: // Constructor // symbol: symbol to track (NULL = current chart symbol) // tf: timeframe to track (PERIOD_CURRENT = current chart timeframe) BarMqlRates(string symbol = NULL, ENUM_TIMEFRAMES tf = PERIOD_CURRENT); // Destructor ~BarMqlRates(); // Fetch latest bar data from broker // count: number of bars to copy (default: 10) // Returns: true on success, false on failure bool Refresh(int count = 10); // Individual bar accessors (shift = 0 is the current bar) datetime Time(int shift = 0); double Open(int shift = 0); double High(int shift = 0); double Low(int shift = 0); double Close(int shift = 0); long Volume(int shift = 0); long TickVolume(int shift = 0); // Typed series accessors ITimeSeries* TimeSeries(); ITimeSeries* OpenSeries(); ITimeSeries* HighSeries(); ITimeSeries* LowSeries(); ITimeSeries* CloseSeries(); ITimeSeries* VolumeSeries(); ITimeSeries* TickVolumeSeries(); // Returns the total number of bars loaded int Size(); }; //+------------------------------------------------------------------+ BarMqlRates::BarMqlRates(string symbol = NULL, ENUM_TIMEFRAMES tf = PERIOD_CURRENT) { m_symbol = symbol == NULL ? _Symbol : symbol; m_tf = tf == 0 ? (ENUM_TIMEFRAMES)Period() : tf; ArraySetAsSeries(m_rates, 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)); } // Destructor BarMqlRates::~BarMqlRates() { delete m_timeSeries; delete m_openSeries; delete m_highSeries; delete m_lowSeries; delete m_closeSeries; delete m_volumeSeries; delete m_tickVolumeSeries; } // Fetch latest bar data from broker // count: number of bars to copy (default: 10) // Returns: true on success, false on failure bool BarMqlRates::Refresh(int count = 10) { return CopyRates(m_symbol, m_tf, 0, count, m_rates) == count; } // Individual bar accessors (shift = 0 is the current bar) datetime BarMqlRates::Time(int shift = 0) { return m_rates[shift].time; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double BarMqlRates::Open(int shift = 0) { return m_rates[shift].open; } double BarMqlRates::High(int shift = 0) { return m_rates[shift].high; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double BarMqlRates::Low(int shift = 0) { return m_rates[shift].low; } double BarMqlRates::Close(int shift = 0) { return m_rates[shift].close; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ long BarMqlRates::Volume(int shift = 0) { return m_rates[shift].real_volume; } long BarMqlRates::TickVolume(int shift = 0) { return m_rates[shift].tick_volume; } // Typed series accessors ITimeSeries* BarMqlRates::TimeSeries() { return m_timeSeries; } ITimeSeries* BarMqlRates::OpenSeries() { return m_openSeries; } ITimeSeries* BarMqlRates::HighSeries() { return m_highSeries; } ITimeSeries* BarMqlRates::LowSeries() { return m_lowSeries; } ITimeSeries* BarMqlRates::CloseSeries() { return m_closeSeries; } ITimeSeries* BarMqlRates::VolumeSeries() { return m_volumeSeries; } ITimeSeries* BarMqlRates::TickVolumeSeries() { return m_tickVolumeSeries; } // Returns the total number of bars loaded int BarMqlRates::Size() { return ArraySize(m_rates); } //+------------------------------------------------------------------+