OnInit function processed before rates info is updated... how to fix?

 

Hi,

Sometimes, when I manually change the symbol on a chart, there's a delay between the moment the chart loads from the saved cache and the moment it updates its rates info and prints the new ticks/bars received from the server. This is rare but sometimes the delay is long enough that it results in my OnInit function being processed before the rates info gets updated. When no EA's are running, it's not that big of an issue because the chart's ticks, bars and indicators will update as soon as the rates info is finished updating, but when an EA is on the chart and performing calculations in OnInit, it will always result in the OnInit function providing incorrect data. It doesn't happen all the time, but often enough to be a concern. Does anyone have any advice on how to fix this?

I assume that the rates update would trigger an OnTick event. So there might be a way to re-process my OnInit code with a function called from the first OnTick event received after the chart loads but I'm not sure this is the correct way to prevent the issue and I am looking for some advice...

Thanks in advance!

 
Jeepack: OnInit function being processed before the rates info gets updated.

Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
 
William Roeder #:

Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

Ok thanks! Do you have any suggestions for what to do when no ticks are coming in? ...like during the weekend?

I'm not sure so I'll have to test it next weekend but if my terminal downloads ticks from the server while the market is closed, if I open an FX chart on the weekend for example, will that generate an OnTick event even if the ticks aren't coming in "live"? If so, then I'll just process calculations once with OnInit and once more with the first OnTick event (this way I can still get calculations done on weekends even if no new ticks need to be downloaded).

 
Jeepack #: Do you have any suggestions for what to do when no ticks are coming in? ...like during the weekend?
  1. No ticks means nothing has changed. There is nothing to do.
  2. No ticks have been downloaded; the market is closed. There is nothing to do.
 
William Roeder #:
  1. No ticks means nothing has changed. There is nothing to do.
  2. No ticks have been downloaded; the market is closed. There is nothing to do.

I get what you're saying, there can be exceptions to that though depending on how you use the EA, but I think I'll be able to manage workarounds based on the advice you gave me so thanks!

Reason: