File Coverage

blib/lib/Finance/GeniusTrader/DateTime.pm
Criterion Covered Total %
statement 6 64 9.3
branch 0 90 0.0
condition n/a
subroutine 2 9 22.2
pod n/a
total 8 163 4.9


line stmt bran cond sub pod time code
1             package Finance::GeniusTrader::DateTime;
2              
3             # Copyright 2000-2002 Raphaël Hertzog, Fabien Fulhaber
4             # This file is distributed under the terms of the General Public License
5             # version 2 or (at your option) any later version.
6              
7 1     1   6 use strict;
  1         2  
  1         49  
8 1         1805 use vars qw(@ISA @EXPORT $PERIOD_TICK $PERIOD_1MIN $PERIOD_5MIN $PERIOD_10MIN
9             $PERIOD_15MIN $PERIOD_30MIN $HOUR $PERIOD_2HOUR $PERIOD_3HOUR $PERIOD_4HOUR
10 1     1   5 $DAY $WEEK $MONTH $YEAR %NAMES);
  1         2  
11              
12             require Exporter;
13             @ISA = qw(Exporter);
14             @EXPORT = qw($PERIOD_TICK $PERIOD_1MIN $PERIOD_5MIN $PERIOD_10MIN
15             $PERIOD_15MIN $PERIOD_30MIN $HOUR $PERIOD_2HOUR $PERIOD_3HOUR $PERIOD_4HOUR $DAY $WEEK $MONTH $YEAR);
16              
17             #ALL# use Log::Log4perl qw(:easy);
18              
19             $PERIOD_TICK = 1;
20             $PERIOD_1MIN = 10;
21             $PERIOD_5MIN = 30;
22             $PERIOD_10MIN = 40;
23             $PERIOD_15MIN = 45;
24             $PERIOD_30MIN = 50;
25             $HOUR = 60;
26             $PERIOD_2HOUR = 62;
27             $PERIOD_3HOUR = 64;
28             $PERIOD_4HOUR = 66;
29             $DAY = 70;
30             $WEEK = 80;
31             $MONTH = 90;
32             $YEAR = 100;
33              
34             %NAMES = (
35             $PERIOD_TICK => "tick",
36             $PERIOD_1MIN => "1min",
37             $PERIOD_5MIN => "5min",
38             $PERIOD_10MIN => "10min",
39             $PERIOD_15MIN => "15min",
40             $PERIOD_30MIN => "30min",
41             $HOUR => "hour",
42             $PERIOD_2HOUR => "2hour",
43             $PERIOD_3HOUR => "3hour",
44             $PERIOD_4HOUR => "4hour",
45             $DAY => "day",
46             $WEEK => "week",
47             $MONTH => "month",
48             $YEAR => "year"
49             );
50              
51             require Finance::GeniusTrader::DateTime::Tick;
52             require Finance::GeniusTrader::DateTime::1Min;
53             require Finance::GeniusTrader::DateTime::5Min;
54             require Finance::GeniusTrader::DateTime::10Min;
55             require Finance::GeniusTrader::DateTime::15Min;
56             require Finance::GeniusTrader::DateTime::30Min;
57             require Finance::GeniusTrader::DateTime::Hour;
58             require Finance::GeniusTrader::DateTime::2Hour;
59             require Finance::GeniusTrader::DateTime::3Hour;
60             require Finance::GeniusTrader::DateTime::4Hour;
61             require Finance::GeniusTrader::DateTime::Day;
62             require Finance::GeniusTrader::DateTime::Week;
63             require Finance::GeniusTrader::DateTime::Month;
64             require Finance::GeniusTrader::DateTime::Year;
65              
66             =head1 NAME
67              
68             Finance::GeniusTrader::DateTime - Manage TimeFrames and provides date/time helper functions
69              
70             =head1 DESCRIPTION
71              
72             This module exports all the variable describing the available "periods"
73             commonly used for trading : $PERIOD_TICK $PERIOD_1MIN, $PERIOD_5MIN,
74             $PERIOD_10MIN, $PERIOD_15MIN, $PERIOD_30MIN, $HOUR, $PERIOD_2HOUR,
75             $PERIOD_3HOUR, $PERIOD_4HOUR, $DAY, $WEEK, $MONTH, $YEAR.
76              
77             The timeframes are represented by those variables which are only numbers.
78             You can compare those numbers to know which timeframe is smaller or which
79             one is bigger.
80              
81             It also provides several functions to manipulate dates and periods. Those
82             functions use modules Finance::GeniusTrader::DateTime::* to do the actual work depending on
83             the selected timeframe.
84              
85             =head2 Functions provided by submodules
86              
87             map_date_to_time($date) is a function returning a time (ie a number of
88             seconds since 1970) representing that date in the history. It is usually
89             corresponding to the first second of the given period.
90              
91             map_time_to_date($time) is the complementary function. It will return a
92             date describing the period that includes the given time.
93              
94             =head2 Functions
95              
96             =over
97              
98             =item C<< Finance::GeniusTrader::DateTime::map_date_to_time($timeframe, $date) >>
99              
100             =item C<< Finance::GeniusTrader::DateTime::map_time_to_date($timeframe, $time) >>
101              
102             Those are the generic functions used to convert a date into a time and vice
103             versa.
104              
105             =cut
106             sub map_date_to_time {
107 0     0     my ($timeframe, $date) = @_;
108              
109 0 0         $timeframe == $PERIOD_TICK && return Finance::GeniusTrader::DateTime::Tick::map_date_to_time($date);
110 0 0         $timeframe == $PERIOD_1MIN && return Finance::GeniusTrader::DateTime::1Min::map_date_to_time($date);
111 0 0         $timeframe == $PERIOD_5MIN && return Finance::GeniusTrader::DateTime::5Min::map_date_to_time($date);
112 0 0         $timeframe == $PERIOD_10MIN && return Finance::GeniusTrader::DateTime::10Min::map_date_to_time($date);
113 0 0         $timeframe == $PERIOD_15MIN && return Finance::GeniusTrader::DateTime::15Min::map_date_to_time($date);
114 0 0         $timeframe == $PERIOD_30MIN && return Finance::GeniusTrader::DateTime::30Min::map_date_to_time($date);
115 0 0         $timeframe == $HOUR && return Finance::GeniusTrader::DateTime::Hour::map_date_to_time($date);
116 0 0         $timeframe == $PERIOD_2HOUR && return Finance::GeniusTrader::DateTime::2Hour::map_date_to_time($date);
117 0 0         $timeframe == $PERIOD_3HOUR && return Finance::GeniusTrader::DateTime::3Hour::map_date_to_time($date);
118 0 0         $timeframe == $PERIOD_4HOUR && return Finance::GeniusTrader::DateTime::4Hour::map_date_to_time($date);
119 0 0         $timeframe == $DAY && return Finance::GeniusTrader::DateTime::Day::map_date_to_time($date);
120 0 0         $timeframe == $WEEK && return Finance::GeniusTrader::DateTime::Week::map_date_to_time($date);
121 0 0         $timeframe == $MONTH && return Finance::GeniusTrader::DateTime::Month::map_date_to_time($date);
122 0 0         $timeframe == $YEAR && return Finance::GeniusTrader::DateTime::Year::map_date_to_time($date);
123             }
124              
125             sub map_time_to_date {
126 0     0     my ($timeframe, $time) = @_;
127              
128 0 0         $timeframe == $PERIOD_TICK && return Finance::GeniusTrader::DateTime::Tick::map_time_to_date($time);
129 0 0         $timeframe == $PERIOD_1MIN && return Finance::GeniusTrader::DateTime::1Min::map_time_to_date($time);
130 0 0         $timeframe == $PERIOD_5MIN && return Finance::GeniusTrader::DateTime::5Min::map_time_to_date($time);
131 0 0         $timeframe == $PERIOD_10MIN && return Finance::GeniusTrader::DateTime::10Min::map_time_to_date($time);
132 0 0         $timeframe == $PERIOD_15MIN && return Finance::GeniusTrader::DateTime::15Min::map_time_to_date($time);
133 0 0         $timeframe == $PERIOD_30MIN && return Finance::GeniusTrader::DateTime::30Min::map_time_to_date($time);
134 0 0         $timeframe == $HOUR && return Finance::GeniusTrader::DateTime::Hour::map_time_to_date($time);
135 0 0         $timeframe == $PERIOD_2HOUR && return Finance::GeniusTrader::DateTime::2Hour::map_time_to_date($time);
136 0 0         $timeframe == $PERIOD_3HOUR && return Finance::GeniusTrader::DateTime::3Hour::map_time_to_date($time);
137 0 0         $timeframe == $PERIOD_4HOUR && return Finance::GeniusTrader::DateTime::4Hour::map_time_to_date($time);
138 0 0         $timeframe == $DAY && return Finance::GeniusTrader::DateTime::Day::map_time_to_date($time);
139 0 0         $timeframe == $WEEK && return Finance::GeniusTrader::DateTime::Week::map_time_to_date($time);
140 0 0         $timeframe == $MONTH && return Finance::GeniusTrader::DateTime::Month::map_time_to_date($time);
141 0 0         $timeframe == $YEAR && return Finance::GeniusTrader::DateTime::Year::map_time_to_date($time);
142             }
143              
144             =item C<< Finance::GeniusTrader::DateTime::convert_date($date, $orig_timeframe, $dest_timeframe) >>
145              
146             This function does convert the given date from the $orig_timeframe in a
147             date of the $dest_timeframe. Take care that the destination timeframe must be
148             bigger than the original timeframe.
149              
150             =cut
151             sub convert_date {
152 0     0     my ($date, $orig, $dest) = @_;
153             #WAR# WARN "the destination time frame must be bigger" if ( $orig <= $dest);
154 0           return map_time_to_date($dest, map_date_to_time($orig, $date));
155             }
156              
157             =item C<< Finance::GeniusTrader::DateTime::list_of_timeframe() >>
158              
159             Returns the list of timeframes that are managed by the DateTime framework.
160              
161             =cut
162             sub list_of_timeframe {
163             return (
164 0     0     $PERIOD_TICK, $PERIOD_1MIN, $PERIOD_5MIN, $PERIOD_10MIN,
165             $PERIOD_15MIN, $PERIOD_30MIN, $HOUR, $PERIOD_2HOUR, $PERIOD_3HOUR,
166             $PERIOD_4HOUR, $DAY, $WEEK, $MONTH, $YEAR
167             );
168             }
169              
170             =item C<< Finance::GeniusTrader::DateTime::name_of_timeframe($tf) >>
171              
172             Return the official name of the corresponding timeframe.
173              
174             =cut
175             sub name_of_timeframe {
176 0     0     my ($tf) = @_;
177 0           return $NAMES{$tf};
178             }
179              
180             =item C<< Finance::GeniusTrader::DateTime::name_to_timeframe($name) >>
181              
182             Returns the timeframe associated to the given name.
183              
184             =cut
185             sub name_to_timeframe {
186 0     0     my ($name) = @_;
187 0           foreach (keys %NAMES)
188             {
189 0 0         if ($NAMES{$_} eq $name)
190             {
191 0           return $_;
192             }
193             }
194 0           return undef;
195             }
196              
197             =item C<< Finance::GeniusTrader::DateTime::timeframe_ratio($first, $second) >>
198              
199             Returns how many times the second timeframe fits in the first one.
200              
201             =cut
202             sub timeframe_ratio {
203 0     0     my ($first, $second) = @_;
204 0 0         return 1 if ($first == $second);
205 0 0         if ($first < $second)
206             {
207 0           return (1 / timeframe_ratio($second, $first));
208             }
209              
210 0 0         $first == $PERIOD_TICK && die("Cannot set timeframe ratio for tick data");
211 0 0         $first == $PERIOD_1MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 60);
212 0 0         $first == $PERIOD_5MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 12);
213 0 0         $first == $PERIOD_10MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 6);
214 0 0         $first == $PERIOD_15MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 4);
215 0 0         $first == $PERIOD_30MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 2);
216 0 0         $first == $HOUR && return Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second);
217 0 0         $first == $PERIOD_2HOUR && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) * 2);
218 0 0         $first == $PERIOD_3HOUR && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) * 3);
219 0 0         $first == $PERIOD_4HOUR && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) * 4);
220 0 0         $first == $DAY && return Finance::GeniusTrader::DateTime::Day::timeframe_ratio($second);
221 0 0         $first == $WEEK && return Finance::GeniusTrader::DateTime::Week::timeframe_ratio($second);
222 0 0         $first == $MONTH && return Finance::GeniusTrader::DateTime::Month::timeframe_ratio($second);
223 0 0         $first == $YEAR && return Finance::GeniusTrader::DateTime::Year::timeframe_ratio($second);
224             }
225              
226             =pod
227              
228             =back
229              
230             =cut
231             1;