December 28, 2007
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:
Filed by Herman at 12:42 pm under Real-Time AFL Programming


(5 votes, average: 4.4 out of 5)
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 ;)