-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategies.js
145 lines (127 loc) · 3.44 KB
/
strategies.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
"use strict";
var _ = require('underscore');
function sum(ary) {
return _.reduce(ary, function(x, y) { return x + y; }, 0);
}
function Strategy(f, name) {
this.populate = f;
this.slow = [];
this.fast = [];
this.name = name;
}
var Strategies = {
populate: function (arr) {
this.SMA.populate(arr);
this.LWMA.populate(arr);
this.EMA.populate(arr);
this.TMA.populate(arr);
},
SMA: new Strategy((function(datapoints) {
function SMA_optimize(datapoints, n)
{
return Math.round( (_.last(datapoints)/n - datapoints[datapoints.length-n-1]/n) * 1000) /1000;
}
var temp_sum;
var t = datapoints.length;
if(t<= 5)
{
this.fast.push( Math.round( (sum(datapoints)/t) * 1000) / 1000 );
this.slow.push(_.last(this.fast));
}
else if(t <= 20)
{
temp_sum = sum(_.last(datapoints, 5));
this.slow.push( Math.round( ( (temp_sum + sum(_.first(datapoints,t-5))) /t) *1000 ) /1000 );
this.fast.push( Math.round( (temp_sum/5) *1000 )/1000 );
}
else
{
this.fast.push(_.last(this.fast) + SMA_optimize(datapoints,5));
this.slow.push(_.last(this.slow) + SMA_optimize(datapoints,20));
}
}), 'SMA'),
LWMA: new Strategy((function(datapoints) {
function LWMA_optimize(datapoints, n)
{
var factors = _.range(1,n+1);
var datapoints = _.last(datapoints, n);
return Math.round( sum(
_.map(_.zip(datapoints,factors), function(array){return array[0]*array[1];})
) /sum(factors) *1000) /1000;
}
var t = datapoints.length;
if(t<= 5)
{
this.fast.push(LWMA_optimize(datapoints, t));
this.slow.push(_.last(this.fast));
}
else if(t <= 20)
{
this.fast.push(LWMA_optimize(datapoints, 5));
this.slow.push(LWMA_optimize(datapoints, t));
}
else
{
this.fast.push(LWMA_optimize(datapoints, 5));
this.slow.push(LWMA_optimize(datapoints, 20));
}
}), 'LWMA'),
EMA: new Strategy((function(datapoints) {
function EMA_optimize(lastp, n, prev)
{
var value = prev + ((lastp - prev) * (2/(n+1)));
return Math.round(value*1000) /1000;
}
var t = datapoints.length;
var lastprice = _.last(datapoints);
if(t == 1)
{
this.fast.push(lastprice);
this.slow.push(lastprice);
}
else
{
var fastEMA = _.last(this.fast);
var slowEMA = _.last(this.slow);
this.fast.push( EMA_optimize( lastprice, 5, fastEMA) );
this.slow.push( EMA_optimize( lastprice, 20, slowEMA) );
}
}), 'EMA'),
TMA: new Strategy((function(datapoints) {
function TMA_optimize(data, n)
{
return Math.round( ( (sum(data)/n) *1000 )) / 1000;
}
var t = datapoints.length;
if(t<= 5)
{
this.fast.push( TMA_optimize( _.last(Strategies.SMA.fast, t), t));
this.slow.push( TMA_optimize( _.last(Strategies.SMA.slow, t), t));
}
else if(t <= 20)
{
this.fast.push( TMA_optimize( _.last(Strategies.SMA.fast, 5), 5));
this.slow.push( TMA_optimize( _.last(Strategies.SMA.slow, t), t));
}
else
{
this.fast.push( TMA_optimize( _.last(Strategies.SMA.fast, 5), 5));
this.slow.push( TMA_optimize( _.last(Strategies.SMA.slow, 20), 20));
}
}), 'TMA')
};
exports.Strategies = Strategies;
/*Debugging
var dp = [1,2,3,4,5,6,7,8,9,10];
for (var i = 0; i < dp.length; i++) {
Strategies.populate(_.first(dp,i+1));
};
console.log(Strategies.SMA.slow);
console.log(Strategies.SMA.fast);
console.log(Strategies.LWMA.slow);
console.log(Strategies.LWMA.fast);
console.log(Strategies.EMA.slow);
console.log(Strategies.EMA.fast);
console.log(Strategies.TMA.slow);
console.log(Strategies.TMA.fast);
*/