[Functions] Calculating time intervals easily

Studies that have been contributed to the community by other users. If you’ve got something useful to share, that’s great!
User avatar
JoshM
Posts: 2195
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1544 times
Been thanked: 1565 times
Contact:

[Functions] Calculating time intervals easily

Postby JoshM » 01 Sep 2014

Following this topic I've made a few functions to make working with time intervals in PowerLanguage easier.

The current contents (might be expanded in the near future):
  • TimeInterval.Minutes(BarsBack_Oldest, BarsBack_Newest): calculates the time interval in total number of minutes between the bars back (relative to the current bar).
  • TimeInterval.MinutesAbs(BarsBackFirst, BarsBackSecond): calculates the time interval in the absolute number of minutes between both (relative to the current bar).
  • TimeSince.Minutes(BarsBack): returns the time interval in minutes of the current computer time minus the time of a bar a certain 'BarsBack' back. Note that this is affected by time zone difference between your trading chart and your computer time.
TimeIntervalFunctions_1-9-14.pla
(5.4 KiB) Downloaded 484 times
Example of how to use:

Code: Select all

once (LastBarOnChart_s = true) begin

// Time interval between current bar (0) and four bars back
Print("Prev: ", FormatTime("HH:mm:ss", DateTime[4]));
Print("Current: ", FormatTime("HH:mm:ss", DateTime));

Print("Minutes: ", TimeInterval.Minutes(4, 0), NewLine);


// Absolute time interval between previous bar and
// bar 12 bars back
Print("Prev: ", FormatTime("HH:mm:ss", DateTime[12]));
Print("Current: ", FormatTime("HH:mm:ss", DateTime[1]));

Print("Minutes: ", TimeInterval.MinutesAbs(1, 12), NewLine);

// Time interval between the bar 8 bars back and the
// current pc time (affected by time zone difference of pc and chart!)
Print("Prev: ", FormatTime("HH:mm:ss", DateTime[8]));
Print("Current: ", FormatTime("HH:mm:ss", ComputerDateTime));

Print("Minutes: ", TimeSince.Minutes(8), NewLine);

end;
Example output:

Code: Select all

Prev: 11:35:00
Current: 13:35:00
Minutes: 120.00

Prev: 07:35:00
Current: 13:05:00
Minutes: 330.00

Prev: 09:35:00
Current: 13:08:57
Minutes: 213.96
To learn more about calculating time intervals, see here. Suggestions for other functions/usage situations are welcome.

FATSTrader
Posts: 15
Joined: 14 Feb 2011
Has thanked: 7 times
Been thanked: 3 times

Re: [Functions] Calculating time intervals easily

Postby FATSTrader » 01 Nov 2014

THanks, Josh. Great work!


Return to “User Contributed Studies and Indicator Library”