Real-Time Bar-Period Timing

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.

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
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"+ 
"time: "+Now(2)+"\n"+ 
"Interval: "+NumToStr(TimeFrame,1.0)+"\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);

For testing and code verification timing is displayed in the chart title:

clip_image002

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

One Response to “Real-Time Bar-Period Timing”

  1. March 29th, 2008 | 10:37 am

    Oh! Wonderful job!
    Very interesting and useful post.
    Thx, your blog in my RSS reader now
    We’ll expect many new interesting posts from you ;)

Leave a reply