From 7435df76844f5f6df24905e43fd860c48899b3df Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Tue, 23 Sep 2008 10:33:36 -0700 Subject: [PATCH] Adding first version of math.finance. --- extra/math/finance/authors.txt | 1 + extra/math/finance/finance-docs.factor | 38 +++++++++++++++++++++++++ extra/math/finance/finance-tests.factor | 8 ++++++ extra/math/finance/finance.factor | 36 +++++++++++++++++++++++ extra/math/finance/summary.txt | 1 + 5 files changed, 84 insertions(+) create mode 100644 extra/math/finance/authors.txt create mode 100644 extra/math/finance/finance-docs.factor create mode 100644 extra/math/finance/finance-tests.factor create mode 100644 extra/math/finance/finance.factor create mode 100644 extra/math/finance/summary.txt diff --git a/extra/math/finance/authors.txt b/extra/math/finance/authors.txt new file mode 100644 index 0000000000..e091bb8164 --- /dev/null +++ b/extra/math/finance/authors.txt @@ -0,0 +1 @@ +John Benediktsson diff --git a/extra/math/finance/finance-docs.factor b/extra/math/finance/finance-docs.factor new file mode 100644 index 0000000000..9094a5bff6 --- /dev/null +++ b/extra/math/finance/finance-docs.factor @@ -0,0 +1,38 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: help.markup help.syntax ; + +IN: math.finance + +HELP: enumerate +{ $values { "seq" "a sequence" } { "newseq" "a sequence" } } +{ $description "Returns a new sequence where each element is an array of { value, index }" } ; + +HELP: sma +{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } +{ $description "Returns the Simple Moving Average with the specified periodicity." } ; + +HELP: ema +{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } +{ $description + "Returns the Exponential Moving Average with the specified periodicity, calculated by:\n" + { $list + "A = 2.0 / (N + 1)" + "EMA[t] = (A * SEQ[t]) + ((1-A) * EMA[t-1])" } +} ; + +HELP: macd +{ $values { "seq" "a sequence" } { "n1" "short number of periods" } { "n2" "long number of periods" } { "newseq" "a sequence" } } +{ $description + "Returns the Moving Average Converge of the sequence, calculated by:\n" + { $list "MACD[t] = EMA2[t] - EMA1[t]" } +} ; + +HELP: momentum +{ $values { "seq" "a sequence" } { "n" "number of periods" } { "newseq" "a sequence" } } +{ $description + "Returns the Momentum of the sequence, calculated by:\n" + { $list "MOM[t] = SEQ[t] - SEQ[t-n]" } +} ; + diff --git a/extra/math/finance/finance-tests.factor b/extra/math/finance/finance-tests.factor new file mode 100644 index 0000000000..dce701bb2f --- /dev/null +++ b/extra/math/finance/finance-tests.factor @@ -0,0 +1,8 @@ +USING: kernel math math.functions math.finance tools.test ; + +IN: math.finance.tests + +[ { 2 4 } ] [ { 1 3 5 } 2 sma ] unit-test + +[ { 1 3 1 } ] [ { 1 3 2 6 3 } 2 momentum ] unit-test + diff --git a/extra/math/finance/finance.factor b/extra/math/finance/finance.factor new file mode 100644 index 0000000000..75ea693862 --- /dev/null +++ b/extra/math/finance/finance.factor @@ -0,0 +1,36 @@ +! Copyright (C) 2008 John Benediktsson +! See http://factorcode.org/license.txt for BSD license + +USING: arrays kernel grouping math math.statistics sequences ; + +IN: math.finance + +: enumerate ( seq -- newseq ) + #! Returns a sequence where each element and its index + -1 swap [ [ 1+ ] dip swap [ 2array ] keep swap ] { } map-as swap drop ; + +: ema ( seq n -- newseq ) + #! An exponentially-weighted moving average: + #! A = 2.0 / (N + 1) + #! EMA[t] = (A * VAL[t]) + ((1-A) * EMA[t-1]) + 1+ 2.0 swap / dup 1 swap - swap rot + [ [ dup ] dip * ] map swap drop 0 swap + [ [ dup ] 2dip [ * ] dip + dup ] map + [ drop drop ] dip 1 tail-slice >array ; + +: sma ( seq n -- newseq ) + #! Simple moving average + clump [ mean ] map ; + +: macd ( seq n1 n2 -- newseq ) + #! Moving Average Convergence Divergence + #! MACD[t] = EMA2[t] - EMA1[t] + rot dup ema [ swap ema ] dip [ - ] 2map ; + +: momentum ( seq n -- newseq ) + #! Momentum + #! M[t] = P[t] - P[t-n] + 2dup tail-slice -rot swap [ length ] keep + [ - neg ] dip swap head-slice [ - ] 2map ; + + diff --git a/extra/math/finance/summary.txt b/extra/math/finance/summary.txt new file mode 100644 index 0000000000..9fb3bc8aa7 --- /dev/null +++ b/extra/math/finance/summary.txt @@ -0,0 +1 @@ +Moving averages and other calculations useful for finance.