Expert Advisors • Indicators • Scripts • Libraries

MQL.RobotFX.org is the biggest collection of MetaTrader expert advisors (MT5 & MT4), indicators, scripts and libraries that can be used to improve trading results, minimize risks or simply automate trading tasks

Metatrader 5 Libraries | OnTickMulti | Expert Advisor Code

Fresh MQL5 code release – perfect for enhancing your MT4/MT5 strategies.

In MT5, the NewTick event only occurs on the symbol on which the EA is running. Therefore, various tricks are used for multisymbol trading.

This library creates a multisymbol OnTick on all specified trading symbols. A convenient wrapper of the old implementation.


Example.

Let's show the work of the library on the example of a multisymbol Expert Advisor that counts the number of incoming ticks for each specified symbol.

#include <fxsaber\OnTickMulti\OnTickMulti.mqh> // Multisymbol OnTick.

int TicksCounter[]; // The counter of incoming ticks of each given character.

void OnInit()
{
  // Initialise the counter of incoming ticks.
  ArrayResize(TicksCounter, ArraySize(OnTickMultiObject.Symbols));
  ArrayInitialize(TicksCounter, 0);
}

double OnTester()
{
  // Print the number of arrived ticks for each given character.
  for (uint i = ArraySize(TicksCounter); (bool)i--;)
    Print(OnTickMultiObject.Symbols[i] + " - " + (string)TicksCounter[i] + " ticks.");

  ArrayPrint(OnTickMultiObject.Symbols); // List of symbols OnTickMulti.

  return(0);
}

// Multisymbol OnTick.
void OnTickMulti( const string &Symb, const uint &Index )
{
  TicksCounter[Index]++; // Increased the counter of incoming ticks by the given symbol.
}


Result.

To check the correctness of the Expert Advisor, let's run it in MT5-tester.

After comma in the input we set the symbols to which the multisymbol OnTick will react.


The tester generated the following (see the Log).

2023.01.12 23:59:59   AUDJPY - 1618389 ticks.
2023.01.12 23:59:59   GBPUSD - 1116822 ticks.
2023.01.12 23:59:59   EURUSD - 906489 ticks.
2023.01.12 23:59:59   "EURUSD" "GBPUSD" "AUDJPY"
final balance 10000.00 pips
OnTester result 0
EURUSD,M1: 906489 ticks, 12897 bars generated. Environment synchronized in 0:00:00.020. Test passed in 0:00:01.723 (including ticks preprocessing 0:00:00.188).
EURUSD,M1: total time from login to stop testing 0:00:01.743 (including 0:00:00.248 for history data synchronization)
3641700 total ticks for all symbols
AUDJPY: generate 1618389 ticks in 0:00:00.078, passed to tester 1618389 ticks
EURUSD: generate 906489 ticks in 0:00:00.047, passed to tester 906489 ticks
GBPUSD: generate 1116822 ticks in 0:00:00.063, passed to tester 1116822 ticks

The highlighted part shows that absolutely all the ticks generated by the Tester for the specified symbols were processed by the multisymbol OnTick.


What for?

  • With this library, the result of the Expert Advisor's work in the Tester can be independent of the selected basic symbol.
  • Ticks on the required symbols are not skipped.

Classic MACD strategy made fully automatic – the MACD Expert Advisor for MT4 handles it all. Check it out.

Thanks for reading. Discover premium MetaTrader solutions at RobotFX.

47647

Best MetaTrader Indicators + Profitable Expert Advisors