Binance Exchange¶
Binance Account Types¶
Binance provides multiple account types. For testing environment, you can use account types with _TESTNET suffix.
from nexustrader.exchange.binance import BinanceAccountType
account_type = BinanceAccountType.USD_M_FUTURE # or other account types
BinanceAccountType.SPOT: Spot accountBinanceAccountType.MARGIN: Cross margin accountBinanceAccountType.ISOLATED_MARGIN: Isolated margin accountBinanceAccountType.USD_M_FUTURE: USD-M futures accountBinanceAccountType.COIN_M_FUTURE: COIN-M futures accountBinanceAccountType.PORTFOLIO_MARGIN: Portfolio margin accountBinanceAccountType.SPOT_TESTNET: Spot testnet accountBinanceAccountType.USD_M_FUTURE_TESTNET: USD-M futures testnet accountBinanceAccountType.COIN_M_FUTURE_TESTNET: COIN-M futures testnet account
Note
For futures trading on testnet, make sure to use USD_M_FUTURE_TESTNET or COIN_M_FUTURE_TESTNET account types. For spot trading on testnet, use SPOT_TESTNET. Only SPOT, USD_M_FUTURE, COIN_M_FUTURE can be passed to public_conn_config.
Binance Config¶
The most important part is the config object. You need to pass the config object to the Engine class. The config receives the following parameters:
strategy_id: The id of the strategyuser_id: The id of the userstrategy: The strategy classbasic_config: The basic config of the exchangepublic_conn_config: The public connector config of the exchangeprivate_conn_config: The private connector config of the exchange
from nexustrader.exchange.binance import BinanceAccountType
from nexustrader.config import Config, PublicConnectorConfig, PrivateConnectorConfig, BasicConfig
# The strategy code....
config = Config(
strategy_id="buy_and_sell_binance",
user_id="user_test",
strategy=Demo(),
basic_config={
ExchangeType.BINANCE: BasicConfig(
settings_key="BINANCE.FUTURE.TESTNET_1",
testnet=True,
)
},
public_conn_config={
ExchangeType.BINANCE: [
PublicConnectorConfig(
account_type=BinanceAccountType.USD_M_FUTURE_TESTNET,
)
]
},
private_conn_config={
ExchangeType.BINANCE: [
PrivateConnectorConfig(
account_type=BinanceAccountType.USD_M_FUTURE_TESTNET,
)
]
}
)
engine = Engine(config)
if __name__ == "__main__":
try:
engine.start()
finally:
engine.dispose()