{"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

    \n
  1. Limiting the message rate to Interactive Brokers (IB) to 50\/sec (API Error 100).<\/li>\n
  2. Inserting small delays before polling IB Status, to allow for Internet delays. <\/li>\n
  3. Staggering (interlacing) portfolio trades to spread out the action. <\/li>\n
  4. Measuring and optimizing AFL execution time. <\/li>\n
  5. Modifying orders after a small delay (to ensure fills).<\/li>\n
  6. Periodic execution of tasks, for example, Watchlist scanning, Display and Status refresh, calculations based on slow changing variables, etc.<\/li>\n
  7. Time-Stamping events, for example order placement.<\/li>\n
  8. Collecting\/preprocessing quotes.<\/li>\n
  9. Overlay live tick-charts on faster and easier to manage 1-minute charts. <\/li>\n<\/ol>\n

    Most of these tasks can be accomplished using just three custom timing functions: <\/p>\n

      \n
    1. GetElapsedTime(): A function that returns elapsed time since reset. <\/li>\n
    2. SetDelay(): A function that returns time left to reach a future time.<\/li>\n
    3. GetDelayTrigger(): A function that returns a trigger when a delay times out.<\/li>\n<\/ol>\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

      \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>);\r    if( <\/span>Cancel <\/span>) \r        {\r        <\/span>StaticVarSet<\/span>(<\/span>"TO"<\/span>+<\/span>TimerName<\/span>,-<\/span>1<\/span>);\r        <\/span>StaticVarSet<\/span>(<\/span>"DS"<\/span>+<\/span>TimerName<\/span>, <\/span>0<\/span>);\r        }\r    else if( <\/span>MsecDelay <\/span>) \r        {\r        <\/span>StaticVarSet<\/span>(<\/span>"TO"<\/span>+<\/span>TimerName<\/span>,<\/span>HRCounter<\/span>+<\/span>MsecDelay <\/span>);\r        }\r    <\/span>TimeOutTime <\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"TO"<\/span>+<\/span>TimerName<\/span>));\r    <\/span>DelayCount <\/span>= <\/span>Max<\/span>(<\/span>0<\/span>, <\/span>TimeOutTime <\/span>- <\/span>HRCounter <\/span>);\r    <\/span>StaticVarSet<\/span>(<\/span>"DC"<\/span>+<\/span>Timername<\/span>, <\/span>DelayCount<\/span>);\r    return <\/span>DelayCount<\/span>;\r    }\r\rfunction <\/span>getDelayTrigger<\/span>( <\/span>TimerName <\/span>)\r    {\r    <\/span>DelayCount <\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"DC"<\/span>+<\/span>TimerName<\/span>));\r    <\/span>DelayState <\/span>= <\/span>DelayCount <\/span>> <\/span>0<\/span>;\r    <\/span>PrevDelayState <\/span>= <\/span>Nz<\/span>(<\/span>StaticVarGet<\/span>(<\/span>"DS"<\/span>+<\/span>TimerName<\/span>));\r    <\/span>StaticVarSet<\/span>(<\/span>"DS"<\/span>+<\/span>TimerName<\/span>, <\/span>DelayState<\/span>);\r    return <\/span>DelayState <\/span>< <\/span>PrevDelayState<\/span>;\r    }\r \r<\/span>RequestTimedRefresh<\/span>( <\/span>1<\/span>);\r\r<\/span>_SECTION_BEGIN<\/span>(<\/span>"TIMER 1"<\/span>);\r<\/span>Reset1 <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"1 - Reset"<\/span>,<\/span>"RESET"<\/span>);\r<\/span>MSecDelay1 <\/span>= <\/span>Param<\/span>(<\/span>"1 - Delay (mS)"<\/span>,<\/span>1000<\/span>,<\/span>0<\/span>,<\/span>10000<\/span>,<\/span>10<\/span>);\rif( <\/span>ParamTrigger<\/span>(<\/span>"1 - Start"<\/span>, <\/span>"START"<\/span>) ) <\/span>setDelay<\/span>( <\/span>"Timer1"<\/span>, <\/span>MSecDelay1<\/span>, <\/span>0 <\/span>);\rif( <\/span>ParamTrigger<\/span>(<\/span>"1 - Cancel"<\/span>, <\/span>"CANCEL"<\/span>) ) <\/span>setDelay<\/span>( <\/span>"Timer1"<\/span>, <\/span>MSecDelay1<\/span>, <\/span>1 <\/span>);\r<\/span>_SECTION_END<\/span>();\r\r<\/span>_SECTION_BEGIN<\/span>(<\/span>"TIMER 2"<\/span>);\r<\/span>Reset2 <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"2 - Reset"<\/span>,<\/span>"RESET"<\/span>);\r<\/span>MSecDelay2 <\/span>= <\/span>Param<\/span>(<\/span>"2 - Delay (mS)"<\/span>,<\/span>4000<\/span>,<\/span>0<\/span>,<\/span>10000<\/span>,<\/span>10<\/span>);\rif( <\/span>ParamTrigger<\/span>(<\/span>"2 - Start"<\/span>, <\/span>"START"<\/span>) ) <\/span>setDelay<\/span>( <\/span>"Timer2"<\/span>, <\/span>MSecDelay2<\/span>, <\/span>0 <\/span>);\rif( <\/span>ParamTrigger<\/span>(<\/span>"2 - Cancel"<\/span>, <\/span>"CANCEL"<\/span>) ) <\/span>setDelay<\/span>( <\/span>"Timer2"<\/span>, <\/span>MSecDelay2<\/span>, <\/span>1 <\/span>);\r<\/span>_SECTION_END<\/span>();\r\r<\/span>_SECTION_BEGIN<\/span>(<\/span>"TIMER 3"<\/span>);\r<\/span>Reset3 <\/span>= <\/span>ParamTrigger<\/span>(<\/span>"3 - Reset"<\/span>,<\/span>"RESET"<\/span>);\r<\/span>MSecDelay3 <\/span>= <\/span>Param<\/span>(<\/span>"3 - Delay (mS)"<\/span>,<\/span>8000<\/span>,<\/span>0<\/span>,<\/span>10000<\/span>,<\/span>10<\/span>);\rif( <\/span>ParamTrigger<\/span>(<\/span>"3 - Start"<\/span>, <\/span>"START"<\/span>) ) <\/span>setDelay<\/span>( <\/span>"Timer3"<\/span>, <\/span>MSecDelay3<\/span>, <\/span>0 <\/span>);\rif( <\/span>ParamTrigger<\/span>(<\/span>"3 - Cancel"<\/span>, <\/span>"CANCEL"<\/span>) ) <\/span>setDelay<\/span>( <\/span>"Timer3"<\/span>, <\/span>MSecDelay3<\/span>, <\/span>1 <\/span>);\r<\/span>_SECTION_END<\/span>();\r\r<\/span>ET1     <\/span>= <\/span>getElapsedTime<\/span>( <\/span>"Timer1"<\/span>, <\/span>Reset1 <\/span>);\r<\/span>ETA1     <\/span>= <\/span>setDelay<\/span>( <\/span>"Timer1"<\/span>, <\/span>0<\/span>, <\/span>0 <\/span>);\r<\/span>TT1     <\/span>= <\/span>getDelayTrigger<\/span>( <\/span>"Timer1"<\/span>);\r\r<\/span>ET2     <\/span>= <\/span>getElapsedTime<\/span>( <\/span>"Timer2"<\/span>, <\/span>Reset2 <\/span>);\r<\/span>ETA2     <\/span>= <\/span>setDelay<\/span>( <\/span>"Timer2"<\/span>, <\/span>0<\/span>, <\/span>0 <\/span>);\r<\/span>TT2     <\/span>= <\/span>getDelayTrigger<\/span>( <\/span>"Timer2"<\/span>);\r\r<\/span>ET3     <\/span>= <\/span>getElapsedTime<\/span>( <\/span>"Timer3"<\/span>, <\/span>Reset3 <\/span>);\r<\/span>ETA3     <\/span>= <\/span>setDelay<\/span>( <\/span>"Timer3"<\/span>, <\/span>0<\/span>, <\/span>0 <\/span>);\r<\/span>TT3     <\/span>= <\/span>getDelayTrigger<\/span>( <\/span>"Timer3"<\/span>);\r\r<\/span>Title <\/span>= <\/span>"\\nFilename: "<\/span>+<\/span>Filename<\/span>+<\/span>"\\n\\n"<\/span>+\r<\/span>"MilliSeconds Since Computer Startup: "<\/span>+<\/span>NumToStr<\/span>(<\/span>GetPerformanceCounter<\/span>(<\/span>False<\/span>),<\/span>1.3<\/span>)+<\/span>"\\n\\n"<\/span>+\r<\/span>"Timer     ElapsedTime     Delay     Trigger\\n\\n"<\/span>+\r<\/span>"T1      "<\/span>+<\/span>NumToStr<\/span>(<\/span>ET1<\/span>,<\/span>10.0<\/span>)+<\/span>"     "<\/span>+<\/span>NumToStr<\/span>(<\/span>ETA1<\/span>,<\/span>7.0<\/span>)+<\/span>"      "<\/span>+<\/span>WriteIf<\/span>(<\/span>TT1<\/span>,<\/span>"TRIGGER1"<\/span>,<\/span>""<\/span>)+<\/span>"\\n\\n"<\/span>+\r<\/span>"T2      "<\/span>+<\/span>NumToStr<\/span>(<\/span>ET2<\/span>,<\/span>10.0<\/span>)+<\/span>"     "<\/span>+<\/span>NumToStr<\/span>(<\/span>ETA2<\/span>,<\/span>7.0<\/span>)+<\/span>"      "<\/span>+<\/span>WriteIf<\/span>(<\/span>TT2<\/span>,<\/span>"TRIGGER2"<\/span>,<\/span>""<\/span>)+<\/span>"\\n\\n"<\/span>+<\/span><\/pre>\n

      Edited by Al Venosa.<\/p>\n","protected":false},"excerpt":{"rendered":"

      Before continuing with this post you should carefully read the AmiBroker Help topic for the getPerformanceCounter(). Measuring time is an important aspect of all real-time intraday trading systems. Typical tasks requiring high-resolution timing include: Limiting the message rate to Interactive Brokers (IB) to 50\/sec (API Error 100). Inserting small delays before polling IB Status, to […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[64],"tags":[],"_links":{"self":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1421"}],"collection":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/comments?post=1421"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1421\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=1421"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=1421"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=1421"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}