forked from juo6442/moothumb
-
Notifications
You must be signed in to change notification settings - Fork 14
/
predictor.js
423 lines (379 loc) · 15.1 KB
/
predictor.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
const Days = {
SUN: 0,
MON1: 1,
MON2: 2,
TUE1: 3,
TUE2: 4,
WED1: 5,
WED2: 6,
THU1: 7,
THU2: 8,
FRI1: 9,
FRI2: 10,
SAT1: 11,
SAT2: 12,
length: 13,
};
const Method = {
PRICE: 0,
PREV_PRICE_DIFF: 1,
PREV_PRICE_RATIO: 2,
PREV_PRICE_RATIO_DIFF: 3,
PURCHASE_PRICE_RATIO: 4,
}
const WavePatternTransitionType = {
RISING: 'R',
TWO_TIMES_FALLING: '2',
THREE_TIMES_FALLING: '3',
};
class PredictionRange {
constructor(min, max) {
this.min = min - this.tolerance;
this.max = max + this.tolerance;
}
}
class Transition {
constructor(method, min, max) {
this.method = method;
this.min = min;
this.max = max;
}
}
function predict(parameters, prices) {
PredictionRange.prototype.tolerance = parameters.tolerance;
return {
wave: predictWavePattern(parameters.wave, prices),
falling: predictFallingPattern(parameters.falling, prices),
thirdPeriod: predictThirdPeriodPattern(parameters.thirdPeriod, prices),
fourthPeriod: predictFourthPeriodPattern(parameters.fourthPeriod, prices),
};
}
function predictWavePattern(parameters, prices) {
const predictions = new Map();
for (let twoTimesFallingStartDay of parameters.twoTimesFallingStartDays) {
for (let threeTimesFallingStartDay of parameters.threeTimesFallingStartDays) {
const minMargin = 1;
if (twoTimesFallingStartDay <= threeTimesFallingStartDay
&& twoTimesFallingStartDay + 2 + minMargin > threeTimesFallingStartDay) {
continue;
} else if (threeTimesFallingStartDay < twoTimesFallingStartDay
&& threeTimesFallingStartDay + 3 + minMargin > twoTimesFallingStartDay) {
continue;
}
// Load parameters
function getTransitionFromDay(day) {
switch (day) {
case twoTimesFallingStartDay + 0:
return parameters.twoTimesFalling1Transition;
case twoTimesFallingStartDay + 1:
return parameters.twoTimesFalling2Transition;
case threeTimesFallingStartDay + 0:
return parameters.threeTimesFalling1Transition;
case threeTimesFallingStartDay + 1:
return parameters.threeTimesFalling2Transition;
case threeTimesFallingStartDay + 2:
return parameters.threeTimesFalling3Transition;
default:
return parameters.risingTransition;
}
}
function getTypeFromDay(day) {
switch(day) {
case twoTimesFallingStartDay + 0:
case twoTimesFallingStartDay + 1:
return WavePatternTransitionType.TWO_TIMES_FALLING;
case threeTimesFallingStartDay + 0:
case threeTimesFallingStartDay + 1:
case threeTimesFallingStartDay + 2:
return WavePatternTransitionType.THREE_TIMES_FALLING;
default:
return WavePatternTransitionType.RISING;
}
}
const transitions = new Array(Days.length);
let key = '';
for (let i = Days.MON1; i < Days.length; i++){
transitions[i] = getTransitionFromDay(i);
key += getTypeFromDay(i);
}
// Predict
predictions.set(key, calcPrediction(prices, transitions));
}
}
return predictions;
}
function predictFallingPattern(parameters, prices) {
// Load parameters
const transitions = new Array(Days.length);
transitions[Days.MON1] = parameters.mon1Transition;
transitions.fill(parameters.otherDaysTransition, Days.MON2);
// Predict
return calcPrediction(prices, transitions);
}
function predictThirdPeriodPattern(parameters, prices) {
const predictions = new Array(Days.length);
for (let risingStartDay of parameters.risingStartDays) {
// Load parameters
const transitions = new Array(Days.length);
transitions[Days.MON1] = parameters.mon1Transition;
transitions.fill(parameters.beforeRisingTransition, Days.MON2, risingStartDay);
const risingTransitionSequence = [
parameters.rising1Transition,
parameters.rising2Transition,
parameters.rising3Transition,
parameters.rising4Transition,
parameters.rising5Transition,
parameters.rising6Transition,
];
for (let i = risingStartDay; i < Days.length; i++) {
let offset = i - risingStartDay;
if (offset < risingTransitionSequence.length) {
transitions[i] = risingTransitionSequence[offset];
} else {
transitions[i] = parameters.afterRisingTransition;
}
}
// Predict
predictions[risingStartDay] = calcPrediction(prices, transitions);
}
return predictions;
}
function predictFourthPeriodPattern(parameters, prices) {
const predictions = predictThirdPeriodPattern(parameters, prices);
if (!parameters.hasFourthPeriodPeak) {
return predictions;
}
// Make fouth period peak
for (let risingStartDay of parameters.risingStartDays) {
const prediction = predictions[risingStartDay];
if (!prediction) {
continue;
}
if (prices[risingStartDay + 3]) {
if (!prices[risingStartDay + 2]) {
prediction[risingStartDay + 2].max = Math.min(
prediction[risingStartDay + 2].max,
prices[risingStartDay + 3]);
}
if (prices[risingStartDay + 4]) {
prediction[risingStartDay + 4].max = Math.min(
prediction[risingStartDay + 4].max,
prices[risingStartDay + 3]);
}
} else {
if (prices[risingStartDay + 2]) {
prediction[risingStartDay + 3].min = Math.max(
prediction[risingStartDay + 3].min,
prices[risingStartDay + 2]);
}
if (prices[risingStartDay + 4]) {
prediction[risingStartDay + 3].min = Math.max(
prediction[risingStartDay + 3].min,
prices[risingStartDay + 4]);
}
}
}
return predictions;
}
function calcPrediction(prices, transitions) {
const purchasePriceMinMax = getPurchacePriceMinMax(prices[Days.SUN]);
const prediction = new Array(Days.length);
prediction[Days.SUN] = new PredictionRange(
purchasePriceMinMax[0],
purchasePriceMinMax[1],
);
const pricesCopy = [...prices];
for (let purchasePrice of purchasePriceMinMax) {
pricesCopy[Days.SUN] = purchasePrice;
const eachPrediction = calcEachPrediction(pricesCopy, transitions);
if (!eachPrediction) {
continue;
}
for (let i = Days.MON1; i < Days.length; i++) {
if (!prediction[i]) {
prediction[i] = eachPrediction[i];
} else {
prediction[i].min = Math.min(prediction[i].min, eachPrediction[i].min);
prediction[i].max = Math.max(prediction[i].max, eachPrediction[i].max);
}
}
}
for (let i = Days.MON1; i < Days.length; i++) {
if (prices[i]) {
prediction[i] = new PredictionRange(prices[i], prices[i]);
} else if (!prediction[i]){
return null;
}
}
return prediction;
}
// TODO: refactoring
function calcEachPrediction(prices, transitions) {
let prediction = new Array(Days.length);
// Forward prediction
for (let i = Days.MON1; i < Days.length; i++) {
switch (transitions[i].method) {
case Method.PRICE:
prediction[i] = new PredictionRange(
transitions[i].min,
transitions[i].max,
);
break;
case Method.PURCHASE_PRICE_RATIO:
prediction[i] = new PredictionRange(
prices[Days.SUN] * transitions[i].min / 100,
prices[Days.SUN] * transitions[i].max / 100,
);
break;
case Method.PREV_PRICE_DIFF:
if (prices[i - 1]) {
prediction[i] = new PredictionRange(
prices[i - 1] + transitions[i].min,
prices[i - 1] + transitions[i].max,
);
} else {
prediction[i] = new PredictionRange(
prediction[i - 1].min + transitions[i].min,
prediction[i - 1].max + transitions[i].max,
);
}
break;
case Method.PREV_PRICE_RATIO_DIFF:
prediction[i] = new PredictionRange(0, 0);
for(var j = i; j >= Days.SUN; j--) {
if (prices[j] && j != i) {
prediction[i].min += prices[j];
prediction[i].max += prices[j];
break;
} else if (transitions[j].method == Method.PRICE
|| transitions[j].method == Method.PREV_PRICE_DIFF
|| transitions[j].method == Method.PREV_PRICE_RATIO) {
prediction[i].min += prediction[j].min;
prediction[i].max += prediction[j].max;
break;
} else if (transitions[j].method == Method.PURCHASE_PRICE_RATIO) {
prediction[i].min += prices[Days.SUN] * (transitions[j].min / 100);
prediction[i].max += prices[Days.SUN] * (transitions[j].max / 100);
break;
} else if (transitions[j].method == Method.PREV_PRICE_RATIO_DIFF) {
prediction[i].min += prices[Days.SUN] * (transitions[j].min / 100);
prediction[i].max += prices[Days.SUN] * (transitions[j].max / 100);
continue;
}
}
break;
case Method.PREV_PRICE_RATIO:
if (prices[i - 1]) {
prediction[i] = new PredictionRange(
prices[i - 1] * transitions[i].min / 100,
prices[i - 1] * transitions[i].max / 100,
);
} else {
prediction[i] = new PredictionRange(
prediction[i - 1].min * transitions[i].min / 100,
prediction[i - 1].max * transitions[i].max / 100,
);
}
break;
}
}
// Backward prediction
for(let i = Days.length - 1; i >= Days.MON2; i--){
let backwardPrediction;
switch (transitions[i].method){
case Method.PREV_PRICE_DIFF:
if (prices[i]){
backwardPrediction = new PredictionRange(
prices[i] - transitions[i].max,
prices[i] - transitions[i].min,
);
} else {
backwardPrediction = new PredictionRange(
prediction[i].min - transitions[i].max,
prediction[i].max - transitions[i].min,
);
}
break;
case Method.PREV_PRICE_RATIO_DIFF:
var spMin = 0;
var spMax = 0;
for (var j = i; j <= Days.SAT2; j++) {
if (transitions[j].method == Method.PRICE
|| transitions[j].method == Method.PURCHASE_PRICE_RATIO) {
break;
} else if (transitions[j].method == Method.PREV_PRICE_DIFF
|| transitions[j].method == Method.PREV_PRICE_RATIO) {
if (prices[j - 1]) {
backwardPrediction = new PredictionRange(
prices[j - 1] - prices[Days.SUN] * spMax / 100,
prices[j - 1] - prices[Days.SUN] * spMin / 100,
);
break;
} else {
backwardPrediction = new PredictionRange(
prediction[j - 1].min - prices[Days.SUN] * spMax / 100 ,
prediction[j - 1].max - prices[Days.SUN] * spMin / 100 ,
);
break;
}
} else if (transitions[j].method == Method.PREV_PRICE_RATIO_DIFF) {
spMin += transitions[j].min;
spMax += transitions[j].max;
if (prices[j]) {
backwardPrediction = new PredictionRange(
prices[j] - prices[Days.SUN] * spMax / 100,
prices[j] - prices[Days.SUN] * spMin / 100,
);
break;
} else {
continue;
}
} else {
break;
}
}
break;
case Method.PREV_PRICE_RATIO:
if (prices[i]) {
backwardPrediction = new PredictionRange(
prices[i] * 100 / transitions[i].max,
prices[i] * 100 / transitions[i].min,
);
} else {
backwardPrediction = new PredictionRange(
prediction[i].min * 100 / transitions[i].max,
prediction[i].max * 100 / transitions[i].min,
);
}
break;
}
if (backwardPrediction) {
if (!prediction[i - 1]) {
prediction[i - 1] = new PredictionRange(backwardPrediction.min, backwardPrediction.max);
} else {
prediction[i - 1].min = Math.min(prediction[i - 1].min, backwardPrediction.min);
prediction[i - 1].max = Math.max(prediction[i - 1].max, backwardPrediction.max);
}
}
}
for (let i = Days.MON1; i < Days.length; i++) {
if (!prices[i] || !prediction[i]) {
continue;
}
if (!isPredictionAcceptable(prediction[i], prices[i])) {
return null;
}
}
return prediction;
}
function getPurchacePriceMinMax(price) {
const min = 90;
const max = 110;
if (price && min <= price && price <= max) {
return [price, price];
}
return [min, max];
}
function isPredictionAcceptable(prediction, realValue) {
return prediction.min <= realValue && realValue <= prediction.max;
}