Order Book - Loren1166/NautilusTrader- GitHub Wiki
Order Book 订单簿
class Level 级别
Bases: object
class Level(object):
"""
Represents a read-only order book Level.
表示只读订单簿级别。
A price level on one side of the order book with one or more individual orders.
订单簿一侧的价格级别,带有一个或多个单独的订单。
Parameters:
参数:
price (Price) – The price for the level.
price (Price) - 级别价格。
orders (list [BookOrder ]) – The orders for the level.
orders (list [BookOrder ]) - 该级别的订单。
Raises: ValueError – If orders is empty.
引发:ValueError - 如果 orders 为空。
"""
def __init__(self, price: Price, orders: list):
...
Properties 属性
@property
def price(self) -> Price:
"""
Price
Return the price for the level.
返回级别价格。
Return type: Price
Type: Level.price
"""
...
Methods 方法
def exposure(self) -> float:
"""
Return the exposure at this level (price * volume).
返回此级别的风险敞口(价格 * 数量)。
Return type: double
"""
...
def orders(self) -> list:
"""
Return the orders for the level.
返回该级别的订单。
Return type: list[BookOrder]
"""
...
def size(self) -> float:
"""
Return the size at this level.
返回此级别的规模。
Return type: double
"""
...
class OrderBook 订单簿
Bases: Data
class OrderBook(Data):
"""
Provides an order book which can handle L1/L2/L3 granularity data.
提供一个可以处理 L1/L2/L3 粒度数据的订单簿。
"""
def __init__(self, instrument_id: InstrumentId, book_type: BookType) -> None:
...
Properties 属性
@property
def book_type(self) -> BookType:
"""
BookType
Return the order book type.
返回订单簿类型。
Return type: BookType
Type: OrderBook.book_type
"""
...
@property
def count(self) -> int:
"""
int
Return the books update count.
返回订单簿更新计数。
Return type: int
Type: OrderBook.count
"""
...
@property
def instrument_id(self) -> InstrumentId:
"""
InstrumentId
Return the books instrument ID.
返回订单簿的金融工具 ID。
Return type: InstrumentId
Type: OrderBook.instrument_id
"""
...
@property
def sequence(self) -> int:
"""
int
Return the last sequence number for the book.
返回订单簿的最后一个序列号。
Return type: int
Type: OrderBook.sequence
"""
...
@property
def ts_event(self) -> int:
"""
int
UNIX timestamp (nanoseconds) when the data event occurred.
数据事件发生时的 UNIX 时间戳(纳秒)。
Return type: int
Type: OrderBook.ts_event
"""
...
@property
def ts_init(self) -> int:
"""
int
UNIX timestamp (nanoseconds) when the object was initialized.
对象初始化时的 UNIX 时间戳(纳秒)。
Return type: int
Type: OrderBook.ts_init
"""
...
@property
def ts_last(self) -> int:
"""
int
Return the UNIX timestamp (nanoseconds) when the order book was last updated.
返回订单簿最后更新时的 UNIX 时间戳(纳秒)。
Return type: int
Type: OrderBook.ts_last
"""
...
Methods 方法
def add(self, order: BookOrder, ts_event: int, flags: int = 0, sequence: int = 0) -> void:
"""
Add the given order to the book.
将给定的订单添加到订单簿。
Parameters:
参数:
order (BookOrder) – The order to add.
order (BookOrder) - 要添加的订单。
ts_event (uint64_t) – UNIX timestamp (nanoseconds) when the book event occurred.
ts_event (uint64_t) - 订单簿事件发生时的 UNIX 时间戳(纳秒)。
flags (uint8_t , default 0) – The record flags bit field, indicating event end and data information.
flags (uint8_t,默认 0) - 记录标志位字段,指示事件结束和数据信息。
sequence (uint64_t , default 0) – The unique sequence number for the update. If default 0 then will increment the sequence.
sequence (uint64_t,默认 0) - 更新的唯一序列号。如果默认为 0,则将递增序列。
Raises: RuntimeError – If the book type is L1_MBP.
引发:RuntimeError - 如果订单簿类型为 L1_MBP。
"""
...
def apply(self, data) -> void:
"""
Apply the given data to the order book.
将给定的数据应用于订单簿。
Parameters: delta (OrderBookDelta , OrderBookDeltas) – The data to apply.
参数:delta (OrderBookDelta,OrderBookDeltas) - 要应用的数据。
"""
...
def apply_delta(self, delta: OrderBookDelta) -> void:
"""
Apply the order book delta.
应用订单簿增量。
Parameters: delta (OrderBookDelta) – The delta to apply.
参数:delta (OrderBookDelta) - 要应用的增量。
Raises: ValueError – If delta.book_type is not equal to self.type.
引发:ValueError - 如果 delta.book_type 不等于 self.type。
"""
...
def apply_deltas(self, deltas: OrderBookDeltas) -> void:
"""
Apply the bulk deltas to the order book.
将批量增量应用于订单簿。
Parameters: deltas (OrderBookDeltas) – The deltas to apply.
参数:deltas (OrderBookDeltas) - 要应用的增量。
"""
...
def apply_depth(self, depth: OrderBookDepth10) -> void:
"""
Apply the depth update to the order book.
将深度更新应用于订单簿。
Parameters: depth (OrderBookDepth10) – The depth update to apply.
参数:depth (OrderBookDepth10) - 要应用的深度更新。
"""
...
def asks(self) -> list:
"""
Return the bid levels for the order book.
返回订单簿的买价级别。
Returns: Sorted in ascending order of price.
返回值:按价格升序排序。
Return type: list[Level]
"""
...
def best_ask_price(self):
"""
Return the best ask price in the book (if no asks then returns None).
返回订单簿中的最佳卖价(如果没有卖价,则返回 None)。
Return type: double
"""
...
def best_ask_size(self):
"""
Return the best ask size in the book (if no asks then returns None).
返回订单簿中的最佳卖量(如果没有卖价,则返回 None)。
Return type: double or None
"""
...
def best_bid_price(self):
"""
Return the best bid price in the book (if no bids then returns None).
返回订单簿中的最佳买价(如果没有买价,则返回 None)。
Return type: double
"""
...
def best_bid_size(self):
"""
Return the best bid size in the book (if no bids then returns None).
返回订单簿中的最佳买量(如果没有买价,则返回 None)。
Return type: double
"""
...
def bids(self) -> list:
"""
Return the bid levels for the order book.
返回订单簿的买价级别。
Returns: Sorted in descending order of price.
返回值:按价格降序排序。
Return type: list[Level]
"""
...
def check_integrity(self) -> void:
"""
Check book integrity.
检查订单簿完整性。
For all order books:
对于所有订单簿:
The bid side price should not be greater than the ask side price.
买价不得高于卖价。
Raises: RuntimeError – If book integrity check fails.
引发:RuntimeError - 如果订单簿完整性检查失败。
"""
...
def clear(self, ts_event: int, sequence: int = 0) -> void:
"""
Clear the entire order book.
清除整个订单簿。
"""
...
def clear_asks(self, ts_event: int, sequence: int = 0) -> void:
"""
Clear the asks from the order book.
从订单簿中清除卖价。
"""
...
def clear_bids(self, ts_event: int, sequence: int = 0) -> void:
"""
Clear the bids from the order book.
从订单簿中清除买价。
"""
...
def delete(self, order: BookOrder, ts_event: int, flags: int = 0, sequence: int = 0) -> void:
"""
Cancel the given order in the book.
取消订单簿中的给定订单。
Parameters:
参数:
order (Order) – The order to delete.
order (Order) - 要删除的订单。
ts_event (uint64_t) – UNIX timestamp (nanoseconds) when the book event occurred.
ts_event (uint64_t) - 订单簿事件发生时的 UNIX 时间戳(纳秒)。
flags (uint8_t , default 0) – The record flags bit field, indicating event end and data information.
flags (uint8_t,默认 0) - 记录标志位字段,指示事件结束和数据信息。
sequence (uint64_t , default 0) – The unique sequence number for the update. If default 0 then will increment the sequence.
sequence (uint64_t,默认 0) - 更新的唯一序列号。如果默认为 0,则将递增序列。
"""
...
@classmethod
def fully_qualified_name(cls) -> str:
"""
Return the fully qualified name for the Data class.
返回 Data 类的完全限定名称。
Return type: str
"""
...
def get_avg_px_for_quantity(self, quantity: Quantity, order_side: OrderSide) -> float:
"""
Return the average price expected for the given quantity based on the current state of the order book.
根据订单簿的当前状态返回给定数量的预期平均价格。
Parameters:
参数:
quantity (Quantity) – The quantity for the calculation.
quantity (Quantity) - 用于计算的数量。
order_side (OrderSide) – The order side for the calculation.
order_side (OrderSide) - 用于计算的订单方向。
Return type: double
Raises: ValueError – If order_side is equal to NO_ORDER_SIDE
引发:ValueError - 如果 order_side 等于 NO_ORDER_SIDE。
Warning:
If no average price can be calculated then will return 0.0 (zero).
如果无法计算平均价格,则将返回 0.0(零)。
"""
...
def get_quantity_for_price(self, price: Price, order_side: OrderSide) -> float:
"""
Return the current total quantity for the given price based on the current state of the order book.
根据订单簿的当前状态返回给定价格的当前总数量。
Parameters:
参数:
price (Price) – The quantity for the calculation.
price (Price) - 用于计算的数量。
order_side (OrderSide) – The order side for the calculation.
order_side (OrderSide) - 用于计算的订单方向。
Return type: double
Raises: ValueError – If order_side is equal to NO_ORDER_SIDE
引发:ValueError - 如果 order_side 等于 NO_ORDER_SIDE。
"""
...
@classmethod
def is_signal(cls, name: str = '') -> bool:
"""
Determine if the current class is a signal type, optionally checking for a specific signal name.
确定当前类是否是信号类型,可以选择检查特定的信号名称。
Parameters:
参数:
name (str , optional) – The specific signal name to check. If name not provided or if an empty string is passed, the method checks whether the class name indicates a general signal type. If name is provided, the method checks if the class name corresponds to that specific signal.
name (str,可选) - 要检查的特定信号名称。如果未提供名称或传递空字符串,则该方法将检查类名是否指示通用信号类型。如果提供了名称,则该方法将检查类名是否与该特定信号相对应。
Returns: True if the class name matches the signal type or the specific signal name, otherwise False.
返回值:如果类名与信号类型或特定信号名称匹配,则返回 True,否则返回 False。
Return type: bool
"""
...
def midpoint(self):
"""
Return the mid point (if no market exists then returns None).
返回中间点(如果不存在市场,则返回 None)。
Return type: double or None
"""
...
def pprint(self, num_levels: int = 3) -> str:
"""
Return a string representation of the order book in a human-readable table format.
以人类可读的表格格式返回订单簿的字符串表示形式。
Parameters:
参数:
num_levels (int) – The number of levels to include.
num_levels (int) - 要包含的级别数。
Return type: str
"""
...
def reset(self) -> void:
"""
Reset the order book (clear all stateful values).
重置订单簿(清除所有有状态值)。
"""
...
def simulate_fills(self, order: Order, price_prec: int, is_aggressive: bool) -> list:
"""
Simulate filling the book with the given order.
使用给定的订单模拟填充订单簿。
Parameters:
参数:
order (Order) – The order to simulate fills for.
order (Order) - 要为其模拟填充的订单。
price_prec (uint8_t) – The price precision for the fills.
price_prec (uint8_t) - 填充的价格精度。
"""
...
def spread(self):
"""
Return the top-of-book spread (if no bids or asks then returns None).
返回订单簿顶部的价差(如果没有买价或卖价,则返回 None)。
Return type: double or None
"""
...
def update(self, order: BookOrder, ts_event: int, flags: int = 0, sequence: int = 0) -> void:
"""
Update the given order in the book.
更新订单簿中的给定订单。
Parameters:
参数:
order (Order) – The order to update.
order (Order) - 要更新的订单。
ts_event (uint64_t) – UNIX timestamp (nanoseconds) when the book event occurred.
ts_event (uint64_t) - 订单簿事件发生时的 UNIX 时间戳(纳秒)。
flags (uint8_t , default 0) – The record flags bit field, indicating event end and data information.
flags (uint8_t,默认 0) - 记录标志位字段,指示事件结束和数据信息。
sequence (uint64_t , default 0) – The unique sequence number for the update. If default 0 then will increment the sequence.
sequence (uint64_t,默认 0) - 更新的唯一序列号。如果默认为 0,则将递增序列。
"""
...
def update_quote_tick(self, tick: QuoteTick) -> void:
"""
Update the order book with the given quote tick.
使用给定的报价 tick 更新订单簿。
This operation is only valid for L1_MBP books maintaining a top level.
此操作仅对维护顶级价格的 L1_MBP 订单簿有效。
Parameters: tick (QuoteTick) – The quote tick to update with.
参数:tick (QuoteTick) - 要更新的报价 tick。
Raises: RuntimeError – If book_type is not L1_MBP.
引发:RuntimeError - 如果 book_type 不是 L1_MBP。
"""
...
def update_trade_tick(self, tick: TradeTick) -> void:
"""
Update the order book with the given trade tick.
使用给定的交易 tick 更新订单簿。
This operation is only valid for L1_MBP books maintaining a top level.
此操作仅对维护顶级价格的 L1_MBP 订单簿有效。
Parameters: tick (TradeTick) – The trade tick to update with.
参数:tick (TradeTick) - 要更新的交易 tick。
Raises: RuntimeError – If book_type is not L1_MBP.
引发:RuntimeError - 如果 book_type 不是 L1_MBP。
"""
...