{"id":1421,"date":"2007-11-10T21:09:54","date_gmt":"2007-11-10T21:09:54","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/11\/10\/high-precision-delay-and-interval-timing\/"},"modified":"2008-02-08T13:29:37","modified_gmt":"2008-02-08T13:29:37","slug":"high-precision-delay-and-interval-timing","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/11\/10\/high-precision-delay-and-interval-timing\/","title":{"rendered":"High-Precision Delay and Interval Timing"},"content":{"rendered":"
Before continuing with this post you should carefully read the AmiBroker Help topic for the getPerformanceCounter()<\/a>. <\/p>\n Measuring time is an important aspect of all real-time intraday trading systems. Typical tasks requiring high-resolution timing include: <\/p>\n Most of these tasks can be accomplished using just three custom timing functions: <\/p>\n This post provides example functions in a demo application. To allow the use of many timers each function requires you to provide a TimerName, which will be used to retrieve timer information. Static variables are Global and can be read from anywhere; this means you have to be careful not to cross-reference the timers by using the same TimerName from different panes or windows. When running multiple copies of the same code you will need to key the TimerNames. For more on how to do this, see Keying Static Variables<\/a> in the Real-Time AFL Programming<\/a> category. <\/p>\n The timers below are implemented using the getPerformanceCounter(). This function returns the amount of time elapsed since the computer was last started. Tomasz recently explained this as follows: “The underlying high frequency counter runs all the time since computer start. What ‘reset’ flag really does is to store last value so next time you read it, it gets subtracted from last value giving you the difference. If reset is false, the last value is set to zero, and you get the original number of ‘clock ticks’ since computer start<\/em>“. <\/p>\n The timers in this post do their own sampling of the underlying high frequency counter, and the getPerformanceCounter() Reset argument is always left set to False. <\/p>\n The getPerformanceCounter() returns values with microsecond resolution; however, the practical accuracy is severely limited by interruptions from the computer’s operating system. Do not expect much better than about 50-millisecond absolute accuracy. Aside from designing your own dedicated trading hardware (to replace the PC) there isn’t much that can be done about this. If you are brave, you can experiment with increasing program priority in your Task Manager window. <\/p>\n Chart refreshes are most often initiated by an arriving quote, but they can also be initiated by mouse clicks, tooltip, and various chart operations. This means that, when market activity is low and things are not happening as fast as you would like, you can force extra AFL executions by clicking on your chart. You can verify this by running the code below and, while clicking rapidly on the chart, observe that the timer counts displayed will update more rapidly. <\/p>\n You can ensure a one-second chart refresh by adding a RequestTimedRefresh(1) to your code. If the frequency of your arriving data is slow, your AFL code may execute only sporadically. Since your code must execute to read your timers, the resolution of your timers will be limited by the chart refresh rate. If your chart refreshes once a second your timing resolution will be one second! <\/p>\n Normally most of the AFL code in an Indicator window executes when your chart refreshes; however, to obtain speed advantages, you may execute non-critical sections (like account information and System Status) of your code less frequently using a timer. You can also execute small sections of code more frequently by placing them inside a well-controlled loop. If you do this, be sure to limit the maximum time your code can spend inside the loop to one second or less. <\/p>\n For fast trading systems, the frequency of AFL executions (chart refreshes) may be slow and this may make it difficult for you to get LMT fills. There is no way to have a program that requires 50 milliseconds per pass to execute 20 times per second. <\/p>\n Considering the interval between AFL executions, it is important to plan the layout of your code so that all events are handled in the most efficient order. If you don’t, transmittance of your order could well be delayed by up to a full second. There are situations where you want to invoke an immediate re-execution of your code. In some cases you might want to do this after placing an order to check order status before the next quote or refresh. Although it should be used sparingly this is possible by calling the RefreshAll(): <\/p>\n\n
\n
\r<\/span><<\/span>p<\/span>><\/span>This <\/span>function <\/span>can only be called once a second<\/span>; <\/span>calling it faster will not result in more frequent chart refreshes<\/span>. <\/span>This means you should only call it when really needed<\/span>. <<\/span>p<\/span>><\/span>The code presented below is <\/span>for <\/span>demonstration only<\/span>. <\/span>The getElapsedTime<\/span>() <\/span>lets you measure elapsed time from the moment of Reset<\/span>. <\/span>The first argument passes the name you assign to the <\/span>static <\/span>timer<\/span>; <\/span>this allows you to <\/span>use <\/span>the same <\/span>function <\/span>to time different events<\/span>. <\/span>The second argument is a Reset flag<\/span>. <\/span>When this Reset is True<\/span>, <\/span>the <\/span>function <\/span>samples the underlying high frequency counter <\/span>and <\/span>uses it <\/span>for <\/span>later reference<\/span>. <\/span>When you call the <\/span>function <\/span>with the Reset argument set to False<\/span>, <\/span>it calculates the elapsed time by subtracting the earlier sampled value from the current value of the Performance Counter<\/span>. <<\/span>p<\/span>><\/span>The setDelay<\/span>() function <\/span>lets you Start<\/span>, <\/span>Read<\/span>, and <\/span>Cancel a time delay<\/span>. <\/span>The TimerName argument functions <\/span>as <\/span>in the getElapsedTime<\/span>(). <\/span>Calling the setDelay<\/span>() <\/span>with the mSecDelay argument set to a non<\/span>-<\/span>zero value will start the Delay timer<\/span>. <\/span>Calling it with the mSecDelay argument set to zero will make it <\/span>return <\/span>the current count<\/span>-<\/span>down time in millseconds<\/span>. <\/span>Calling the <\/span>function <\/span>with the cancel argument set to True will terminate the delay<\/span>. <<\/span>p<\/span>><\/span>The getDelayTrigger<\/span>() function <\/span>returns a trigger<\/span>. <\/span>This is a signal that is true <\/span>for <\/span>only one pass through the code<\/span>. <\/span>Triggers are frequently used in real<\/span>-<\/span>time trading systems<\/span>. <\/span>They are needed to prevent multiple actions when a signal becomes True<\/span>. <<\/span>p<\/span>><\/span>To run the code<\/span>, <\/span>copy the formula to an Indicator <\/span>and <\/span>click Insert<\/span>. <\/span>You<\/span>'ll see a chart window like Figure 1 below: \r\r<p align=\"center\">Figure 1. Result from running the example code. \r<a href='<\/span>http<\/span>:<\/span>\/\/www.amibroker.org\/userkb\/2007\/11\/10\/high-precision-delay-and-interval-timing\/timerdisplayjpg\/' rel='attachment wp-att-1422' title='timerdisplay.jpg'><img src='http:\/\/www.amibroker.org\/userkb\/wp-content\/uploads\/2007\/11\/timerdisplay.jpg' alt='timerdisplay.jpg' \/><\/a>\r\r<\/span><<\/span>p<\/span>><\/span>The example code maintains three timers<\/span>, <\/span>T1<\/span>, <\/span>T2 <\/span>and <\/span>T3<\/span>. <\/span>All timing values are expressed in milliseconds<\/span>. <\/span>In Figure<\/span>-<\/span>1 the Elapsed Time shown is measured from timer Reset<\/span>. <\/span>The Delay shown is the time remaining after Start<\/span>, <\/span>until the delay times out<\/span>. <\/span>The line <\/span>for <\/span>Timer T2 shows that its Delay just timed<\/span>-<\/span>out <\/span>and <\/span>produced a trigger<\/span>. <\/span>Timer T3 still has a Delay in progress<\/span>. <\/span>Right<\/span>-<\/span>click on the chart to open the Param window<\/span>: \r\r<<\/span>p align<\/span>=<\/span>"center"<\/span>><\/span>Figure 2. Param window<\/span>. \r<<\/span>a href<\/span>=<\/span>'http:\/\/www.amibroker.org\/userkb\/2007\/11\/10\/high-precision-delay-and-interval-timing\/timerparampng\/' <\/span>rel<\/span>=<\/span>'attachment wp-att-1423' <\/span>title<\/span>=<\/span>'timerparam.png'<\/span>><<\/span>img src<\/span>=<\/span>'http:\/\/www.amibroker.org\/userkb\/wp-content\/uploads\/2007\/11\/timerparam.png' <\/span>alt<\/span>=<\/span>'timerparam.png' <\/span>\/><\/<\/span>a<\/span>>\r\r<<\/span>p<\/span>>If <\/span>you click one of the timer Resets in the Param window you<\/span>'ll see the ElapsedTime in the corresponding row go to zero, and then start to increment sporadically when your chart refreshes. Without live data this would be at approximately 1-second intervals, as determined by the RequestTimedRefresh(1); <p>If you click Start for one of the timers this will start a delay. You can see how it counts down in the Delay column. Click the timer'<\/span>s Cancel to terminate the Delay<\/span>. <\/span>Note that whenever a Delay times out<\/span>, <\/span>the word <\/span>"Trigger" <\/span>briefly appears in the third column<\/span>. \r\r\rfunction <\/span>RefreshAll<\/span>()\r {\r <\/span>oAB <\/span>= <\/span>CreateObject<\/span>(<\/span>"Broker.Application"<\/span>);\r <\/span>oAB<\/span>.<\/span>RefreshAll<\/span>();\r }\r\rfunction <\/span>getElapsedTime<\/span>( <\/span>TimerName<\/span>, <\/span>Reset <\/span>)\r {\r if( <\/span>Reset <\/span>) \r {\r <\/span>TimeRef<\/span>= <\/span>GetPerformanceCounter<\/span>(<\/span>False<\/span>);\r <\/span>StaticVarSet<\/span>(<\/span>TimerName<\/span>,<\/span>TimeRef<\/span>);\r }\r <\/span>TimeRef <\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>TimerName<\/span>));\r <\/span>ElapsedTime <\/span>= <\/span>GetPerformanceCounter<\/span>(<\/span>False<\/span>) - <\/span>TimeRef<\/span>;\r return <\/span>ElapsedTime<\/span>;\r }\r\rfunction <\/span>setDelay<\/span>( <\/span>TimerName<\/span>, <\/span>MsecDelay<\/span>, <\/span>Cancel <\/span>)\r {\r <\/span>HRCounter<\/span>= <\/span>GetPerformanceCounter<\/span>(<\/span>False<\/span>