Never miss Ticks

 
Hi everyone, so I made a multi-symbol and multi-timeframe EA but the backtest and live results are different, some trades are missed on the live trade. How can this be fixed? Also the trailing stop seems to hit earlier compared to the backtest. Should I just make the EA single timeframe and symbol or is there a way not to miss ticks generated
 

You do not give enough information. For example, what test method did you use? You can not expect "generated" ticks to match live ticks.

Anyways, the EA's OnTick handler only gets called from ticks of the chart it is attached to, or in tester the selected symbol. Put it on a different symbol == different tick stream == different results.

There are ways to never miss ticks (copyticks/copyticksrange) however if your EA is based on bars (indicators) it is almost impossible to correct them on a tick level. The whole structure of the EA should be providing for that in its core logic.

 
Enrique Dangeroux #:

You do not give enough information. For example, what test method did you use? You can not expect "generated" ticks to match live ticks.

Anyways, the EA's OnTick handler only gets called from ticks of the chart it is attached to, or in tester the selected symbol. Put it on a different symbol == different tick stream == different results.

There are ways to never miss ticks (copyticks/copyticksrange) however if your EA is based on bars (indicators) it is almost impossible to correct them on a tick level. The whole structure of the EA should be providing for that in its core logic.

Thanks for the reply the EA is not based on bars. How can I fix then, do I just place the EA on the individual symbols?

 
Derrick Mutange: Hi everyone, so I made a multi-symbol and multi-timeframe EA but the backtest and live results are different, some trades are missed on the live trade. How can this be fixed? Also the trailing stop seems to hit earlier compared to the backtest. Should I just make the EA single timeframe and symbol or is there a way not to miss ticks generated
Derrick Mutange #:Thanks for the reply the EA is not based on bars. How can I fix then, do I just place the EA on the individual symbols?
  1. Is this on MT4 or MT5?
  2. If on MT5, are you processing the ticks via the CopyTicks() function or just reacting to the current tick via OnTicks() handler?
  3. If it is multy-symbol, how are you reacting to tick events of other symbols? Are you using the method of a "subscribed" indicator issuing custom chart events?
  4. You do realise that in live trading stops suffer slippage and that on the Strategy Tester, unless it is with real-ticks the stops do not suffer any realistic slippage?
EDIT: If this is MT4, then please note that the MT4 Strategy Tester does not support multi-symbol trading, and the stops NEVER suffer slippage, so the tests and live trading will ALWAYS be different.
     
    Fernando Carreiro #:
    1. Is this on MT4 or MT5?
    2. If on MT5, are you processing the ticks via the CopyTicks() function or just reacting the current OnTicks() handler?
    3. If it is multy-symbol, how are you reacting to tick events of other symbols? Are you using the method of a "subscribed" indicator issuing custom chart events?
    4. You do realise that in live trading stops suffer slippage and that on the Strategy Tester, unless it is with real-ticks the stops do not suffer any realistic slippage?
    EDIT: If this is MT4, then please note that the MT4 Strategy Tester does not support multi-symbol trading, and the stops NEVER suffer slippage, so the tests and live trading will ALWAYS be different.

      No its MT5 code

       
      Derrick Mutange #:No its MT5 code
      You did not answer the rest of the questions!
       
      Fernando Carreiro #:
      You did not answer the rest of the questions!

      1. No its MT5

      2.Processing with only ontick() handler

      3. It reacts to other symbols since I have an array with the stored symbol like on the code below

      4. Yes

      input string symbols = "EURUD,XAUUSD,US30";
      input symbol_array[];
      
      //+------------------------------------------------------------------+
      int OnInit()
        { 
            if(symbols_trade == "CURRENT") 
               {
                  number_of_symbols = 1;
                  
                  ArrayResize(symbol_array, 1);
                  symbol_array[0] = Symbol(); 
            
                  Print("EA will process ", symbol_array[0], " only");
               }
            else
               {  
                  string symbols_to_use = symbols_trade;
                  
                  number_of_symbols = StringSplit(symbols_to_use, ',', symbol_array);
                  
                  Print("EA will process: ", symbols_to_use);
               }
      }
      
      
      void OnTick()
              {
                      for(int s = 0; s < number_of_symbols; s++)
                              {
                                      string symbol = symbol_array[s];
                                      string ask = SymbolInfoDouble(symbol,SYMBOL_ASK);
                              }
              }
       
      Derrick Mutange #: 1. No its MT5, 2.Processing with only ontick() handler, 3. It reacts to other symbols since I have an array with the stored symbol like on the code below, 4. Yes

      Ok, then that is the problem!

      If you only react on OnTick(), then you are reacting only to ticks from the current symbol. You will miss all tick events from the other symbols.

      Also, if your event handler takes to long then you will also miss ticks from the current symbol too.

      To resolve the issue you will need to make use of a combination of OnTick() and OnTimer() or use a slave indicator to send out a custom chart events to signal tick events on other symbols (see below).

      And to access all the tick data without missing any, you will need to use CopyTicks() function.

      Also read the following ...

      Forum on trading, automated trading systems and testing trading strategies

      Can a buffer update in the middle of two commands?

      Fernando Carreiro, 2022.04.27 13:11

      The OnTick() event handler gets called on every new tick for the current symbol only, but what will you do if there are no new ticks for the current symbol, but there have been ticks for other symbols you are trading with the EA?

      By using the OnTimer() in combination with OnTick(), set at a regular interval, you still get to query the current state of other symbols, even if the current symbol is inactive. Consider it a "heartbeat" function or protocol.

      By using EURUSD as your primary chart, you will have plenty of tick activity since its one of the most active symbols, but it also has lull periods compared to other symbols, or not trade at all compared to some other symbols. What if you are also trading BitCoin on your EA which trades during the weekend, but EURUSD does not?

      By using the OnTimer() as well, your EA will continue to query all the other symbols and act accordingly even if the primary chart is on pause.

      Articles

      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.

       
      Fernando Carreiro #:

      Ok, then that is the problem!

      If you only react on OnTick(), then you are reacting only to ticks from the current symbol. You will miss all tick events from the other symbols.

      Also, if your event handler takes to long then you will also miss ticks from the current symbol too.

      To resolve the issue you will need to make use of a combination of OnTick() and OnTimer() or use a slave indicator to send out a custom chart events to signal tick events on other symbols (see below).

      And to access all the tick data without missing any, you will need to use CopyTicks() function.

      Also read the following ...


      Thank you again, been trying to implent the OnTimeer, do I set an EventKiller on the onit and instead ontick I use OnTimer? Thank you again

       
      Derrick Mutange #:Thank you again, been trying to implent the OnTimeer, do I set an EventKiller on the onit and instead ontick I use OnTimer? Thank you again

      Use both! Have both call a shared function that processes ticks using CopyTicks().

      Use the millisecond timestamp and a duplicate count to synchronise your reading of the ticks.

       
      Fernando Carreiro #:

      Use both! Have both call a shared function that processes ticks using CopyTicks().

      Use the millisecond timestamp and a duplicate count to synchronise your reading of the ticks.

      Thank you so much , let me test it out look at the code below and let me know if I have made any mistakes

      //+------------------------------------------------------------------+
      int OnInit()
        { 
            EventSetTimer(1);
            if(symbols_trade == "CURRENT") 
               {
                  number_of_symbols = 1;
                  
                  ArrayResize(symbol_array, 1);
                  symbol_array[0] = Symbol(); 
            
                  Print("EA will process ", symbol_array[0], " only");
               }
            else
               {  
                  string symbols_to_use = symbols_trade;
                  
                  number_of_symbols = StringSplit(symbols_to_use, ',', symbol_array);
                  
                  Print("EA will process: ", symbols_to_use);
               }
            return(INIT_SUCCEEDED);
        }
      
      //+------------------------------------------------------------------+
      void OnDeinit(const int reason)
        {
            EventKillTimer();
            Comment("");
        }
      
      //+------------------------------------------------------------------+
      void OnTimer()
        {
            OPEN();
            CheckForClose();
        }
      
      //+------------------------------------------------------------------+
      void OnTick()
        {
            OPEN();
            CheckForClose();
        }
      Reason: