| 1 | package Finance::GeniusTrader::Indicators::VOSC; | ||
| 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 | # Standards-Version: 1.0 | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use vars qw(@ISA @NAMES @DEFAULT_ARGS); | ||
| 11 | |||
| 12 | use Finance::GeniusTrader::Indicators; | ||
| 13 | use Finance::GeniusTrader::Indicators::SMA; | ||
| 14 | use Finance::GeniusTrader::Prices; | ||
| 15 | |||
| 16 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 17 | @NAMES = ("VOSC[#1]", "VOSC-volume[#1]"); | ||
| 18 | @DEFAULT_ARGS = (7); | ||
| 19 | |||
| 20 | =head1 NAME | ||
| 21 | |||
| 22 | Finance::GeniusTrader::Indicators::VOSC - | ||
| 23 | |||
| 24 | =head1 OVERVIEW | ||
| 25 | |||
| 26 | =head1 CALCULATION | ||
| 27 | |||
| 28 | =head1 EXAMPLES | ||
| 29 | |||
| 30 | Finance::GeniusTrader::Indicators::VOSC->new() | ||
| 31 | Finance::GeniusTrader::Indicators::VOSC->new([20]) | ||
| 32 | |||
| 33 | =head1 LINKS | ||
| 34 | |||
| 35 | =cut | ||
| 36 | |||
| 37 | sub initialize { | ||
| 38 | my $self = shift; | ||
| 39 | |||
| 40 | # Initialize SMA | ||
| 41 | $self->{'sma'} = Finance::GeniusTrader::Indicators::SMA->new([ $self->{'args'}->get_arg_names(1), "{I:Generic:ByName ". $self->get_name(1) ."}"]); | ||
| 42 | |||
| 43 | # Can't add this dependency since the indicator will not be | ||
| 44 | # able to be computed at the check time | ||
| 45 | |||
| 46 | #$self->add_indicator_dependency($self->{'sma'}, 1); | ||
| 47 | $self->add_prices_dependency($self->{'args'}->get_arg_constant(1)); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub calculate { | ||
| 51 | my ($self, $calc, $i) = @_; | ||
| 52 | my $indic = $calc->indicators; | ||
| 53 | my $prices = $calc->prices; | ||
| 54 | my $vosc_name = $self->get_name(0); | ||
| 55 | my $volume_name = $self->get_name(1); | ||
| 56 | my $volume = 0; | ||
| 57 | |||
| 58 | return if ($indic->is_available($vosc_name, $i)); | ||
| 59 | return if (! $self->check_dependencies($calc, $i)); | ||
| 60 | |||
| 61 | # Determine how much days are required for the calculation | ||
| 62 | my $nb_days = $self->{'args'}->get_arg_values($calc, $i, 1); | ||
| 63 | |||
| 64 | for (my $n = 0; $n < $nb_days; $n++) { | ||
| 65 | |||
| 66 | # Return if volume is available | ||
| 67 | next if $indic->is_available($volume_name, $i - $n); | ||
| 68 | |||
| 69 | # Calculate volume | ||
| 70 | if ($prices->at($i - $n)->[$CLOSE] > $prices->at($i - $n)->[$OPEN]) { | ||
| 71 | $volume = $prices->at($i - $n)->[$VOLUME]; | ||
| 72 | } | ||
| 73 | if ($prices->at($i - $n)->[$CLOSE] < $prices->at($i - $n)->[$OPEN]) { | ||
| 74 | $volume = -$prices->at($i - $n)->[$VOLUME]; | ||
| 75 | } | ||
| 76 | if ($prices->at($i - $n)->[$CLOSE] eq $prices->at($i - $n)->[$OPEN]) { | ||
| 77 | $volume = 0; | ||
| 78 | } | ||
| 79 | |||
| 80 | # Return results | ||
| 81 | $indic->set($volume_name, $i - $n, $volume); | ||
| 82 | |||
| 83 | } | ||
| 84 | |||
| 85 | # Calculate and get VOSC | ||
| 86 | $self->{'sma'}->calculate($calc, $i); | ||
| 87 | my $vosc_value = $indic->get($self->{'sma'}->get_name, $i); | ||
| 88 | |||
| 89 | # Return the results | ||
| 90 | $indic->set($vosc_name, $i, $vosc_value); | ||
| 91 | } | ||
| 92 | |||
| 93 | 1; |