-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatePlot.js
67 lines (59 loc) · 3.09 KB
/
updatePlot.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
async function updateAllTracesLoop (plot, indices, startDate) {
return indices.map(async (index, indexNum, indices) => {
let dataUpdate = {};
dataUpdate = await maybeGetFromStore(index);
let promises = [];
let allDonePromises = [];
if (dataUpdate) {
allDonePromises.push(
Plotly.extendTraces(plot, {x: [dataUpdate.x], y: [dataUpdate.y], marketDay: [dataUpdate.marketDay]}, [traceIndexByName(index)]).then(
x => {
console.log("Index: " + index + "loaded frome storage.");
}
)
);
} else {
console.log("start to build: " + index);
if (index === "RGBITR") {
promises.push(updateTraceLoop(plot, index, startDate, engine = "stock"));
promises.push(updateTraceLoop(plot, index, startDate, engine = "state"));
} else {
promises.push(updateTraceLoop(plot, index, startDate, engine = "stock"));
}
allDonePromises.push(Promise.all(promises).then(val => {
return Promise.all(val.flat());
}).then(val => {
let sortedData = sortByDate(plot.data[traceIndexByName(index)]);
let expandedData = expandTimeseries(sortedData);
plot.data[traceIndexByName(index)].x = expandedData.x;
plot.data[traceIndexByName(index)].y = expandedData.y;
plot.data[traceIndexByName(index)].marketDay = expandedData.marketDay;
maybeStore(index, plot.data[traceIndexByName(index)]);
console.log(index + " put to storage.");
}).catch((err) => {
throw new Error('Error in maybeStore promise chain' + err.message);
})
)
}
return allDonePromises;
});
}
async function updateTraceLoop (plot, index, startDate, engine) {
let lastRecord = getLastRecord(index, startDate, engine);
let firstValue = getFirstValue(index, startDate);
[lastRecord, firstValue] = await Promise.all([lastRecord, firstValue]);
let promises = [];
for (let i = 0; i < lastRecord; i += 100) {
let promise = updateTraceData(plot, index, startDate, i, firstValue, engine);
promises.push(promise);
}
return promises;
}
async function updateTraceData (plot, index, startDate, fromIndex, firstValue, engine) {
let traceDataChunk = await axios.get("https://iss.moex.com/iss/history/engines/"+engine+"/markets/index/securities/"+index+".json?start="+fromIndex+"&from="+startDate);
let dataUpdate = await extract(traceDataChunk.data.history.data, 2, engine==="stock" ? 5 : 7 );
dataUpdate.y = await normalize(dataUpdate.y, firstValue);
// dataUpdate = expandTimeseries(dataUpdate);
Plotly.extendTraces(plot, {x: [dataUpdate.x], y: [dataUpdate.y]}, [traceIndexByName(index)]);
return true;
}