临时叉
这是原作的临时叉子 节点命令簿。 。 。 .
这打破了API合同,转而用于所有价格和大小计算. 这是为了让更多的人 精确的顺序书.
节点.js 顺序书
以 TypeScript 书写的超快节点.js 顺序书
用于高频交易(HFT):火箭:火箭:
(掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声) (掌声)!
目录
特征
最初从 去订书,这本订货书已经增加了新的功能
- 标准价格时间优先
- 支持市场和限制订单
- 支助
post-only限制顺序 - 支持有条件的命令 停止限制、停止市场和OCO
- 支持生效时间 GTC、FOK和IOC
- 支持取消命令
- 支持订单价格和/或大小更新
- 在服务器启动期间恢复订单簿的快照和日志功能
- 业绩好(每秒300克以上)
机器 : ASUS 专家书 第11回 Gen Intel(R) Core(TM) i7-1165G7, 2.80Ghz, 16GB RAM, Node.js v18.4.0.
安装
用 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)
有条件的命令 
版本 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
捐款
我将非常感谢为改进这一项目而提供的任何捐助。 请在弄脏手之前遵循以下准则.
- 叉子存储器
- 创建您的分支( gitt 退账 - b my - bbranch)
- 向您的分支提交任何更改
- 推动您的更改到您的远程分支
- 打开拉动请求
捐款
如果这个项目能帮助你减少开发时间
- USDT( TRC20): (中文(简体) )
TXArNxsq2Ee8Jvsk45PudVio52Joiq1yEe - BTC: TC
1GYDVSAQNgG7MFhV5bk15XJy3qoE4NFenp - BTC(BEP20): (英语)
0xf673ee099be8129ec05e2f549d96ebea24ac5d97 - ETH(ERC20): (英语)
0xf673ee099be8129ec05e2f549d96ebea24ac5d97 - BNB (BEP20): B:
0xf673ee099be8129ec05e2f549d96ebea24ac5d97