微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用传入的 Binance WebSocket 数据?

如何解决如何使用传入的 Binance WebSocket 数据?

我的问题是一般如何使用 WebSocket 数据。我正在使用这个包 https://www.npmjs.com/package/binance获取币安实时行情。我想要做的是获取实时数据并将其与我使用 axios 调用的其他一些交换数据进行比较。但我得到的是连续的币安数据流,不知道如何使用它,我试图在 stream 中运行函数,但这似乎没有帮助,因为数据流是连续的,而且 axios 请求需要时间返回。如果我的问题不完整,我可以添加更多。

解决方法

binance 包仅支持 websocket 流,但 REST API(directly 或使用 different 包)将帮助您更轻松地实现目标。

您可以拥有两个功能 - 每个功能都用于从交易所检索价格,然后比较不同来源的价格。

const axios = require('axios');

const getBinanceBtcPrice = async () => {
    // binance allows retrieving a single symbol
    // if you omit the param,it would return prices of all symbols
    // and you'd have to loop through them like in the WazirX example
    const response = await axios.get('https://api.binance.com/api/v3/avgPrice?symbol=BTCUSDT');
    return response.data.price;
}

const getWazirxBtcPrice = async () => {
    // WazirX only returns all symbol data
    const response = await axios.get('https://api.wazirx.com/api/v2/market-status');

    // you need to loop through all of them,until you find the desired pair
    for (let market of response.data.markets) {
        // in your case `btc` and `usdt` (mind the lowecase letters)
        if (market.baseMarket === 'btc' && market.quoteMarket === 'usdt') {
            // mind that WazirX only returns price in full integer,// it does not return floating numbers
            return market.last;
        }
    }

    // did not find the combination
    return null;
}

const run = async () => {
    const binancePrice = await getBinanceBtcPrice();
    const wazirxPrice = await getWazirxBtcPrice();

    console.log(binancePrice);
    console.log(wazirxPrice);
}

run();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?