GeniusTrader/PricesTools.pm untested

filter: Statementstmt 0.0% 0 / 132 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 215 CC 21 SCAR 53.4
CC
21
Cov
0%
convert_prices_in_a_new_time_frame
342.0
select_prices_by_period
20.0
multiply_prices_by_number
2.0
CRAP 208.4
1 package Finance::GeniusTrader::PricesTools;
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 use strict;
8 use vars qw(@ISA @EXPORT $DAILY $MONTHLY $YEARLY $WEEKLY);
9 use Finance::GeniusTrader::Prices;
10 use Date::Calc qw(Date_to_Days Day_of_Week Week_Number);
11  
12 require Exporter;
13 @ISA = qw(Exporter);
14 @EXPORT = qw($DAILY $MONTHLY $YEARLY $WEEKLY multiply_prices_by_number divide_prices_by_number convert_prices_in_a_new_time_frame reverse_prices select_prices_by_period);
15  
16 $DAILY = 0;
17 $MONTHLY = 1;
18 $YEARLY = 2;
19 $WEEKLY = 3;
20  
21 =head1 NAME
22  
23 Finance::GeniusTrader::PricesTools - Utility functions for manipulating Finance::GeniusTrader::Prices
24  
25 =head1 DESCRIPTION
26  
27 This package provide some simple functions to merge data from an existing
28 Finance::GeniusTrader::Prices to a new Finance::GeniusTrader::Prices object. It's especially usefull to convert
29 daily data to a new time frame (ie: weekly/monthly).
30  
31 =head2 Examples
32  
33 convert_prices_in_a_new_time_frame($prices, $WEEKLY);
34 select_prices_by_period($prices, "2000-01-01", "2000-12-31");
35  
36 multiply_prices_by_number($prices, 3.45);
37 divide_prices_by_number($prices, 2.5);
38  
39 reverse_prices($prices)
40  
41 =head2 Ideas for later
42  
43 adjust prices by the last rate of a currency
44 adjust prices by daily historical rate of a currency
45  
46 =cut
47  
48 sub convert_prices_in_a_new_time_frame {
49 my ($prices, $time_frame) = @_;
50 my $period = 0;
51 my $new_period = 0;
52 my $new_period_name = "";
53 my $last_period_name = "";
54 my $start = 0;
55 my ($open, $high, $low, $close, $volume, $date) = 0;
56  
57 # Return if we don't have enouth parameters
58 return if not $prices;
59 return if !$time_frame;
60
61 my $quotes = Finance::GeniusTrader::Prices->new();
62  
63 for (my $i = 0; $i < $prices->count(); $i++) {
64  
65 # Split the date from the database to something usable
66 my ($year, $month, $day) = split(/-/, $prices->at($i)->[$DATE]);
67  
68 # Initialize period
69 if ($time_frame eq $DAILY) {
70 $period = $day;
71 }
72 if ($time_frame eq $MONTHLY) {
73 $period = $month;
74 }
75 if ($time_frame eq $YEARLY) {
76 $period = $year;
77 }
78 if ($time_frame eq $WEEKLY) {
79 $period = Date_to_Days($year, $month, $day) - Day_of_Week($year, $month, $day);
80 }
81
82 # Check if the current price is in the current period or not
83 if ($new_period_name eq $period) {
84 $new_period = 0;
85 } else {
86 $new_period_name = $period;
87 $new_period = 1;
88 }
89  
90 # Add a record in the new time frame, for a finished period
91 if ($i > 0) {
92 if ($new_period eq 1) {
93 $open = $prices->at($start)->[$OPEN];
94 $close = $prices->at($i - 1)->[$CLOSE];
95 $quotes->add_prices([ $open, $high, $low, $close, $volume, $date ]);
96 $start = $i;
97 $high = $low = $volume = 0;
98 }
99 }
100
101 # Initialize date according to the specified time frame
102 if (($new_period_name eq $period) or ($date eq 0)) {
103 if ($time_frame eq $DAILY) {
104 $date = $year . "-" . $month . "-" . $day;
105 }
106 if ($time_frame eq $MONTHLY) {
107 $date = $year . "-" . $month;
108 }
109 if ($time_frame eq $YEARLY) {
110 $date = $year;
111 }
112 if ($time_frame eq $WEEKLY) {
113 $date = $year . "-" . Week_Number($year, $month, $day);
114 }
115 }
116  
117 # Calculate $high, $low and $volume
118 if ((!$high) or ($high eq 0) or ($prices->at($i)->[$HIGH] > $high)) {
119 $high = $prices->at($i)->[$HIGH];
120 }
121 if ((!$low) or ($low eq 0) or ($prices->at($i)->[$LOW] < $low)) {
122 $low = $prices->at($i)->[$LOW];
123 }
124 $volume += $prices->at($i)->[$VOLUME];
125  
126 # Add a record in the new time frame, for the last and not yet
127 # finished period
128 if ($i eq ($prices->count() - 1)) {
129 $open = $prices->at($start)->[$OPEN];
130 $close = $prices->at($i)->[$CLOSE];
131 $quotes->add_prices([ $open, $high, $low, $close, $volume, $date ]);
132 }
133 }
134 return $quotes;
135 }
136  
137 sub select_prices_by_period {
138 my ($prices, $first_date, $last_date) = @_;
139 my $quotes = Finance::GeniusTrader::Prices->new();
140  
141 # Initialize $first_date to the first record's date and $last_date to the last record's date
142 # if these arguments are not available
143 if (!$first_date) {
144 $first_date = $prices->at(0)->[$DATE];
145 }
146 if (!$last_date) {
147 $last_date = $prices->at($prices->count() - 1)->[$DATE];
148 }
149
150 # Split $first_date and $last_date to something usable
151 my ($first_date_year, $first_date_month, $first_date_day) = split(/-/, $first_date);
152 my ($last_date_year, $last_date_month, $last_date_day) = split(/-/, $last_date);
153
154 for (my $i = 0; $i < $prices->count(); $i++) {
155  
156 # Split record's date to something usable
157 my ($year, $month, $day) = split(/-/, $prices->at($i)->[$DATE]);
158  
159 # Add a record in the new Finance::GeniusTrader::Prices object if (First Date < Record's Date) and (Record's Date < Last Date)
160 if ( (Date_to_Days($first_date_year, $first_date_month, $first_date_day) <= Date_to_Days($year, $month, $day)) and
161 (Date_to_Days($year, $month, $day) <= Date_to_Days($last_date_year, $last_date_month, $last_date_day)) ) {
162  
163 $quotes->add_prices([ $prices->at($i)->[$OPEN], $prices->at($i)->[$HIGH], $prices->at($i)->[$LOW], $prices->at($i)->[$CLOSE], $prices->at($i)->[$VOLUME], $prices->at($i)->[$DATE] ]);
164 }
165 }
166 return $quotes;
167 }
168  
169 sub multiply_prices_by_number {
170 my ($prices, $number) = @_;
171 my $quotes = Finance::GeniusTrader::Prices->new();
172
173 for (my $i = 0; $i < $prices->count(); $i++) {
174 $quotes->add_prices([ ($prices->at($i)->[$OPEN] * $number), ($prices->at($i)->[$HIGH] * $number), ($prices->at($i)->[$LOW] * $number), ($prices->at($i)->[$CLOSE] * $number), ($prices->at($i)->[$VOLUME] * $number), $prices->at($i)->[$DATE] ]);
175 }
176 return $quotes;
177 }
178  
179 sub divide_prices_by_number {
180 my ($prices, $number) = @_;
181 my $quotes = Finance::GeniusTrader::Prices->new();
182  
183 for (my $i = 0; $i < $prices->count(); $i++) {
184 $quotes->add_prices([ ($prices->at($i)->[$OPEN] / $number), ($prices->at($i)->[$HIGH] / $number), ($prices->at($i)->[$LOW] / $number), ($prices->at($i)->[$CLOSE] / $number), ($prices->at($i)->[$VOLUME] / $number), $prices->at($i)->[$DATE] ]);
185 }
186 return $quotes;
187 }
188  
189 sub reverse_prices {
190 my ($prices) = @_;
191 my $quotes = Finance::GeniusTrader::Prices->new();
192 my $max = $prices->count();
193
194 for (my $i = 0; $i < $max; $i++) {
195 my $n = $max - $i - 1;
196 $quotes->add_prices([ $prices->at($n)->[$OPEN], $prices->at($n)->[$HIGH], $prices->at($n)->[$LOW], $prices->at($n)->[$CLOSE], $prices->at($n)->[$VOLUME], $prices->at($n)->[$DATE] ]);
197 }
198 return $quotes;
199 }
200  
201 1;