diff --git a/rustybot/src/managers.rs b/rustybot/src/managers.rs index 014a2a8..ca066af 100644 --- a/rustybot/src/managers.rs +++ b/rustybot/src/managers.rs @@ -230,6 +230,8 @@ impl PositionManager { } pub async fn update(&mut self, tick: u64) -> Result { + debug!("Updating {}", self.pair); + let opt_active_positions = self.client.active_positions(&self.pair).await?; let mut events = vec![]; @@ -431,44 +433,52 @@ impl OrderManager { } } +pub struct PairManager { + pair: SymbolPair, + price_manager: PriceManagerHandle, + order_manager: OrderManagerHandle, + position_manager: PositionManagerHandle, + dispatcher: Dispatcher, +} + +impl PairManager { + pub fn new(pair: SymbolPair, client: Client) -> Self { + Self { + pair: pair.clone(), + price_manager: PriceManagerHandle::new(pair.clone(), client.clone()), + order_manager: OrderManagerHandle::new( + pair.clone(), + client.clone(), + Box::new(FastOrderStrategy {}), + ), + position_manager: PositionManagerHandle::new( + pair.clone(), + client.clone(), + Box::new(TrailingStop::new()), + ), + dispatcher: Dispatcher::new(), + } + } +} + pub struct ExchangeManager { kind: ExchangeKind, - price_managers: Vec, - order_managers: Vec, - position_managers: Vec, - dispatcher: Dispatcher, + pair_managers: Vec, client: Client, } impl ExchangeManager { pub fn new(kind: &ExchangeKind, pairs: &Vec) -> Self { let client = Client::new(kind); + let pair_managers = pairs + .into_iter() + .map(|x| PairManager::new(x.clone(), client.clone())) + .collect(); - let mut position_managers = Vec::new(); - let mut order_managers = Vec::new(); - let mut price_managers = Vec::new(); - - for p in pairs { - position_managers.push(PositionManagerHandle::new( - p.clone(), - client.clone(), - Box::new(TrailingStop::new()), - )); - order_managers.push(OrderManagerHandle::new( - p.clone(), - client.clone(), - Box::new(FastOrderStrategy {}), - )); - price_managers.push(PriceManagerHandle::new(p.clone(), client.clone())); - } - - ExchangeManager { + Self { kind: kind.clone(), - position_managers, - order_managers, - price_managers, + pair_managers, client, - dispatcher: Dispatcher::new(), } } @@ -483,9 +493,9 @@ impl ExchangeManager { async fn update_position_managers(&mut self, tick: u64) -> Result { let mut futures: FuturesUnordered<_> = self - .position_managers + .pair_managers .iter_mut() - .map(|x| x.update(tick)) + .map(|x| x.position_manager.update(tick)) .collect(); while let Some(x) = futures.next().await {} @@ -495,9 +505,9 @@ impl ExchangeManager { async fn update_price_managers(&mut self, tick: u64) -> Result { let mut futures: FuturesUnordered<_> = self - .price_managers + .pair_managers .iter_mut() - .map(|x| x.update(tick)) + .map(|x| x.price_manager.update(tick)) .collect(); while let Some(x) = futures.next().await {}