nautechsystems / nautilus_trader

A high-performance algorithmic trading platform and event-driven backtester
https://nautilustrader.io
GNU Lesser General Public License v3.0
1.7k stars 398 forks source link

Add venue option to Binance adapter #1738

Closed DevRoss closed 5 days ago

DevRoss commented 6 days ago

Pull Request

Include a summary of the changes.

Type of change

Delete options that are not relevant.

How has this change been tested?

with existing binance_futures_testnet_ema_cross_bracket_algo.py, simply replace "BINANCE" with "BINANCE_FUTURE_01"

from decimal import Decimal

from nautilus_trader.adapters.binance.common.enums import BinanceAccountType
from nautilus_trader.adapters.binance.config import BinanceDataClientConfig
from nautilus_trader.adapters.binance.config import BinanceExecClientConfig
from nautilus_trader.adapters.binance.factories import BinanceLiveDataClientFactory
from nautilus_trader.adapters.binance.factories import BinanceLiveExecClientFactory
from nautilus_trader.config import InstrumentProviderConfig
from nautilus_trader.config import LiveExecEngineConfig
from nautilus_trader.config import LoggingConfig
from nautilus_trader.config import TradingNodeConfig
from nautilus_trader.examples.algorithms.twap import TWAPExecAlgorithm
from nautilus_trader.examples.strategies.ema_cross_bracket_algo import EMACrossBracketAlgo
from nautilus_trader.examples.strategies.ema_cross_bracket_algo import EMACrossBracketAlgoConfig
from nautilus_trader.live.node import TradingNode
from nautilus_trader.model.data import BarType
from nautilus_trader.model.identifiers import ExecAlgorithmId
from nautilus_trader.model.identifiers import InstrumentId
from nautilus_trader.model.identifiers import TraderId
from nautilus_trader.model.identifiers import Venue

# *** THIS IS A TEST STRATEGY WITH NO ALPHA ADVANTAGE WHATSOEVER. ***
# *** IT IS NOT INTENDED TO BE USED TO TRADE LIVE WITH REAL MONEY. ***

# Configure the trading node
config_node = TradingNodeConfig(
    trader_id=TraderId("TESTER-001"),
    logging=LoggingConfig(log_level="INFO"),
    exec_engine=LiveExecEngineConfig(
        reconciliation=True,
        reconciliation_lookback_mins=1440,
    ),
    data_clients={
        "BINANCE_FUTURE_01": BinanceDataClientConfig(
            api_key=None,  # 'BINANCE_API_KEY' env var
            api_secret=None,  # 'BINANCE_API_SECRET' env var
            account_type=BinanceAccountType.USDT_FUTURE,
            base_url_http=None,  # Override with custom endpoint
            base_url_ws=None,  # Override with custom endpoint
            us=False,  # If client is for Binance US
            testnet=True,  # If client uses the testnet
            instrument_provider=InstrumentProviderConfig(load_all=True),
            venue=Venue('BINANCE_FUTURE_01')
        ),
    },
    exec_clients={
        "BINANCE_FUTURE_01": BinanceExecClientConfig(
            api_key=None,  # 'BINANCE_API_KEY' env var
            api_secret=None,  # 'BINANCE_API_SECRET' env var
            account_type=BinanceAccountType.USDT_FUTURE,
            base_url_http=None,  # Override with custom endpoint
            base_url_ws=None,  # Override with custom endpoint
            us=False,  # If client is for Binance US
            testnet=True,  # If client uses the testnet
            instrument_provider=InstrumentProviderConfig(load_all=True),
            venue=Venue('BINANCE_FUTURE_01')
        ),
    },
    timeout_connection=30.0,
    timeout_reconciliation=10.0,
    timeout_portfolio=10.0,
    timeout_disconnection=10.0,
    timeout_post_stop=3.0,
)

# Instantiate the node with a configuration
node = TradingNode(config=config_node)

# Configure your strategy
symbol = "ETHUSDT-PERP"
strat_config = EMACrossBracketAlgoConfig(
    order_id_tag="001",
    instrument_id=InstrumentId.from_str(f"{symbol}.BINANCE_FUTURE_01"),
    external_order_claims=[InstrumentId.from_str(f"{symbol}.BINANCE_FUTURE_01")],
    bar_type=BarType.from_str(f"{symbol}.BINANCE_FUTURE_01-1-MINUTE-LAST-EXTERNAL"),
    fast_ema_period=10,
    slow_ema_period=20,
    bracket_distance_atr=1.0,
    trade_size=Decimal("0.100"),
    emulation_trigger="BID_ASK",
    entry_exec_algorithm_id=ExecAlgorithmId("TWAP"),
    entry_exec_algorithm_params={"horizon_secs": 5.0, "interval_secs": 0.5},
)

# Instantiate your strategy and execution algorithm
strategy = EMACrossBracketAlgo(config=strat_config)
exec_algorithm = TWAPExecAlgorithm()

# Add your strategy and execution algorithm and modules
node.trader.add_strategy(strategy)
node.trader.add_exec_algorithm(exec_algorithm)

# Register your client factories with the node (can take user-defined factories)
node.add_data_client_factory("BINANCE_FUTURE_01", BinanceLiveDataClientFactory)
node.add_exec_client_factory("BINANCE_FUTURE_01", BinanceLiveExecClientFactory)
node.build()

# Stop and dispose of the node with SIGINT/CTRL+C
if __name__ == "__main__":
    try:
        node.run()
    finally:
        node.dispose()
cjdsellers commented 5 days ago

Hi @DevRoss

Many thanks for this contribution! It's a nice tidy up. I'll also confirm this pattern for the other adapter clients too.