octave-patch-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-patch-tracker] [patch #8670] macd function for financial package


From: anonymous
Subject: [Octave-patch-tracker] [patch #8670] macd function for financial package
Date: Fri, 08 May 2015 20:24:41 +0000
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36

URL:
  <http://savannah.gnu.org/patch/?8670>

                 Summary: macd function for financial package
                 Project: GNU Octave
            Submitted by: None
            Submitted on: Fri 08 May 2015 08:24:39 PM UTC
                Category: None
                Priority: 5 - Normal
                  Status: None
                 Privacy: Public
             Assigned to: None
        Originator Email: address@hidden
             Open/Closed: Open
         Discussion Lock: Any

    _______________________________________________________

Details:

## Copyright (C) 2015 Adam Thompson <adammilesthompson [at] gmail [dot] com>
##
## This program is free software; you can redistribute it and/or modify it
under
## the terms of the GNU General Public License as published by the Free
Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but
WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more
## details.
##
## You should have received a copy of the GNU General Public License along
with
## this program; if not, see <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} address@hidden, nineperma}] =} macd
(@var{data})
## @deftypefnx {Function File} address@hidden, nineperma}] =} macd
(@var{data}, @var{dim})
##
## Calculate the Moving Average Convergence/Divergence (MACD)
## line of an asset from the vector of prices (@var{data}). 
## Also calculate the nine-period exponential moving average 
## from the MACD line.  If given, @var{dim} indicates whether
## each row is a set of observations (dim = 2) or each 
## column is a set of observations (dim = 1, the default).
##
## The MACD line is calculated as the twelve-period
## exponential moving average (EMA) minus the 26-period EMA.
## Closing prices are typically used for the moving 
## averages.  The nine-period EMA of the MACD line is used as
## the signal line.
## @end deftypefn

function [macdvec, nineperma] = macd (data, dim = 1)

        ## Constants:
        S_PER = 12;
        L_PER = 26;
        SL_PER = 9;
        ## End constants

  ## Check input and set the defaults
  if nargin < 1 || nargin > 2
    print_usage ();
  elseif nargin < 2
    dim = 1;
  endif
  
  if dim == 2
        data = data';
  end

        ## MACD line:
        ## 12-day EMA:
        alpha = 2 / (S_PER + 1);
        s_ema = filter (alpha, [1 (alpha - 1)], data, data(1) * (1 - alpha));
        # Formula for calculating EMA provided by James Sherman, Jr.
        #
http://octave.1599824.n4.nabble.com/vectorized-moving-average-td2132090.html
        ## 26-day EMA:
        alpha = 2 / (L_PER + 1);
        l_ema = filter (alpha, [1 (alpha - 1)], data, data(1) * (1 - alpha));
        ## MACD:
        macdvec = s_ema - l_ema;
        
        ## Signal line:
        alpha = 2 / (SL_PER + 1);
        nineperma = filter (alpha, [1 (alpha - 1)], macdvec, macdvec(1) * (1 -
alpha));

endfunction

## Tests
%!shared d, m, n, a, b
%! d = [46.84 47.07 45.92 47.24 47.64 47.3 48.22 46.98 46.41 44.78 46.29 47.99
47.47 49.19 48.85 48.13 49.13 50.91 51.01 50.48 50.88 51.2 50.85 50.16 48.44
49.1 47.67 45.43 46.47 48.76 50.08 50.74 51.91 51.11 49.36 48.96 49.28 49.02
48.24 49.71];
%! m = [0 0.0183 -0.0592 -0.014 0.0535 0.0786 0.1708 0.1421 0.0726 -0.1127
-0.1362 -0.0174 0.0344 0.2118 0.3212 0.3459 0.441 0.6525 0.8188 0.8974 0.9807
1.0603 1.0827 1.0329 0.8448 0.7405 0.5363 0.1915 0.0021 0.0364 0.1681 0.3221
0.5324 0.6272 0.5548 0.4598 0.4057 0.338 0.2188 0.2402];
%! n = [0 0.0037 -0.0089 -0.0099 0.0027 0.0179 0.0485 0.0672 0.0683 0.0321
-0.0016 -0.0047 0.0031 0.0448 0.1001 0.1493 0.2076 0.2966 0.401 0.5003 0.5964
0.6892 0.7679 0.8209 0.8257 0.8086 0.7542 0.6416 0.5137 0.4183 0.3682 0.359
0.3937 0.4404 0.4633 0.4626 0.4512 0.4286 0.3866 0.3573];
%! [a, b] = macd (d);
%!assert([a, b], [m, n], 0.0001)
%! [a, b] = macd (d, 1);
%!assert([a, b], [m, n], 0.0001)
%! [a, b] = macd (d', 2);
%!assert([a, b], [m, n], 0.0001)





    _______________________________________________________

Reply to this item at:

  <http://savannah.gnu.org/patch/?8670>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]