]> gitweb.factorcode.org Git - factor.git/blob - extra/math/finance/finance.factor
Merge branch 'master' of git://github.com/mrjbq7/factor
[factor.git] / extra / math / finance / finance.factor
1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: arrays assocs kernel grouping sequences shuffle
5 math math.functions math.statistics math.vectors ;
6
7 IN: math.finance
8
9 : enumerate ( seq -- newseq )
10     <enum> >alist ;
11
12 <PRIVATE
13
14 : weighted ( x y a -- z ) 
15     tuck [ * ] [ 1 swap - * ] 2bi* + ;
16
17 : a ( n -- a ) 
18     1 + 2 swap / ;
19
20 : first-rest ( seq -- first rest )
21     [ first ] keep 1 tail-slice ;
22
23 PRIVATE>
24
25 : ema ( seq n -- newseq )
26     a swap first-rest swap [ [ dup ] 2dip swap rot weighted ] accumulate 2nip ;
27
28 : sma ( seq n -- newseq )
29     clump [ mean ] map ;
30
31 : macd ( seq n1 n2 -- newseq )
32     rot dup ema [ swap ema ] dip v- ;
33
34 : momentum ( seq n -- newseq )
35     2dup tail-slice -rot swap [ length ] keep
36     [ - neg ] dip swap head-slice v- ;
37