-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
160 lines (140 loc) · 5.92 KB
/
main.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
const plot = document.getElementById("graphDiv");
const plot2 = document.getElementById("simDiv");
// let bondPlot = document.getElementById("bondPlot");
// let bondIndices = [ "RUGBITR1Y", "RUGBITR3Y", "RUGBITR5Y", "RUGBITR10Y", "RUGBITR5+", "RGBITR"];
// let bondStartDate = "2010-12-30";
// 2010-12-30
// let indices = ["MCFTRR", "RGBITR", "RUGBITR10Y", "RUGBITR5Y", "RUGBITR3Y", "RUGBITR1Y"];
// 2003-02-26 2002-12-30 2
// let indices = ["RTSSTDTRR", "RUGBITR5+"];
// let startDate = "2010-12-30";
let indices = ["MCFTRR", "RGBITR"];
let startDate = "2003-02-26";
// let indices = ["MCFTRR", "RUGBITR5+", "RGBITR"];
// let startDate = "2010-12-30";
let endDate = "";
let rebalancePeriod = 365;
let activeModel = {};
document.getElementById('start-date').value = startDate;
document.getElementById('start-date').min = startDate;
document.getElementById('rebalance-period').value = rebalancePeriod;
document.getElementById('balance-slider').oninput = updateSliderLables;
// addBlankPlot(bondPlot);
// addTracesToPlot(bondPlot, bondIndices, bondStartDate);
addBlankPlot(plot);
addTracesToPlot(plot, indices);
(async function(){
let res = await updateAllTracesLoop(plot, indices, startDate);
console.log(res);
res = await Promise.all(res);
console.log(res);
res = await Promise.all(res.flat());
console.log(res);
// res = await Promise.all(res.flat());
// console.log(res);
document.getElementById("run-button").disabled = false;
document.getElementById("run-button2").disabled = false;
plot.layout.xaxis.autorange = false;
addCAGRs();
let endDate = plot.data[0].x.slice(-1)[0];
document.getElementById('start-date').max = endDate;
document.getElementById('rebalance-period').max = moment.duration(moment(endDate).diff(moment(startDate))).as("days");
})()
function addCAGRs() {
console.log("CAGRS strart")
const colors = [
'#1f77b4', // muted blue
'#ff7f0e', // safety orange]
'grey'
]
for (i=0; i<3; i++) {
let startDate = moment(plot.data[i].x[0], "YYYY-MM-DD");
let endDate = moment(plot.data[i].x[plot.data[i].x.length-1], "YYYY-MM-DD");
let durationInDays = moment.duration(endDate.diff(startDate)).as("days");
let startVal = plot.data[i].y[0];
let endVal = plot.data[i].y[plot.data[i].y.length-1];
let CAGR = (endVal / startVal) ** (1 / (durationInDays/364));
let CAGRtrace = makeCAGRtrace(CAGR);
CAGRtrace.line = {};
CAGRtrace.line.color = colors[i];
CAGRtrace.line.dash = "dot";
CAGRtrace.line.width = 1;
CAGRtrace.textfont = {color: colors[i]}
Plotly.addTraces(plot, CAGRtrace);
}
return true;
}
let simulationWorker = new Worker('simulation.js');
function startSimulation() {
simulationWorker.postMessage(plot.data);
console.log('Message posted to worker');
return true;
}
const evaluatedModels = [];
let simTrace = {x: [], y: [], marker: {color: [], colorbar: {title: "Ребалансировка, дней"}, size: 12}, type: "scatter", mode: "markers"};
let simTraceSD = {x: [], y: [], xaxis: "x", yaxis: "y2", marker: {color: [], colorbar: {title: "Ребалансировка, дней"}, size: 12}, type: "scatter", mode: "markers"};
let r = 0;
simulationWorker.onmessage = function(e) {
console.log('Message received from worker');
// let CAGRtrace = makeCAGRtrace(e.data.weightedCAGR);
// CAGRtrace.name = "Share part: " + e.data.sharesParts + "; SD: " + Math.round(e.data.standardDeviation*1000)/1000;
// CAGRtrace.showlegend = true;
// Plotly.addTraces(plot, CAGRtrace);
evaluatedModels.push(e.data);
simTrace.x.push(e.data.sharesParts);
simTraceSD.x.push(e.data.sharesParts);
simTrace.y.push(e.data.weightedCAGR);
simTraceSD.y.push(e.data.standardDeviation);
simTrace.marker.color.push(e.data.rebalancePeriod);
simTraceSD.marker.color.push(e.data.rebalancePeriod);
// console.log(simTrace);
Plotly.react("simDiv", [simTrace, simTraceSD], {datarevision: r++, height: 700,xaxis: {title: 'Доля акций'}, yaxis: {title: 'CAGR'}, yaxis2: {title: 'CAGR standard deviation'},
xaxis2: {title: 'Доля акций'}, hovermode: 'closest' , showlegend: false, grid: {
rows: 2,
columns: 1,
subplots:[['xy'], ['xy2']],
roworder:'top to bottom'
}});
return true;
}
function makeCAGRtrace(CAGR, showLegend = false, textPosition = "left", startIndex = 0, daysPerPoint = 90) {
const dayCAGR = CAGR**(1/364);
let newX = plot.data[0].x.slice(startIndex, plot.data[0].x.length);
let newY = newX.map((x, i) => dayCAGR**i);
newX = newX.filter((el, i, arr) => {
if (i===0) {return true} else if (i===arr.length-1) {return true} else if (i % daysPerPoint === 0) {return true}
});
newY = newY.filter((el, i, arr) => {
if (i===0) {return true} else if (i===arr.length-1) {return true} else if (i % daysPerPoint === 0) {return true}
});
const annotation = [];
annotation[newY.length-1] = "CAGR:"+Math.round(CAGR*1000)/1000;
return {x: newX, y: newY, type: "scatter", mode: 'lines+text', text: annotation, textposition: textPosition, showlegend: showLegend, hoverinfo: "skip"};
}
plot.layout = {
showlegend: true,
legend: {"orientation": "h", x: 0.5, y: -0.1},
hovermode:'x',
margin: {
t: 10, //top margin
l: 20, //left margin
r: 20, //right margin
b: 10 //bottom margin
}
};
// plot.layout = {
// // showlegend: true,
// // legend: {"orientation": "h", x: 0.5, y: -0.1},
// // hovermode:'x',
// // margin: {
// // t: 10, //top margin
// // l: 20, //left margin
// // r: 20, //right margin
// // b: 10 //bottom margin
// // }
// };
window.onresize = function() {
Plotly.Plots.resize(plot);
Plotly.Plots.resize(plot2);
// Plotly.Plots.resize(bondPlot);
};