|
Immediately after the program has been attached to the chart, it starts working with the init() function. The init()
function of an expert advisor or a custom indicator attached to the chart will run just after client terminal has started
and history data (this concerns only experts, but not indicators) have been loaded additionally, after the symbol and/or
chart period have been changed, after the program has been recompiled in MetaEditor, after inputs have been changed from
the window of expert or custom indicator settings. An expert will also be initialized after the account has been changed.
Every program attached to a chart completes its work with the deinit() function. The deinit() function runs at the client
terminal shutdown, at chart closing, immediately before the symbol and/or chart period is changed, at successful recompiling
of the program, at changing of inputs, or at changing of the account. One can see the reason of deinitialization using the
UninitializeReason() function during execution of the deinit() function. The deinit()
function must be executed within 2.5 seconds. If the function has not completed its execution within this time period, it will
be completed forcedly. Scripts are an exception to this rule, as they normally finish their working independently, without any
commands from outside. If a script works very long (due to endless loop, foe example), it can be finished by an outside command
(at deletion of the script from the chart context menu, at attaching of a new script to the same chart, at closing of the chart,
at changing of the symbol and/or chart period). In this case, the deinit() function is limited by 2.5 seconds, too.
At incoming of new quotes, the start() function of the attached experts and custom indicators will be executed. If the start()
function launched at the preceding quote was running when a new quote came, the new quote will be skipped by the expert. All new
quotes income while the program was being executed are skipped by the program until the current execution of the start() function
has been completed. After that, the start() function will be run only when a successive new quote incomes.
For custom indicators, the start() function will be launched for recalculation after the current chart symbol or timeframe
has been changed independently on new quotes incoming.
The start() function will not be run when the expert properties window is open. The latter cannot be opened during the expert execution.
Detaching of the program from the chart, change of symbol and/or chart period, change of the account, closing of the chart, as
well as shutdown of the client terminal will interrupt execution of the program. If the start() function was being executed at
the moment when the stop working command was given, the time remaining for its work is limited by 2.5 seconds. The program can
get to know that it is tried to shut it down with the built-in function of IsStopped() and finish
its work correctly.
Execution of scripts does not depend on incoming quotes. At change of symbol and/or chart period, the script will finish its work
and be unloaded from the client terminal.
Scripts and experts work in their own thread. Custom indicators work in the main interface thread. If a custom indicator has been called
with the iCustom() function, this indicator works in the thread of the program that has called it.
Library (imported) functions work in the calling program thread, as well.
|