1 ! Copyright (C) 2008 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: arrays assocs kernel grouping sequences shuffle
5 math math.functions math.statistics math.vectors ;
9 : enumerate ( seq -- newseq )
14 : weighted ( x y a -- z )
15 tuck [ * ] [ 1 swap - * ] 2bi* + ;
20 : first-rest ( seq -- first rest )
21 [ first ] keep 1 tail-slice ;
25 : ema ( seq n -- newseq )
26 a swap first-rest swap [ [ dup ] 2dip swap rot weighted ] accumulate 2nip ;
28 : sma ( seq n -- newseq )
31 : macd ( seq n1 n2 -- newseq )
32 rot dup ema [ swap ema ] dip v- ;
34 : momentum ( seq n -- newseq )
35 2dup tail-slice -rot swap [ length ] keep
36 [ - neg ] dip swap head-slice v- ;