]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/pointfig.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / pointfig.m
1 ## Copyright (C) 2008 Bill Denney <bill@denney.ws>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} pointfig (@var{asset})
18 ##
19 ## Plot the point figure chart of an @var{asset}.  Upward price
20 ## movements are plotted as Xs and downward movements are plotted as Os.
21 ##
22 ## @seealso{bolling, candle, dateaxis, highlow, movavg}
23 ## @end deftypefn
24
25 function pointfig (asset)
26
27   if nargin != 1
28     print_usage ();
29   endif
30
31   upmask    = asset(2:end) > asset(1:end-1);
32   # if the data is equal, it will not change the trend
33   equalmask = asset(2:end) == asset(1:end-1);
34   downmask  = asset(2:end) < asset(1:end-1);
35
36   lx        = 0;
37   ly        = 0;
38   direction = 0;
39   up        = zeros(0,2);
40   down      = zeros(0,2);
41
42   for i = 1:length (upmask)
43     if direction > 0 && (upmask(i) || equalmask(i))
44       ## moving in the same direction as previously: up
45       ly         += 1;
46       up(end+1,:) = [lx ly];
47     elseif direction < 0 && (downmask(i) || equalmask(i))
48       ## moving in the same direction as previously: down
49       ly           -= 1;
50       down(end+1,:) = [lx ly];
51     else
52       ## moving in a different direction than previously
53       lx += 1;
54       if upmask(i)
55         up(end+1,:) = [lx ly];
56         direction   = 1;
57       else
58         down(end+1,:) = [lx ly];
59         direction     = -1;
60       endif
61     endif
62   endfor
63
64   hstat = ishold();
65   hold("on");
66   plot(up(:,1), up(:,2), "x", "color", [0 0 1]);
67   plot(down(:,1), down(:,2), "o", "color", [1 0 0]);
68   if ! hstat
69     hold("off");
70   endif
71
72 endfunction
73
74 ## Tests
75 %!shared a
76 %! a = [1 2 3 2 4 2 1];
77 %!test
78 %! [s l] = movavg(a, 2, 4);
79 %! assert(s, [1 1.5 2.5 2.5 3 3 1.5])
80 %! assert(l, [1 1.5 2 2 2.75 2.75 2.25])
81 %!test
82 %! [s l] = movavg(a', 2, 4);
83 %! assert(s, [1;1.5;2.5;2.5;3;3;1.5])
84 %! assert(l, [1;1.5;2;2;2.75;2.75;2.25])
85 %!test
86 %! [s l] = movavg(a, 3, 4, 1);
87 %! assert(s, [3 4.8 7 7 9.5 8 5.5]./3, 10*eps)
88 %! assert(l, [1 11/7 20/9 2.2 3 2.7 2], 10*eps)