You should not be doing anything related to history data or trade data in the OnInit event. It should be used strictly for code initialisation (including Indicator handles) and parameter verification and nothing else.
All the data related activity should be handled in the other event handlers, not in the OnInit. Please read the following for how to synchronise and organise the data — Organizing Data Access
Also, please note that the OnTick() event handler is only called for new ticks for the current symbol. It is not suitable for multi-currency.
For multi-currency, you need to adopt a different approach of event handling, such as using OnTimer() or using custom chart events from specialised indicators for this purpose.
The Implementation of a Multi-currency Mode in MetaTrader 5
Konstantin Gruzdev, 2011.02.18 17:58
For a long time multi-currency analysis and multi-currency trading has been of interest to people. The opportunity to implement a full fledged multi-currency regime became possible only with the public release of MetaTrader 5 and the MQL5 programming language. In this article we propose a way to analyze and process all incoming ticks for several symbols. As an illustration, let's consider a multi-currency RSI indicator of the USDx dollar index.
You should not be doing anything related to history data or trade data in the OnInit event. It should be used strictly for code initialisation (including Indicator handles) and parameter verification and nothing else.
All the data related activity should be handled in the other event handlers, not in the OnInit. Please read the following for how to synchronise and organise the data — Organizing Data Access
Also, please note that the OnTick() event handler is only called for new ticks for the current symbol. It is not suitable for multi-currency.
For multi-currency, you need to adopt a different approach of event handling, such as using OnTimer() or using custom chart events from specialised indicators for this purpose.
Thank you, Fernando! I really appreciate your help. I will read the documentation the documentation and work on resolving this issue.
I'm not into MQL5 but searching on internet i found a lot of person suggesting using a loop over symbol inside OnTick to obtain a multi currency expert advisor. Are you indicating that solutions like this one might not be the best approach?
- darwinex
- github.com
Imagine some of the symbols you were trading were cryptos (e.g BTCUSD), but the current chart on which the EA is running is EURUSD.
So, during the week end, no ticks arrive for EURUSD, and so the OnTick() event handler is never called.
How will you process the data for BTCUSD (which is 24/7), if there are no incoming ticks on EURUSD (which is only 24/5)?
You can still use OnTick() but you need to combine it with other event handlers so that processing can be carried out.
Examples are using OnTimer(), or using OnChartEvent() with custom events being processed for specialised indicators that are open for the required symbols.
But there can be various ways of tackling the issue depending on the strategy's requirements.
Imagine some of the symbols you were trading were cryptos (e.g BTCUSD), but the current chart on which the EA is running is EURUSD.
So, during the week end, no ticks arrive for EURUSD, and so the OnTick() event handler is never called.
How will you process the data for BTCUSD (which is 24/7), if there are no incoming ticks on EURUSD (which is only 24/5)?
You can still use OnTick() but you need to combine it with other event handlers so that processing can be carried out.
Examples are using OnTimer(), or using OnChartEvent() with custom events being processed for specialised indicators that are open for the required symbols.
But there can be various ways of tackling the issue depending on the strategy's requirements.
Thank you both for your answer!
After delving into the documentation, it seems that employing an indicator to trigger OnChartEvent is considered the most effective solution. However I read that it doesn't work for backtesting. This poses a bit of a challenge for me, as optimizing the EA relies heavily on the results derived from backtesting.
I'm wondering if anyone has come across a workaround for this particular issue. Any suggestions or alternative approaches would be greatly appreciated!
It does work in the Strategy Tester as long as it is the EA processing the custom chart events — "When testing in an EA, we can handle custom events using the OnChartEvent() function"
Thank you both for your answer!
After delving into the documentation, it seems that employing an indicator to trigger OnChartEvent is considered the most effective solution. However I read that it doesn't work for backtesting. This poses a bit of a challenge for me, as optimizing the EA relies heavily on the results derived from backtesting.
I'm wondering if anyone has come across a workaround for this particular issue. Any suggestions or alternative approaches would be greatly appreciated!
You can use this ready-made wrapper to immediately write code without being distracted by the features of the tester.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi everyone, like the title I have this weird problem
The onInit function is the following. The symbols are stored in the input variable Symbols separated by ",". I have some array for checking new bar in different timefrimes (such as TimeLastCheckTickProcessed) for each symbol and then I have an array of OBDealer a custom class that use a custom indicator to manage the EA.
I provide you the code of OnInit because somewhere I read that MQL5 download history basing on the OnInit function.. but I don't know if it makes any sense.
Have you got some idea? Thanks