| 1 | # Copyright 2007, 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::Trendscore; | ||
| 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 | |||
| 27 | # http://trader.online.pl/MSZ/e-w-Chandes_Trendscore.html | ||
| 28 | # Formula, sample on polish WIG20 from 2000. | ||
| 29 | # | ||
| 30 | # http://www.biz-analyst.com/library/stocks_commodities_trend.pdf [gone] | ||
| 31 | # Tushar Chande, "Rating Trend Strength", TASC V.11:9 September 1993, | ||
| 32 | # reproduced in a collection of trend related TASC articles. | ||
| 33 | # (Apparently http://store.traders.com/trarpap.html) | ||
| 34 | # | ||
| 35 | |||
| 36 | |||
| 37 | sub longname { __('Trendscore') } | ||
| 38 | *shortname = \&longname; | ||
| 39 | sub manual { __p('manual-node','Trendscore') } | ||
| 40 | |||
| 41 | use constant | ||
| 42 | { type => 'indicator', | ||
| 43 | hlines => [ 0 ], | ||
| 44 | minimum => -10, | ||
| 45 | maximum => 10, | ||
| 46 | }; | ||
| 47 | |||
| 48 | sub new { | ||
| 49 | my ($class, $parent) = @_; | ||
| 50 | ### Trendscore new(): "@_" | ||
| 51 | |||
| 52 | return $class->SUPER::new | ||
| 53 | (parent => $parent, | ||
| 54 | arrays => { values => [] }, | ||
| 55 | array_aliases => { }); | ||
| 56 | } | ||
| 57 | |||
| 58 | use constant _KEEP => 21; | ||
| 59 | |||
| 60 | sub fill_part { | ||
| 61 | my ($self, $lo, $hi) = @_; | ||
| 62 | my $parent = $self->{'parent'}; | ||
| 63 | |||
| 64 | my $start = $parent->find_before ($lo, _KEEP - 1); | ||
| 65 | $parent->fill ($start, $hi); | ||
| 66 | my $p = $parent->values_array; # parent | ||
| 67 | |||
| 68 | my $s = $self->values_array; # self | ||
| 69 | $hi = min ($hi, $#$p); | ||
| 70 | if ($#$s < $hi) { $#$s = $hi; } # pre-extend | ||
| 71 | |||
| 72 | # @a is current points accumulated, or undef in empty positions. | ||
| 73 | # $a[0] is the newest point. | ||
| 74 | # | ||
| 75 | my @a; | ||
| 76 | foreach my $i ($start .. $lo-1) { | ||
| 77 | my $value = $p->[$i] // next; | ||
| 78 | unshift @a, $value; | ||
| 79 | } | ||
| 80 | |||
| 81 | foreach my $i ($lo .. $hi) { | ||
| 82 | my $value = $p->[$i] // next; | ||
| 83 | |||
| 84 | unshift @a, $value; | ||
| 85 | if (@a > _KEEP) { pop @a; } | ||
| 86 | |||
| 87 | # $a[0] is today, $a[1] is 1 day ago; look at 11 through 20 days ago | ||
| 88 | if (11 < @a) { | ||
| 89 | my $total = 0; | ||
| 90 | for (my $i = 11; $i < @a; $i++) { | ||
| 91 | $total += ($value >= $a[$i] ? 1 : -1); | ||
| 92 | } | ||
| 93 | $s->[$i] = $total; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | 1; | ||
| 99 | __END__ |