Skip to content

Commit 9410846

Browse files
committed
Add EMACrossLongOnly AAPL.XNAS bars example
1 parent 3813932 commit 9410846

3 files changed

+119
-9
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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()

examples/backtest/databento_ema_cross_long_only_spy_trades.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
# Configure backtest engine
4141
config = BacktestEngineConfig(
4242
trader_id=TraderId("BACKTESTER-001"),
43-
logging=LoggingConfig(
44-
log_level="INFO",
45-
log_colors=True,
46-
),
43+
logging=LoggingConfig(log_level="INFO"),
4744
)
4845

4946
# Build the backtest engine

examples/backtest/databento_ema_cross_long_only_tsla_trades.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
# Configure backtest engine
4141
config = BacktestEngineConfig(
4242
trader_id=TraderId("BACKTESTER-001"),
43-
logging=LoggingConfig(
44-
log_level="INFO",
45-
log_colors=True,
46-
),
43+
logging=LoggingConfig(log_level="INFO"),
4744
)
4845

4946
# Build the backtest engine
@@ -83,7 +80,7 @@
8380
config = EMACrossLongOnlyConfig(
8481
instrument_id=TSLA_NYSE.id,
8582
bar_type=BarType.from_str(f"{TSLA_NYSE.id}-1-MINUTE-LAST-INTERNAL"),
86-
trade_size=Decimal(500),
83+
trade_size=Decimal(1000),
8784
fast_ema_period=10,
8885
slow_ema_period=20,
8986
)

0 commit comments

Comments
 (0)