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 400 forks source link

Cannot pull out account information in SANDBOX mode #1657

Closed VeraLyu closed 1 month ago

VeraLyu commented 1 month ago

Bug Report

When I tested with sandbox executing client, and I want to pull account information, it reports error. Also Sandbox does not differentiate spot or future.

Expected Behavior

report account status in strategy.

Actual Behavior

for real exec_client, it reports the right account status, but for sandbox it give out error: 2024-05-21T00:48:26.828083495Z [ERROR] TESTER-001.Portfolio: Cannot get account: no account registered for BINANCE.

Steps to Reproduce the Problem

in trading node config:

exec_clients={

    #     "BINANCE": BinanceExecClientConfig(
    #         api_key=os.getenv("BINANCE_FUTURES_API_KEY"),
    #         api_secret=os.getenv("BINANCE_FUTURES_API_SECRET"),
    #         account_type=BinanceAccountType.USDT_FUTURE,
    #         instrument_provider=InstrumentProviderConfig(load_all=True),
    #     ),
    # },
    exec_clients={
        "SANDBOX": SandboxExecutionClientConfig(
                venue="BINANCE",
                currency="USDC",
                balance=10_000_000,
        ),
    },

in strategy: self.account = self.portfolio.account(Venue("BINANCE")) self.log.info(f"{self.account.balances()}")

davidsblom commented 1 month ago

Made a change in the referenced pull request. Does that fix your issue?

VeraLyu commented 1 month ago

Sorry my test with binance failed with creating executing client, it worked in previous version: my code: exec_clients={ "SANDBOX": SandboxExecutionClientConfig( venue="BINANCE", currency="USDC", balance=10_000_000, default_leverage=10.0, oms_type="NETTING", account_type="MARGIN", ), }, Error: File "/home/ubuntu/crypto_level2/nautilus_trader/./examples/live/binance/binance_trader_with_controller.py", line 188, in node = asyncio.run(main()) File "/usr/lib/python3.10/asyncio/runners.py", line 44, in run return loop.run_until_complete(main) File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete File "/home/ubuntu/crypto_level2/nautilus_trader/./examples/live/binance/binance_trader_with_controller.py", line 175, in main node.build() File "/home/ubuntu/crypto_level2/nautilus_trader/nautilus_trader/live/node.py", line 263, in build self._builder.build_exec_clients(self._config.exec_clients) File "/home/ubuntu/crypto_level2/nautilus_trader/nautilus_trader/live/node_builder.py", line 247, in build_exec_clients client = factory.create(**factory_kws) File "/home/ubuntu/crypto_level2/nautilus_trader/nautilus_trader/adapters/sandbox/factory.py", line 70, in create exec_client = SandboxExecutionClient( File "/home/ubuntu/crypto_level2/nautilus_trader/nautilus_trader/adapters/sandbox/execution.py", line 111, in init self.exchange = SimulatedExchange( File "nautilus_trader/backtest/exchange.pyx", line 224, in nautilus_trader.backtest.exchange.SimulatedExchange.init self.add_instrument(instrument) File "nautilus_trader/backtest/exchange.pyx", line 320, in nautilus_trader.backtest.exchange.SimulatedExchange.add_instrument Condition.equal(instrument.id.venue, self.id, "instrument.id.venue", "self.id") File "nautilus_trader/core/correctness.pyx", line 306, in nautilus_trader.core.correctness.Condition.equal raise make_exception( ValueError: The 'instrument.id.venue' <class 'nautilus_trader.model.identifiers.Venue'> of BINANCE was not equal to the 'self.id' <class 'nautilus_trader.model.identifiers.Venue'> of SANDBOX

davidsblom commented 1 month ago

That is unfortunate. What if you use "BINANCE" as the key for the SandboxExecutionClientConfig instead of "SANDBOX"? The instruments you use in your strategy need to use the same venue I believe.

I'm not able to test Binance in my country so this is a bit of guessing on my part.

VeraLyu commented 1 month ago

Yeah, it works after I changed the name to "BINANCE", and account information successfully listed: 2024-05-22T00:28:59.517509477Z [INFO] TESTER-001.Portfolio: Updated AccountState(account_id=BINANCE-001, account_type=MARGIN, base_currency=USDC, is_reported=True, balances=[AccountBalance(total=10_000_000.00000000 USDC, locked=0.00000000 USDC, free=10_000_000.00000000 USDC)], margins=[], event_id=5f0f4a3f-44f8-4d57-b61d-0f9d66553d69)