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

For testing and code verification timing is displayed in the chart title: <\/p>\n

\"clip_image002\"<\/a><\/p>\n","protected":false},"excerpt":{"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>function GetSecondNum()  <p>{  <p>Time = Now(4);  <p>Seconds = int(Time%100);  <p>Minutes = int(Time\/100%100);  <p>Hours = int(Time\/10000%100);  <p>SecondNum= int(Hours*60*60+Minutes*60+Seconds);  <p>return SecondNum;  <p>}  <p> <p>RequestTimedRefresh(1);  <p>TimeFrame = Interval(); <p>SecNumber = GetSecondNum();  <p>Newperiod = SecNumber%TimeFrame == 0;  <p>SecsLeft = SecNumber-int(SecNumber\/TimeFrame)*TimeFrame;  <p>SecsToGo = TimeFrame – SecsLeft;  <p>if( NewPeriod )   <p>{  <p>Say("New period");  […]<\/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\/1496"}],"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=1496"}],"version-history":[{"count":0,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/posts\/1496\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/media?parent=1496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/categories?post=1496"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.amibroker.org\/editable_userkb\/wp-json\/wp\/v2\/tags?post=1496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}