| 1 | package Finance::GeniusTrader::Indicators::MFI; | ||
| 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::Indicators::TP; | ||
| 12 | use Finance::GeniusTrader::Prices; | ||
| 13 | |||
| 14 | @ISA = qw(Finance::GeniusTrader::Indicators); | ||
| 15 | @NAMES = ("MFI[#1]"); | ||
| 16 | |||
| 17 | =head1 Finance::GeniusTrader::Indicators::MFI | ||
| 18 | |||
| 19 | =head2 Overview | ||
| 20 | |||
| 21 | The Money Flow Index (MFI) is a momentum indicator that measures the strength of money flowing in and out of a security. It is related to the Relative Strength Index (RSI), but where the RSI only incorporates prices, the Money Flow Index accounts for volume. | ||
| 22 | |||
| 23 | =head2 Interpretation | ||
| 24 | |||
| 25 | Look for divergence between the indicator and the price action. If the price trends higher and the MFI trends lower (or vice versa), a reversal may be imminent. | ||
| 26 | |||
| 27 | Look for market tops to occur when the MFI is above 80. Look for market bottoms to occur when the MFI is below 20. | ||
| 28 | |||
| 29 | =head2 Calculation | ||
| 30 | |||
| 31 | The Money Flow Index requires a series of calculations. | ||
| 32 | |||
| 33 | Money Flow = Typical Price * Volume | ||
| 34 | |||
| 35 | If today's Typical Price is greater than yesterday's Typical Price, it is considered as a Positive Money Flow and if today's price is less, it is considered as a Negative Money Flow. | ||
| 36 | |||
| 37 | Positive Money Flow is the sum of the Positive Money over the specified number of periods. Negative Money Flow is the sum of the Negative Money over the specified number of periods. | ||
| 38 | The Money Ratio is then calculated by dividing the Positive Money Flow by the Negative Money Flow. | ||
| 39 | |||
| 40 | Money Ratio = Positive Money Flow / Negative Money Flow | ||
| 41 | |||
| 42 | Money Flow Index = 100 - ( 100 / ( 1 + Money Ratio )) | ||
| 43 | |||
| 44 | =head2 Parameters | ||
| 45 | |||
| 46 | The standard MFI works with a fourteen-day parameter : n = 14 | ||
| 47 | |||
| 48 | =head2 Example | ||
| 49 | |||
| 50 | Finance::GeniusTrader::Indicators::MFI->new() | ||
| 51 | Finance::GeniusTrader::Indicators::MFI->new([8]) | ||
| 52 | |||
| 53 | =head2 Links | ||
| 54 | |||
| 55 | http://www.equis.com/free/taaz/moneyflow.html | ||
| 56 | http://www.linnsoft.com/tour/techind/mfi.htm | ||
| 57 | |||
| 58 | =cut | ||
| 59 | sub new { | ||
| 60 | my $type = shift; | ||
| 61 | my $class = ref($type) || $type; | ||
| 62 | my ($args) = @_; | ||
| 63 | my $self = { 'args' => defined($args) ? $args : [14] }; | ||
| 64 | |||
| 65 | $args->[0] = 14 if (! defined($args->[0])); | ||
| 66 | |||
| 67 | return manage_object(\@NAMES, $self, $class, $self->{'args'}, ""); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub initialize { | ||
| 71 | my $self = shift; | ||
| 72 | |||
| 73 | $self->{'tp'} = Finance::GeniusTrader::Indicators::TP->new(); | ||
| 74 | |||
| 75 | $self->add_indicator_dependency($self->{'tp'}, $self->{'args'}[0] + 1); | ||
| 76 | } | ||
| 77 | |||
| 78 | =pod | ||
| 79 | |||
| 80 | =head2 Finance::GeniusTrader::Indicators::TP::calculate($calc, $day) | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub calculate { | ||
| 85 | my ($self, $calc, $i) = @_; | ||
| 86 | my $indic = $calc->indicators; | ||
| 87 | my $prices = $calc->prices; | ||
| 88 | my $period = $self->{'args'}[0]; | ||
| 89 | my $tp = $self->{'tp'}; | ||
| 90 | my $tp_name = $tp->get_name; | ||
| 91 | my $today_typical_price = 0; | ||
| 92 | my $yesterday_typical_price = 0; | ||
| 93 | my $money_flow_index_name = $self->get_name(0); | ||
| 94 | my $money_flow = 0; | ||
| 95 | my $sum_of_positive_money_flow = 0; | ||
| 96 | my $sum_of_negative_money_flow = 0; | ||
| 97 | my $money_flow_index_value = 0; | ||
| 98 | my $money_ratio = 0; | ||
| 99 | |||
| 100 | return if ($indic->is_available($money_flow_index_name, $i)); | ||
| 101 | return if (! $self->check_dependencies($calc, $i)); | ||
| 102 | |||
| 103 | for (my $n = $i - $period + 1; $n <= $i; $n++) { | ||
| 104 | # Get the Typical Prices | ||
| 105 | $today_typical_price = $indic->get($tp_name, $n); | ||
| 106 | $yesterday_typical_price = $indic->get($tp_name, $n - 1); | ||
| 107 | |||
| 108 | # Calculate the Money Flow | ||
| 109 | $money_flow = $today_typical_price * $prices->at($n)->[$VOLUME]; | ||
| 110 | |||
| 111 | if ($today_typical_price > $yesterday_typical_price) { | ||
| 112 | $sum_of_positive_money_flow += $money_flow; | ||
| 113 | } | ||
| 114 | if ($today_typical_price < $yesterday_typical_price) { | ||
| 115 | $sum_of_negative_money_flow += $money_flow; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | if ($sum_of_negative_money_flow != 0) { | ||
| 120 | $money_ratio = $sum_of_positive_money_flow / $sum_of_negative_money_flow; | ||
| 121 | $money_flow_index_value = 100 - ( 100 / ( 1 + $money_ratio)); | ||
| 122 | } | ||
| 123 | |||
| 124 | # Return the results | ||
| 125 | $indic->set($money_flow_index_name, $i, $money_flow_index_value); | ||
| 126 | } | ||
| 127 | |||
| 128 | 1; |