| 1 | # Copyright 2006, 2007, 2009, 2011 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::VIDYA; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use List::Util qw(min max); | ||
| 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::KAMA; | ||
| 28 | use App::Chart::Series::Derived::VIDYAalpha; | ||
| 29 | |||
| 30 | |||
| 31 | # http://trader.online.pl/ELZ/t-i-VIDYA.html | ||
| 32 | # Formulas and S&P500 sample chart. | ||
| 33 | # | ||
| 34 | # http://www.fmlabs.com/reference/VIDYA.htm | ||
| 35 | # | ||
| 36 | # http://www.working-money.com/documentation/FEEDbk_docs/Archive/0298/TradersTips/Tips9802.html | ||
| 37 | # TASC trader's tips February 1998, formulas. | ||
| 38 | # | ||
| 39 | |||
| 40 | |||
| 41 | sub longname { __('VIDYA - Variable Index Dynamic') } | ||
| 42 | sub shortname { __('VIDYA') } | ||
| 43 | sub manual { __p('manual-node','Variable Index Dynamic Average') } | ||
| 44 | |||
| 45 | use constant | ||
| 46 | { type => 'average', | ||
| 47 | parameter_info => App::Chart::Series::Derived::VIDYAalpha::parameter_info(), | ||
| 48 | }; | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my ($class, $parent, $N_fast, $N_slow) = @_; | ||
| 52 | |||
| 53 | $N_fast //= parameter_info()->[0]->{'default'}; | ||
| 54 | ($N_fast > 0) || croak "VIDYA bad N_fast: $N_fast"; | ||
| 55 | |||
| 56 | $N_slow //= parameter_info()->[1]->{'default'}; | ||
| 57 | ($N_slow > 0) || croak "VIDYA bad N_slow: $N_slow"; | ||
| 58 | |||
| 59 | return $class->SUPER::new | ||
| 60 | (parent => $parent, | ||
| 61 | parameters => [ $N_fast, $N_slow ], | ||
| 62 | arrays => { values => [] }, | ||
| 63 | array_aliases => { }); | ||
| 64 | } | ||
| 65 | |||
| 66 | # warmup_count() gives a fixed amount, based on a guessed EMA alpha 0.1 | ||
| 67 | # (giving GUESS_EMA_WARMUP==65), which is probably slower than occurs in | ||
| 68 | # practice. | ||
| 69 | # | ||
| 70 | # warmup_count_for_position() calculates a value on actual data, working | ||
| 71 | # backwards. In practice it's as little as about 100. | ||
| 72 | # | ||
| 73 | use constant GUESS_EMA_WARMUP => | ||
| 74 | App::Chart::Series::Derived::EMA->warmup_count | ||
| 75 | (App::Chart::Series::Derived::EMA::alpha_to_N | ||
| 76 | (0.1)); | ||
| 77 | ### GUESS_EMA_WARMUP: GUESS_EMA_WARMUP() | ||
| 78 | |||
| 79 | sub warmup_count { | ||
| 80 | my ($self_or_class, $N_fast, $N_slow) = @_; | ||
| 81 | return GUESS_EMA_WARMUP | ||
| 82 | + App::Chart::Series::Derived::VIDYAalpha->warmup_count($N_fast, $N_slow); | ||
| 83 | } | ||
| 84 | sub warmup_count_for_position { | ||
| 85 | return App::Chart::Series::Derived::KAMA::warmup_count_for_position_alphaclass | ||
| 86 | (@_, 'App::Chart::Series::Derived::VIDYAalpha'); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub proc { | ||
| 90 | my ($class, $N_fast, $N_slow) = @_; | ||
| 91 | my $alpha_proc = App::Chart::Series::Derived::VIDYAalpha->proc | ||
| 92 | ($N_fast, $N_slow); | ||
| 93 | my $ama_proc = App::Chart::Series::Derived::KAMA->adaptive_ema_proc(); | ||
| 94 | |||
| 95 | return sub { | ||
| 96 | my ($value) = @_; | ||
| 97 | my $alpha = $alpha_proc->($value) // return; | ||
| 98 | return $ama_proc->($alpha, $value); | ||
| 99 | }; | ||
| 100 | } | ||
| 101 | |||
| 102 | 1; | ||
| 103 | __END__ |