{"id":1496,"date":"2007-12-28T12:42:04","date_gmt":"2007-12-28T12:42:04","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2007\/12\/28\/real-time-bar-period-timing\/"},"modified":"2007-12-28T12:49:13","modified_gmt":"2007-12-28T12:49:13","slug":"real-time-bar-period-timing","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2007\/12\/28\/real-time-bar-period-timing\/","title":{"rendered":"Real-Time Bar-Period Timing"},"content":{"rendered":"
In real-time trading one often needs to know when a new period starts and how much time there is left before the period ends. The code below provides this information. Be sure to synchronize your system’s clock.<\/p>\n
<\/span><<\/span>p<\/span>>function <\/span>GetSecondNum<\/span>() \r<<\/span>p<\/span>>{ \r<<\/span>p<\/span>><\/span>Time <\/span>= <\/span>Now<\/span>(<\/span>4<\/span>); \r<<\/span>p<\/span>><\/span>Seconds <\/span>= <\/span>int<\/span>(<\/span>Time<\/span>%<\/span>100<\/span>); \r<<\/span>p<\/span>><\/span>Minutes <\/span>= <\/span>int<\/span>(<\/span>Time<\/span>\/<\/span>100<\/span>%<\/span>100<\/span>); \r<<\/span>p<\/span>><\/span>Hours <\/span>= <\/span>int<\/span>(<\/span>Time<\/span>\/<\/span>10000<\/span>%<\/span>100<\/span>); \r<<\/span>p<\/span>><\/span>SecondNum<\/span>= <\/span>int<\/span>(<\/span>Hours<\/span>*<\/span>60<\/span>*<\/span>60<\/span>+<\/span>Minutes<\/span>*<\/span>60<\/span>+<\/span>Seconds<\/span>); \r<<\/span>p<\/span>>return <\/span>SecondNum<\/span>; \r<<\/span>p<\/span>>} \r<<\/span>p<\/span>>\r<<\/span>p<\/span>><\/span>RequestTimedRefresh<\/span>(<\/span>1<\/span>); \r<<\/span>p<\/span>><\/span>TimeFrame <\/span>= <\/span>Interval<\/span>(); <<\/span>p<\/span>><\/span>SecNumber <\/span>= <\/span>GetSecondNum<\/span>(); \r<<\/span>p<\/span>><\/span>Newperiod <\/span>= <\/span>SecNumber<\/span>%<\/span>TimeFrame <\/span>== <\/span>0<\/span>; \r<<\/span>p<\/span>><\/span>SecsLeft <\/span>= <\/span>SecNumber<\/span>-<\/span>int<\/span>(<\/span>SecNumber<\/span>\/<\/span>TimeFrame<\/span>)*<\/span>TimeFrame<\/span>; \r<<\/span>p<\/span>><\/span>SecsToGo <\/span>= <\/span>TimeFrame <\/span>- <\/span>SecsLeft<\/span>; \r<<\/span>p<\/span>>if( <\/span>NewPeriod <\/span>) \r<<\/span>p<\/span>>{ \r<<\/span>p<\/span>><\/span>Say<\/span>(<\/span>"New period"<\/span>); \r<<\/span>p<\/span>><\/span>Plot<\/span>(<\/span>1<\/span>,<\/span>""<\/span>,<\/span>colorYellow<\/span>,<\/span>styleArea<\/span>|<\/span>styleOwnScale<\/span>,<\/span>0<\/span>,<\/span>1<\/span>); \r<<\/span>p<\/span>>} \r<<\/span>p<\/span>><\/span>Title <\/span>= <\/span>"\\n"<\/span>+ \r<<\/span>p<\/span>><\/span>"time: "<\/span>+<\/span>Now<\/span>(<\/span>2<\/span>)+<\/span>"\\n"<\/span>+ \r<<\/span>p<\/span>><\/span>"Interval: "<\/span>+<\/span>NumToStr<\/span>(<\/span>TimeFrame<\/span>,<\/span>1.0<\/span>)+<\/span>"\\n"<\/span>+ \r<<\/span>p<\/span>><\/span>"Second Number: "<\/span>+<\/span>NumToStr<\/span>(<\/span>SecNumber<\/span>,<\/span>1.0<\/span>,<\/span>False<\/span>)+<\/span>"\\n"<\/span>+ \r<<\/span>p<\/span>><\/span>"Seconds Left: "<\/span>+<\/span>NumToStr<\/span>(<\/span>SecsLeft<\/span>,<\/span>1.0<\/span>,<\/span>False<\/span>)+<\/span>"\\n"<\/span>+ \r<<\/span>p<\/span>><\/span>"Seconds To Go: "<\/span>+<\/span>NumToStr<\/span>(<\/span>SecsToGo<\/span>,<\/span>1.0<\/span>,<\/span>False<\/span>); \r<<\/span>p<\/span>><\/span><\/pre>\nFor testing and code verification timing is displayed in the chart title: <\/p>\n