February 18, 2008
Real-Time Bar Period Timing
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | function GetSecondNum() { Time = Now( 4 ); Seconds = int( Time % 100 ); Minutes = int( Time / 100 % 100 ); Hours = int( Time / 10000 % 100 ); SecondNum = int( Hours * 60 * 60 + Minutes * 60 + Seconds ); return SecondNum; } RequestTimedRefresh( 1 ); TimeFrame = Interval(); SecNumber = GetSecondNum(); Newperiod = SecNumber % TimeFrame == 0; SecsLeft = SecNumber - int( SecNumber / TimeFrame ) * TimeFrame; SecsToGo = TimeFrame - SecsLeft; if ( NewPeriod ) { Say( "New period" ); Plot( 1, "", colorYellow, styleArea | styleOwnScale, 0, 1 ); } Title = "\n" + " Current Time: " + Now( 2 ) + "\n" + "Chart Interval: " + NumToStr( TimeFrame, 1.0 ) + " Seconds\n" + " Second Number: " + NumToStr( SecNumber, 1.0, False ) + "\n" + " Seconds Left: " + NumToStr( SecsLeft, 1.0, False ) + "\n" + " Seconds To Go: " + NumToStr( SecsToGo, 1.0, False ); Plot( C, "", 1, 128 ); |
For verification, timing is displayed in the chart title:
Edited by Al Venosa.
Filed by Herman at 2:31 pm under Real-Time AFL Programming


(3 votes, average: 4.33 out of 5)
Hi,
great work. I have a question. If I want to use it in intra day trading, how to do it?
My problem is that if I want to know value of previous bar Low, how to do it in intraday? I have data in 30 seconds ticks and in Amibroker I set 10 minutes ticks. But i I write Low, -1 it is low of yesterday and no the previous bar.
Thanks
Your best bet is to ask this question on one of the AmiBroker forums.
Good luck,
herman
Testing for new period/bar start in this way (Newperiod = SecNumber % TimeFrame == 0;) is not quite reliable!
1 second refresh is not quarantied! It may sometimes take longer. So Newperiod may not get true value is some cases. It is much safer to compare last bar’s time to a static value where we store the time when last new bar was signaled.
something like this:
if (StaticVarGet(”LastNewBarSignal”) != LastValue(TimeNum())
{
StaticVarSet(”LastNewBarSignal”, LastValue(TimeNum());
….
}
P.s:
This will signal a new bar when you start up amibroker as well!
Y