use ahash::AHashMap;
use log::LevelFilter;
use nautilus_coinbase::{
common::{consts::COINBASE_CLIENT_ID, enums::CoinbaseEnvironment},
config::{CoinbaseDataClientConfig, CoinbaseExecClientConfig},
factories::{CoinbaseDataClientFactory, CoinbaseExecutionClientFactory},
};
use nautilus_common::{enums::Environment, logging::logger::LoggerConfig};
use nautilus_live::{config::LiveExecEngineConfig, node::LiveNode};
use nautilus_model::{
enums::AccountType,
identifiers::{AccountId, InstrumentId, StrategyId, TraderId},
types::Quantity,
};
use nautilus_testkit::testers::{ExecTester, ExecTesterConfig};
use nautilus_trading::strategy::StrategyConfig;
use ustr::Ustr;
const COINBASE_ENVIRONMENT: CoinbaseEnvironment = CoinbaseEnvironment::Live;
const TRADER_ID: &str = "TESTER-001";
const ACCOUNT_ID: &str = "COINBASE-001";
const NODE_NAME: &str = "COINBASE-EXEC-TESTER-001";
const STRATEGY_ID: &str = "EXEC_TESTER-001";
const INSTRUMENT_ID: &str = "BTC-USDC.COINBASE";
const ORDER_QTY: &str = "0.0001";
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
dotenvy::dotenv().ok();
let environment = Environment::Live;
let coinbase_environment = COINBASE_ENVIRONMENT;
let trader_id = TraderId::from(TRADER_ID);
let account_id = AccountId::from(ACCOUNT_ID);
let node_name = NODE_NAME.to_string();
let client_id = *COINBASE_CLIENT_ID;
let instrument_id = InstrumentId::from(INSTRUMENT_ID);
let data_config = CoinbaseDataClientConfig {
environment: coinbase_environment,
api_key: None, api_secret: None, ..Default::default()
};
let exec_config = CoinbaseExecClientConfig {
environment: coinbase_environment,
api_key: None, api_secret: None, account_type: AccountType::Cash,
retail_portfolio_id: None,
..Default::default()
};
let data_factory = CoinbaseDataClientFactory::new();
let exec_factory = CoinbaseExecutionClientFactory::new(trader_id, account_id);
let exec_engine_config = LiveExecEngineConfig {
open_check_interval_secs: Some(10.0),
position_check_interval_secs: Some(30.0),
..Default::default()
};
let log_config = LoggerConfig {
stdout_level: LevelFilter::Info,
module_level: AHashMap::from_iter([(Ustr::from("nautilus_coinbase"), LevelFilter::Debug)]),
..Default::default()
};
let mut node = LiveNode::builder(trader_id, environment)?
.with_name(node_name)
.with_load_state(false)
.with_save_state(false)
.with_logging(log_config)
.with_exec_engine_config(exec_engine_config)
.with_reconciliation(true)
.with_delay_post_stop_secs(5)
.add_data_client(None, Box::new(data_factory), Box::new(data_config))?
.add_exec_client(None, Box::new(exec_factory), Box::new(exec_config))?
.build()?;
let order_qty = Quantity::from(ORDER_QTY);
let tester_config = ExecTesterConfig::builder()
.base(StrategyConfig {
strategy_id: Some(StrategyId::from(STRATEGY_ID)),
external_order_claims: Some(vec![instrument_id]),
..Default::default()
})
.instrument_id(instrument_id)
.client_id(client_id)
.order_qty(order_qty)
.tob_offset_ticks(500)
.use_post_only(true)
.enable_limit_buys(true)
.enable_limit_sells(true)
.enable_stop_buys(false)
.enable_stop_sells(false)
.cancel_orders_on_stop(true)
.close_positions_on_stop(true)
.reduce_only_on_stop(false)
.log_data(false)
.build()?;
let tester = ExecTester::new(tester_config);
node.add_strategy(tester)?;
node.run().await?;
Ok(())
}