HighTick


Creating A Series of Code Execution Delays Using The Timer Object

It is often desirable when issuing orders from a strategy to delay the components of order generation by one or more seconds, 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 resusing a single Timer Object:

Code Example Showing How to Create a Series of Code execution delays using the Timer Object

Fig. 1. Code Creating a Series of Execution Delays Between Code Segments

Running this code as a strategy, produces the following print log:

Print Log output

Fig. 2 Print Log output

Coding Notes

 
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:

Strategy TimerDelayedStepsExample

Function DateTime

 

 

 

Bookmark and Share