Administrator
2025-09-02 3c7c28b5e39c59e2bbaf7641e55f39953ae22c8c
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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OKX 标记价格K线频道</title>
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/chartjs-adapter-date-fns"></script>
    <script src="https://cdn.jsdelivr.net/npm/chartjs-chart-financial"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .channel {
            margin-bottom: 20px;
            padding: 15px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        .channel h3 {
            margin-top: 0;
        }
        .status {
            font-weight: bold;
        }
        .connected {
            color: green;
        }
        .disconnected {
            color: red;
        }
        .messages {
            height: 300px;
            overflow-y: scroll;
            border: 1px solid #eee;
            padding: 10px;
            margin-top: 10px;
            background-color: #f9f9f9;
        }
        .chart-container {
            width: 100%;
            height: 300px;
            margin-top: 20px;
        }
        .mode-toggle {
            margin: 10px 0;
            padding: 10px;
            background-color: #f0f0f0;
            border-radius: 5px;
        }
        .mode-toggle button {
            margin: 0 5px;
            padding: 5px 10px;
            border: 1px solid #ccc;
            background-color: #fff;
            cursor: pointer;
        }
        .mode-toggle button.active {
            background-color: #007bff;
            color: white;
        }
    </style>
</head>
<body>
    <h1>OKX 标记价格K线频道</h1>
    
    <div class="mode-toggle">
        <span>当前模式:</span>
        <button id="demoModeBtn" class="active" onclick="switchMode('demo')">模拟盘 (wspap)</button>
        <button id="liveModeBtn" onclick="switchMode('live')">实盘 (ws)</button>
    </div>
    
    <div class="channel" id="markPriceCandleChannel">
        <h3>标记价格K线频道</h3>
        <p>URL: <span id="markPriceCandleUrl">wss://wspap.okx.com:8443/ws/v5/business</span></p>
        <p>状态: <span class="status disconnected" id="markPriceCandleStatus">未连接</span></p>
        <button onclick="toggleConnection()">连接/断开</button>
        <button onclick="clearMessages()">清除消息</button>
        <div class="chart-container">
            <canvas id="markPriceCandleChart"></canvas>
        </div>
        <div class="messages" id="markPriceCandleMessages"></div>
    </div>
 
    <script>
        // 存储WebSocket连接对象
        let connection = null;
        
        // 存储各模式的URL
        const urls = {
            demo: 'wss://wspap.okx.com:8443/ws/v5/business',
            live: 'wss://ws.okx.com:8443/ws/v5/business'
        };
        
        // 当前模式,默认为模拟盘
        let currentMode = 'demo';
        
        // 标记价格K线数据
        const markPriceCandleData = [];
        const markPriceCandleTimes = [];
        let markPriceCandleChart = null;
        
        // 初始化图表
        function initChart() {
            const ctx = document.getElementById('markPriceCandleChart').getContext('2d');
            markPriceCandleChart = new Chart(ctx, {
                type: 'line',
                data: {
                    labels: [],
                    datasets: [{
                        label: 'ETH-USDT Mark Price',
                        data: [],
                        borderColor: 'rgb(75, 192, 192)',
                        tension: 0.1
                    }]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    scales: {
                        x: {
                            type: 'time',
                            time: {
                                unit: 'minute'
                            }
                        },
                        y: {
                            beginAtZero: false
                        }
                    }
                }
            });
        }
        
        // 更新图表
        function updateChart() {
            if (markPriceCandleChart) {
                // 为折线图准备数据
                const times = markPriceCandleData.map(item => item.x);
                const prices = markPriceCandleData.map(item => item.c); // 使用收盘价
                
                markPriceCandleChart.data.labels = times;
                markPriceCandleChart.data.datasets[0].data = prices;
                markPriceCandleChart.update();
            }
        }
        
        // 连接/断开WebSocket连接
        function toggleConnection() {
            if (connection) {
                // 如果已经连接,则断开连接
                connection.close();
                connection = null;
                document.getElementById('markPriceCandleStatus').textContent = '未连接';
                document.getElementById('markPriceCandleStatus').className = 'status disconnected';
            } else {
                // 如果未连接,则建立连接
                connectToChannel();
            }
        }
        
        // 清除消息
        function clearMessages() {
            const messagesDiv = document.getElementById('markPriceCandleMessages');
            messagesDiv.innerHTML = '';
            
            // 清除图表数据
            markPriceCandleData.length = 0;
            updateChart();
        }
        
        // 切换模式
        function switchMode(mode) {
            // 更新当前模式
            currentMode = mode;
            
            // 更新按钮状态
            document.getElementById('demoModeBtn').classList.toggle('active', mode === 'demo');
            document.getElementById('liveModeBtn').classList.toggle('active', mode === 'live');
            
            // 更新URL显示
            document.getElementById('markPriceCandleUrl').textContent = urls[mode];
        }
        
        // 连接到频道
        function connectToChannel() {
            const ws = new WebSocket(urls[currentMode]);
            
            ws.onopen = function() {
                console.log('标记价格K线频道连接成功');
                document.getElementById('markPriceCandleStatus').textContent = '已连接';
                document.getElementById('markPriceCandleStatus').className = 'status connected';
                
                // 订阅标记价格K线数据 (BTC-USD-190628, 1分钟K线)
                const subscribeMsg = {
                    op: 'subscribe',
                    args: [
                        {
                            channel: 'mark-price-candle1m',
                            instId: 'ETH-USDT'
                        }
                    ]
                };
                ws.send(JSON.stringify(subscribeMsg));
                
                // 初始化图表
                if (!markPriceCandleChart) {
                    initChart();
                }
            };
            
            ws.onmessage = function(event) {
                const message = event.data;
                console.log('标记价格K线频道收到消息:', message);
                
                // 在页面上显示消息
                const messagesDiv = document.getElementById('markPriceCandleMessages');
                const messageElement = document.createElement('div');
                
                // 尝试解析JSON消息并格式化显示
                try {
                    const jsonData = JSON.parse(message);
                    
                    // 检查是否为标记价格K线数据
                    if (jsonData.arg && jsonData.arg.channel && jsonData.arg.channel.startsWith('mark-price-candle') && 
                        jsonData.arg.instId === 'ETH-USDT' && jsonData.data && jsonData.data.length > 0) {
                        // 处理标记价格K线数据
                        // 标记价格K线数据格式: [ts, o, h, l, c]
                        // ts: 开始时间, o: 开盘价, h: 最高价, l: 最低价, c: 收盘价
                        const candleData = jsonData.data[0];
                        const ts = parseInt(candleData[0]); // 时间戳
                        const o = parseFloat(candleData[1]); // 开盘价
                        const h = parseFloat(candleData[2]); // 最高价
                        const l = parseFloat(candleData[3]); // 最低价
                        const c = parseFloat(candleData[4]); // 收盘价
                        
                        // 添加时间戳到时间数组
                        markPriceCandleTimes.push(ts);
                        
                        // 添加K线数据到数组
                        markPriceCandleData.push({
                            x: ts,
                            o: o,
                            h: h,
                            l: l,
                            c: c
                        });
                        
                        // 限制数据点数量,只保留最近的30个
                        if (markPriceCandleData.length > 30) {
                            markPriceCandleData.shift();
                            markPriceCandleTimes.shift();
                        }
                        
                        // 更新图表
                        updateChart();
                        
                        console.log('标记价格K线数据:', candleData);
                    }
                    
                    messageElement.innerHTML = `<strong>[${new Date().toLocaleTimeString()}]</strong> ${JSON.stringify(jsonData, null, 2)}`;
                } catch (e) {
                    // 如果不是JSON格式,则直接显示
                    messageElement.innerHTML = `<strong>[${new Date().toLocaleTimeString()}]</strong> ${message}`;
                }
                
                messagesDiv.appendChild(messageElement);
                messagesDiv.scrollTop = messagesDiv.scrollHeight;
            };
            
            ws.onerror = function(error) {
                console.error('标记价格K线频道连接错误:', error);
                document.getElementById('markPriceCandleStatus').textContent = '连接错误';
                document.getElementById('markPriceCandleStatus').className = 'status disconnected';
            };
            
            ws.onclose = function() {
                console.log('标记价格K线频道连接已关闭');
                document.getElementById('markPriceCandleStatus').textContent = '连接已关闭';
                document.getElementById('markPriceCandleStatus').className = 'status disconnected';
                connection = null;
            };
            
            connection = ws;
        }
    </script>
</body>
</html>