Files
siloqy/docs/ChatGPT-Market Quote Python Example (1).md

5.4 KiB
Raw Permalink Blame History

Market Quote Python Example

User: HJ Normey (hjnormey@gmail.com)
Created: 8/28/2025 17:55
Updated: 8/28/2025 18:16
Exported: 8/28/2025 18:24
Link: https://chatgpt.com/c/68b07bee-306c-8326-9fd6-27b6f18ce7ea
Taken from

NautilusTrader Market/Tick Quote Objects

NautilusTrader represents market data ticks with two core classes in its Python API: QuoteTick (for top-of-book quotes) and TradeTick (for individual trade ticks). These are defined in the nautilus_trader.model.data module and backed by Rust/Cython for speed. A QuoteTick holds the best bid and ask and related metadata, while a TradeTick holds a single trades details. Both include timestamps (nanosecond UNIX epoch) for the event and when the object was created.

  • QuoteTick (Market Quote): Contains fields for an instruments best bid and ask at a moment. Key fields include:
    • instrument_id (InstrumentId) the symbol or contract identifier.
    • bid_price, ask_price (Price) the top-of-book bid and ask prices.
    • bid_size, ask_size (Quantity) the corresponding bid/ask sizes (volumes).
    • ts_event, ts_init (int) timestamps (nanoseconds) for when the quote occurred and when the object was initialized.
      The QuoteTick docstring explains it “represents a single quote tick in a market” with bid/ask prices and sizesgitee.com.
  • TradeTick (Trade Quote): Contains fields for a single executed trade. Key fields include:
    • instrument_id (InstrumentId) the symbol or contract.
    • price (Price) the executed trade price.
    • size (Quantity) the traded quantity.
    • aggressor_side (AggressorSide enum) which side (buy/sell) initiated the trade.
    • trade_id (TradeId) the venues trade identifier.
    • ts_event, ts_init (int) timestamps for when the trade tick occurred and object creation.
      The TradeTick docstring notes it “represents a single trade tick” with instrument, price, size, aggressor side, and trade IDgitee.com.

These objects support conversion from raw values or Python dictionaries (e.g. from_raw(...), from_dict(...)) and have methods to_dict() for easy serialization. For example, QuoteTick.from_raw(instrument, bid_raw, ask_raw, price_prec, size_prec, ts_event, ts_init) creates a quote from raw fixed-point values. All numeric fields use precise types (Price, Quantity) to preserve fixed-point precisiongitee.comgitee.com.

Usage in Python

In Python, import these classes from the model as follows:

from nautilus_trader.model.data import QuoteTick, TradeTick, InstrumentId, Price, Quantity, AggressorSide, TradeId

# Example: create a quote tick (using made-up raw values and precisions)
instr = InstrumentId("AAPL")  # or appropriate symbol
quote = QuoteTick.from_raw(
    instr,
    bid_price_raw=1452300, ask_price_raw=1452500,  # raw ints
    bid_price_prec=4, ask_price_prec=4,
    bid_size_raw=1000000, ask_size_raw=800000,
    bid_size_prec=0, ask_size_prec=0,
    ts_event=1680000000000000000, ts_init=1680000000000000000
)
print(quote)  # e.g. QuoteTick(instrument_id=AAPL, bid_price=145.23, ask_price=145.25, ...)

# Example: create a trade tick
trade = TradeTick(
    instrument_id=instr,
    price=Price(145.25, precision=4),
    size=Quantity(500, precision=0),
    aggressor_side=AggressorSide.BUY,
    trade_id=TradeId("T12345"),
    ts_event=1680000000000001000,
    ts_init=1680000000000001000
)
print(trade)  # e.g. TradeTick(instrument_id=AAPL, price=145.25, size=500, aggressor_side=BUY, ...)

These classes and their methods match the NautilusTrader data model: QuoteTick holds bid/ask fields and TradeTick holds price/size/aggressor fields, as documented in the sourcegitee.comgitee.com.

Sources: NautilusTrader 1.203.0 source code (nautilus_trader/model/data.pyx), showing the definitions and docstrings for QuoteTick and TradeTickgitee.comgitee.com.


Powered by ChatGPT Exporter