« App/Chart/Series/Derived/AdaptiveLaguerreAlpha.pm
↑ Summary
App/Chart/Series/Derived/FRAMAalpha.pm »
| 1 | # Copyright 2007, 2009, 2010, 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::EPMA; | ||
| 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::SMA; | ||
| 28 | |||
| 29 | |||
| 30 | # As EPMA: | ||
| 31 | # | ||
| 32 | # http://www.linnsoft.com/tour/techind/movAvg.htm | ||
| 33 | # Formulas LSMA as endpoint, and EPMA as weightings "3*i-n-1". | ||
| 34 | # Sample Nasdaq 100 (symbol QQQ, yahoo now ^IXIC) from 2001 of both | ||
| 35 | # (they come out the same). | ||
| 36 | # | ||
| 37 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/062001/Letters/Letters.html | ||
| 38 | # TASC letters June 2001, Don Kraska on the equivalence of his "3j-m-1" | ||
| 39 | # (j=1 to j=m where price[m] latest), with what John Bellantoni | ||
| 40 | # described in April 2001 letters. | ||
| 41 | # | ||
| 42 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/0198/Abstracts_new/TILLSON/Tillson9801.html | ||
| 43 | # Excerpt from TASC January 1998. Sample chart of EPMA (and IE/2) on | ||
| 44 | # Hewlett Packard (symbol HPQ) from 1996/7. | ||
| 45 | # | ||
| 46 | # | ||
| 47 | # As LSQMA: | ||
| 48 | # | ||
| 49 | # http://www.fmlabs.com/reference/LstSqrMA.htm | ||
| 50 | # Formula, described as LSQMA. | ||
| 51 | # | ||
| 52 | # | ||
| 53 | # Time Series Forecast: | ||
| 54 | # | ||
| 55 | # http://www.marketscreen.com/help/AtoZ/default.asp?hideHF=&Num=102 | ||
| 56 | # Sample chart of microsoft (symbol MSFT) from 1992, split adjusted. | ||
| 57 | # | ||
| 58 | # http://www.prophet.net/analyze/popglossary.jsp?studyid=TSF | ||
| 59 | # http://www.traderslog.com/time-series-forecast.htm (same chart) | ||
| 60 | # Sample chart of fisher scientific .. but what symbol, what year? | ||
| 61 | # | ||
| 62 | # | ||
| 63 | # As "Modified Moving Average": | ||
| 64 | # | ||
| 65 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/012000/Abstracts_new/Sharp/Sharp.html | ||
| 66 | # Start of Joe Sharp's TASC article January 2000 (rest for sale). | ||
| 67 | # | ||
| 68 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/012000/TradersTips/TradersTips.html | ||
| 69 | # TASC traders' tips January 2000, various formulas. | ||
| 70 | # Technifilter coefficients look doubtful. | ||
| 71 | # | ||
| 72 | # http://www.traders.com/Documentation/FEEDbk_docs/Archive/062000/Letters/Letters.html | ||
| 73 | # TASC letters June 2000, Tim O'Sullivan pointing out MMA is the same | ||
| 74 | # as EPMA. | ||
| 75 | # | ||
| 76 | # http://www.linnsoft.com/tour/techind/mma.htm | ||
| 77 | # Formula, but sample chart is only an intraday. | ||
| 78 | # | ||
| 79 | # http://trader.online.pl/MSZ/e-w-Modified_Moving_Average.html | ||
| 80 | # Formulas for N=2, N=3, N=4, and N=10. | ||
| 81 | # http://trader.online.pl/MSZ/e-w-Modified_Moving_Average_II.html | ||
| 82 | # Formulas for N=20. | ||
| 83 | # http://trader.online.pl/ELZ/t-i-Modified_Moving_Averages.html | ||
| 84 | # Formula for general N, per traders tips, from www.omegaresearch.com. | ||
| 85 | # Sample chart S&P 500 (yahoo symbol ^GSPC) from 2001, looks like the | ||
| 86 | # default N=2 shown in the formula. | ||
| 87 | # | ||
| 88 | # http://www.tradecision.com/support/tasc_tips/modified_moving_average.htm | ||
| 89 | # http://www.tradecision.com/downloads/tasc_tips/ModifiedMovingAverage.tnd | ||
| 90 | # Formula. | ||
| 91 | # | ||
| 92 | |||
| 93 | #----------------------------------------------------------------------------- | ||
| 94 | |||
| 95 | # The equivalence of the linreg a+b*endpos and the "3i" stepping weighted | ||
| 96 | # moving average is easily shown. With a=mean, b=slope, p0 today, p1 | ||
| 97 | # yesterday, etc, then a+b*endpos is | ||
| 98 | # | ||
| 99 | # p0 + p1 + ... + pN (N-1) (N-1)/2 * p0 + ... + (-(N-1)/2) * pN | ||
| 100 | # ------------------ + ----- * ---------------------------------- | ||
| 101 | # N 2 ((N-1)/2)^2 + ... + (-(N-1)/2)^2 | ||
| 102 | # | ||
| 103 | # The sum of the squares in the denominator is (N^3-N)/12 which is | ||
| 104 | # N*(N+1)*(N-1)/12 so the N-1 factor cancels, leaving a factor on each p[i] | ||
| 105 | # | ||
| 106 | # 1 1 12 * ((N-1)/2 - i) | ||
| 107 | # --- + --- * ------------------- | ||
| 108 | # N 2 N*(N+1) | ||
| 109 | # | ||
| 110 | # which is | ||
| 111 | # | ||
| 112 | # N+1 + 3N - 3 - 6i | ||
| 113 | # ----------------- | ||
| 114 | # N*(N+1) | ||
| 115 | # | ||
| 116 | # and then | ||
| 117 | # | ||
| 118 | # 2N - 1 - 3i | ||
| 119 | # ---------- | ||
| 120 | # N*(N+1)/2 | ||
| 121 | # | ||
| 122 | # Weights like that can be used directly to calculate the EPMA, but for now | ||
| 123 | # let the linreg-calc-proc code do the work. | ||
| 124 | |||
| 125 | |||
| 126 | |||
| 127 | sub longname { __('EPMA - Endpoint MA') } | ||
| 128 | sub shortname { __('EPMA') } | ||
| 129 | sub manual { __p('manual-node','Endpoint Moving Average') } | ||
| 130 | |||
| 131 | use constant | ||
| 132 | { type => 'average', | ||
| 133 | units => 'price-slope', | ||
| 134 | parameter_info => [ { name => __('Days'), | ||
| 135 | key => 'epma_days', | ||
| 136 | type => 'integer', | ||
| 137 | minimum => 1, | ||
| 138 | default => 20 } ], | ||
| 139 | }; | ||
| 140 | |||
| 141 | sub new { | ||
| 142 | my ($class, $parent, $N) = @_; | ||
| 143 | |||
| 144 | $N //= parameter_info()->[0]->{'default'}; | ||
| 145 | ($N > 0) || croak "EPMA bad N: $N"; | ||
| 146 | |||
| 147 | return $class->SUPER::new | ||
| 148 | (parent => $parent, | ||
| 149 | N => $N, | ||
| 150 | parameters => [ $N ], | ||
| 151 | arrays => { values => [] }, | ||
| 152 | array_aliases => { }); | ||
| 153 | } | ||
| 154 | *warmup_count = \&App::Chart::Series::Derived::SMA::warmup_count; # $N-1 | ||
| 155 | |||
| 156 | sub proc { | ||
| 157 | my ($class, $N) = @_; | ||
| 158 | my $linreg_proc = App::Chart::Series::Calculation->linreg($N); | ||
| 159 | return sub { | ||
| 160 | my ($y) = @_; | ||
| 161 | return ($linreg_proc->($y))[0]; | ||
| 162 | }; | ||
| 163 | } | ||
| 164 | |||
| 165 | 1; | ||
| 166 | __END__ |