Is it possible to work with live ticks in MQL5 indicators? - page 2

 
Fernando Carreiro #:

I have already given you an example of just that, from the CodeBase, in my post .

That CodeBase publication serves as an example for both "live" and/or historical tick data. The process is the similar.

I guess this is it then, it's complicated, but I guess this is the way to go about it in mql5. I couldn't find something like that, so thanks for posting

 
Fernando Carreiro #:

No, I did not say that it does not "skip" ticks. That is incorrect. What I said was that it processes "tick events".

It processes ticks in the same way as OnTick() and It DOES SKIP ticks if the processing takes too long.

Are you sure OnCalculate() skips ticks??

//+------------------------------------------------------------------+
//|                                                   Playground.mq5 |
//|              Copyright 2022, Freie Netze UG (haftungsbeschränkt) |
//|                                       https://www.freie-netze.de |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
#property indicator_plots 0


// Global static variables
static datetime current_candle  = NULL;
static int      call_counter    = NULL;

int OnInit()
{
    // Return 
    return(INIT_SUCCEEDED);
}



void OnDeinit(const int reason)
{
    // Return
    return;
}



int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
{
    // Local init
    const int index = rates_total - 1;

    // New period
    if(current_candle < time[index])
    {
        call_counter    = NULL;
    }
    current_candle = time[index];
    call_counter++;
    
    // print current call details
    printf("Period Time: %s; Tick Volume: %llu; Call counter: %i", TimeToString(time[index]), tick_volume[index], call_counter); 
        
    ulong wait_timer = GetTickCount() + 2500;
    while(wait_timer > GetTickCount());

    // Return
    return(rates_total);
}

EDIT:

As far as I have experienced, it does not skip ticks....

 
Dominik Egert #: Are you sure OnCalculate() skips ticks??

It was a long time ago when I experimented. However, since MetaTrader has evolved a lot, I may be wrong now. I will have to test it again.

Also, when the OnCalculate() takes too long to process, MetaTrader logs a warning to the Experts log.

 
Dominik Egert #:

Are you sure OnCalculate() skips ticks??

EDIT:

As far as I have experienced, it does not skip ticks....

The purpose of this function is work with the data passed in the parameters, which are ohlc bars, not tick data.
To check need use CopyTicks in OnCalculate to know how many ticks has arrived since the last call.
Also the result will depends on the liquidity of the symbol, as in some markets and symbol we may have tens of ticks every milisecond while in others just feel ticks by minute.
 

See documentation.

A program gets events only from the chart it is running on. All events are handled one after another in the order of their receipt. If the queue already contains the NewTick event or this event is in the processing stage, then the new NewTick event is not added to mql5 application queue.

Documentation on MQL5: Event Handling
Documentation on MQL5: Event Handling
  • www.mql5.com
Event Handling - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

It was a long time ago when I experimented. However, since MetaTrader has evolved a lot, I may be wrong now. I will have to test it again.

Also, when the OnCalculate() takes too long to process, MetaTrader logs a warning to the Experts log.

Ill extend the "indicator" I posted for a more comprehensive test setup, and post it.

Would be interesting to see if there is a difference between MT4 and MT5.
 
Samuel Manoel De Souza #:
The purpose of this function is work with the data passed in the parameters, which are ohlc bars, not tick data.
To check need use CopyTicks in OnCalculate to know how many ticks has arrived since the last call.
Also the result will depends on the liquidity of the symbol, as in some markets and symbol we may have tens of ticks every milisecond while in others just feel ticks by minute.
On symbols with tick volume, this should be enough for testing. Although the OnCalculate will be called for every tick update, also for those not contributing to tick volume.

And, no, OnCalculate is called for tick updates as well... not only for ohlc data on period basis.
 
Samuel Manoel De Souza #:

See documentation.

A program gets events only from the chart it is running on. All events are handled one after another in the order of their receipt. If the queue already contains the NewTick event or this event is in the processing stage, then the new NewTick event is not added to mql5 application queue.

This passage is related to OnTick event handling, not OnCalculate.

OnCalculate, as I will show with code, is called for each tick arrived, and these calls are queued and processed one by one, without skipping any ticks.
 
Dominik Egert #: This passage is related to OnTick event handling, not OnCalculate. OnCalculate, as I will show with code, is called for each tick arrived, and these calls are queued and processed one by one, without skipping any ticks.

If it is indeed the case that OnCalculate gets called for EVERY tick, then that would mean that during high volatility, the indicators (which share a single thread with the chart itself), can easily "stall" that thread and "freeze" the chart completely. That seems somewhat "dangerous".

EDIT: The documentation does in fact state ....

"The Calculate event is generated only for indicators right after the Init event is sent and at any change of price data. It is processed by the OnCalculate function."

So, it could be interpreted the way you describe, and it would explain why charts tend to stall a lot during high impact news.

I sure hope that is not the case, however. If it is, it may make me want to rethink some of my EAs and have them recalculate everything internally without using Indicators.

Documentation on MQL5: MQL5 programs / Client Terminal Events
Documentation on MQL5: MQL5 programs / Client Terminal Events
  • www.mql5.com
Client Terminal Events - MQL5 programs - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:
If it is indeed the case that OnCalculate gets called for EVERY tick, then that would mean that during high volatility, the indicators (which share a single thread with the chart itself), can easily "stall" that thread and "freeze" the chart completely. That seems somewhat "dangerous".

I was to say that, but i though the documentation was enough to proof the point when it says "  If the queue already contains the NewTick event or this event is in the processing stage, then the new NewTick event is not added to mql5 application queue. "

Documentation on MQL5: MQL5 programs / Client Terminal Events
Documentation on MQL5: MQL5 programs / Client Terminal Events
  • www.mql5.com
Client Terminal Events - MQL5 programs - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: