« GeniusTrader/MoneyManagement/PositionSizeLimit.pm
↑ Summary
GeniusTrader/Indicators/PercentagePosition.pm »
| 1 | package Finance::GeniusTrader::Indicators::OBV; | ||
| 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 @NAMES); | ||
| 9 | |||
| 10 | use Finance::GeniusTrader::Indicators; | ||
| 11 | use Finance::GeniusTrader::Prices; | ||
| 12 | |||
| 13 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 14 | @NAMES = ("OBV"); | ||
| 15 | |||
| 16 | =head1 Finance::GeniusTrader::Indicators::OBV | ||
| 17 | |||
| 18 | =head2 Overview | ||
| 19 | |||
| 20 | On Balance Volume (OBV) is a momentum indicator that relates volume price change. | ||
| 21 | |||
| 22 | On Balance Volume was developed by Joe Granville and originally presented in his book New Strategy of Daily Stock Market Timing for Maximum Profits. | ||
| 23 | |||
| 24 | =head2 Calculation | ||
| 25 | |||
| 26 | On Balance Volume is calculated by adding the day's volume to a cumulative total when the security's price closes up, and subtracting the day's volume when the security's price closes down. | ||
| 27 | |||
| 28 | If today's close is greater than yesterday's close then : | ||
| 29 | OBV = Yesterday's OBV + Today's Volume | ||
| 30 | |||
| 31 | If today's close is less than yesterday's close then : | ||
| 32 | OBV = Yesterday's OBV - Today's Volume | ||
| 33 | |||
| 34 | If today's close is equal to yesterday's close then : | ||
| 35 | OBV = Yesterday's OBV | ||
| 36 | |||
| 37 | =head2 Example | ||
| 38 | |||
| 39 | Finance::GeniusTrader::Indicators::OBV->new() | ||
| 40 | |||
| 41 | =head2 Links | ||
| 42 | |||
| 43 | =cut | ||
| 44 | sub new { | ||
| 45 | my $type = shift; | ||
| 46 | my $class = ref($type) || $type; | ||
| 47 | my $self = { 'args' => [] }; | ||
| 48 | |||
| 49 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ""); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub initialize { | ||
| 53 | my ($self) = @_; | ||
| 54 | |||
| 55 | $self->add_prices_dependency(2); | ||
| 56 | } | ||
| 57 | |||
| 58 | =head2 Finance::GeniusTrader::Indicators::OBV::calculate($calc, $day) | ||
| 59 | |||
| 60 | =cut | ||
| 61 | sub calculate { | ||
| 62 | my ($self, $calc, $i) = @_; | ||
| 63 | my $name = $self->get_name; | ||
| 64 | my $obv = 0; | ||
| 65 | |||
| 66 | return if ($calc->indicators->is_available($name, $i)); | ||
| 67 | return if (! $self->check_dependencies($calc, $i)); | ||
| 68 | |||
| 69 | my $prices = $calc->prices; | ||
| 70 | my $diff = 0; | ||
| 71 | |||
| 72 | if ($calc->indicators->is_available($name, $i - 1)) { | ||
| 73 | $obv = $calc->indicators->get($name, $i - 1); | ||
| 74 | $diff = $prices->at($i)->[$LAST] - $prices->at($i - 1)->[$LAST]; | ||
| 75 | if ($diff > 0) { | ||
| 76 | $obv += $prices->at($i)->[$VOLUME]; | ||
| 77 | } | ||
| 78 | if ($diff < 0) { | ||
| 79 | $obv -= $prices->at($i)->[$VOLUME]; | ||
| 80 | } | ||
| 81 | $calc->indicators->set($name, $i, $obv); | ||
| 82 | } else { | ||
| 83 | for(my $n = 1; $n <= $i; $n++) | ||
| 84 | { | ||
| 85 | $diff = $prices->at($n)->[$LAST] - $prices->at($n - 1)->[$LAST]; | ||
| 86 | if ($diff > 0) { | ||
| 87 | $obv += $prices->at($n)->[$VOLUME]; | ||
| 88 | } | ||
| 89 | if ($diff < 0) { | ||
| 90 | $obv -= $prices->at($n)->[$VOLUME]; | ||
| 91 | } | ||
| 92 | $calc->indicators->set($name, $n, $obv); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub calculate_interval { | ||
| 98 | my ($self, $calc, $first, $last) = @_; | ||
| 99 | |||
| 100 | # Calculate OBV for the last record | ||
| 101 | # so that all intermediate results will be stored | ||
| 102 | $self->calculate($calc, $last); | ||
| 103 | } | ||
| 104 | |||
| 105 | 1; |