@fuzefinance/orderbook-bignumber

中文文档
10.1.0发布于 2025/02/04MITTypeScriptESM + CJS近 30 天下载 2571

节点.js Lmit Order Book用于高频交易(HFT).

中文使用文档
文档目录26 节
  1. 目录
  2. 特征
  3. 安装
  4. 使用量
  5. 有条件的命令
  6. 关于主要职能
  7. 创建顺序
  8. 创建限制顺序
  9. 创建市场顺序
  10. 创建停止限制顺序
  11. 创建停止市场秩序
  12. 创建 OCO (One- Cancels- the- Other) 命令
  13. 修改已有顺序
  14. 取消顺序
  15. 排序书选项
  16. 抓图
  17. 日记日志
  18. 启用日志
  19. 发展
  20. 构建
  21. 测试
  22. 覆盖范围
  23. 基准制定
  24. 捐款
  25. 捐款
  26. 许可证

NPM Version Package License NPM Downloads CircleCI Codecov Built with TypeScript

临时叉

这是原作的临时叉子 节点命令簿。 。 。 .

这打破了API合同,转而用于所有价格和大小计算. 这是为了让更多的人 精确的顺序书.

节点.js 顺序书

以 TypeScript 书写的超快节点.js 顺序书
用于高频交易(HFT):火箭:火箭:

(掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声)!

目录

特征

最初从 去订书,这本订货书已经增加了新的功能

  • 标准价格时间优先
  • 支持市场和限制订单
  • 支助 post-only 限制顺序 New
  • 支持有条件的命令 停止限制、停止市场和OCO New Experimental
  • 支持生效时间 GTC、FOK和IOC New
  • 支持取消命令
  • 支持订单价格和/或大小更新 New
  • 在服务器启动期间恢复订单簿的快照和日志功能 New
  • 业绩好(每秒300克以上)

机器 : ASUS 专家书 第11回 Gen Intel(R) Core(TM) i7-1165G7, 2.80Ghz, 16GB RAM, Node.js v18.4.0.

nodejs-order-book-benchmark

安装

用 npm 安装 :

npm install nodejs-order-book

用纱线安装 :

yarn add nodejs-order-book

用 pnpm 安装 :

pnpm add nodejs-order-book

使用量

要开始使用订单簿, 您需要导入 OrderBook 并创建新实例 :

import { OrderBook } from 'nodejs-order-book'

const ob = new OrderBook()

然后您就可以使用下一个主要功能:

ob.createOrder({ type: 'limit' | 'market', side: 'buy' | 'sell', size: number, price?: number, id?: string, postOnly?: boolean, timeInForce?: 'GTC' | 'FOK' | 'IOC' })

ob.limit({ id: string, side: 'buy' | 'sell', size: number, price: number, postOnly?: boolean, timeInForce?: 'GTC' | 'FOK' | 'IOC' })

ob.market({ side: 'buy' | 'sell', size: number })

ob.modify(orderID: string, { side: 'buy' | 'sell', size: number, price: number })

ob.cancel(orderID: string)

有条件的命令 Experimental

版本 v6.1.0 引入对有条件命令的支持 Stop Market, (中文(简体) ), Stop Limit 财务报告和审定财务报表 OCO. 尽管这些新特征的测试覆盖率为100%,但由于还没有用现实世界的情景进行测试,这些特征尚未被认为稳定。 为此,如果想要使用有条件的命令,则需要将命令书与 experimentalConditionalOrders 选项设置为 true。 。 。 .

import { OrderBook } from 'nodejs-order-book'

const ob = new OrderBook({ experimentalConditionalOrders: true })

ob.createOrder({ type: 'stop_limit' | 'stop_market' | 'oco', side: 'buy' | 'sell', size: number, price?: number, id?: string, stopPrice?: number, timeInForce?: 'GTC' | 'FOK' | 'IOC', stopLimitTimeInForce?: 'GTC' | 'FOK' | 'IOC' })

ob.stopLimit({ id: string, side: 'buy' | 'sell', size: number, price: number, stopPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })

ob.stopMarket({ side: 'buy' | 'sell', size: number, stopPrice: number })

ob.oco({ id: string, side: 'buy' | 'sell', size: number, price: number, stopPrice: number, stopLimitPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC', stopLimitTimeInForce?: 'GTC' | 'FOK' | 'IOC' })

关于主要职能

在命令书上加一个命令 你可以叫将军 createOrder() 函数或调用基础 limit(), (中文(简体) ), market(), (中文(简体) ), stopLimit(), (中文(简体) ), stopMarket() 或者说 oco() 函数

创建顺序

// Create limit order
ob.createOrder({ type: 'limit', side: 'buy' | 'sell', size: number, price: number, id: string, postOnly?: boolean, timeInForce?: 'GTC' | 'FOK' | 'IOC' })

// Create market order
ob.createOrder({ type: 'market', side: 'buy' | 'sell', size: number })

// Create stop limit order
ob.createOrder({ type: 'stop_limit', side: 'buy' | 'sell', size: number, price: number, id: string, stopPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })

// Create stop market order
ob.createOrder({ type: 'stop_market', side: 'buy' | 'sell', size: number, stopPrice: number })

// Create OCO order
ob.createOrder({ type: 'oco', side: 'buy' | 'sell', size: number, stopPrice: number, stopLimitPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC', stopLimitTimeInForce?: 'GTC' | 'FOK' | 'IOC' })

创建限制顺序

/**
 * Create a limit order. See {@link LimitOrderOptions} for details.
 *
 * @param options
 * @param options.side - `sell` or `buy`
 * @param options.id - Unique order ID
 * @param options.size - How much of currency you want to trade in units of base currency
 * @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
 * @param options.postOnly - When `true` the order will be rejected if immediately matches and trades as a taker. Default is `false`
 * @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
 * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
 */
ob.limit({ side: 'buy' | 'sell', id: string, size: number, price: number, postOnly?: boolean, timeInForce?: 'GTC' | 'FOK' | 'IOC' })

例如:

ob.limit({ side: "sell", id: "uniqueID", size: 55, price: 100 })

asks: 110 -> 5      110 -> 5
      100 -> 1      100 -> 56
--------------  ->  --------------
bids: 90  -> 5      90  -> 5
      80  -> 1      80  -> 1

done    - null
partial - null
ob.limit({ side: "buy", id: "uniqueID", size: 7, price: 120 })

asks: 110 -> 5
      100 -> 1
--------------  ->  --------------
bids: 90  -> 5      120 -> 1
      80  -> 1      90  -> 5
                    80  -> 1

done    - 2 (or more orders)
partial - uniqueID order
ob.limit({ side: "buy", id: "uniqueID", size: 3, price: 120 })

asks: 110 -> 5
      100 -> 1      110 -> 3
--------------  ->  --------------
bids: 90  -> 5      90  -> 5
      80  -> 1      80  -> 1

done    - 1 order with 100 price, (may be also few orders with 110 price) + uniqueID order
partial - 1 order with price 110

创建市场顺序

/**
 * Create a market order. See {@link MarketOrderOptions} for details.
 *
 * @param options
 * @param options.side - `sell` or `buy`
 * @param options.size - How much of currency you want to trade in units of base currency
 * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
 */
ob.market({ side: 'buy' | 'sell', size: number })

例如:

ob.market({ side: 'sell', size: 6 })

asks: 110 -> 5      110 -> 5
      100 -> 1      100 -> 1
--------------  ->  --------------
bids: 90  -> 5      80 -> 1
      80  -> 2

done         - 2 (or more orders)
partial      - 1 order with price 80
quantityLeft - 0
ob.market({ side: 'buy', size: 10 })

asks: 110 -> 5
      100 -> 1
--------------  ->  --------------
bids: 90  -> 5      90  -> 5
      80  -> 1      80  -> 1

done         - 2 (or more orders)
partial      - null
quantityLeft - 4

创建停止限制顺序

/**
 * Create a stop limit order. See {@link StopLimitOrderOptions} for details.
 *
 * @param options
 * @param options.side - `sell` or `buy`
 * @param options.id - Unique order ID
 * @param options.size - How much of currency you want to trade in units of base currency
 * @param options.price - The price at which the order is to be fullfilled, in units of the quote currency
 * @param options.stopPrice - The price at which the order will be triggered.
 * @param options.timeInForce - Time-in-force type supported are: GTC, FOK, IOC. Default is GTC
 * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
 */
ob.stopLimit({ side: 'buy' | 'sell', id: string, size: number, price: number, stopPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC' })

创建停止市场秩序

/**
 * Create a stop market order. See {@link StopMarketOrderOptions} for details.
 *
 * @param options
 * @param options.side - `sell` or `buy`
 * @param options.size - How much of currency you want to trade in units of base currency
 * @param options.stopPrice - The price at which the order will be triggered.
 * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
 */
ob.stopMarket({ side: 'buy' | 'sell', size: number, stopPrice: number })

创建 OCO (One- Cancels- the- Other) 命令

/**
 * Create an OCO (One-Cancels-the-Other) order.
 * OCO order combines a `stop_limit` order and a `limit` order, where if stop price
 * is triggered or limit order is fully or partially fulfilled, the other is canceled.
 * Both orders have the same `side` and `size`. If you cancel one of the orders, the
 * entire OCO order pair will be canceled.
 *
 * For BUY orders the `stopPrice` must be above the current price and the `price` below the current price
 * For SELL orders the `stopPrice` must be below the current price and the `price` above the current price
 *
 * See {@link OCOOrderOptions} for details.
 *
 * @param options
 * @param options.side - `sell` or `buy`
 * @param options.id - Unique order ID
 * @param options.size - How much of currency you want to trade in units of base currency
 * @param options.price - The price of the `limit` order at which the order is to be fullfilled, in units of the quote currency
 * @param options.stopPrice - The price at which the `stop_limit` order will be triggered.
 * @param options.stopLimitPrice - The price of the `stop_limit` order at which the order is to be fullfilled, in units of the quote currency.
 * @param options.timeInForce - Time-in-force of the `limit` order. Type supported are: GTC, FOK, IOC. Default is GTC
 * @param options.stopLimitTimeInForce - Time-in-force of the `stop_limit` order. Type supported are: GTC, FOK, IOC. Default is GTC
 * @returns An object with the result of the processed order or an error. See {@link IProcessOrder} for the returned data structure
 */
ob.oco({ side: 'buy' | 'sell', id: string, size: number, price: number, stopPrice: number, stopLimitPrice: number, timeInForce?: 'GTC' | 'FOK' | 'IOC', stopLimitTimeInForce?: 'GTC' | 'FOK' | 'IOC' })

修改已有顺序

/**
 * Modify an existing order with given ID. When an order is modified by price or quantity,
 * it will be deemed as a new entry. Under the price-time-priority algorithm, orders are
 * prioritized according to their order price and order time. Hence, the latest orders
 * will be placed at the back of the matching order queue.
 *
 * @param orderID - The ID of the order to be modified
 * @param orderUpdate - An object with the modified size and/or price of an order. The shape of the object is `{size, price}`.
 * @returns An object with the result of the processed order or an error
 */
ob.modify(orderID: string, { size: number, price: number })

例如:

ob.limit({ side: "sell", id: "uniqueID", size: 55, price: 100 })

asks: 110 -> 5      110 -> 5
      100 -> 1      100 -> 56
--------------  ->  --------------
bids: 90  -> 5      90  -> 5
      80  -> 1      80  -> 1

// Modify the size from 55 to 65
ob.modify("uniqueID", { size: 65 })

asks: 110 -> 5      110 -> 5
      100 -> 56     100 -> 66
--------------  ->  --------------
bids: 90  -> 5      90  -> 5
      80  -> 1      80  -> 1


// Modify the price from 100 to 110
ob.modify("uniqueID", { price: 110 })

asks: 110 -> 5      110 -> 70
      100 -> 66     100 -> 1
--------------  ->  --------------
bids: 90  -> 5      90  -> 5
      80  -> 1      80  -> 1

取消顺序

/**
 * Remove an existing order with given ID from the order book
 *
 * @param orderID - The ID of the order to be removed
 * @returns The removed order if exists or `undefined`
 */
ob.cancel(orderID: string)

例如:

ob.cancel("myUniqueID-Sell-1-with-100")

asks: 110 -> 5
      100 -> 1      110 -> 5
--------------  ->  --------------
bids: 90  -> 5      90  -> 5
      80  -> 1      80  -> 1

排序书选项

订单可以通过传递给构建者来初始化以下选项:

抓图

A 类 snapshot 代表特定时间点的订单书状态。 它包括下列属性:

  • asks: 要求命令清单,每份都有 price 和相关的清单 orders。 。 。 .
  • bids: 标书清单,各有一份 price 和相关的清单 orders。 。 。 .
  • stopBook: 对象为 bids 财务报告和审定财务报表 asks 与每个 StopOrder 在订书.
  • ts:一个时间戳,以Unix时间戳格式表示拍摄时的快照.
  • lastOp: 快照中包含的上一个操作的ID

快照对于将订单书恢复到之前的状态至关重要. 订单可以在处理任何日记日志之前从快照恢复,确保一致性和准确性. 在拍摄了快照后,您可以安全地删除所有日志 lastOp 编号.

说明: 命令书的快照返回含有 array 联 合 国 bids 财务报告和审定财务报表 asks,然后是顺序对象的数组。 如果快照保存到数据库中作为 string,在初始化订单本时,确保以原始格式通过快照。 例如,通过使用 JSON.parse 将字符串转换回其原始对象形式.

const ob = new OrderBook({ enableJournaling: true})

// after every order save the log to the database
const order = ob.limit({ side: "sell", id: "uniqueID", size: 55, price: 100 })
await saveLog(order.log)

// ... after some time take a snapshot of the order book and save it on the database

const snapshot = ob.snapshot()
await saveSnapshot(JSON.stringify(snapshot))

// If you want you can safely remove all logs preceding the `lastOp` id of the snapshot, and continue to save each subsequent log to the database
await removePreviousLogs(snapshot.lastOp)

// On server restart get the snapshot and logs from the database and initialize the order book
const logs = await getLogs()
const snapshot = await getSnapshot()

const ob = new OrderBook({ snapshot: JSON.parse(snapshot), journal: log, enableJournaling: true })

日记日志

这个 journal 选项期望您可以通过设置获取的日记日志数组 enableJournaling 真实的。 当提供日记时,命令书会重放所有操作,使命令书与上个日志的状态相同.

// Assuming 'logs' is an array of log entries retrieved from the database

const logs = await getLogs()
const ob = new OrderBook({ journal: logs, enableJournalLog: true })

通过将快照与日记合并,可以有效地恢复和审计订单簿的状态.

启用日志

enabledJournaling 是决定是启用日记还是禁用日记的配置设置。 启用时, 属性 log 将添加到每次行动的反应正文中。 日志必须保存到数据库中,然后当命令书的新实例被即时使用时即可被使用.

const ob = new OrderBook({ enableJournaling: true }) // false by default

// after every order save the log to the database
const order = ob.limit({ side: "sell", id: "uniqueID", size: 55, price: 100 })
await saveLog(order.log)

发展

构建

在您 dist 文件夹中构建生产( 分发) 文件 :

npm run build

测试

运行所有单元测试

npm run test

覆盖范围

运行测试范围

npm run test:cov

基准制定

运行基准前, 请确定使用 npm run build 第一个

npm run bench

捐款

我将非常感谢为改进这一项目而提供的任何捐助。 请在弄脏手之前遵循以下准则.

  1. 叉子存储器
  2. 创建您的分支( gitt 退账 - b my - bbranch)
  3. 向您的分支提交任何更改
  4. 推动您的更改到您的远程分支
  5. 打开拉动请求

捐款

如果这个项目能帮助你减少开发时间

  • USDT( TRC20): (中文(简体) ) TXArNxsq2Ee8Jvsk45PudVio52Joiq1yEe
  • BTC: TC 1GYDVSAQNgG7MFhV5bk15XJy3qoE4NFenp
  • BTC(BEP20): (英语) 0xf673ee099be8129ec05e2f549d96ebea24ac5d97
  • ETH(ERC20): (英语) 0xf673ee099be8129ec05e2f549d96ebea24ac5d97
  • BNB (BEP20): B: 0xf673ee099be8129ec05e2f549d96ebea24ac5d97

许可证

版权 安德烈·法西纳,许可 麻省理工学院。 。 。 .

版本与发布记录

@fuzefinance/orderbook-bignumber 共发布 2 个版本,首次发布于 2025/01/31;过去 12 个月发布了 0 个版本。

版本发布日期距今
10.1.0latest2025/02/041.8 年前
10.0.02025/01/311.8 年前

关于 @fuzefinance/orderbook-bignumber 的常见问题

@fuzefinance/orderbook-bignumber 是什么?

节点.js Lmit Order Book用于高频交易(HFT).

如何安装 @fuzefinance/orderbook-bignumber?

在项目目录运行 npm install @fuzefinance/orderbook-bignumber,或使用 pnpm add / yarn add / bun add。

@fuzefinance/orderbook-bignumber 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fuzefinance/orderbook-bignumber 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fuzefinance/orderbook-bignumber 可以商用吗?

@fuzefinance/orderbook-bignumber 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fuzefinance/orderbook-bignumber 的下载量有多少?

根据 npm 官方下载统计,@fuzefinance/orderbook-bignumber 在 2026/10/16 – 2026/11/14 这 30 天内被下载 2,571 次,日均约 86 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fuzefinance/orderbook-bignumber 还在维护吗?

最新版本 10.1.0 发布于 2025/02/04(1.8 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2025/01/31。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。