概述
crypto-exchanges-rest-client
是一款基于 REST API 的加密货币交易所客户端,可以用于方便地查询加密货币市场信息和执行交易操作。本文将介绍该包的使用方法和相关注意事项。
安装
使用 npm 进行安装:
npm install crypto-exchanges-rest-client
用法说明
初始化客户端
使用 createClient()
方法创建一个客户端实例:
const { createClient } = require("crypto-exchanges-rest-client"); const client = createClient({ exchange: "binance", apiKey: "yourApiKey", secretKey: "yourSecretKey", });
exchange
:交易所名称(当前支持binance
、huobi
、okex
);apiKey
:API Key,用于访问交易所 API;secretKey
:Secret Key,用于签名请求消息。
查询市场信息
使用对应的方法获取市场信息:
const result = await client.getTicker("btcusdt"); console.log(result);
getTicker(symbol: string)
:获取指定交易对的市场行情信息;getDepth(symbol: string, limit: number?)
:获取指定交易对的市场深度信息;getTrades(symbol: string, limit: number?)
:获取指定交易对的最近成交记录。
执行交易
目前支持以下交易操作:
await client.buy(symbol, amount, price); await client.sell(symbol, amount, price); await client.cancel(symbol, orderId); await client.getOrders(symbol); await client.getOrder(symbol, orderId);
例如,如下代码实现了使用 Binance API 购买 0.1 个 BTC:
-- -------------------- ---- ------- ----- - ------------ - - ---------------------------------------- ----- ------ - -------------- --------- ---------- ------- ------------- ---------- ---------------- --- ----- ------ - ---------- ----- ------ - ---- ----- ----- - ------ ----- ------------------ ------- -------展开代码
注意事项
- 通过 API 访问交易所可能需要一些额外的配置和认证,具体参数请查看对应的交易所文档;
- 调用交易 API 可能需要一些额外的权限(如开通交易权限、配置子账户等),请在使用前仔细阅读交易所文档并确认您的权限情况;
- 请注意安全性,不要轻易泄露您的 API Key 和 Secret Key;
- 目前仅支持部分交易所的 REST API,若缺少您需要的功能或 API,请您及时联系开发人员或提交 Issue;
- 请谨慎使用交易功能,我们不对您的交易行为承担任何责任。
示例代码
以下代码实现了使用 Binance API 查询指定交易对的市场行情和深度信息:
-- -------------------- ---- ------- ----- - ------------ - - ---------------------------------------- ----- ------ - -------------- --------- ---------- ------- ------------- ---------- ---------------- --- ----- -------- ------ - ----- ------ - ---------- -- -------- ----- ------- - ----- ------------------------- --------------------- -- -------- ----- ------- - ----- ----------------------- ---- --------------------- -- -------- ----- ------- - ----- ------------------------ ---- --------------------- - -------展开代码
来源:JavaScript中文网 ,转载请注明来源 https://www.javascriptcn.com/post/60066b5e51ab1864dac67134