Use of Timer Object to Delay Execution of Code in EasyLanguage

The Timer Object can be used to delay the execution of Easylanguage code.  This may be useful when issuing orders from a strategy to , to ensure each step is received by the trade server and acknowledged before proceeding to the next step. Failure to do so can create out-of-sync conditions with strategies that use OrderTickets to generate or cancel orders, or when restarting the strategy to re-sync the real-world position and strategy position.

The following example of code shows how a series of code steps within a strategy or indicator may be delayed a user-specified number of seconds by reusing a single Timer Object:

Delaying EasyLanguage Code Execuition Using the Timer Object

Fig. 1. Code Creating a Series of Execution Delays Between Code Segments Using the Timer Object

Running this code as a strategy, produces the following print log:
print log from delaying code execution using the timer object in Easylanguage
Fig. 2 Print Log output

Line 26

Timer1.AutoReset must be set to false to prevent the timer from firing more than once per step.

Lines 32-45

Method Step1() contains the first segment of code to be executed at the beginning of a series of delayed code segments. Timer1.Elapsed += Step2; assigns method Step2 as the next step to be executed when the timer elapses. The notation += actually adds method Step2 to the list of methods that will all fire when the timer elapses. Because Timer1.Elapsed is pointing to a list of methods, rather than a single method, subsequent re-use of the same timer object to point to the method to execute in the next step will require removing Step2 from the existing list, before adding the new method to be executed.

Lines 57-58

In Method Step2(), the notation “-=” removes the previously assigned method Step2() from the list of methods that the timer object will trigger when it expires, and the notation “+=” adds the new Method Step3() to the list of methods that will be executed when the timer is re-started and then expires.

Print Log

Fourth column shows computer time to the nearest second, verifying the delay between each code segment is the number of seconds specified in the input parameters.

Downloads

Initial posted version:  01/16/14

Latest Update:  01/16/14

*.ELD files are compiled for TS 9.1

All ELD and code text files packaged here:

TimerDelayedStepsExample.zip

The code may be visualized here:

Function DateTime