-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.js
125 lines (117 loc) · 3.49 KB
/
predict.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
const tf = require("@tensorflow/tfjs-node");
const fs = require("fs");
const Utils = require("./utils");
const express = require("express");
const modelFilePath = "./model";
const candlestickMap = {
down: 0,
doji: 1,
up: 2
};
const trendMap = {
uptrend: 2,
neutral: 1,
downtrend: 0
};
const eventMap = {
bb_bottom_cross_up: 1,
bb_bottom_cross_down: 2,
bb_top_cross_up: 3,
bb_top_cross_up: 4,
bb_top_cross_down: 5,
supertrend_sell: 6,
supertrend_buy: 7,
cci_oversold_start: 8,
cci_oversold_end: 9,
cci_overbought_start: 10,
cci_overbought_end: 11,
rsi7_oversold_start: 12,
rsi7_oversold_end: 13,
rsi7_overbought_start: 14,
rsi7_overbought_end: 15,
rsi14_oversold_start: 16,
rsi14_oversold_end: 17,
rsi14_overbought_start: 18,
rsi14_overbought_end: 19,
mfi_oversold_start: 20,
mfi_oversold_end: 21,
mfi_overbought_start: 22,
mfi_overbought_end: 23,
macd_bearish_momentum_decay: 24,
macd_bullish_momentum_decay: 25,
macd_bullish_crossover: 26,
macd_bearish_crossover: 27,
emaFast_bullish_crossover: 28,
emaFast_bearish_crossover: 29,
emaSlow_bullish_crossover: 30,
emaSlow_bearish_crossover: 31
};
var inputData = [];
async function analyzeTimeSeries() {
var data = (await Utils.readJSON("BTCUSDT")).map((object) => {
return [
object.close,
object.open,
object.high,
object.low,
object.volume,
Math.abs(object.atr14) || 0,
Math.abs(object.cci14) || 0,
Math.abs(object.mfi14) || 0,
Math.abs(object.rsi7) || 0,
Math.abs(object.rsi14) || 0,
Math.abs(eventMap[object.events[0]] || 0),
Math.abs(trendMap[object.trend] || 0),
Math.abs(candlestickMap[object.candlestick] || 0)
].map(normalize());
});
for (let i = 0; i < data.length; i++) {
inputData.push(data[i]);
}
const model = await loadModel();
const predictionInputData = inputData.slice(-1);
const predictionInputTensor = tf.tensor(predictionInputData, [predictionInputData.length, 1, predictionInputData[0].length]);
const predictions = model.predict(predictionInputTensor);
const predictedValues = Array.from(predictions.dataSync());
console.log("Prediction", {
original: Utils.round(inputData.slice(-1)[0].map(denormalize())[0]),
prediction: Utils.round(predictedValues.map(denormalize())[0])
});
}
function normalize() {
var delta = 100000;
return function (val) {
return val / delta;
};
}
function denormalize() {
var delta = 100000;
return function (val) {
return val * delta;
};
}
function createModel() {
var model = tf.sequential();
model.add(tf.layers.lstm({ units: 64, inputShape: [1, inputData[0].length] }));
model.add(tf.layers.dense({ units: 1 }));
return model;
}
async function saveModel(model) {
await model.save(`file://${modelFilePath}`);
console.log("Model saved");
}
// Load or create the model
async function loadModel() {
if (fs.existsSync(`${modelFilePath}/model.json`)) {
// Load the existing model
const model = await tf.loadLayersModel(`file://${modelFilePath}/model.json`);
console.log("Loaded model from file");
return model;
} else {
// Create a new model
const model = createModel();
console.log("Created a new model");
return model;
}
}
analyzeTimeSeries();