Martingale42/nautilus-dev / skills/nt-trading
nt-trading
VerifiedUse when working with strategy logic, order execution, risk management, position/portfolio tracking, or exec algorithms in NautilusTrader.
references/examples/crypto_ema_cross_ethusdt_trade_ticks.py
View on GitHub#!/usr/bin/env python3
# -------------------------------------------------------------------------------------------------
# Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
# https://nautechsystems.io
#
# Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------------------------
import time
from decimal import Decimal
import pandas as pd
from nautilus_trader.adapters.binance import BINANCE_VENUE
from nautilus_trader.backtest.config import BacktestEngineConfig
from nautilus_trader.backtest.engine import BacktestEngine
from nautilus_trader.config import LoggingConfig
from nautilus_trader.examples.algorithms.twap import TWAPExecAlgorithm
from nautilus_trader.examples.strategies.ema_cross_twap import EMACrossTWAP
from nautilus_trader.examples.strategies.ema_cross_twap import EMACrossTWAPConfig
from nautilus_trader.model.currencies import ETH
from nautilus_trader.model.currencies import USDT
from nautilus_trader.model.data import BarType
from nautilus_trader.model.enums import AccountType
from nautilus_trader.model.enums import BookType
from nautilus_trader.model.enums import OmsType
from nautilus_trader.model.identifiers import TraderId
from nautilus_trader.model.objects import Money
from nautilus_trader.persistence.wranglers import TradeTickDataWrangler
from nautilus_trader.test_kit.providers import TestDataProvider
from nautilus_trader.test_kit.providers import TestInstrumentProvider
if __name__ == "__main__":
# Configure backtest engine
config = BacktestEngineConfig(
trader_id=TraderId("BACKTESTER-001"),
logging=LoggingConfig(
log_level="INFO",
log_colors=True,
use_pyo3=False,
),
)
# Build the backtest engine
engine = BacktestEngine(config=config)
# Add a trading venue (multiple venues possible)
engine.add_venue(
venue=BINANCE_VENUE,
oms_type=OmsType.NETTING,
book_type=BookType.L1_MBP,
account_type=AccountType.CASH, # Spot CASH account (not for perpetuals or futures)
base_currency=None, # Multi-currency account
starting_balances=[Money(1_000_000.0, USDT), Money(10.0, ETH)],
trade_execution=True, # Only use with L1_MBP book type or throttled book data
)
# Add instruments
ETHUSDT_BINANCE = TestInstrumentProvider.ethusdt_binance()
engine.add_instrument(ETHUSDT_BINANCE)
# Add data
provider = TestDataProvider()
wrangler = TradeTickDataWrangler(instrument=ETHUSDT_BINANCE)
ticks = wrangler.process(provider.read_csv_ticks("binance/ethusdt-trades.csv"))
engine.add_data(ticks)
# Configure your strategy
strategy_config = EMACrossTWAPConfig(
instrument_id=ETHUSDT_BINANCE.id,
bar_type=BarType.from_str("ETHUSDT.BINANCE-250-TICK-LAST-INTERNAL"),
trade_size=Decimal("0.10"),
fast_ema_period=10,
slow_ema_period=20,
twap_horizon_secs=10.0,
twap_interval_secs=2.5,
)
# Instantiate and add your strategy
strategy = EMACrossTWAP(config=strategy_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(BINANCE_VENUE))
print(engine.trader.generate_order_fills_report())
print(engine.trader.generate_positions_report())
# Generate interactive tearsheet (requires: pip install plotly>=6.3.1)
try:
from nautilus_trader.analysis import TearsheetConfig
from nautilus_trader.analysis.tearsheet import create_tearsheet
print("\nGenerating tearsheet...")
# Try different themes: "plotly_white", "plotly_dark", "nautilus", "nautilus_dark"
tearsheet_config = TearsheetConfig(theme="plotly_white") # Change this to test themes!
create_tearsheet(
engine=engine,
output_path="crypto_ethusdt_tearsheet.html",
config=tearsheet_config,
)
print("Tearsheet saved to: crypto_ethusdt_tearsheet.html")
print(f"Theme: {tearsheet_config.theme}")
print("Open this file in your browser to view interactive charts!")
except ImportError:
print("\nPlotly not installed. Install with: pip install plotly>=6.3.1")
print(" Then re-run to generate tearsheets.")
# For repeated backtest runs make sure to reset the engine
engine.reset()
# Good practice to dispose of the object
engine.dispose()
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |