-
Notifications
You must be signed in to change notification settings - Fork 1
/
LRC.js
115 lines (94 loc) · 2.37 KB
/
LRC.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
/*
* Linear regression curve
*/
var log = require('../../core/log');
var Indicator = function(settings) {
this.input = 'price';
this.depth = settings;
this.result = false;
this.age = 0;
this.history = [];
this.x = [];
/*
* Do not use array(depth) as it might not be implemented
*/
for (var i = 0; i < this.depth; i++) {
this.history.push(0.0);
this.x.push(i);
}
// log.debug("Created LRC indicator with h: ", this.depth);
}
Indicator.prototype.update = function(price) {
// We need sufficient history to get the right result.
if(this.result === false && this.age < this.depth) {
this.history[this.age] = price;
this.age++;
this.result = false;
// log.debug("Waiting for sufficient age: ", this.age, " out of ", this.depth);
//
return;
}
this.age++;
// shift history
for (var i = 0; i < (this.depth - 1); i++) {
this.history[i] = this.history[i+1];
}
this.history[this.depth-1] = price;
this.calculate(price);
// log.debug("Checking LRC: ", this.result.toFixed(8), "\tH: ", this.age);
return;
}
/*
* Least squares linear regression fitting.
*/
function linreg(values_x, values_y) {
var sum_x = 0;
var sum_y = 0;
var sum_xy = 0;
var sum_xx = 0;
var count = 0;
/*
* We'll use those variables for faster read/write access.
*/
var x = 0;
var y = 0;
var values_length = values_x.length;
if (values_length != values_y.length) {
throw new Error('The parameters values_x and values_y need to have same size!');
}
/*
* Nothing to do.
*/
if (values_length === 0) {
return [ [], [] ];
}
/*
* Calculate the sum for each of the parts necessary.
*/
for (var v = 0; v < values_length; v++) {
x = values_x[v];
y = values_y[v];
sum_x += x;
sum_y += y;
sum_xx += x*x;
sum_xy += x*y;
count++;
}
/*
* Calculate m and b for the formular:
* y = x * m + b
*/
var m = (count*sum_xy - sum_x*sum_y) / (count*sum_xx - sum_x*sum_x);
var b = (sum_y/count) - (m*sum_x)/count;
return [m, b];
}
/*
* Handle calculations
*/
Indicator.prototype.calculate = function(price) {
// get the reg
var reg = linreg(this.x, this.history);
// y = a * x + b
this.result = ((this.depth-1) * reg[0]) + reg[1];
}
module.exports = Indicator;