]> gitweb.factorcode.org Git - factor.git/blob - extra/math/finance/finance.factor
Merge branch 'master' of git://factorcode.org/git/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 <PRIVATE
10
11 : weighted ( x y a -- z ) 
12     tuck [ * ] [ 1 swap - * ] 2bi* + ;
13
14 : a ( n -- a ) 
15     1 + 2 swap / ;
16
17 PRIVATE>
18
19 : ema ( seq n -- newseq )
20     a swap unclip [ [ dup ] 2dip swap rot weighted ] accumulate 2nip ;
21
22 : sma ( seq n -- newseq )
23     clump [ mean ] map ;
24
25 : macd ( seq n1 n2 -- newseq )
26     rot dup ema [ swap ema ] dip v- ;
27
28 : momentum ( seq n -- newseq )
29     2dup tail-slice -rot swap [ length ] keep
30     [ - neg ] dip swap head-slice v- ;
31