//+------------------------------------------------------------------+ //| Runtime.mqh | //| Copyright 2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2025, MetaQuotes Ltd." #property link "https://www.mql5.com" #property version "1.00" #include "Market/DataManager.mqh" #include "Market/DataProcessor.mqh" #include "Events/EventBus/EventBus.mqh" #include "Events/LoadingListener.mqh" //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class ListenerRegistration { public: EventType eventType; IEventListener* listener; }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ class Runtime final { private: bool init; EventBus* eventBus; DataManager* dataManager; DataProcessor* dataProcessor; LoadingListener* loaderListener; CArrayList* m_registrations; void SubscribeAll(); void UnsubscribeAll(); void ClearRegistrations(); public: Runtime(); ~Runtime(); int Initialize(EAConfig &eaConfig); void Update(int count = 3); void AddListener(EventType eventType, IEventListener* listener); void AddAnalyser(AbstractAnalyser* analyser) { dataProcessor.AddAnalyser(analyser) ; } }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ Runtime::Runtime() { init = false; eventBus = new EventBus(); dataManager = new DataManager(eventBus); dataProcessor = new DataProcessor(eventBus, dataManager); m_registrations = new CArrayList(); loaderListener = new LoadingListener(); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ Runtime::~Runtime() { UnsubscribeAll(); ClearRegistrations(); delete dataManager; delete eventBus; delete dataProcessor; delete m_registrations; delete loaderListener; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int Runtime::Initialize(EAConfig &eaConfig) { if(!loaderListener.Initialize()) return INIT_FAILED; eventBus.Subscribe(LOADING, loaderListener); // --- Loading sequence ------------------------------------------- eventBus.Push(new LoadingEvent(START, 6, "Starting...")); eventBus.Dispatch(); eventBus.Push(new LoadingEvent(UPDATE, "Initializing Data Manager...")); eventBus.Dispatch(); dataManager.Initialize(eaConfig); eventBus.Push(new LoadingEvent(UPDATE, "Subscribing...")); eventBus.Dispatch(); AddListener(NEW_BAR, dataProcessor); SubscribeAll(); Sleep(500); eventBus.Push(new LoadingEvent(UPDATE, "Initializing Trade Executor...")); eventBus.Dispatch(); //executor.SetMagic(eaConfig.magicNumber); //executor.SetDeviation(eaConfig.Slippage); Sleep(500); eventBus.Push(new LoadingEvent(UPDATE, "Initializing Systems...")); eventBus.Dispatch(); Sleep(500); eventBus.Push(new LoadingEvent(UPDATE, "Launching...")); eventBus.Dispatch(); Sleep(500); eventBus.Push(new LoadingEvent(UPDATE, "Finished")); eventBus.Dispatch(); Sleep(25); eventBus.Push(new LoadingEvent(CLOSE, "")); eventBus.Dispatch(); return INIT_SUCCEEDED; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void Runtime::Update(int count = 3) { if(init == true) { dataManager.Update(count); } else { dataProcessor.Launch(); init = true; } eventBus.Dispatch(); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void Runtime::AddListener(EventType eventType, IEventListener* listener) { ListenerRegistration* reg = new ListenerRegistration(); reg.eventType = eventType; reg.listener = listener; m_registrations.Add(reg); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void Runtime::SubscribeAll() { for(int i = 0; i < m_registrations.Size(); i++) { ListenerRegistration* reg = m_registrations.At(i); eventBus.Subscribe(reg.eventType, reg.listener); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void Runtime::UnsubscribeAll() { for(int i = 0; i < m_registrations.Size(); i++) { ListenerRegistration* reg = m_registrations.At(i); eventBus.Unsubscribe(reg.eventType, reg.listener); } } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void Runtime::ClearRegistrations() { for(int i = 0; i < m_registrations.Size(); i++) delete m_registrations.At(i); m_registrations.Clear(); } //+------------------------------------------------------------------+