我如何计算从 Binance Websocket Java 脚本获得的出价和要价
How Can I Do Calculation in Bid And Ask Price which I am getting From Binance Websocket Java Script
我正在获取 Btc 期货报价和报价以及 Btc 现货报价和报价的实时价格我想从 Btc 期货报价中减去 Btc 现货报价,即价差。我也将它们转换为 Float 但在控制台中我得到 NaN
let btcspotws = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@bookTicker');
let btcspotbidElement = document.getElementById('btcspot-bid');
let btcspotaskElement = document.getElementById('btcspot-ask');
var btcspotask
var btcfut1ask
btcspotws.onmessage = (event) => {
//console.log(event.data);
let btcspotobject = JSON.parse(event.data);
let btcspotbid = parseFloat(btcspotobject.b).toFixed(2);
btcspotbidElement.innerText = btcspotbid;
let btcspotask = parseFloat(btcspotobject.a).toFixed(2);
btcspotaskElement.innerText = btcspotask;
};
let btcfut1ws = new WebSocket('wss://dstream.binance.com/stream?streams=btcusd_220325@bookTicker');
let btcfut1bidElement = document.getElementById('btcfut1-bid');
let btcfut1askElement = document.getElementById('btcfut1-ask');
btcfut1ws.onmessage = (event) => {
//console.log(event.data);
let btcfut1object = JSON.parse(event.data);
let btcfut1bid = parseFloat(btcfut1object.data.b).toFixed(2);
btcfut1bidElement.innerText = btcfut1bid;
let btcfut1ask = parseFloat(btcfut1object.data.a).toFixed(2);
btcfut1askElement.innerText = btcfut1ask;
};
console.log(btcspotaskElement - btcfut1bidElement)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Live Crypto Price</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@300&display=swap" rel="stylesheet">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style.css">
</head>
<body>
<table style="width:70%">
<tr>
<th>BTC</th>
<th>Ask</th>
<th>Bid</th>
<th>Spread</th>
</tr>
<tr>
<td><b>BTC/USDT</b></td>
<td id="btcspot-ask">Loading...</td>
<td id="btcspot-bid">Loading...</td>
<td>-</td>
</tr>
<tr>
<td><b>BTC USD 25 Mar</b></td>
<td id="btcfut1-ask">Loading...</td>
<td id="btcfut1-bid">Loading...</td>
<td id="btc-spread">Loading...</td>
</tr>
</table>
</div>
<script src="app.js"></script>
</body>
</html>
你很接近,我对你的代码段做了一些调整:
let btcspotbidElement = document.getElementById('btcspot-bid');
let btcspotaskElement = document.getElementById('btcspot-ask');
let btcfut1bidElement = document.getElementById('btcfut1-bid');
let btcfut1askElement = document.getElementById('btcfut1-ask');
let btcspreadElement = document.getElementById('btc-spread');
let btcspotask;
let btcspotbid;
let btcfut1ask;
let btcfut1bid;
let btcspotws = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@bookTicker');
btcspotws.onmessage = (event) => {
let btcspotobject = JSON.parse(event.data);
btcspotbid = parseFloat(btcspotobject.b).toFixed(2);
btcspotbidElement.innerText = btcspotbid;
btcspotask = parseFloat(btcspotobject.a).toFixed(2);
btcspotaskElement.innerText = btcspotask;
};
let btcfut1ws = new WebSocket('wss://dstream.binance.com/stream?streams=btcusd_220325@bookTicker');
btcfut1ws.onmessage = (event) => {
let btcfut1object = JSON.parse(event.data);
btcfut1bid = parseFloat(btcfut1object.data.b).toFixed(2);
btcfut1bidElement.innerText = btcfut1bid;
btcfut1ask = parseFloat(btcfut1object.data.a).toFixed(2);
btcfut1askElement.innerText = btcfut1ask;
// Here you can display the spread:
btcspreadElement.innerText = parseFloat(btcspotask-btcfut1bid).toFixed(2);
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Live Crypto Price</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@300&display=swap" rel="stylesheet">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style.css">
</head>
<body>
<table style="width:70%">
<tr>
<th>BTC</th>
<th>Ask</th>
<th>Bid</th>
<th>Spread</th>
</tr>
<tr>
<td><b>BTC/USDT</b></td>
<td id="btcspot-ask">Loading...</td>
<td id="btcspot-bid">Loading...</td>
<td>-</td>
</tr>
<tr>
<td><b>BTC USD 25 Mar</b></td>
<td id="btcfut1-ask">Loading...</td>
<td id="btcfut1-bid">Loading...</td>
<td id="btc-spread">Loading...</td>
</tr>
</table>
</div>
<script src="app.js"></script>
</body>
</html>
我正在获取 Btc 期货报价和报价以及 Btc 现货报价和报价的实时价格我想从 Btc 期货报价中减去 Btc 现货报价,即价差。我也将它们转换为 Float 但在控制台中我得到 NaN
let btcspotws = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@bookTicker');
let btcspotbidElement = document.getElementById('btcspot-bid');
let btcspotaskElement = document.getElementById('btcspot-ask');
var btcspotask
var btcfut1ask
btcspotws.onmessage = (event) => {
//console.log(event.data);
let btcspotobject = JSON.parse(event.data);
let btcspotbid = parseFloat(btcspotobject.b).toFixed(2);
btcspotbidElement.innerText = btcspotbid;
let btcspotask = parseFloat(btcspotobject.a).toFixed(2);
btcspotaskElement.innerText = btcspotask;
};
let btcfut1ws = new WebSocket('wss://dstream.binance.com/stream?streams=btcusd_220325@bookTicker');
let btcfut1bidElement = document.getElementById('btcfut1-bid');
let btcfut1askElement = document.getElementById('btcfut1-ask');
btcfut1ws.onmessage = (event) => {
//console.log(event.data);
let btcfut1object = JSON.parse(event.data);
let btcfut1bid = parseFloat(btcfut1object.data.b).toFixed(2);
btcfut1bidElement.innerText = btcfut1bid;
let btcfut1ask = parseFloat(btcfut1object.data.a).toFixed(2);
btcfut1askElement.innerText = btcfut1ask;
};
console.log(btcspotaskElement - btcfut1bidElement)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Live Crypto Price</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@300&display=swap" rel="stylesheet">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style.css">
</head>
<body>
<table style="width:70%">
<tr>
<th>BTC</th>
<th>Ask</th>
<th>Bid</th>
<th>Spread</th>
</tr>
<tr>
<td><b>BTC/USDT</b></td>
<td id="btcspot-ask">Loading...</td>
<td id="btcspot-bid">Loading...</td>
<td>-</td>
</tr>
<tr>
<td><b>BTC USD 25 Mar</b></td>
<td id="btcfut1-ask">Loading...</td>
<td id="btcfut1-bid">Loading...</td>
<td id="btc-spread">Loading...</td>
</tr>
</table>
</div>
<script src="app.js"></script>
</body>
</html>
你很接近,我对你的代码段做了一些调整:
let btcspotbidElement = document.getElementById('btcspot-bid');
let btcspotaskElement = document.getElementById('btcspot-ask');
let btcfut1bidElement = document.getElementById('btcfut1-bid');
let btcfut1askElement = document.getElementById('btcfut1-ask');
let btcspreadElement = document.getElementById('btc-spread');
let btcspotask;
let btcspotbid;
let btcfut1ask;
let btcfut1bid;
let btcspotws = new WebSocket('wss://stream.binance.com:9443/ws/btcusdt@bookTicker');
btcspotws.onmessage = (event) => {
let btcspotobject = JSON.parse(event.data);
btcspotbid = parseFloat(btcspotobject.b).toFixed(2);
btcspotbidElement.innerText = btcspotbid;
btcspotask = parseFloat(btcspotobject.a).toFixed(2);
btcspotaskElement.innerText = btcspotask;
};
let btcfut1ws = new WebSocket('wss://dstream.binance.com/stream?streams=btcusd_220325@bookTicker');
btcfut1ws.onmessage = (event) => {
let btcfut1object = JSON.parse(event.data);
btcfut1bid = parseFloat(btcfut1object.data.b).toFixed(2);
btcfut1bidElement.innerText = btcfut1bid;
btcfut1ask = parseFloat(btcfut1object.data.a).toFixed(2);
btcfut1askElement.innerText = btcfut1ask;
// Here you can display the spread:
btcspreadElement.innerText = parseFloat(btcspotask-btcfut1bid).toFixed(2);
};
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Live Crypto Price</title>
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans:wght@300&display=swap" rel="stylesheet">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<link rel="stylesheet" href="style.css">
</head>
<body>
<table style="width:70%">
<tr>
<th>BTC</th>
<th>Ask</th>
<th>Bid</th>
<th>Spread</th>
</tr>
<tr>
<td><b>BTC/USDT</b></td>
<td id="btcspot-ask">Loading...</td>
<td id="btcspot-bid">Loading...</td>
<td>-</td>
</tr>
<tr>
<td><b>BTC USD 25 Mar</b></td>
<td id="btcfut1-ask">Loading...</td>
<td id="btcfut1-bid">Loading...</td>
<td id="btc-spread">Loading...</td>
</tr>
</table>
</div>
<script src="app.js"></script>
</body>
</html>