File size: 48,007 Bytes
85653bc | 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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 | <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Kronos Financial Prediction Web UI</title>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
color: #333;
}
.container {
max-width: 1400px;
margin: 0 auto;
padding: 20px;
}
.header {
text-align: center;
margin-bottom: 30px;
color: white;
}
.header h1 {
font-size: 2.5rem;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.header p {
font-size: 1.1rem;
opacity: 0.9;
}
.main-content {
display: grid;
grid-template-columns: 1fr 2fr;
gap: 30px;
margin-bottom: 30px;
}
.control-panel {
background: white;
border-radius: 15px;
padding: 25px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
height: fit-content;
}
.control-panel h2 {
color: #4a5568;
margin-bottom: 20px;
font-size: 1.5rem;
border-bottom: 2px solid #e2e8f0;
padding-bottom: 10px;
}
.form-group {
margin-bottom: 20px;
}
.form-group label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #4a5568;
}
.form-group select,
.form-group input {
width: 100%;
padding: 12px;
border: 2px solid #e2e8f0;
border-radius: 8px;
font-size: 14px;
transition: border-color 0.3s ease;
}
.form-group select:focus,
.form-group input:focus {
outline: none;
border-color: #667eea;
}
/* Prediction quality parameter styles */
.form-group input[type="range"] {
width: 70%;
margin-right: 10px;
}
.form-group input[type="number"] {
width: 100%;
}
.form-group span {
display: inline-block;
min-width: 40px;
font-weight: 600;
color: #667eea;
}
.form-text {
font-size: 12px;
color: #718096;
margin-top: 5px;
}
.btn {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border: none;
padding: 12px 24px;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s ease, box-shadow 0.2s ease;
width: 100%;
margin-bottom: 10px;
}
.btn:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
.btn:disabled {
opacity: 0.6;
cursor: not-allowed;
transform: none;
}
.btn-secondary {
background: linear-gradient(135deg, #718096 0%, #4a5568 100%);
}
.btn-success {
background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
}
.btn-warning {
background: linear-gradient(135deg, #ed8936 0%, #dd6b20 100%);
}
.status {
padding: 15px;
border-radius: 8px;
margin-bottom: 20px;
font-weight: 500;
}
.status.success {
background: #c6f6d5;
color: #22543d;
border: 1px solid #9ae6b4;
}
.status.error {
background: #fed7d7;
color: #742a2a;
border: 1px solid #feb2b2;
}
.status.info {
background: #bee3f8;
color: #2a4365;
border: 1px solid #90cdf4;
}
.status.warning {
background: #fef5e7;
color: #744210;
border: 1px solid #fbd38d;
}
.chart-container {
background: white;
border-radius: 15px;
padding: 25px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.chart-container h2 {
color: #4a5568;
margin-bottom: 20px;
font-size: 1.5rem;
border-bottom: 2px solid #e2e8f0;
padding-bottom: 10px;
}
#chart {
width: 100%;
height: 600px;
}
.data-info {
background: #f7fafc;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 15px;
margin-bottom: 20px;
}
.data-info h3 {
color: #4a5568;
margin-bottom: 10px;
font-size: 1.1rem;
}
.data-info p {
margin-bottom: 5px;
color: #4a5568;
}
.data-info strong {
color: #2d3748;
}
/* Time window selector styles */
.time-window-container {
background: #f7fafc;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 20px;
margin-bottom: 20px;
}
.time-window-container h3 {
color: #4a5568;
margin-bottom: 15px;
font-size: 1.1rem;
}
.time-window-info {
display: flex;
justify-content: space-between;
margin-bottom: 15px;
font-size: 12px;
color: #666;
}
.time-window-slider {
position: relative;
margin-bottom: 10px;
}
.slider-track {
position: relative;
height: 6px;
background: #e2e8f0;
border-radius: 3px;
cursor: pointer;
}
.slider-handle {
position: absolute;
top: -7px;
width: 20px;
height: 20px;
background: #667eea;
border-radius: 50%;
cursor: grab;
border: 2px solid white;
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
z-index: 10;
}
.slider-handle:hover {
background: #5a67d8;
transform: scale(1.1);
}
.slider-handle:active {
cursor: grabbing;
}
.slider-selection {
position: absolute;
height: 6px;
background: #48bb78;
border-radius: 3px;
top: 0;
}
.slider-labels {
display: flex;
justify-content: space-between;
font-size: 11px;
color: #999;
margin-top: 5px;
}
/* Comparison analysis styles */
.comparison-section {
background: #f7fafc;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 20px;
margin-top: 20px;
}
.comparison-section h3 {
color: #4a5568;
margin-bottom: 15px;
font-size: 1.1rem;
}
.comparison-info {
background: white;
border: 1px solid #e2e8f0;
border-radius: 6px;
padding: 15px;
margin-bottom: 15px;
}
.comparison-table {
width: 100%;
border-collapse: collapse;
margin-top: 15px;
}
.comparison-table th,
.comparison-table td {
border: 1px solid #e2e8f0;
padding: 8px;
text-align: center;
font-size: 12px;
}
.comparison-table th {
background: #f7fafc;
font-weight: 600;
}
.error-stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
margin-top: 15px;
}
.error-stat {
background: white;
border: 1px solid #e2e8f0;
border-radius: 6px;
padding: 15px;
text-align: center;
}
.error-stat h4 {
color: #4a5568;
margin-bottom: 5px;
font-size: 0.9rem;
}
.error-stat .value {
font-size: 1.5rem;
font-weight: 600;
color: #667eea;
}
.error-stat .unit {
font-size: 0.8rem;
color: #718096;
}
.loading {
display: none;
text-align: center;
padding: 20px;
}
.loading.show {
display: block;
}
.spinner {
border: 4px solid #f3f3f3;
border-top: 4px solid #667eea;
border-radius: 50%;
width: 40px;
height: 40px;
animation: spin 1s linear infinite;
margin: 0 auto 10px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.model-info {
background: #e6fffa;
border: 1px solid #81e6d9;
border-radius: 8px;
padding: 15px;
margin-bottom: 20px;
}
.model-info h3 {
color: #234e52;
margin-bottom: 10px;
font-size: 1.1rem;
}
.model-info p {
margin-bottom: 5px;
color: #234e52;
}
.model-info strong {
color: #0f2027;
}
@media (max-width: 768px) {
.main-content {
grid-template-columns: 1fr;
}
.container {
padding: 10px;
}
.header h1 {
font-size: 2rem;
}
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🚀 Kronos Financial Prediction Web UI</h1>
<p>AI-based financial K-line data prediction analysis platform</p>
</div>
<div class="main-content">
<div class="control-panel">
<h2>🎯 Control Panel</h2>
<!-- Model Selection -->
<div class="form-group">
<label for="model-select">Select Model:</label>
<select id="model-select">
<option value="">Please load available models first</option>
</select>
<small class="form-text">Select the Kronos model to use</small>
</div>
<!-- Device Selection -->
<div class="form-group">
<label for="device-select">Select Device:</label>
<select id="device-select">
<option value="cpu">CPU</option>
<option value="cuda">CUDA (NVIDIA GPU)</option>
<option value="mps">MPS (Apple Silicon)</option>
</select>
<small class="form-text">Select the device to run the model on</small>
</div>
<!-- Model Status -->
<div id="model-status" class="status info" style="display: none;">
Model status information
</div>
<!-- Load Model Button -->
<button id="load-model-btn" class="btn btn-secondary">
🔄 Load Model
</button>
<hr style="margin: 20px 0; border: 1px solid #e2e8f0;">
<!-- Binance Data Selection -->
<div class="form-group">
<label for="symbol-select">选择交易对:</label>
<select id="symbol-select">
<option value="">请先加载交易对列表</option>
</select>
<small class="form-text">选择要分析的加密货币交易对</small>
</div>
<div class="form-group">
<label for="timeframe-select">选择时间周期:</label>
<select id="timeframe-select">
<option value="">请先加载时间周期列表</option>
</select>
<small class="form-text">选择K线数据的时间间隔</small>
</div>
<div class="form-group">
<label for="data-limit">数据数量:</label>
<select id="data-limit">
<option value="400">400条</option>
<option value="1000" selected>1000条</option>
<option value="1500">1500条</option>
</select>
<small class="form-text">获取最近的K线数据条数</small>
</div>
<button id="load-data-btn" class="btn btn-secondary">
📈 获取币安数据
</button>
<!-- Data Information Display -->
<div id="data-info" class="data-info" style="display: none;">
<h3>📊 Data Information</h3>
<p><strong>Rows:</strong> <span id="data-rows">-</span></p>
<p><strong>Columns:</strong> <span id="data-cols">-</span></p>
<p><strong>Time Range:</strong> <span id="data-time-range">-</span></p>
<p><strong>Price Range:</strong> <span id="data-price-range">-</span></p>
<p><strong>Time Frequency:</strong> <span id="data-timeframe">-</span></p>
<p><strong>Prediction Columns:</strong> <span id="data-prediction-cols">-</span></p>
</div>
<hr style="margin: 20px 0; border: 1px solid #e2e8f0;">
<!-- Time Window Selection -->
<div class="form-group">
<label for="lookback-input">回看窗口大小:</label>
<input type="number" id="lookback-input" value="400" min="50" max="1000" step="10">
<small class="form-text">用于预测的历史数据点数量(默认400)</small>
</div>
<!-- Prediction Parameters -->
<div class="form-group">
<label for="lookback">回看窗口大小:</label>
<input type="number" id="lookback" value="400" readonly>
<small class="form-text">自动从上方输入框同步</small>
</div>
<div class="form-group">
<label for="pred-len">Prediction Length:</label>
<input type="number" id="pred-len" value="120" readonly>
<small class="form-text">Fixed at 120 data points</small>
</div>
<!-- Prediction Quality Parameters -->
<div class="form-group">
<label for="temperature">Prediction Temperature (T):</label>
<input type="range" id="temperature" value="1.0" min="0.1" max="2.0" step="0.1">
<span id="temperature-value">1.0</span>
<small class="form-text">Controls prediction randomness, higher values make predictions more diverse, lower values make predictions more conservative</small>
</div>
<div class="form-group">
<label for="top-p">Nucleus Sampling Parameter (top_p):</label>
<input type="range" id="top-p" value="0.9" min="0.1" max="1.0" step="0.1">
<span id="top-p-value">0.9</span>
<small class="form-text">Controls prediction diversity, higher values consider broader probability distributions</small>
</div>
<div class="form-group">
<label for="sample-count">Sample Count:</label>
<input type="number" id="sample-count" value="1" min="1" max="5" step="1">
<small class="form-text">Generate multiple prediction samples to improve quality (recommended 1-3)</small>
</div>
<button id="predict-btn" class="btn btn-success">
🔮 Start Prediction
</button>
<!-- Loading Status -->
<div id="loading" class="loading">
<div class="spinner"></div>
<p>Processing, please wait...</p>
</div>
</div>
<div class="chart-container">
<h2>📈 Prediction Results Chart</h2>
<div id="chart"></div>
<!-- Comparison Analysis -->
<div id="comparison-section" class="comparison-section" style="display: none;">
<h3>📊 Prediction vs Actual Data Comparison</h3>
<div id="comparison-info" class="comparison-info">
<p><strong>Prediction Type:</strong> <span id="prediction-type">-</span></p>
<p><strong>Comparison Data:</strong> <span id="comparison-data">-</span></p>
</div>
<div class="error-stats">
<div class="error-stat">
<h4>Mean Absolute Error</h4>
<div class="value" id="mae">-</div>
<div class="unit">Price Units</div>
</div>
<div class="error-stat">
<h4>Root Mean Square Error</h4>
<div class="value" id="rmse">-</div>
<div class="unit">Price Units</div>
</div>
<div class="error-stat">
<h4>Mean Absolute Percentage Error</h4>
<div class="value" id="mape">-</div>
<div class="unit">%</div>
</div>
</div>
<div class="error-details">
<h4>Detailed Comparison Data:</h4>
<div style="max-height: 300px; overflow-y: auto;">
<table class="comparison-table">
<thead>
<tr>
<th>Time</th>
<th>Actual Open</th>
<th>Predicted Open</th>
<th>Actual High</th>
<th>Predicted High</th>
<th>Actual Low</th>
<th>Predicted Low</th>
<th>Actual Close</th>
<th>Predicted Close</th>
</tr>
</thead>
<tbody id="comparison-tbody">
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
// Global variables
let currentSymbol = null;
let currentInterval = null;
let currentLimit = null;
let currentDataInfo = null;
let availableModels = [];
let modelLoaded = false;
// Initialize after page loads
document.addEventListener('DOMContentLoaded', function() {
initializeApp();
});
// Initialize application
async function initializeApp() {
console.log('🚀 Initializing Kronos Web UI...');
// Load available models
await loadAvailableModels();
// Load symbols and timeframes
await loadSymbols();
await loadTimeframes();
// Set up event listeners
setupEventListeners();
// Initialize lookback input sync
initializeLookbackSync();
// Initialize button states
updatePredictButtonStatus();
console.log('✅ Application initialization completed');
}
// Load available models
async function loadAvailableModels() {
try {
const response = await axios.get('/api/available-models');
if (response.data.model_available) {
availableModels = response.data.models;
populateModelSelect();
console.log('✅ Available models loaded successfully:', availableModels);
} else {
console.warn('⚠️ Kronos model library not available');
showStatus('warning', 'Kronos model library not available, will use simulated prediction');
}
} catch (error) {
console.error('❌ Failed to load available models:', error);
showStatus('error', 'Failed to load available models');
}
}
// Populate model selection dropdown
function populateModelSelect() {
const modelSelect = document.getElementById('model-select');
modelSelect.innerHTML = '<option value="">Please select model</option>';
Object.entries(availableModels).forEach(([key, model]) => {
const option = document.createElement('option');
option.value = key;
option.textContent = `${model.name} (${model.params}) - ${model.description}`;
modelSelect.appendChild(option);
});
}
// Load model
async function loadModel() {
const modelKey = document.getElementById('model-select').value;
const device = document.getElementById('device-select').value;
if (!modelKey) {
showStatus('error', 'Please select a model to load');
return;
}
try {
showLoading(true);
document.getElementById('load-model-btn').disabled = true;
const response = await axios.post('/api/load-model', {
model_key: modelKey,
device: device
});
if (response.data.success) {
modelLoaded = true;
showStatus('success', response.data.message);
updateModelStatus();
updatePredictButtonStatus();
console.log('✅ Model loaded successfully:', response.data.model_info);
} else {
showStatus('error', response.data.error);
}
} catch (error) {
console.error('❌ Model loading failed:', error);
showStatus('error', `Model loading failed: ${error.response?.data?.error || error.message}`);
} finally {
showLoading(false);
document.getElementById('load-model-btn').disabled = false;
}
}
// Update model status
async function updateModelStatus() {
try {
const response = await axios.get('/api/model-status');
const status = response.data;
if (status.loaded) {
showStatus('success', `Model loaded: ${status.current_model.name} on ${status.current_model.device}`);
} else if (status.available) {
showStatus('info', 'Model available but not loaded');
} else {
showStatus('warning', 'Model library not available');
}
} catch (error) {
console.error('❌ Failed to get model status:', error);
}
}
// Load symbols list
async function loadSymbols() {
try {
const response = await axios.get('/api/symbols');
const symbols = response.data;
const symbolSelect = document.getElementById('symbol-select');
symbolSelect.innerHTML = '<option value="">请选择交易对</option>';
symbols.forEach(symbol => {
const option = document.createElement('option');
option.value = symbol.symbol;
option.textContent = symbol.name;
symbolSelect.appendChild(option);
});
console.log('✅ 交易对列表加载成功:', symbols);
} catch (error) {
console.error('❌ 加载交易对列表失败:', error);
showStatus('error', '加载交易对列表失败');
}
}
// Load timeframes list
async function loadTimeframes() {
try {
const response = await axios.get('/api/timeframes');
const timeframes = response.data;
const timeframeSelect = document.getElementById('timeframe-select');
timeframeSelect.innerHTML = '<option value="">请选择时间周期</option>';
timeframes.forEach(timeframe => {
const option = document.createElement('option');
option.value = timeframe.value;
option.textContent = `${timeframe.label} - ${timeframe.description}`;
timeframeSelect.appendChild(option);
});
// 默认选择1小时
timeframeSelect.value = '1h';
console.log('✅ 时间周期列表加载成功:', timeframes);
} catch (error) {
console.error('❌ 加载时间周期列表失败:', error);
showStatus('error', '加载时间周期列表失败');
}
}
// Load Binance data
async function loadData() {
const symbol = document.getElementById('symbol-select').value;
const interval = document.getElementById('timeframe-select').value;
const limit = parseInt(document.getElementById('data-limit').value);
if (!symbol) {
showStatus('error', '请选择交易对');
return;
}
if (!interval) {
showStatus('error', '请选择时间周期');
return;
}
try {
showLoading(true);
document.getElementById('load-data-btn').disabled = true;
const response = await axios.post('/api/load-data', {
symbol: symbol,
interval: interval,
limit: limit
});
if (response.data.success) {
currentSymbol = symbol;
currentInterval = interval;
currentLimit = limit;
currentDataInfo = response.data.data_info;
// 添加调试信息
console.log('🔍 Data loaded, setting variables:');
console.log(' symbol:', symbol, '-> currentSymbol:', currentSymbol);
console.log(' interval:', interval, '-> currentInterval:', currentInterval);
console.log(' limit:', limit, '-> currentLimit:', currentLimit);
console.log(' data_info exists:', !!response.data.data_info);
showDataInfo(response.data.data_info);
showStatus('success', response.data.message);
// Update prediction button status
updatePredictButtonStatus();
console.log('✅ 币安数据加载成功:', response.data.data_info);
} else {
showStatus('error', response.data.error);
}
} catch (error) {
console.error('❌ 加载币安数据失败:', error);
showStatus('error', `加载币安数据失败: ${error.response?.data?.error || error.message}`);
} finally {
showLoading(false);
document.getElementById('load-data-btn').disabled = false;
}
}
// Update prediction button status
function updatePredictButtonStatus() {
const predictBtn = document.getElementById('predict-btn');
// 添加调试信息
console.log('🔍 Updating predict button status:');
console.log(' modelLoaded:', modelLoaded);
console.log(' currentSymbol:', currentSymbol);
console.log(' currentInterval:', currentInterval);
console.log(' currentLimit:', currentLimit);
// 只要选择了 交易对/周期/数量 且模型已加载,即可预测
if (modelLoaded && currentSymbol && currentInterval && currentLimit) {
predictBtn.textContent = '🔮 Start Prediction';
console.log('✅ Predict button enabled');
} else {
if (!modelLoaded) {
predictBtn.textContent = '🔮 请先加载模型';
console.log('❌ Model not loaded');
} else {
predictBtn.textContent = '🔮 请先选择交易对/周期/数量';
console.log('❌ Missing symbol/interval/limit');
}
}
}
// Display data information
function showDataInfo(dataInfo) {
document.getElementById('data-info').style.display = 'block';
document.getElementById('data-rows').textContent = dataInfo.rows;
document.getElementById('data-cols').textContent = dataInfo.columns.length;
// 格式化时间显示(东八区时间,已经是 yyyy-MM-dd HH:mm:ss 格式)
const timeRange = `${dataInfo.start_date} 至 ${dataInfo.end_date} (北京时间)`;
document.getElementById('data-time-range').textContent = timeRange;
document.getElementById('data-price-range').textContent = `${dataInfo.price_range.min.toFixed(4)} - ${dataInfo.price_range.max.toFixed(4)}`;
document.getElementById('data-timeframe').textContent = dataInfo.timeframe;
document.getElementById('data-prediction-cols').textContent = dataInfo.prediction_columns.join(', ');
// Initialize time window slider
initializeTimeWindowSlider(dataInfo);
}
// Lookback window input sync
// Initialize time window slider
function initializeTimeWindowSlider(dataInfo) {
console.log('🔧 Initializing time window slider with data:', dataInfo);
// For now, just log the data info since the slider functionality is not fully implemented
// This prevents the "function not defined" error
}
// Initialize lookback input sync
function initializeLookbackSync() {
const lookbackInput = document.getElementById('lookback-input');
const lookbackDisplay = document.getElementById('lookback');
if (lookbackInput && lookbackDisplay) {
// 同步输入框值到显示框
lookbackInput.addEventListener('input', function() {
lookbackDisplay.value = this.value;
});
// 初始化同步
lookbackDisplay.value = lookbackInput.value;
console.log('✅ Lookback input sync initialized');
}
}
// Update slider based on input fields
function updateSliderFromInputs() {
if (!sliderData) return;
// Fixed window size: 400 + 120 = 520 data points
const lookback = 400;
const predLen = 120;
const windowSize = lookback + predLen; // Fixed at 520
// Calculate slider position
const totalRows = sliderData.totalRows;
if (windowSize > totalRows) {
// If window size exceeds total data amount, show error
showStatus('error', `Insufficient data, need at least ${windowSize} data points, currently only ${totalRows} available`);
return;
}
// Calculate slider position (default select first half of data)
const startPercentage = 0.1; // Start from 10%
const endPercentage = startPercentage + (windowSize / totalRows);
// Update handle positions
updateStartHandle(startPercentage);
updateEndHandle(endPercentage);
// Update display information
updateSliderFromHandles();
}
// Start prediction
async function startPrediction() {
console.log('🔮 Start Prediction button clicked!');
console.log(' modelLoaded:', modelLoaded);
// 直接从下拉框读取选择,不依赖已加载数据
const symbolSel = document.getElementById('symbol-select').value;
const intervalSel = document.getElementById('timeframe-select').value;
const limitSel = parseInt(document.getElementById('data-limit').value);
// 同步到全局(用于按钮状态/日志)
currentSymbol = symbolSel || currentSymbol;
currentInterval = intervalSel || currentInterval;
currentLimit = limitSel || currentLimit;
console.log(' selectedSymbol:', symbolSel, 'selectedInterval:', intervalSel, 'selectedLimit:', limitSel);
if (!symbolSel || !intervalSel || !limitSel) {
showStatus('error', '请先选择交易对、时间周期和数据数量');
return;
}
if (!modelLoaded) {
console.log('❌ Model not loaded');
showStatus('error', '请先加载模型');
return;
}
try {
showLoading(true);
document.getElementById('predict-btn').disabled = true;
const lookback = parseInt(document.getElementById('lookback-input').value) || 400;
const predLen = parseInt(document.getElementById('pred-len').value);
console.log('🔮 Starting prediction with parameters:');
console.log(' lookback:', lookback);
console.log(' predLen:', predLen);
console.log(' symbol:', symbolSel);
console.log(' interval:', intervalSel);
// Get prediction quality parameters
const temperature = parseFloat(document.getElementById('temperature').value);
const topP = parseFloat(document.getElementById('top-p').value);
const sampleCount = parseInt(document.getElementById('sample-count').value);
let predictionParams = {
symbol: symbolSel,
interval: intervalSel,
limit: limitSel,
lookback: lookback,
pred_len: predLen,
temperature: temperature,
top_p: topP,
sample_count: sampleCount
};
console.log('🚀 Starting prediction, parameters:', predictionParams);
const response = await axios.post('/api/predict', predictionParams);
if (response.data.success) {
// Display prediction results
displayPredictionResult(response.data);
showStatus('success', response.data.message);
} else {
showStatus('error', response.data.error);
}
} catch (error) {
console.error('❌ Prediction failed:', error);
showStatus('error', `Prediction failed: ${error.response?.data?.error || error.message}`);
} finally {
showLoading(false);
document.getElementById('predict-btn').disabled = false;
}
}
// Display prediction results
function displayPredictionResult(result) {
// Display chart
const chartData = JSON.parse(result.chart);
// 解码后端返回的 dtype/bdata 压缩数组,转换为原生数组
const decodedChart = decodePlotlyBdata(chartData);
Plotly.newPlot('chart', decodedChart.data, decodedChart.layout);
// Display comparison analysis (if actual data exists)
if (result.has_comparison) {
displayComparisonAnalysis(result);
} else {
document.getElementById('comparison-section').style.display = 'none';
}
}
// 递归解码 Plotly JSON 中的 {dtype, bdata} 字段为原生数组
function decodePlotlyBdata(node) {
if (Array.isArray(node)) {
return node.map(decodePlotlyBdata);
}
if (node && typeof node === 'object') {
if (Object.prototype.hasOwnProperty.call(node, 'bdata') && Object.prototype.hasOwnProperty.call(node, 'dtype')) {
try {
const bytes = atob(node.bdata);
const buffer = new ArrayBuffer(bytes.length);
const view = new Uint8Array(buffer);
for (let i = 0; i < bytes.length; i++) view[i] = bytes.charCodeAt(i);
let typed;
switch (node.dtype) {
case 'f8':
typed = new Float64Array(buffer);
break;
case 'f4':
typed = new Float32Array(buffer);
break;
case 'i4':
typed = new Int32Array(buffer);
break;
case 'u4':
typed = new Uint32Array(buffer);
break;
case 'i2':
typed = new Int16Array(buffer);
break;
case 'u2':
typed = new Uint16Array(buffer);
break;
case 'i1':
typed = new Int8Array(buffer);
break;
case 'u1':
typed = new Uint8Array(buffer);
break;
default:
typed = new Float32Array(buffer);
}
return Array.from(typed);
} catch (e) {
console.warn('Failed to decode bdata, fallback to original node', e);
return node;
}
}
const out = {};
for (const k in node) {
if (Object.prototype.hasOwnProperty.call(node, k)) {
out[k] = decodePlotlyBdata(node[k]);
}
}
return out;
}
return node;
}
// Display comparison analysis
function displayComparisonAnalysis(result) {
document.getElementById('comparison-section').style.display = 'block';
// Update comparison information
document.getElementById('prediction-type').textContent = result.prediction_type;
document.getElementById('comparison-data').textContent = `${result.actual_data.length} actual data points`;
// Calculate error statistics
const errorStats = getPredictionQuality(result.prediction_results, result.actual_data);
// Display error statistics
document.getElementById('mae').textContent = errorStats.mae.toFixed(4);
document.getElementById('rmse').textContent = errorStats.rmse.toFixed(4);
document.getElementById('mape').textContent = errorStats.mape.toFixed(2);
// Fill comparison table
fillComparisonTable(result.prediction_results, result.actual_data);
}
// Calculate prediction quality metrics
function getPredictionQuality(predictions, actuals) {
if (!predictions || !actuals || predictions.length === 0 || actuals.length === 0) {
return { mae: 0, rmse: 0, mape: 0 };
}
const minLen = Math.min(predictions.length, actuals.length);
let mae = 0, rmse = 0, mape = 0;
for (let i = 0; i < minLen; i++) {
const pred = predictions[i];
const act = actuals[i];
// Use closing price to calculate errors
const error = Math.abs(pred.close - act.close);
const percentError = (error / act.close) * 100;
mae += error;
rmse += error * error;
mape += percentError;
}
mae /= minLen;
rmse = Math.sqrt(rmse / minLen);
mape /= minLen;
return { mae, rmse, mape };
}
// Fill comparison table
function fillComparisonTable(predictions, actuals) {
const tbody = document.getElementById('comparison-tbody');
tbody.innerHTML = '';
const minLen = Math.min(predictions.length, actuals.length);
for (let i = 0; i < minLen; i++) {
const pred = predictions[i];
const act = actuals[i];
const row = document.createElement('tr');
row.innerHTML = `
<td>${new Date(pred.timestamp).toLocaleString()}</td>
<td>${act.open.toFixed(4)}</td>
<td>${pred.open.toFixed(4)}</td>
<td>${act.high.toFixed(4)}</td>
<td>${pred.high.toFixed(4)}</td>
<td>${act.low.toFixed(4)}</td>
<td>${pred.low.toFixed(4)}</td>
<td>${act.close.toFixed(4)}</td>
<td>${pred.close.toFixed(4)}</td>
`;
tbody.appendChild(row);
}
}
// Set up event listeners
function setupEventListeners() {
// Load model button
document.getElementById('load-model-btn').addEventListener('click', loadModel);
// Load data button
document.getElementById('load-data-btn').addEventListener('click', loadData);
// Prediction button
document.getElementById('predict-btn').addEventListener('click', startPrediction);
// 选择变化时,同步全局并更新按钮可用性(无需先获取数据)
document.getElementById('symbol-select').addEventListener('change', function() {
currentSymbol = this.value || null;
updatePredictButtonStatus();
});
document.getElementById('timeframe-select').addEventListener('change', function() {
currentInterval = this.value || null;
updatePredictButtonStatus();
});
document.getElementById('data-limit').addEventListener('change', function() {
const v = parseInt(this.value);
currentLimit = isNaN(v) ? null : v;
updatePredictButtonStatus();
});
// Prediction quality parameter sliders
document.getElementById('temperature').addEventListener('input', function() {
document.getElementById('temperature-value').textContent = this.value;
});
document.getElementById('top-p').addEventListener('input', function() {
document.getElementById('top-p-value').textContent = this.value;
});
// Update slider when lookback window size changes
document.getElementById('lookback').addEventListener('input', updateSliderFromInputs);
document.getElementById('pred-len').addEventListener('input', updateSliderFromInputs);
}
// Display status information
function showStatus(type, message) {
const statusDiv = document.getElementById('model-status');
statusDiv.className = `status ${type}`;
statusDiv.textContent = message;
statusDiv.style.display = 'block';
// Auto-hide
setTimeout(() => {
statusDiv.style.display = 'none';
}, 5000);
}
// Show/hide loading status
function showLoading(show) {
const loadingDiv = document.getElementById('loading');
if (show) {
loadingDiv.classList.add('show');
} else {
loadingDiv.classList.remove('show');
}
}
</script>
</body>
</html>
|