MQL5-Google-Onedrive/.jules/bolt.md
google-labs-jules[bot] ef4b2ed0d5 feat(performance): Add new bar check to OnTick()
💡 What:
This commit introduces a performance optimization to the `OnTick()` function in the main Expert Advisor. It adds a check at the very beginning of the function to determine if a new bar has formed on the signal timeframe.

🎯 Why:
The `OnTick()` function is executed on every incoming price tick, which can happen multiple times per second. The core trading logic, however, is based on analyzing closed bars. Without this check, the EA was performing expensive calculations (like `CopyRates`, indicator calculations, and loop-based analysis) on every single tick, even when no new trading information was available. This was highly inefficient and consumed unnecessary CPU resources.

📊 Impact:
This change dramatically reduces the EA's computational overhead. The expensive logic inside `OnTick()` will now run only once per bar, instead of on every tick. This leads to a significant reduction in CPU usage, making the EA run much more efficiently, especially in volatile markets.

🔬 Measurement:
The improvement can be verified by observing the EA's execution frequency in the MetaTrader 5 "Experts" tab. Before this change, the log would show frequent processing. After this change, processing logs will only appear once at the beginning of each new bar on the selected timeframe.
2026-01-05 17:04:38 +00:00

3 lines
629 B
Markdown

## 2024-07-25 - MQL5 OnTick New Bar Check
**Learning:** A critical performance optimization for bar-based MQL5 Expert Advisors is to add a check at the beginning of the `OnTick()` function to exit early if a new bar has not yet formed. This prevents expensive calculations (like `CopyRates`) from running on every price tick, as `OnTick` is triggered by price changes, not bar changes.
**Action:** Always implement a static variable to track the last processed bar's timestamp. At the start of `OnTick`, compare the latest bar's time with the stored time and `return` immediately if they are the same, preventing redundant work.