Confused about OnTick vs OnNewCandle best practices

 

I currently coded an EA all in the OnTick function. Similar to this template:

void OnTick() {
  bool longSignal = check MACD cross up;
  bool shortSignal = check MACD cross down;

  if (OrdersTotal() > 0) {
    if (shortSignal)
      OrderClose(Buy orders);
    if (longSignal)
      OrderClose(Sell orders);
  }

  if (OrdersTotal() == 0) {
    if (longSignal)
      OrderSend(Buy);
    if (shortSignal)
      OrderSend(Sell);
  }
}

I noticed this works how I want and expect during backtesting. 


When forward testing live though, I am seeing it do things like spam multiple orders within seconds, even though it is on the Daily chart. 


After researching this phenomenon, I found some information about how this might be related to ticks vs candles. I was wondering if someone could tell me more about this idea and point me in the right direction and provide some guidance on what should be done OnTick vs. OnNewCandle? I already noticed that probably OrderSend() should be done only on new candles or else it will spam orders like I saw. Should things like signals also be checked only on new candles?


Also, during backtesting I usually pick the "on Open Prices" option because the "on Ticks" option takes much longer. Is this the reason why I didn't notice any issues during backtesting? When using this testing mode, does it just automatically take OnTick() and only run it for new candles?


Please advise. Thank you!

Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
Documentation on MQL5: Constants, Enumerations and Structures / Indicator Constants / Price Constants
  • www.mql5.com
Calculations of technical indicators require price values and/or values of volumes, on which calculations will be performed. There are 7 predefined identifiers from the ENUM_APPLIED_PRICE enumeration, used to specify the desired price base for calculations. If a technical indicator uses for calculations price data, type of which is set by...
 

please check your long or short signal carefully, they must be mutually exclusive.

anyway, if you need to run some functions once a candle, you can use Volume() function to check the volume of the latest candle.

if(Volume[0]==1) {}   //run some functions once when a new candle appeared
 

For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
          New candle - MQL4 programming forum #3 2014.04.04

I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.

 
jackycode:

anyway, if you need to run some functions once a candle, you can use Volume() function to check the volume of the latest candle.

I really wouldn't use Volume[0] to test for new bars. There have been repeated real-life tests saying that it's not fully reliable.

For example: https://www.mql5.com/en/forum/125906#comment_3272369 and the topics it links to. While those discussions are over 10 years old, I wouldn't assume that it's become any more reliable during that time.

 
Yuan Gao #:

please check your long or short signal carefully, they must be mutually exclusive.

anyway, if you need to run some functions once a candle, you can use Volume() function to check the volume of the latest candle.

Are you a genius? it took me 5 lines to do what you could in 1 sentence. 
Reason: