use std::{cell::RefCell, rc::Rc};
use nautilus_common::{
cache::Cache,
clients::{DataClient, ExecutionClient},
clock::Clock,
};
use nautilus_live::ExecutionClientCore;
use nautilus_model::{
enums::{AccountType, OmsType},
identifiers::ClientId,
};
use nautilus_system::factories::{ClientConfig, DataClientFactory, ExecutionClientFactory};
use crate::{
common::{
consts::{BINANCE, BINANCE_VENUE},
enums::BinanceProductType,
},
config::{BinanceDataClientConfig, BinanceExecClientConfig},
futures::{data::BinanceFuturesDataClient, execution::BinanceFuturesExecutionClient},
spot::{data::BinanceSpotDataClient, execution::BinanceSpotExecutionClient},
};
#[derive(Debug, Clone)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.binance", from_py_object)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.binance")
)]
pub struct BinanceDataClientFactory;
impl BinanceDataClientFactory {
#[must_use]
pub const fn new() -> Self {
Self
}
}
impl Default for BinanceDataClientFactory {
fn default() -> Self {
Self::new()
}
}
impl DataClientFactory for BinanceDataClientFactory {
fn create(
&self,
name: &str,
config: &dyn ClientConfig,
_cache: Rc<RefCell<Cache>>,
_clock: Rc<RefCell<dyn Clock>>,
) -> anyhow::Result<Box<dyn DataClient>> {
let binance_config = config
.as_any()
.downcast_ref::<BinanceDataClientConfig>()
.ok_or_else(|| {
anyhow::anyhow!(
"Invalid config type for BinanceDataClientFactory. Expected BinanceDataClientConfig, was {config:?}",
)
})?
.clone();
let client_id = ClientId::from(name);
let product_type = binance_config
.product_types
.first()
.copied()
.unwrap_or(BinanceProductType::Spot);
match product_type {
BinanceProductType::Spot => {
let client = BinanceSpotDataClient::new(client_id, binance_config)?;
Ok(Box::new(client))
}
BinanceProductType::UsdM | BinanceProductType::CoinM => {
let client =
BinanceFuturesDataClient::new(client_id, binance_config, product_type)?;
Ok(Box::new(client))
}
_ => {
anyhow::bail!("Unsupported product type for Binance data client: {product_type:?}")
}
}
}
fn name(&self) -> &'static str {
BINANCE
}
fn config_type(&self) -> &'static str {
stringify!(BinanceDataClientConfig)
}
}
#[derive(Debug, Clone)]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.binance", from_py_object)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.binance")
)]
pub struct BinanceExecutionClientFactory;
impl BinanceExecutionClientFactory {
#[must_use]
pub const fn new() -> Self {
Self
}
}
impl Default for BinanceExecutionClientFactory {
fn default() -> Self {
Self::new()
}
}
impl ExecutionClientFactory for BinanceExecutionClientFactory {
fn create(
&self,
name: &str,
config: &dyn ClientConfig,
cache: Rc<RefCell<Cache>>,
) -> anyhow::Result<Box<dyn ExecutionClient>> {
let binance_config = config
.as_any()
.downcast_ref::<BinanceExecClientConfig>()
.ok_or_else(|| {
anyhow::anyhow!(
"Invalid config type for BinanceExecutionClientFactory. Expected BinanceExecClientConfig, was {config:?}",
)
})?
.clone();
let product_type = binance_config
.product_types
.first()
.copied()
.unwrap_or(BinanceProductType::Spot);
match product_type {
BinanceProductType::Spot => {
let account_type = AccountType::Cash;
let oms_type = OmsType::Hedging;
let core = ExecutionClientCore::new(
binance_config.trader_id,
ClientId::from(name),
*BINANCE_VENUE,
oms_type,
binance_config.account_id,
account_type,
None, cache,
);
let client = BinanceSpotExecutionClient::new(core, binance_config)?;
Ok(Box::new(client))
}
BinanceProductType::UsdM | BinanceProductType::CoinM => {
let account_type = AccountType::Margin;
let oms_type = OmsType::Netting;
let core = ExecutionClientCore::new(
binance_config.trader_id,
ClientId::from(name),
*BINANCE_VENUE,
oms_type,
binance_config.account_id,
account_type,
None, cache,
);
let client = BinanceFuturesExecutionClient::new(core, binance_config)?;
Ok(Box::new(client))
}
_ => {
anyhow::bail!(
"Unsupported product type for Binance execution client: {product_type:?}"
)
}
}
}
fn name(&self) -> &'static str {
BINANCE
}
fn config_type(&self) -> &'static str {
stringify!(BinanceExecClientConfig)
}
}
#[cfg(test)]
mod tests {
use nautilus_system::factories::DataClientFactory;
use rstest::rstest;
use super::*;
#[rstest]
fn test_binance_data_client_factory_creation() {
let factory = BinanceDataClientFactory::new();
assert_eq!(factory.name(), "BINANCE");
assert_eq!(factory.config_type(), "BinanceDataClientConfig");
}
#[rstest]
fn test_binance_data_client_factory_default() {
let factory = BinanceDataClientFactory;
assert_eq!(factory.name(), "BINANCE");
}
}