From c7c4dd59024b452e22c3f0a50798faf1df0c8b7b Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Wed, 17 Feb 2021 16:23:34 +0000 Subject: [PATCH] changed visibility of ActiveOrder's fields. implemented getters --- rustybot/src/connectors.rs | 53 +++++++++++++-------------- rustybot/src/managers.rs | 28 ++++++++------- rustybot/src/models.rs | 73 +++++++++++++++++++++++++++++++++----- rustybot/src/strategy.rs | 17 ++++----- 4 files changed, 112 insertions(+), 59 deletions(-) diff --git a/rustybot/src/connectors.rs b/rustybot/src/connectors.rs index 596ece5..5b84ee7 100644 --- a/rustybot/src/connectors.rs +++ b/rustybot/src/connectors.rs @@ -123,7 +123,7 @@ impl Client { .active_orders(pair) .await? .into_iter() - .filter(|x| &x.symbol == pair) + .filter(|x| &x.pair() == &pair) .collect()) } @@ -350,7 +350,7 @@ impl Connector for BitfinexConnector { // adding leverage, if any match order.leverage() { // TODO: CHANGEME!!!! - Some(leverage) => pre_leverage.with_leverage(15), + Some(_leverage) => pre_leverage.with_leverage(15), // Some(leverage) => pre_leverage.with_leverage(leverage.round() as u32), None => pre_leverage, } @@ -468,7 +468,7 @@ impl Connector for BitfinexConnector { impl From<&ActiveOrder> for CancelOrderForm { fn from(o: &ActiveOrder) -> Self { - Self::from_id(o.id) + Self::from_id(o.id()) } } @@ -476,21 +476,18 @@ impl TryFrom<&bitfinex::responses::OrderResponse> for ActiveOrder { type Error = BoxError; fn try_from(response: &OrderResponse) -> Result { - Ok(Self { - exchange: Exchange::Bitfinex, - id: response.id(), - group_id: response.gid(), - client_id: Some(response.cid()), - symbol: SymbolPair::from_str(response.symbol())?, - details: OrderForm::new( - SymbolPair::from_str(response.symbol())?, - response.into(), - response.into(), - response.amount(), - ), - creation_timestamp: 0, - update_timestamp: 0, - }) + let pair = SymbolPair::from_str(response.symbol())?; + + Ok(ActiveOrder::new( + Exchange::Bitfinex, + response.id(), + pair.clone(), + OrderForm::new(pair, response.into(), response.into(), response.amount()), + response.mts_create(), + response.mts_update(), + ) + .with_group_id(response.gid()) + .with_client_id(Some(response.cid()))) } } @@ -670,16 +667,16 @@ impl From<&bitfinex::orders::ActiveOrder> for ActiveOrder { fn from(order: &bitfinex::orders::ActiveOrder) -> Self { let pair = SymbolPair::from_str(&order.symbol()).expect("Invalid symbol!"); - Self { - exchange: Exchange::Bitfinex, - id: order.id(), - group_id: order.group_id().map(|x| x as u64), - client_id: Some(order.client_id()), - symbol: pair.clone(), - details: OrderForm::new(pair, order.into(), order.into(), order.amount()), - creation_timestamp: order.creation_timestamp(), - update_timestamp: order.update_timestamp(), - } + ActiveOrder::new( + Exchange::Bitfinex, + order.id(), + pair.clone(), + OrderForm::new(pair, order.into(), order.into(), order.amount()), + order.creation_timestamp(), + order.update_timestamp(), + ) + .with_client_id(Some(order.client_id())) + .with_group_id(order.group_id()) } } diff --git a/rustybot/src/managers.rs b/rustybot/src/managers.rs index 0d828c0..ebc04ba 100644 --- a/rustybot/src/managers.rs +++ b/rustybot/src/managers.rs @@ -434,13 +434,13 @@ impl OrderManager { let open_orders = self.client.active_orders(&self.pair).await?; let position_orders: Vec<_> = position_orders .iter() - .filter_map(|&x| open_orders.iter().find(|y| y.id == x)) + .filter_map(|&x| open_orders.iter().find(|y| y.id() == x)) .collect(); for order in position_orders { match self.client.cancel_order(order).await { - Ok(_) => info!("Order #{} closed successfully.", order.id), - Err(e) => error!("Could not close order #{}: {}", order.id, e), + Ok(_) => info!("Order #{} closed successfully.", order.id()), + Err(e) => error!("Could not close order #{}: {}", order.id(), e), } } } @@ -460,10 +460,10 @@ impl OrderManager { match self.tracked_positions.get_mut(&position_id) { None => { self.tracked_positions - .insert(position_id, vec![active_order.id]); + .insert(position_id, vec![active_order.id()]); } Some(position_orders) => { - position_orders.push(active_order.id); + position_orders.push(active_order.id()); } } } @@ -493,7 +493,9 @@ impl OrderManager { let opt_position_order = open_orders .iter() // avoid using direct equality, using error margin instead - .find(|x| (x.details.amount().neg() - position.amount()).abs() < 0.0000001); + .find(|x| { + (x.order_form().amount().neg() - position.amount()).abs() < 0.0000001 + }); // checking if the position has an open order. // If so, don't do anything since the order is taken care of @@ -552,29 +554,29 @@ impl OrderManager { for position in positions { let matching_order = open_orders .iter() - .find(|x| x.details.amount().abs() == position.amount().abs()); + .find(|x| x.order_form().amount().abs() == position.amount().abs()); // if an order is found, we insert the order to our internal mapping, if not already present if let Some(matching_order) = matching_order { match self.tracked_positions.get_mut(&position.id()) { Some(position_orders) => { - if !position_orders.contains(&matching_order.id) { + if !position_orders.contains(&matching_order.id()) { trace!( "Mapped order #{} to position #{}", position.id(), - matching_order.id + matching_order.id() ); - position_orders.push(matching_order.id); + position_orders.push(matching_order.id()); } } None => { trace!( "Mapped order #{} to position #{}", position.id(), - matching_order.id + matching_order.id() ); self.tracked_positions - .insert(position.id(), vec![matching_order.id]); + .insert(position.id(), vec![matching_order.id()]); } } } @@ -595,7 +597,7 @@ impl OrderManager { match m { ActionMessage::SubmitOrder { order: order_form } => { info!("Closing open order..."); - info!("\tCancelling open order #{}", &active_order.id); + info!("\tCancelling open order #{}", &active_order.id()); self.client.cancel_order(&active_order).await?; info!("\tSubmitting {}...", order_form.kind()); diff --git a/rustybot/src/models.rs b/rustybot/src/models.rs index 01fa2f6..9e8cc7c 100644 --- a/rustybot/src/models.rs +++ b/rustybot/src/models.rs @@ -150,14 +150,71 @@ impl OrderDetails { #[derive(Clone, Debug)] pub struct ActiveOrder { - pub(crate) exchange: Exchange, - pub(crate) id: u64, - pub(crate) group_id: Option, - pub(crate) client_id: Option, - pub(crate) symbol: SymbolPair, - pub(crate) details: OrderForm, - pub(crate) creation_timestamp: u64, - pub(crate) update_timestamp: u64, + exchange: Exchange, + id: u64, + group_id: Option, + client_id: Option, + pair: SymbolPair, + order_form: OrderForm, + creation_timestamp: u64, + update_timestamp: u64, +} + +impl ActiveOrder { + pub fn new( + exchange: Exchange, + id: u64, + pair: SymbolPair, + order_form: OrderForm, + creation_timestamp: u64, + update_timestamp: u64, + ) -> Self { + Self { + exchange, + id, + group_id: None, + client_id: None, + pair, + order_form, + creation_timestamp, + update_timestamp, + } + } + + pub fn with_group_id(mut self, group_id: Option) -> Self { + self.group_id = group_id; + self + } + + pub fn with_client_id(mut self, client_id: Option) -> Self { + self.client_id = client_id; + self + } + + pub fn exchange(&self) -> Exchange { + self.exchange + } + pub fn id(&self) -> u64 { + self.id + } + pub fn group_id(&self) -> Option { + self.group_id + } + pub fn client_id(&self) -> Option { + self.client_id + } + pub fn pair(&self) -> &SymbolPair { + &self.pair + } + pub fn order_form(&self) -> &OrderForm { + &self.order_form + } + pub fn creation_timestamp(&self) -> u64 { + self.creation_timestamp + } + pub fn update_timestamp(&self) -> u64 { + self.update_timestamp + } } impl Hash for ActiveOrder { diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs index 23d4ecd..2503ba0 100644 --- a/rustybot/src/strategy.rs +++ b/rustybot/src/strategy.rs @@ -1,16 +1,13 @@ use std::collections::HashMap; use std::fmt::{Debug, Formatter}; - use dyn_clone::DynClone; -use log::{info}; +use log::info; use crate::connectors::Connector; use crate::events::{ActionMessage, Event, EventKind, EventMetadata}; use crate::managers::OptionUpdate; -use crate::models::{ - ActiveOrder, OrderBook, OrderForm, OrderKind, Position, PositionProfitState, -}; +use crate::models::{ActiveOrder, OrderBook, OrderForm, OrderKind, Position, PositionProfitState}; use crate::BoxError; /*************** @@ -557,7 +554,7 @@ impl OrderStrategy for MarketEnforce { // long let offer_comparison = { - if order.details.amount() > 0.0 { + if order.order_form().amount() > 0.0 { order_book.highest_bid() } else { order_book.lowest_ask() @@ -567,7 +564,7 @@ impl OrderStrategy for MarketEnforce { // if the best offer is higher than our threshold, // ask the manager to close the position with a market order let order_price = order - .details + .order_form() .price() .ok_or("The active order does not have a price!")?; let delta = (1.0 - (offer_comparison / order_price)).abs() * 100.0; @@ -575,10 +572,10 @@ impl OrderStrategy for MarketEnforce { if delta > self.threshold { messages.push(ActionMessage::SubmitOrder { order: OrderForm::new( - order.symbol.clone(), + order.pair().clone(), OrderKind::Market, - *order.details.platform(), - order.details.amount(), + *order.order_form().platform(), + order.order_form().amount(), ), }) }