-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakePortfolio.js
109 lines (100 loc) · 4.29 KB
/
makePortfolio.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
plot.on('plotly_click', function(data){
document.getElementById('start-date').style.backgroundColor = "salmon";
setTimeout(function() {document.getElementById('start-date').style.backgroundColor = "#fff";}, 300);
document.getElementById('start-date').value = data.points[0].x;
// console.log(data.points[0].x);
return false;
});
function showModel(form) {
let modelTrace = {};
const newStartDate = form.elements.namedItem("start-date").value; //redo to findClosest date
const rebalancePeriod = form.elements.namedItem("rebalance-period").value;
const sharesPart = parseInt(100 - form.elements.namedItem("balance-slider").value) / 100;
if (newStartDate != startDate) {
let deleteIndicies = [];
for (trace in plot.data) {
const startIndex = plot.data[trace].x.indexOf(newStartDate);
if (trace > 1) {
deleteIndicies.push(parseInt(trace));
}
if (trace < 2) {
// data[trace] = sortByDate(data[trace]);
const colors = [
'#1f77b4', // muted blue
'#ff7f0e', // safety orange]
'grey'
]
plot.data[trace].y = normalize2(plot.data[trace].y, startIndex);
let startDate = moment(plot.data[trace].x[startIndex], "YYYY-MM-DD");
let endDate = moment(plot.data[trace].x[plot.data[trace].x.length-1], "YYYY-MM-DD");
let durationInDays = moment.duration(endDate.diff(startDate)).as("days") + 1;
let startVal = plot.data[trace].y[startIndex];
let endVal = plot.data[trace].y[plot.data[trace].y.length-1];
let CAGR = (endVal / startVal) ** (1 / (durationInDays/365));
let CAGRtrace = makeCAGRtrace(CAGR, false, "left", startIndex);
CAGRtrace.line = {};
CAGRtrace.line.color = colors[trace];
CAGRtrace.line.dash = "dot";
CAGRtrace.line.width = 1;
CAGRtrace.textfont = {color: colors[trace]}
Plotly.addTraces(plot, CAGRtrace);
}
}
Plotly.deleteTraces(plot, deleteIndicies);
// addCAGRs();
}
const colorInd = plot.data.length == 4? (plot.data.length/2) % 10 : ((plot.data.length-2)/2) % 10;
let model = makeModel(newStartDate, rebalancePeriod, sharesPart, plot.data, true);
let activeModel = model;
modelTrace.x = model.x;
modelTrace.y = model.y;
modelTrace.type = "scatter";
modelTrace.line = {};
modelTrace.line.color = colors[colorInd];
modelTrace.name = "Портфель А:" + sharesPart.toString() + " О:" + ((100-sharesPart*100)/100).toString() + " Р:" + rebalancePeriod.toString();
Plotly.addTraces(plot, modelTrace);
Plotly.addTraces(plot, {x: activeModel.rebalanceX, y: activeModel.rebalanceY, type: 'scatter', mode: 'markers', showlegend: false, hoverinfo: "skip",
marker: {
color: "white",
size: 9,
symbol: "circle",
opacity: 1,
line: {
width: 2,
color: colors[colorInd]
}
}});
// Plotly.addTraces(plot, rebalances);
// rebalances = [];
startDate = newStartDate;
// Plotly.relayout(plot, {showlegend: true, legend: {"orientation": "h", x: 0.5, y: -0.1}})
return false;
}
// let rebalances = [];
const colors = [
'#1f77b4', // muted blue
'#ff7f0e', // safety orange
'#2ca02c', // cooked asparagus green
'#d62728', // brick red
'#9467bd', // muted purple
'#8c564b', // chestnut brown
'#e377c2', // raspberry yogurt pink
'#7f7f7f', // middle gray
'#bcbd22', // curry yellow-green
'#17becf' // blue-teal
];
// plot.on('plotly_click', function(data){
// let pts = '';
// for(let i=0; i < data.points.length; i++){
// annotate_text = 'x = '+data.points[i].x +
// 'y = '+data.points[i].y.toPrecision(4);
// annotation = {
// text: annotate_text,
// x: data.points[i].x,
// y: parseFloat(data.points[i].y.toPrecision(4))
// }
// annotations = [];
// annotations.push(annotation);
// Plotly.relayout(plot,{annotations: annotations})
// }
// });