| 1 | # Copyright 2008, 2009, 2010 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Series::Derived::DPO; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Locale::TextDomain 1.17; # for __p() | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | |||
| 25 | use base 'App::Chart::Series::Indicator'; | ||
| 26 | use App::Chart::Series::Calculation; | ||
| 27 | use App::Chart::Series::Derived::SMA; | ||
| 28 | |||
| 29 | # http://www.incrediblecharts.com/technical/detrended_price_oscillator.php | ||
| 30 | # Formula, usage guidelines. | ||
| 31 | # | ||
| 32 | # http://www.marketscreen.com/help/AtoZ/default.asp?hideHF=&Num=41 | ||
| 33 | # Formula and description. | ||
| 34 | # | ||
| 35 | |||
| 36 | sub longname { __('DPO - Detrended Price Oscillator') } | ||
| 37 | sub shortname { __('DPO') } | ||
| 38 | sub manual { __p('manual-node','Detrended Price Oscillator') } | ||
| 39 | |||
| 40 | use constant | ||
| 41 | { hlines => [ 0 ], | ||
| 42 | type => 'indicator', | ||
| 43 | units => 'price', | ||
| 44 | parameter_info => [ { name => __('Days'), | ||
| 45 | key => 'dpo_days', | ||
| 46 | type => 'integer', | ||
| 47 | minimum => 1, | ||
| 48 | default => 20 } ], | ||
| 49 | }; | ||
| 50 | |||
| 51 | sub new { | ||
| 52 | my ($class, $parent, $N) = @_; | ||
| 53 | ### DPO new(): "@_" | ||
| 54 | |||
| 55 | $N //= parameter_info()->[0]->{'default'}; | ||
| 56 | ($N > 0) || croak "DPO bad N: $N"; | ||
| 57 | |||
| 58 | return $class->SUPER::new | ||
| 59 | (parent => $parent, | ||
| 60 | N => $N, | ||
| 61 | parameters => [ $N ], | ||
| 62 | arrays => { values => [] }, | ||
| 63 | array_aliases => { }); | ||
| 64 | } | ||
| 65 | sub proc { | ||
| 66 | my ($class_or_self, $N) = @_; | ||
| 67 | my $delta = N_to_delta ($N); | ||
| 68 | |||
| 69 | my $sma_proc = App::Chart::Series::Derived::SMA->proc($N); | ||
| 70 | my $delay_proc = App::Chart::Series::Calculation->delay($delta); | ||
| 71 | |||
| 72 | return sub { | ||
| 73 | my ($value) = @_; | ||
| 74 | my $prevsma = $delay_proc->($sma_proc->($value)); | ||
| 75 | return (defined $prevsma ? $value - $prevsma : undef); | ||
| 76 | }; | ||
| 77 | } | ||
| 78 | sub warmup_count { | ||
| 79 | my ($self_or_class, $N) = @_; | ||
| 80 | my $delta = N_to_delta ($N); | ||
| 81 | return $delta + App::Chart::Series::Derived::SMA->warmup_count($N); | ||
| 82 | } | ||
| 83 | sub N_to_delta { | ||
| 84 | my ($N) = @_; | ||
| 85 | return 1 + int ($N/2); | ||
| 86 | } | ||
| 87 | |||
| 88 | 1; | ||
| 89 | __END__ |