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:

clip_image002

Edited by Al Venosa.

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4 out of 5)
Loading ... Loading ...

2 Responses to “Real-Time Bar Period Timing”

  1. david
    June 17th, 2008 | 8:53 am

    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

  2. Herman
    August 16th, 2008 | 9:29 am

    Your best bet is to ask this question on one of the AmiBroker forums.

    Good luck,
    herman

Leave a reply