-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
323 lines (261 loc) · 12.8 KB
/
index.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
let input_dataset = [];
let result = [];
let data_raw = [];
let sma_vec = [];
let window_size = 50;
let trainingsize = 70;
let fileExcel = null;
$(document).ready(function () {
$('select').formSelect();
$("#input_excel").change((e) => {
fileExcel = e.target.files[0];
})
});
function onClickChangeDataFreq(freq) {
console.log(freq.value);
// $("#input_datafreq").text(freq);
}
function onClickFetchData() {
if(!fileExcel){
alert("فایل اکسل را سایت fipiran دریافت کنید و در این سایت انتخاب کنید");
return
}
$("#btn_fetch_data").hide();
$("#load_fetch_data").show();
let message = "";
$("#div_container_linegraph").show();
var reader = new FileReader();
reader.onload = function (e) {
var data = new Uint8Array(reader.result);
try{
var workbook = XLSX.read(data, { type: 'array' })
}catch(e){
$("#btn_fetch_data").show();
$("#load_fetch_data").hide();
$("#div_container_linegraph").hide();
alert("خطا هنگام خواندن فایل اکسل"+" "+e.message)
return;
}
workbook.SheetNames.forEach(function (sheetName) {
// Here is your object
var daily = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if (daily) {
let symbol = daily[0].LVal18AFC;
let lastTradedDate = daily[0].GDate;
data_raw = [];
sma_vec = [];
let lastPrice = daily[0].ClosePrice;
let ratioOfPrice = 0;
for (let i = 0; i < daily.length; i++) {
const item = daily[i];
if(!item.ClosePrice || !item.GDate){
$("#btn_fetch_data").show();
$("#load_fetch_data").hide();
$("#div_container_linegraph").hide();
alert("فایل اکسل صحیح نمیباشد")
return;
}
const closePrice = parseFloat(item.ClosePrice);
const ratioOfClosePrice = item.ClosePrice / lastPrice;
if(ratioOfClosePrice > 1.1)
ratioOfPrice += ratioOfClosePrice;
lastPrice = closePrice;
const price = closePrice / (ratioOfPrice||1);
const date = new Date(item.GDate.toString().replace(/(\d\d\d\d)(\d\d)(\d\d)/, "$1-$2-$3"));
data_raw.push({
timestamp: date,
price: price
});
}
console.log(data_raw)
data_raw.reverse();
message = "نماد: " + symbol + " (آخرین زمان " + lastTradedDate + ")";
$("#btn_fetch_data").show();
$("#load_fetch_data").hide();
$("#div_linegraph_data_title").text(message);
if (data_raw.length > 0) {
let timestamps = data_raw.map(function (val) { return val['timestamp']; });
let prices = data_raw.map(function (val) { return val['price']; });
let graph_plot = document.getElementById('div_linegraph_data');
Plotly.newPlot(graph_plot, [{ x: timestamps, y: prices, name: "قیمت پایانی",line: {color: "#6C4BDB"} }], { margin: { t: 0 } });
}
$("#div_container_getsma").show();
$("#div_container_getsmafirst").hide();
} else {
$("#div_linegraph_data").text(data['Information']);
}
})
};
reader.onerror = function (ex) {
console.log(ex);
};
reader.readAsArrayBuffer(fileExcel);
}
function onClickDisplaySMA() {
$("#btn_draw_sma").hide();
$("#load_draw_sma").show();
$("#div_container_sma").show();
window_size = parseInt(document.getElementById("input_windowsize").value);
sma_vec = ComputeSMA(data_raw, window_size);
let sma = sma_vec.map(function (val) { return val['avg']; });
let prices = data_raw.map(function (val) { return val['price']; });
let timestamps_a = data_raw.map(function (val) { return val['timestamp']; });
let timestamps_b = data_raw.map(function (val) {
return val['timestamp'];
}).splice(window_size, data_raw.length);
let graph_plot = document.getElementById('div_linegraph_sma');
Plotly.newPlot(graph_plot, [{ x: timestamps_a, y: prices, name: "قیمت پایانی",line: {color: "#6C4BDB"} }], { margin: { t: 0 } });
Plotly.plot(graph_plot, [{ x: timestamps_b, y: sma, name: "SMA" }], { margin: { t: 0 } });
$("#div_linegraph_sma_title").text("قیمت پایانی و میانگین متحرک ساده (دوره زمانی: " + window_size + ")");
$("#btn_draw_sma").show();
$("#load_draw_sma").hide();
$("#div_container_train").show();
$("#div_container_trainfirst").hide();
displayTrainingData();
}
function displayTrainingData() {
$("#div_container_trainingdata").show();
let set = sma_vec.map(function (val) { return val['set']; });
let data_output = "";
for (let index = 0; index < 25; index++) {
data_output += "<tr><td width=\"20px\">" + (index + 1) +
"</td><td>[" + set[index].map(function (val) {
return (Math.round(val['price'] * 10000) / 10000).toString();
}).toString() +
"]</td><td>" + sma_vec[index]['avg'] + "</td></tr>";
}
data_output = "<table class='striped'>" +
"<thead><tr><th scope='col'>#</th>" +
"<th scope='col'>Input (X)</th>" +
"<th scope='col'>Label (Y)</th></thead>" +
"<tbody>" + data_output + "</tbody>" +
"</table>";
$("#div_trainingdata").html(
data_output
);
}
async function onClickTrainModel() {
let epoch_loss = [];
$("#div_container_training").show();
$("#btn_draw_trainmodel").hide();
document.getElementById("div_traininglog").innerHTML = "";
let inputs = sma_vec.map(function (inp_f) {
return inp_f['set'].map(function (val) { return val['price']; })
});
let outputs = sma_vec.map(function (outp_f) { return outp_f['avg']; });
trainingsize = parseInt(document.getElementById("input_trainingsize").value);
let n_epochs = parseInt(document.getElementById("input_epochs").value);
let learningrate = parseFloat(document.getElementById("input_learningrate").value);
let n_hiddenlayers = parseInt(document.getElementById("input_hiddenlayers").value);
inputs = inputs.slice(0, Math.floor(trainingsize / 100 * inputs.length));
outputs = outputs.slice(0, Math.floor(trainingsize / 100 * outputs.length));
let callback = function (epoch, log) {
let logHtml = document.getElementById("div_traininglog").innerHTML;
logHtml = "<div>Epoch: " + (epoch + 1) + " (of " + n_epochs + ")" +
", loss: " + log.loss +
// ", difference: " + (epoch_loss[epoch_loss.length-1] - log.loss) +
"</div>" + logHtml;
epoch_loss.push(log.loss);
document.getElementById("div_traininglog").innerHTML = logHtml;
document.getElementById("div_training_progressbar").style.width = Math.ceil(((epoch + 1) * (100 / n_epochs))).toString() + "%";
document.getElementById("div_training_progressbar").innerHTML = Math.ceil(((epoch + 1) * (100 / n_epochs))).toString() + "%";
let graph_plot = document.getElementById('div_linegraph_trainloss');
Plotly.newPlot(graph_plot, [{ x: Array.from({ length: epoch_loss.length }, (v, k) => k + 1), y: epoch_loss, name: "Loss" }], { margin: { t: 0 } });
};
// console.log('train X', inputs)
// console.log('train Y', outputs)
result = await trainModel(inputs, outputs, window_size, n_epochs, learningrate, n_hiddenlayers, callback);
let logHtml = document.getElementById("div_traininglog").innerHTML;
logHtml = "<div class='green center'>آموزش مدل به اتمام رسید</div>" + logHtml;
document.getElementById("div_traininglog").innerHTML = logHtml;
$("#div_container_validate").show();
$("#div_container_validatefirst").hide();
$("#div_container_predict").show();
$("#div_container_predictfirst").hide();
}
function onClickValidate() {
$("#div_container_validating").show();
$("#load_validating").show();
$("#btn_validation").hide();
let inputs = sma_vec.map(function (inp_f) {
return inp_f['set'].map(function (val) { return val['price']; });
});
// validate on training
let val_train_x = inputs.slice(0, Math.floor(trainingsize / 100 * inputs.length));
// let outputs = sma_vec.map(function(outp_f) { return outp_f['avg']; });
// let outps = outputs.slice(0, Math.floor(trainingsize / 100 * inputs.length));
// console.log('val_train_x', val_train_x)
let val_train_y = makePredictions(val_train_x, result['model'], result['normalize']);
// console.log('val_train_y', val_train_y)
// validate on unseen
let val_unseen_x = inputs.slice(Math.floor(trainingsize / 100 * inputs.length), inputs.length);
// console.log('val_unseen_x', val_unseen_x)
let val_unseen_y = makePredictions(val_unseen_x, result['model'], result['normalize']);
// console.log('val_unseen_y', val_unseen_y)
let timestamps_a = data_raw.map(function (val) { return val['timestamp']; });
let timestamps_b = data_raw.map(function (val) {
return val['timestamp'];
}).splice(window_size, (data_raw.length - Math.floor((100 - trainingsize) / 100 * data_raw.length))); //.splice(window_size, data_raw.length);
// let timestamps_c = data_raw.map(function (val) {
// return val['timestamp'];
// }).splice(window_size + Math.floor(trainingsize / 100 * val_unseen_x.length), data_raw.length);
let timestamps_c = data_raw.map(function (val) {
return val['timestamp'];
}).splice(window_size + Math.floor(trainingsize / 100 * inputs.length), inputs.length);
let sma = sma_vec.map(function (val) { return val['avg']; });
let prices = data_raw.map(function (val) { return val['price']; });
sma = sma.slice(0, Math.floor(trainingsize / 100 * sma.length));
// console.log('sma', sma)
let graph_plot = document.getElementById('div_validation_graph');
Plotly.newPlot(graph_plot, [{ x: timestamps_a, y: prices, name: "قیمت پایانی",line: {color: "#6C4BDB"} }], { margin: { t: 0 } });
Plotly.plot(graph_plot, [{ x: timestamps_b, y: sma, name: "SMA" }], { margin: { t: 0 } });
Plotly.plot(graph_plot, [{ x: timestamps_b, y: val_train_y, name: "پیشبینی آموزشی (train)" }], { margin: { t: 0 } });
Plotly.plot(graph_plot, [{ x: timestamps_c, y: val_unseen_y, name: "پیشبینی تست (test)" }], { margin: { t: 0 } });
$("#load_validating").hide();
}
async function onClickPredict() {
$("#div_container_predicting").show();
$("#load_predicting").show();
$("#btn_prediction").hide();
let inputs = sma_vec.map(function (inp_f) {
return inp_f['set'].map(function (val) { return val['price']; });
});
let pred_X = inputs;
pred_X = pred_X.slice(Math.floor(trainingsize / 100 * pred_X.length), pred_X.length);
let pred_y = makePredictions(pred_X, result['model'], result['normalize']);
window_size = parseInt(document.getElementById("input_windowsize").value);
let timestamps_d = data_raw.map(function (val) {
return val['timestamp'];
}).splice((data_raw.length - window_size), data_raw.length);
// date
let last_date = new Date(timestamps_d[timestamps_d.length - 1]);
let timestamps_e = [];
for(let i=0;i<=pred_y.length;i++){
timestamps_e.push(formatDate(last_date.setDate(last_date.getDate() + i)));
}
let graph_plot = document.getElementById('div_prediction_graph');
Plotly.newPlot(graph_plot, [{ x: timestamps_d, y: pred_X[0], name: "قیمت پایانی", line: {color: "#6C4BDB"} }], { margin: { t: 0 } });
Plotly.plot(graph_plot, [{ x: timestamps_e, y: pred_y, name: "قیمت پایانی آینده", line: {color: "#FF597B"} }], { margin: { t: 0 } });
$("#load_predicting").hide();
}
function ComputeSMA(data, window_size) {
let r_avgs = [], avg_prev = 0;
for (let i = 0; i <= data.length - window_size; i++) {
let curr_avg = 0.00, t = i + window_size;
for (let k = i; k < t && k <= data.length; k++) {
curr_avg += data[k]['price'] / window_size;
}
r_avgs.push({ set: data.slice(i, i + window_size), avg: curr_avg });
avg_prev = curr_avg;
}
return r_avgs;
}
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2) month = '0' + month;
if (day.length < 2) day = '0' + day;
return [year, month, day].join('-');
}