{"id":1547,"date":"2008-02-18T14:31:40","date_gmt":"2008-02-18T14:31:40","guid":{"rendered":"http:\/\/www.amibroker.org\/userkb\/2008\/02\/18\/real-time-bar-period-timing-2\/"},"modified":"2008-02-18T14:38:28","modified_gmt":"2008-02-18T14:38:28","slug":"real-time-bar-period-timing-2","status":"publish","type":"post","link":"http:\/\/www.amibroker.org\/editable_userkb\/2008\/02\/18\/real-time-bar-period-timing-2\/","title":{"rendered":"Real-Time Bar Period Timing"},"content":{"rendered":"
When trading in real-time, one often needs to know when a new period starts and how much time there is left before the period ends. The code below will give you the time remaining to the next bar, the time elapsed since the start of the bar, and the second count since date-change. The timing values will automatically adjust to the selected chart interval. You can use the variables in your system’s code to time various events.<\/h4>\n
This code is shown in a demo configuration, and you will have to adapt it to your personal requirements. To test, simply Apply the code to an Indicator window. For a quick test, select the one-minute time interval. <\/p>\n
<\/span>function <\/span>GetSecondNum<\/span>()\r{\r <\/span>Time <\/span>= <\/span>Now<\/span>( <\/span>4 <\/span>);\r <\/span>Seconds <\/span>= <\/span>int<\/span>( <\/span>Time <\/span>% <\/span>100 <\/span>);\r <\/span>Minutes <\/span>= <\/span>int<\/span>( <\/span>Time <\/span>\/ <\/span>100 <\/span>% <\/span>100 <\/span>);\r <\/span>Hours <\/span>= <\/span>int<\/span>( <\/span>Time <\/span>\/ <\/span>10000 <\/span>% <\/span>100 <\/span>);\r <\/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 return <\/span>SecondNum<\/span>;\r}\r\r<\/span>RequestTimedRefresh<\/span>( <\/span>1 <\/span>);\r<\/span>TimeFrame <\/span>= <\/span>Interval<\/span>();\r<\/span>SecNumber <\/span>= <\/span>GetSecondNum<\/span>();\r<\/span>Newperiod <\/span>= <\/span>SecNumber <\/span>% <\/span>TimeFrame <\/span>== <\/span>0<\/span>;\r<\/span>SecsLeft <\/span>= <\/span>SecNumber <\/span>- <\/span>int<\/span>( <\/span>SecNumber <\/span>\/ <\/span>TimeFrame <\/span>) * <\/span>TimeFrame<\/span>;\r<\/span>SecsToGo <\/span>= <\/span>TimeFrame <\/span>- <\/span>SecsLeft<\/span>;\r\rif ( <\/span>NewPeriod <\/span>)\r{\r <\/span>Say<\/span>( <\/span>"New period" <\/span>);\r <\/span>Plot<\/span>( <\/span>1<\/span>, <\/span>""<\/span>, <\/span>colorYellow<\/span>, <\/span>styleArea <\/span>| <\/span>styleOwnScale<\/span>, <\/span>0<\/span>, <\/span>1 <\/span>);\r}\r\r<\/span>Title <\/span>= <\/span>"\\n" <\/span>+\r\r <\/span>" Current Time: " <\/span>+ <\/span>Now<\/span>( <\/span>2 <\/span>) + <\/span>"\\n" <\/span>+\r <\/span>"Chart Interval: " <\/span>+ <\/span>NumToStr<\/span>( <\/span>TimeFrame<\/span>, <\/span>1.0 <\/span>) + <\/span>" Seconds\\n" <\/span>+\r <\/span>" Second Number: " <\/span>+ <\/span>NumToStr<\/span>( <\/span>SecNumber<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>) + <\/span>"\\n" <\/span>+\r <\/span>" Seconds Left: " <\/span>+ <\/span>NumToStr<\/span>( <\/span>SecsLeft<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>) + <\/span>"\\n" <\/span>+\r <\/span>" Seconds To Go: " <\/span>+ <\/span>NumToStr<\/span>( <\/span>SecsToGo<\/span>, <\/span>1.0<\/span>, <\/span>False <\/span>);\r\r<\/span>Plot<\/span>( <\/span>C<\/span>, <\/span>""<\/span>, <\/span>1<\/span>, <\/span>128 <\/span>);<\/span><\/pre>\nFor verification, timing is displayed in the chart title:<\/p>\n