73 lines
5.4 KiB
Markdown
73 lines
5.4 KiB
Markdown
# 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](https://chatgpt.com/c/68b07bee-306c-8326-9fd6-27b6f18ce7ea)
|
||
Taken from
|
||
- https://github.com/nautechsystems/nautilus_trader/tree/b4ff89adef6276b2c2060680d9123b5c7f3c1a24/nautilus_trader
|
||
|
||
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 trade’s details. Both include timestamps (nanosecond UNIX epoch) for the event and when the object was created.
|
||
|
||
* **QuoteTick (Market Quote):** Contains fields for an instrument’s 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 sizes[gitee.com](https://gitee.com/mirrors/nautilus-trader/blob/master/nautilus_trader/model/data.pyx#:~:text=ts_event%2C%20uint64_t%20ts_init%2C%20%29%20,point%20integer%29.%20bid_size_prec).
|
||
* **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 venue’s 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 ID[gitee.com](https://gitee.com/mirrors/nautilus-trader/blob/master/nautilus_trader/model/data.pyx#:~:text=cdef%20class%20TradeTick%28Data%29%3A%20,0).
|
||
|
||
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 precision[gitee.com](https://gitee.com/mirrors/nautilus-trader/blob/master/nautilus_trader/model/data.pyx#:~:text=ts_event%2C%20uint64_t%20ts_init%2C%20%29%20,point%20integer%29.%20bid_size_prec)[gitee.com](https://gitee.com/mirrors/nautilus-trader/blob/master/nautilus_trader/model/data.pyx#:~:text=cdef%20class%20TradeTick%28Data%29%3A%20,0).
|
||
|
||
Usage in Python
|
||
---------------
|
||
|
||
In Python, import these classes from the model as follows:
|
||
|
||
```python
|
||
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 source[gitee.com](https://gitee.com/mirrors/nautilus-trader/blob/master/nautilus_trader/model/data.pyx#:~:text=ts_event%2C%20uint64_t%20ts_init%2C%20%29%20,point%20integer%29.%20bid_size_prec)[gitee.com](https://gitee.com/mirrors/nautilus-trader/blob/master/nautilus_trader/model/data.pyx#:~:text=cdef%20class%20TradeTick%28Data%29%3A%20,0).
|
||
|
||
**Sources:** NautilusTrader 1.203.0 source code (`nautilus_trader/model/data.pyx`), showing the definitions and docstrings for `QuoteTick` and `TradeTick`[gitee.com](https://gitee.com/mirrors/nautilus-trader/blob/master/nautilus_trader/model/data.pyx#:~:text=ts_event%2C%20uint64_t%20ts_init%2C%20%29%20,point%20integer%29.%20bid_size_prec)[gitee.com](https://gitee.com/mirrors/nautilus-trader/blob/master/nautilus_trader/model/data.pyx#:~:text=cdef%20class%20TradeTick%28Data%29%3A%20,0).
|
||
|
||
|
||
|
||
---
|
||
Powered by [ChatGPT Exporter](https://www.chatgptexporter.com) |