You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am working on a static liquidity heatmap. I would like to see the evolution of the limit order book over time. I am using locally downloaded historical futures data for instrument 6E (EURUSD) from databento.
My code so far (basically little modified data subscriber):
import time
from decimal import Decimal
import pandas as pd
from nautilus_trader.adapters.databento.loaders import DatabentoDataLoader
from nautilus_trader.backtest.engine import BacktestEngine
from nautilus_trader.backtest.engine import BacktestEngineConfig
from nautilus_trader.config import LoggingConfig
from nautilus_trader.model.currencies import USD
from nautilus_trader.model.data import BarType
from nautilus_trader.model.enums import AccountType
from nautilus_trader.model.enums import OmsType
from nautilus_trader.model.identifiers import TraderId
from nautilus_trader.model.identifiers import Venue
from nautilus_trader.model.objects import Money
from nautilus_trader.test_kit.providers import TestInstrumentProvider
from nautilus_trader.model.identifiers import InstrumentId
from strat import MyStrategyConfig
from strat import MyStrategy
if __name__ == "__main__":
# Configure backtest engine
config = BacktestEngineConfig(
trader_id=TraderId("BACKTESTER-001"),
logging=LoggingConfig(
log_level="DEBUG",
log_colors=True,
),
)
# Build the backtest engine
engine = BacktestEngine(config=config)
# Add a trading venue (multiple venues possible)
CME = Venue("GLBX")
engine.add_venue(
venue=CME,
oms_type=OmsType.NETTING,
account_type=AccountType.MARGIN,
base_currency=USD,
starting_balances=[Money(10_000_000.0, USD)],
)
# Add instruments
CME_6E = TestInstrumentProvider.future(symbol="6EJ4", venue="GLBX")
engine.add_instrument(CME_6E)
# Add data
loader = DatabentoDataLoader()
trades = loader.from_dbn_file(
path="data/batch/glbx-mdp3-20240313.mbo.dbn.zst",
instrument_id=CME_6E.id,
include_trades=True
)
engine.add_data(trades)
# Configure your strategy
config = MyStrategyConfig(instrument_id=CME_6E.id)
# Instantiate and add your strategy
strategy = MyStrategy(config=config)
engine.add_strategy(strategy=strategy)
# Instantiate and add your execution algorithm
#exec_algorithm = TWAPExecAlgorithm()
#engine.add_exec_algorithm(exec_algorithm)
time.sleep(0.1)
input("Press Enter to continue...")
# Run the engine (from start to end of data)
engine.run()
# Optionally view reports
'''with pd.option_context(
"display.max_rows",
100,
"display.max_columns",
None,
"display.width",
300,
):
print(engine.trader.generate_account_report(CME_6E))
print(engine.trader.generate_order_fills_report())
print(engine.trader.generate_positions_report())
'''
# For repeated backtest runs make sure to reset the engine
engine.reset()
# Good practice to dispose of the object
engine.dispose()
and
from nautilus_trader.config import StrategyConfig
from nautilus_trader.model.book import OrderBook
from nautilus_trader.model.data import Bar
from nautilus_trader.model.data import BarSpecification
from nautilus_trader.model.data import BarType
from nautilus_trader.model.data import OrderBookDeltas
from nautilus_trader.model.data import QuoteTick
from nautilus_trader.model.data import TradeTick
from nautilus_trader.model.enums import AggregationSource
from nautilus_trader.model.enums import BarAggregation
from nautilus_trader.model.enums import BookType
from nautilus_trader.model.enums import PriceType
from nautilus_trader.model.identifiers import InstrumentId
from nautilus_trader.trading.strategy import Strategy
# *** THIS IS A TEST STRATEGY ***
class MyStrategyConfig(StrategyConfig, frozen=True):
"""
Configuration for ``SubscribeStrategy`` instances.
"""
instrument_id: InstrumentId
book_type: BookType = BookType.L3_MBO
snapshots: bool = False
trade_ticks: bool = False
quote_ticks: bool = False
bars: bool = False
class MyStrategy(Strategy):
"""
A strategy that simply subscribes to data and logs it (typically for testing
adapters)
Parameters
----------
config : OrderbookImbalanceConfig
The configuration for the instance.
"""
def __init__(self, config: MyStrategyConfig) -> None:
super().__init__(config)
self.instrument_id = self.config.instrument_id
self.book: OrderBook | None = None
def on_start(self) -> None:
"""
Actions to be performed on strategy start.
"""
self.instrument = self.cache.instrument(self.instrument_id)
if self.instrument is None:
self.log.error(f"Could not find instrument for {self.instrument_id}")
self.stop()
return
if self.config.book_type:
self.book = OrderBook(
instrument_id=self.instrument.id,
book_type=self.config.book_type,
)
if self.config.snapshots: ########
self.subscribe_order_book_snapshots(
instrument_id=self.instrument_id,
book_type=self.config.book_type,
interval_ms= 1000*60,
depth= 0,
)
else:
self.subscribe_order_book_deltas(
instrument_id=self.instrument_id,
book_type=self.config.book_type,
depth= 100,
)
if self.config.trade_ticks:
self.subscribe_trade_ticks(instrument_id=self.instrument_id)
if self.config.quote_ticks:
self.subscribe_quote_ticks(instrument_id=self.instrument_id)
if self.config.bars:
bar_type: BarType = BarType(
instrument_id=self.instrument_id,
bar_spec=BarSpecification(
step=1,
aggregation=BarAggregation.SECOND,
price_type=PriceType.LAST,
),
aggregation_source=AggregationSource.EXTERNAL,
)
self.subscribe_bars(bar_type)
def on_order_book_deltas(self, deltas: OrderBookDeltas) -> None:
if not self.book:
self.log.error("No book being maintained")
return
self.book.apply_deltas(deltas)
#print(self.book.pprint(num_levels=20))
self.log.info(str(self.book))
#self.log.info(f"best ask: {self.book. best_ask_price()}")
# self.log.info(f"best bid: {self.book. best_bid_price()}")
def on_order_book(self, order_book: OrderBook) -> None:
self.book = order_book
self.log.info(str(self.book))
def on_trade_tick(self, tick: TradeTick) -> None:
self.log.info(str(tick))
def on_quote_tick(self, tick: QuoteTick) -> None:
self.log.info(str(tick))
def on_bar(self, bar: Bar) -> None:
self.log.info(str(bar))
I would very much like to see the progression of the order book over time, as this can be used as a basis for developing ideas for strategies.
I'm surprised that something like this doesn't exist yet. But perhaps also because it is one of the simplest things of all.
Anyway, I will be very happy if someone helps me to reach my goal. Thank you
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello community,
I am working on a static liquidity heatmap. I would like to see the evolution of the limit order book over time. I am using locally downloaded historical futures data for instrument 6E (EURUSD) from databento.
My code so far (basically little modified data subscriber):
and
Terminal Output:
╭──────┬───────┬──────────────────────────────────────────────────────────────────────────╮
│ bids │ price │ asks │
├──────┼───────┼──────────────────────────────────────────────────────────────────────────┤
│ │ 0.00 │ [2, 110, 200, 200, 110, 100, 100] │
│ │ 0.00 │ [30, 177, 110, 100, 22, 21, 15, 24, 8, 10, 100, 100, 100, 100, 100, 100] │
│ │ 0.00 │ [3, 100] │
│ [7] │ 1.12 │ │
│ [7] │ 1.12 │ │
│ [7] │ 1.11 │ │
╰──────┴───────┴──────────────────────────────────────────────────────────────────────────╯
2024-03-13T05:29:20.188896573Z [INFO] BACKTESTER-001.MyStrategy: OrderBook L3_MBO
instrument: 6EJ4.GLBX
sequence: 47803132
ts_last: 1710291779756224421
count: 59886
╭──────┬───────┬──────────────────────────────────────────────────────────────────────────╮
│ bids │ price │ asks │
├──────┼───────┼──────────────────────────────────────────────────────────────────────────┤
│ │ 0.00 │ [2, 110, 200, 200, 110, 100, 100] │
│ │ 0.00 │ [30, 177, 110, 100, 22, 21, 15, 24, 8, 10, 100, 100, 100, 100, 100, 100] │
│ │ 0.00 │ [3, 100] │
│ [7] │ 1.12 │ │
│ [7] │ 1.12 │ │
│ [7] │ 1.11 │ │
╰──────┴───────┴──────────────────────────────────────────────────────────────────────────╯
2024-03-13T05:29:20.188896573Z [INFO] BACKTESTER-001.MyStrategy: OrderBook L3_MBO
instrument: 6EJ4.GLBX
sequence: 47803162
ts_last: 1710291779962759659
count: 59887
Why is price 0.00 on ask side?
I would very much like to see the progression of the order book over time, as this can be used as a basis for developing ideas for strategies.
I'm surprised that something like this doesn't exist yet. But perhaps also because it is one of the simplest things of all.
Anyway, I will be very happy if someone helps me to reach my goal. Thank you
Beta Was this translation helpful? Give feedback.
All reactions