Is it possible to run a timed function inside an indicator in the onCalculate?

 

I want to run a function when the indicator is on the last candle (rates_total-1).

I would like to run it once for the current candle every 30 seconds.

So run the function on a new candle, and when the current candle has an open and close, it's no longer the current candle.

Then, after 30 seconds on a different candle it will run the function again.

How can this be done?



So it would be great if you could have a timed function inside the onCalculate but I don't believe this is possible.

So I have my int onCalculate() with the timed function like this: onTimer(diff) so it's passing a variable to the function outside onCalculate().

I don't believe that this will work though. It doesn't seem to accept parameters.


If anyone has any information that could be useful, thank you.
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Predefined Macro Substitutions
  • www.mql5.com
//| Expert initialization function                                   | //| Expert deinitialization function                                 | //| Expert tick function                                             | //| test1                                                            |...
 
  1. little.trader:

    I want to run a function when the indicator is on the last candle (rates_total-1).

    I would like to run it once for the current candle every 30 seconds.

    Why would you want that? If you don't receive a new tick, nothing has changed; computed values are still correct. If you do, your computed values are now out of date until the timer runs.

  2. little.trader: So run the function on a new candle, and when the current candle has an open and close, it's no longer the current candle.

    Every candle has an open and a close. They are equal on the first tick.

  3. Tell us what you are trying to accomplish, not what you think you need.

    How To Ask Questions The Smart Way. 2004
              The XY Problem

 
William Roeder:
  1. Why would you want that? If you don't receive a new tick, nothing has changed; computed values are still correct. If you do, your computed values are now out of date until the timer runs.

  2. Every candle has an open and a close. They are equal on the first tick.

  3. Tell us what you are trying to accomplish, not what you think you need.

    How To Ask Questions The Smart Way. 2004
              The XY Problem

Hi William,


Thank you for responding to me. Inside a custom indicator I would like to save the high of the candle's high and compare it to the current candle's high after 30 seconds and measure it's difference.

I want to check if it satisfies the condition (of being at least a certain distance) when it is first compared and after 30 seconds. Can you pass values to the onTick function? It doesn't seem that you can.

I am trying to make a more accurate indicator on the current candle, as it only seems to be accurate on historical data.

This means I would need it to be in the for loop of the indicator as it needs i of the current candle.

I want to do this every 30 seconds, but I don't know if this can be done inside the onCalculate.

Thank you very much for trying to help with this!


All the best,

Janice

 

Hi,

its an old question, but i just saw it and was interested in your question. Well, there is a possible way to do it.

You could use a timer, initialized in OnInit to run every 30 seconds. The OnInit function then needs a code to sync to the current time, which need only be done once. Use the Function TimeCurrent() to get current time. Use modulo-operator to extract diff to PeriodSeconds().

Another option to cheat in the code you want into the OnCalcualte function is to check for the time in the time[] array given to the function. It holds all open times of all periods.

And maybe this could also be a solution for you: Skip the on-tick updates in the OnCalculate function by checking the tick_volume[] value being above 1. (But only for the current running period, so when (prev_calculated == (rates_total - 1)). THis will give you the behaviour of being on historic data. - Dont forget to recalculate the last value, because when a new period opens, the open[] and close[] values are (usually) the same, and your indicator will not calculate correctly.

This type of behaviour could be considered as repainting, though.


As much as I can think of a solution to this.

Reason: