| 1 | # Copyright 2003, 2004, 2005, 2006, 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::QStick; | ||
| 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::Derived::SMA; | ||
| 27 | use App::Chart::Series::Derived::IMI; | ||
| 28 | |||
| 29 | # http://trader.online.pl/MSZ/e-w-Chandes_QStick.html | ||
| 30 | # | ||
| 31 | |||
| 32 | sub longname { __('QStick') } | ||
| 33 | *shortname = \&longname; | ||
| 34 | sub manual { __p('manual-node','QStick') } | ||
| 35 | |||
| 36 | use constant | ||
| 37 | { type => 'indicator', | ||
| 38 | units => 'percentage', | ||
| 39 | minimum => 0, | ||
| 40 | maximum => 100, | ||
| 41 | hlines => [ 0 ], | ||
| 42 | parameter_info => [ { name => __('Days'), | ||
| 43 | key => 'qstick_days', | ||
| 44 | type => 'integer', | ||
| 45 | minimum => 1, | ||
| 46 | default => 8 } ], | ||
| 47 | }; | ||
| 48 | |||
| 49 | sub new { | ||
| 50 | my ($class, $parent, $N) = @_; | ||
| 51 | |||
| 52 | $N //= parameter_info()->[0]->{'default'}; | ||
| 53 | ($N > 0) || croak "QStick bad N: $N"; | ||
| 54 | |||
| 55 | return $class->SUPER::new | ||
| 56 | (parent => $parent, | ||
| 57 | parameters => [ $N ], | ||
| 58 | arrays => { values => [] }, | ||
| 59 | array_aliases => { }); | ||
| 60 | } | ||
| 61 | |||
| 62 | # Return a procedure which calculates an intraday momentum index over an | ||
| 63 | # accumulated window of $N values. | ||
| 64 | # | ||
| 65 | # Each call $proc->($open, $close) enters a new point into the window, and | ||
| 66 | # the return is the intraday momentum index up to (and including) that | ||
| 67 | # point. If there's no value the return is undef, which can happen if every | ||
| 68 | # day has $open==$close. | ||
| 69 | # | ||
| 70 | # To prime the window initially, call $proc with $N-1 many points preceding | ||
| 71 | # the first desired. | ||
| 72 | # | ||
| 73 | sub proc { | ||
| 74 | my ($class_or_self, $N) = @_; | ||
| 75 | my $sma_proc = App::Chart::Series::Derived::SMA->proc ($N); | ||
| 76 | |||
| 77 | return sub { | ||
| 78 | my ($open, $close) = @_; | ||
| 79 | if (! defined $open) { return undef; } | ||
| 80 | return $sma_proc->($close - $open); | ||
| 81 | }; | ||
| 82 | } | ||
| 83 | *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1 | ||
| 84 | *fill_part = \&App::Chart::Series::Derived::IMI::fill_part; | ||
| 85 | |||
| 86 | 1; | ||
| 87 | __END__ |