| 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 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use vars qw(@ISA @EXPORT $PERIOD_TICK $PERIOD_1MIN $PERIOD_5MIN $PERIOD_10MIN | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | $PERIOD_15MIN $PERIOD_30MIN $HOUR $PERIOD_2HOUR $PERIOD_3HOUR $PERIOD_4HOUR | ||||||||||
| 10 | 1 | $DAY $WEEK $MONTH $YEAR %NAMES); | |||||||||
| 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 | 0 | ▶ | sub map_date_to_time { | ||||||||
|
Subroutine
map_date_to_time: not called CC=15 SCAR=54.8
| |||||||||||
| 107 | 0 | my ($timeframe, $date) = @_; | |||||||||
| 108 | |||||||||||
| 109 | 0 | ▶ | $timeframe == $PERIOD_TICK && return Finance::GeniusTrader::DateTime::Tick::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | 0 | ▶ | $timeframe == $PERIOD_1MIN && return Finance::GeniusTrader::DateTime::1Min::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | ▶ | $timeframe == $PERIOD_5MIN && return Finance::GeniusTrader::DateTime::5Min::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | 0 | ▶ | $timeframe == $PERIOD_10MIN && return Finance::GeniusTrader::DateTime::10Min::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | ▶ | $timeframe == $PERIOD_15MIN && return Finance::GeniusTrader::DateTime::15Min::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | ▶ | $timeframe == $PERIOD_30MIN && return Finance::GeniusTrader::DateTime::30Min::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | ▶ | $timeframe == $HOUR && return Finance::GeniusTrader::DateTime::Hour::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 0 | ▶ | $timeframe == $PERIOD_2HOUR && return Finance::GeniusTrader::DateTime::2Hour::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | 0 | ▶ | $timeframe == $PERIOD_3HOUR && return Finance::GeniusTrader::DateTime::3Hour::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 0 | ▶ | $timeframe == $PERIOD_4HOUR && return Finance::GeniusTrader::DateTime::4Hour::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | 0 | ▶ | $timeframe == $DAY && return Finance::GeniusTrader::DateTime::Day::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | ▶ | $timeframe == $WEEK && return Finance::GeniusTrader::DateTime::Week::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | ▶ | $timeframe == $MONTH && return Finance::GeniusTrader::DateTime::Month::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 0 | ▶ | $timeframe == $YEAR && return Finance::GeniusTrader::DateTime::Year::map_date_to_time($date); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | 0 | ▶ | sub map_time_to_date { | ||||||||
|
Subroutine
map_time_to_date: not called CC=15 SCAR=54.8
| |||||||||||
| 126 | 0 | my ($timeframe, $time) = @_; | |||||||||
| 127 | |||||||||||
| 128 | 0 | ▶ | $timeframe == $PERIOD_TICK && return Finance::GeniusTrader::DateTime::Tick::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 0 | ▶ | $timeframe == $PERIOD_1MIN && return Finance::GeniusTrader::DateTime::1Min::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | ▶ | $timeframe == $PERIOD_5MIN && return Finance::GeniusTrader::DateTime::5Min::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 131 | 0 | ▶ | $timeframe == $PERIOD_10MIN && return Finance::GeniusTrader::DateTime::10Min::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | 0 | ▶ | $timeframe == $PERIOD_15MIN && return Finance::GeniusTrader::DateTime::15Min::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 133 | 0 | ▶ | $timeframe == $PERIOD_30MIN && return Finance::GeniusTrader::DateTime::30Min::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | 0 | ▶ | $timeframe == $HOUR && return Finance::GeniusTrader::DateTime::Hour::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 135 | 0 | ▶ | $timeframe == $PERIOD_2HOUR && return Finance::GeniusTrader::DateTime::2Hour::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 136 | 0 | ▶ | $timeframe == $PERIOD_3HOUR && return Finance::GeniusTrader::DateTime::3Hour::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 0 | ▶ | $timeframe == $PERIOD_4HOUR && return Finance::GeniusTrader::DateTime::4Hour::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 0 | ▶ | $timeframe == $DAY && return Finance::GeniusTrader::DateTime::Day::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 139 | 0 | ▶ | $timeframe == $WEEK && return Finance::GeniusTrader::DateTime::Week::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | 0 | ▶ | $timeframe == $MONTH && return Finance::GeniusTrader::DateTime::Month::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | 0 | ▶ | $timeframe == $YEAR && return Finance::GeniusTrader::DateTime::Year::map_time_to_date($time); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub convert_date { | ||||||||
|
Subroutine
convert_date: not called CC=1 SCAR=6.9
| |||||||||||
| 152 | 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 | 0 | ▶ | sub list_of_timeframe { | ||||||||
|
Subroutine
list_of_timeframe: not called CC=1 SCAR=6.9
| |||||||||||
| 163 | return ( | ||||||||||
| 164 | 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 | 0 | ▶ | sub name_of_timeframe { | ||||||||
|
Subroutine
name_of_timeframe: not called CC=1 SCAR=6.9
| |||||||||||
| 176 | 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 | 0 | ▶ | sub name_to_timeframe { | ||||||||
|
Subroutine
name_to_timeframe: not called CC=3 SCAR=24.8
| |||||||||||
| 186 | 0 | my ($name) = @_; | |||||||||
| 187 | 0 | foreach (keys %NAMES) | |||||||||
| 188 | { | ||||||||||
| 189 | 0 | ▶ | if ($NAMES{$_} eq $name) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 0 | ▶ | sub timeframe_ratio { | ||||||||
|
Subroutine
timeframe_ratio: not called CC=17 SCAR=57.2
| |||||||||||
| 203 | 0 | my ($first, $second) = @_; | |||||||||
| 204 | 0 | ▶ | return 1 if ($first == $second); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 205 | 0 | ▶ | if ($first < $second) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 206 | { | ||||||||||
| 207 | 0 | return (1 / timeframe_ratio($second, $first)); | |||||||||
| 208 | } | ||||||||||
| 209 | |||||||||||
| 210 | 0 | ▶ | $first == $PERIOD_TICK && die("Cannot set timeframe ratio for tick data"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 211 | 0 | ▶ | $first == $PERIOD_1MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 60); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 212 | 0 | ▶ | $first == $PERIOD_5MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 12); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 213 | 0 | ▶ | $first == $PERIOD_10MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 6); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 214 | 0 | ▶ | $first == $PERIOD_15MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 4); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 215 | 0 | ▶ | $first == $PERIOD_30MIN && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) / 2); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 216 | 0 | ▶ | $first == $HOUR && return Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 217 | 0 | ▶ | $first == $PERIOD_2HOUR && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) * 2); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 218 | 0 | ▶ | $first == $PERIOD_3HOUR && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) * 3); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 219 | 0 | ▶ | $first == $PERIOD_4HOUR && return (Finance::GeniusTrader::DateTime::Hour::timeframe_ratio($second) * 4); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 220 | 0 | ▶ | $first == $DAY && return Finance::GeniusTrader::DateTime::Day::timeframe_ratio($second); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 221 | 0 | ▶ | $first == $WEEK && return Finance::GeniusTrader::DateTime::Week::timeframe_ratio($second); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 222 | 0 | ▶ | $first == $MONTH && return Finance::GeniusTrader::DateTime::Month::timeframe_ratio($second); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 223 | 0 | ▶ | $first == $YEAR && return Finance::GeniusTrader::DateTime::Year::timeframe_ratio($second); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 224 | } | ||||||||||
| 225 | |||||||||||
| 226 | =pod | ||||||||||
| 227 | |||||||||||
| 228 | =back | ||||||||||
| 229 | |||||||||||
| 230 | =cut | ||||||||||
| 231 | 1; | ||||||||||