在币安数据中从 json 开仓、平仓
Get open, close from json in Binance Data
如何打开一个json的baseAssetVolume。我正在使用 Binance NodeJs 包,我想查询 open
和其他值。
这是站点文档中用于获取完整数据的代码
const api = require('binance');
const binanceWS = new api.BinanceWS(true);
const streams = binanceWS.streams;
binanceWS.onCombinedStream(
[
streams.ticker('BNBBTC')
],
streamEvent => {
switch (streamEvent.stream) {
case streams.ticker('BNBBTC'):
console.log(
'Ticker event, update market stats\n',
streamEvent.data
);
break;
}
}
);
这是返回的数据。
{
eventType: '24hrTicker',
eventTime: 1619369591449,
symbol: 'BNBBTC',
priceChange: '0.00014790',
priceChangePercent: '1.474',
weightedAveragePrice: '0.01006976',
previousClose: '0.01003790',
currentClose: '0.01018500',
closeQuantity: '40.36000000',
bestBid: '0.01018460',
bestBidQuantity: '7.55000000',
bestAskPrice: '0.01018500',
bestAskQuantity: '58.91000000',
open: '0.01003710',
high: '0.01026090',
low: '0.00977950',
baseAssetVolume: '401687.43000000',
quoteAssetVolume: '4044.89583374',
openTime: 1619283191026,
closeTime: 1619369591026,
firstTradeId: 136462988,
lastTradeId: 136653082,
trades: 190095
}
在node.js中,可以使用let
声明变量,使用const
声明常量。您可以使用点以及数组键访问对象的属性。
switch (streamEvent.stream) {
case streams.ticker('BNBBTC'):
// declaring constant, accessing the `open` property with a dot
const open = streamEvent.data.open;
// declaring variable, accessing the property as it was an array key
let baseAssetVolume = streamEvent.data['baseAssetVolume'];
break;
}
您也可能 运行 涉及范围界定问题。在 Javascript 中,变量仅在创建它们的块(在您的情况下 streamEvent => {
)和嵌套块中可见。
如果要访问更高作用域中的值,需要在更高作用域中定义变量。
const api = require('binance');
const binanceWS = new api.BinanceWS(true);
const streams = binanceWS.streams;
let open; // declaring the variable here
binanceWS.onCombinedStream(
[
streams.ticker('BNBBTC')
],
streamEvent => {
switch (streamEvent.stream) {
case streams.ticker('BNBBTC'):
open = streamEvent.data.open; // assigning the value here
break;
}
}
);
请注意,onCombinedStream
事件处理程序可能会被执行多次,并且 open = streamEvent.data.open
将在每次执行时分配一个新值。
为防止这种情况发生,您可以检查该值是否已经分配(默认值为null
)。
/*
* Three equal signs for strict comparison.
* Not necessary in this case, but it's a good practice.
*/
if (open === null) {
open = streamEvent.data.open; // assigning the value here
}
代替之前的
open = streamEvent.data.open; // assigning the value here
如何打开一个json的baseAssetVolume。我正在使用 Binance NodeJs 包,我想查询 open
和其他值。
这是站点文档中用于获取完整数据的代码
const api = require('binance');
const binanceWS = new api.BinanceWS(true);
const streams = binanceWS.streams;
binanceWS.onCombinedStream(
[
streams.ticker('BNBBTC')
],
streamEvent => {
switch (streamEvent.stream) {
case streams.ticker('BNBBTC'):
console.log(
'Ticker event, update market stats\n',
streamEvent.data
);
break;
}
}
);
这是返回的数据。
{
eventType: '24hrTicker',
eventTime: 1619369591449,
symbol: 'BNBBTC',
priceChange: '0.00014790',
priceChangePercent: '1.474',
weightedAveragePrice: '0.01006976',
previousClose: '0.01003790',
currentClose: '0.01018500',
closeQuantity: '40.36000000',
bestBid: '0.01018460',
bestBidQuantity: '7.55000000',
bestAskPrice: '0.01018500',
bestAskQuantity: '58.91000000',
open: '0.01003710',
high: '0.01026090',
low: '0.00977950',
baseAssetVolume: '401687.43000000',
quoteAssetVolume: '4044.89583374',
openTime: 1619283191026,
closeTime: 1619369591026,
firstTradeId: 136462988,
lastTradeId: 136653082,
trades: 190095
}
在node.js中,可以使用let
声明变量,使用const
声明常量。您可以使用点以及数组键访问对象的属性。
switch (streamEvent.stream) {
case streams.ticker('BNBBTC'):
// declaring constant, accessing the `open` property with a dot
const open = streamEvent.data.open;
// declaring variable, accessing the property as it was an array key
let baseAssetVolume = streamEvent.data['baseAssetVolume'];
break;
}
您也可能 运行 涉及范围界定问题。在 Javascript 中,变量仅在创建它们的块(在您的情况下 streamEvent => {
)和嵌套块中可见。
如果要访问更高作用域中的值,需要在更高作用域中定义变量。
const api = require('binance');
const binanceWS = new api.BinanceWS(true);
const streams = binanceWS.streams;
let open; // declaring the variable here
binanceWS.onCombinedStream(
[
streams.ticker('BNBBTC')
],
streamEvent => {
switch (streamEvent.stream) {
case streams.ticker('BNBBTC'):
open = streamEvent.data.open; // assigning the value here
break;
}
}
);
请注意,onCombinedStream
事件处理程序可能会被执行多次,并且 open = streamEvent.data.open
将在每次执行时分配一个新值。
为防止这种情况发生,您可以检查该值是否已经分配(默认值为null
)。
/*
* Three equal signs for strict comparison.
* Not necessary in this case, but it's a good practice.
*/
if (open === null) {
open = streamEvent.data.open; // assigning the value here
}
代替之前的
open = streamEvent.data.open; // assigning the value here