|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# ------------------------------------------------------------------------------------------------- |
| 3 | +# Copyright (C) 2015-2024 Nautech Systems Pty Ltd. All rights reserved. |
| 4 | +# https://nautechsystems.io |
| 5 | +# |
| 6 | +# Licensed under the GNU Lesser General Public License Version 3.0 (the "License"); |
| 7 | +# You may not use this file except in compliance with the License. |
| 8 | +# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# ------------------------------------------------------------------------------------------------- |
| 16 | + |
| 17 | +import time |
| 18 | +from decimal import Decimal |
| 19 | + |
| 20 | +import pandas as pd |
| 21 | + |
| 22 | +from nautilus_trader.adapters.databento.loaders import DatabentoDataLoader |
| 23 | +from nautilus_trader.backtest.engine import BacktestEngine |
| 24 | +from nautilus_trader.backtest.engine import BacktestEngineConfig |
| 25 | +from nautilus_trader.config import LoggingConfig |
| 26 | +from nautilus_trader.config import RiskEngineConfig |
| 27 | +from nautilus_trader.examples.strategies.ema_cross_long_only import EMACrossLongOnly |
| 28 | +from nautilus_trader.examples.strategies.ema_cross_long_only import EMACrossLongOnlyConfig |
| 29 | +from nautilus_trader.model.currencies import USD |
| 30 | +from nautilus_trader.model.data import BarType |
| 31 | +from nautilus_trader.model.enums import AccountType |
| 32 | +from nautilus_trader.model.enums import OmsType |
| 33 | +from nautilus_trader.model.identifiers import TraderId |
| 34 | +from nautilus_trader.model.identifiers import Venue |
| 35 | +from nautilus_trader.model.objects import Money |
| 36 | +from nautilus_trader.test_kit.providers import TestInstrumentProvider |
| 37 | +from tests import TEST_DATA_DIR |
| 38 | + |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + # Configure backtest engine |
| 42 | + config = BacktestEngineConfig( |
| 43 | + trader_id=TraderId("BACKTESTER-001"), |
| 44 | + logging=LoggingConfig(log_level="INFO"), |
| 45 | + risk_engine=RiskEngineConfig(bypass=True), |
| 46 | + ) |
| 47 | + |
| 48 | + # Build the backtest engine |
| 49 | + engine = BacktestEngine(config=config) |
| 50 | + |
| 51 | + # Add a trading venue (multiple venues possible) |
| 52 | + NASDAQ = Venue("XNAS") # <-- ISO 10383 MIC |
| 53 | + engine.add_venue( |
| 54 | + venue=NASDAQ, |
| 55 | + oms_type=OmsType.NETTING, |
| 56 | + account_type=AccountType.CASH, |
| 57 | + base_currency=USD, |
| 58 | + starting_balances=[Money(1_000_000.0, USD)], |
| 59 | + ) |
| 60 | + |
| 61 | + # Add instruments |
| 62 | + AAPL_XNAS = TestInstrumentProvider.equity(symbol="AAPL", venue="XNAS") |
| 63 | + engine.add_instrument(AAPL_XNAS) |
| 64 | + |
| 65 | + # Add data |
| 66 | + loader = DatabentoDataLoader() |
| 67 | + |
| 68 | + filenames = [ |
| 69 | + "aapl-xnas-ohlcv-1s-2023.dbn.zst", # <-- Longer load and run time / more accurate execution |
| 70 | + "aapl-xnas-ohlcv-1m-2023.dbn.zst", |
| 71 | + ] |
| 72 | + |
| 73 | + for filename in filenames: |
| 74 | + bars = loader.from_dbn_file( |
| 75 | + path=TEST_DATA_DIR / "databento" / "temp" / filename, |
| 76 | + instrument_id=AAPL_XNAS.id, |
| 77 | + ) |
| 78 | + engine.add_data(bars) |
| 79 | + |
| 80 | + # Configure your strategy |
| 81 | + config = EMACrossLongOnlyConfig( |
| 82 | + instrument_id=AAPL_XNAS.id, |
| 83 | + bar_type=BarType.from_str(f"{AAPL_XNAS.id}-1-MINUTE-LAST-EXTERNAL"), |
| 84 | + trade_size=Decimal(100), |
| 85 | + fast_ema_period=10, |
| 86 | + slow_ema_period=20, |
| 87 | + ) |
| 88 | + |
| 89 | + # Instantiate and add your strategy |
| 90 | + strategy = EMACrossLongOnly(config=config) |
| 91 | + engine.add_strategy(strategy=strategy) |
| 92 | + |
| 93 | + time.sleep(0.1) |
| 94 | + input("Press Enter to continue...") |
| 95 | + |
| 96 | + # Run the engine (from start to end of data) |
| 97 | + engine.run() |
| 98 | + |
| 99 | + # Optionally view reports |
| 100 | + with pd.option_context( |
| 101 | + "display.max_rows", |
| 102 | + 100, |
| 103 | + "display.max_columns", |
| 104 | + None, |
| 105 | + "display.width", |
| 106 | + 300, |
| 107 | + ): |
| 108 | + print(engine.trader.generate_account_report(NASDAQ)) |
| 109 | + print(engine.trader.generate_order_fills_report()) |
| 110 | + print(engine.trader.generate_positions_report()) |
| 111 | + |
| 112 | + # For repeated backtest runs make sure to reset the engine |
| 113 | + engine.reset() |
| 114 | + |
| 115 | + # Good practice to dispose of the object |
| 116 | + engine.dispose() |
0 commit comments