-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvaluation.html
More file actions
285 lines (254 loc) · 12 KB
/
Copy pathvaluation.html
File metadata and controls
285 lines (254 loc) · 12 KB
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
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<head>
<title>Định giá VNINDEX</title>
</head>
<link rel="stylesheet" type="text/css" href="loading.css">
<style>
body {
margin: auto;
display: flex;
}
/* Responsive cho mobile/tablet */
@media (max-width: 768px) {
body {
display:block;
}
}
</style>
<script src="library/highcharts/highcharts.js"></script>
<script src="library/highcharts/highcharts-more.js"></script>
<script src="library/highcharts/exporting.js"></script>
<script src="library/highcharts/export-data.js"></script>
<script src="library/highcharts/full-screen.js"></script>
<body>
<div id="container" style="width: 100%; height: 550px;"></div>
<div id="container2" style="width: 100%; height: 550px;"></div>
<script>
function convertDateFormat(dateStr) {
const [month, day, year] = dateStr.split('/');
return `${day.padStart(2, '0')}/${month.padStart(2, '0')}/${year}`;
}
function drawchart(data, type, id) {
// Chuẩn bị dữ liệu cho biểu đồ
const vnindexData = data.map(item => [new Date(item.Date).getTime(), item.VNINDEX]);
let PlusRangeData, MinusRangeData;
if (type == "P/B") {
PlusRangeData = data.map(item => [new Date(item.Date).getTime(), item.PBplus1std, item.PBplus2std]);
MinusRangeData = data.map(item => [new Date(item.Date).getTime(), item.PBminus2std, item.PBminus1std]);
} else {
PlusRangeData = data.map(item => [new Date(item.Date).getTime(), item.PEplus1std, item.PEplus2std]);
MinusRangeData = data.map(item => [new Date(item.Date).getTime(), item.PEminus2std, item.PEminus1std]);
}
// Tạo biểu đồ Highcharts
Highcharts.chart(id, {
chart: {
zoomType: 'x' // Cho phép zoom trục X
},
title: {
text: ' VNINDEX và định giá ' + type
},
subtitle: {
text: 'Updated: ' + convertDateFormat(data[data.length - 1].Date),
align: 'center', // Căn giữa
style: {
fontSize: '12px', // Kích thước chữ
color: '#666666' // Màu chữ (xám nhạt)
}
},
xAxis: {
type: 'datetime',
title: {
text: ''
},
crosshair: {
enabled: true, // Bật crosshair
color: '#666666', // Màu của đường kẻ dọc (xám nhạt, bạn có thể đổi)
width: 1, // Độ dày của đường kẻ
dashStyle: 'Solid' // Kiểu đường (Solid, Dash, Dot, v.v.)
}
},
yAxis: {
title: {
text: ''
}
},
legend: {
enabled: true
},
// Ẩn credit
credits: {
enabled: false
},
tooltip: {
shared: true,
valueDecimals: 2, // Hiển thị 2 chữ số thập phân cho giá trị
formatter: function() {
// Lấy thời gian từ trục X và định dạng thành ngày/tháng/năm
const date = Highcharts.dateFormat('%d/%m/%Y', this.x);
let s = `<b>${date}</b><br/>`; // Hiển thị ngày tháng năm in đậm
// Lặp qua các điểm dữ liệu để hiển thị giá trị
this.points.forEach(point => {
if (point.series.type === 'line') {
s += `${point.series.name}: ${point.y.toFixed(2)}<br/>`;
} else if (point.series.type === 'arearange') {
s += `${point.series.name}: <b>${point.low.toFixed(2)} - ${point.high.toFixed(2)}</b><br/>`;
}
});
return s;
}
},
plotOptions: {
arearange: {
fillOpacity: 0.3,
lineWidth: 1,
marker: {
enabled: false
}
},
line: {
lineWidth: 1,
marker: {
enabled: true
}
}
},
// Thêm cấu hình exporting
exporting: {
enabled: true, // Bật tính năng xuất dữ liệu
buttons: {
contextButton: {
menuItems: [
'viewFullscreen', // Xem toàn màn hình
'separator', // Dòng phân cách
'downloadPNG', // Tải xuống PNG
'downloadJPEG', // Tải xuống JPEG
'downloadPDF', // Tải xuống PDF
'separator', // Dòng phân cách
'downloadCSV', // Tải xuống CSV
'downloadXLS', // Tải xuống XLS
]
}
}
},
series: [{
type: 'spline',
name: 'VNINDEX',
data: vnindexData,
color: 'blue' // Màu đen cho VNINDEX
},
{
type: 'arearange',
name: 'Vùng đắt theo ' + type,
data: PlusRangeData,
color: '#FF0000', // Màu đỏ cho dải PBplus
fillOpacity: 0.3
},
{
type: 'arearange',
name: 'Vùng rẻ theo ' + type,
data: MinusRangeData,
color: '#00FF00', // Màu xanh lá cho dải PBminus
fillOpacity: 0.3
}
]
});
}
function formatCustomDate(isoString) {
const date = new Date(isoString);
const day = date.getUTCDate();
const month = date.getUTCMonth() + 1; // Tháng bắt đầu từ 0
const year = date.getUTCFullYear();
return `${month}/${day}/${year}`;
}
async function fetchChartAndIndicatorData() {
const toDate = new Date().toISOString().split('T')[0]; // 'YYYY-MM-DD'
const vnindexURL = `https://lite-api.fiintrade.vn/technical/TradingView/GetStockChartData?Code=VNINDEX&DerivativeCode=&Frequency=Daily&From=2012-01-01T00%3A00%3A00.000Z&To=${toDate}T00%3A00%3A00.000Z&Type=Index`;
const peURL = `https://lite-api.fiintrade.vn/technical/TradingView/GetIndicators?OrganCode=VNINDEX&Code=PE&Frequency=Daily&From=2012-01-01T00%3A00%3A00.000Z&To=${toDate}T00%3A00%3A00.000Z&Type=Index`;
const pbURL = `https://lite-api.fiintrade.vn/technical/TradingView/GetIndicators?OrganCode=VNINDEX&Code=PB&Frequency=Daily&From=2012-01-01T00%3A00%3A00.000Z&To=${toDate}T00%3A00%3A00.000Z&Type=Index`;
try {
const [vnindexRes, peRes, pbRes] = await Promise.all([
fetch(vnindexURL),
fetch(peURL),
fetch(pbURL),
]);
if (!vnindexRes.ok || !peRes.ok || !pbRes.ok) {
throw new Error('Không thể lấy dữ liệu từ một hoặc cả ba API');
}
const [Vnindex, PE, PB] = await Promise.all([
vnindexRes.json(),
peRes.json(),
pbRes.json(),
]);
// tính toán
// sửa ngày vnindẽ
const VNINDEX = Vnindex.data.map(item => {
const originalDate = new Date(item.tradingDate);
originalDate.setUTCDate(originalDate.getUTCDate() + 1); // dịch sang ngày kế tiếp
return {
...item,
tradingDate: originalDate.toISOString()
};
});
function mergeVnindexPEPB(vni, pe, pb) {
// Tạo bản đồ tra cứu cho PE và PB theo ngày
const mapPE = pe.reduce((acc, item) => {
const date = new Date(item.tradingDate).toISOString().split('T')[0];
acc[date] = item.value;
return acc;
}, {});
const mapPB = pb.reduce((acc, item) => {
const date = new Date(item.tradingDate).toISOString().split('T')[0];
acc[date] = item.value;
return acc;
}, {});
// Gộp dữ liệu từ vnindex
const jsondata = vni.map(item => {
const date = new Date(item.tradingDate).toISOString().split('T')[0];
return {
Date: formatCustomDate(date),
VNINDEX: item.closePrice,
PE: mapPE[date] ?? null,
PB: mapPB[date] ?? null
};
});
return jsondata;
}
var jsonData = mergeVnindexPEPB(VNINDEX, PE.data, PB.data);
// tính trung bình
var peCal = calculateStats(jsonData.map(d => d.PE))
var pbCal = calculateStats(jsonData.map(d => d.PB))
for (var i = 0; i < jsonData.length; i++) {
jsonData[i].PEplus1std = peCal.meanPlusStd / jsonData[i].PE * jsonData[i].VNINDEX
jsonData[i].PEplus2std = peCal.meanPlus2Std / jsonData[i].PE * jsonData[i].VNINDEX
jsonData[i].PEminus1std = peCal.meanMinusStd / jsonData[i].PE * jsonData[i].VNINDEX
jsonData[i].PEminus2std = peCal.meanMinus2Std / jsonData[i].PE * jsonData[i].VNINDEX
jsonData[i].PBplus1std = pbCal.meanPlusStd / jsonData[i].PB * jsonData[i].VNINDEX
jsonData[i].PBplus2std = pbCal.meanPlus2Std / jsonData[i].PB * jsonData[i].VNINDEX
jsonData[i].PBminus1std = pbCal.meanMinusStd / jsonData[i].PB * jsonData[i].VNINDEX
jsonData[i].PBminus2std = pbCal.meanMinus2Std / jsonData[i].PB * jsonData[i].VNINDEX
}
//console.log('Data:', jsonData);
drawchart(jsonData, "P/E", "container")
drawchart(jsonData, "P/B", "container2")
} catch (error) {
console.error('Lỗi khi gọi API:', error);
}
}
fetchChartAndIndicatorData();
// hàm tính mean và STD
function calculateStats(arr) {
const mean = arr.reduce((sum, val) => sum + val, 0) / arr.length;
const variance = arr.reduce((sum, val) => sum + Math.pow(val - mean, 2), 0) / arr.length;
const stdDev = Math.sqrt(variance);
return {
mean: mean,
meanPlusStd: mean + stdDev,
meanMinusStd: mean - stdDev,
meanPlus2Std: mean + 2 * stdDev,
meanMinus2Std: mean - 2 * stdDev
};
}
</script>
</html>