From bb5d1328d62b7f52d57a63cb8bc8fe864eb5d38c Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Sun, 24 Jan 2021 21:12:06 +0000 Subject: [PATCH] general cleanup --- rustybot/src/bot.rs | 4 -- rustybot/src/connectors.rs | 5 +- rustybot/src/events.rs | 121 ------------------------------------- rustybot/src/managers.rs | 20 +++--- rustybot/src/strategy.rs | 2 - rustybot/src/ticker.rs | 8 +-- 6 files changed, 14 insertions(+), 146 deletions(-) diff --git a/rustybot/src/bot.rs b/rustybot/src/bot.rs index ca30b78..cc57e42 100644 --- a/rustybot/src/bot.rs +++ b/rustybot/src/bot.rs @@ -11,8 +11,6 @@ use crate::BoxError; pub struct BfxBot { ticker: Ticker, - quote: Symbol, - trading_symbols: Vec, exchange_managers: Vec, } @@ -35,8 +33,6 @@ impl BfxBot { BfxBot { ticker: Ticker::new(tick_duration), - quote, - trading_symbols, exchange_managers, } } diff --git a/rustybot/src/connectors.rs b/rustybot/src/connectors.rs index 355cd20..8ad4e46 100644 --- a/rustybot/src/connectors.rs +++ b/rustybot/src/connectors.rs @@ -62,6 +62,7 @@ impl Client { let (best_ask, best_bid) = (order_book.lowest_ask(), order_book.highest_bid()); // updating positions with effective profit/loss + // TODO: change fee with account's taker fee positions.iter_mut().flatten().for_each(|x| { if x.is_short() { x.update_profit_loss(best_ask, 0.2); @@ -117,9 +118,6 @@ impl Debug for dyn Connector { pub struct BitfinexConnector { bfx: Bitfinex, - affiliate_code: Option, - // account_info: String, - // ledger: String, } impl BitfinexConnector { @@ -128,7 +126,6 @@ impl BitfinexConnector { pub fn new(api_key: &str, api_secret: &str) -> Self { BitfinexConnector { bfx: Bitfinex::new(Some(api_key.into()), Some(api_secret.into())), - affiliate_code: Some(BitfinexConnector::AFFILIATE_CODE.into()), } } diff --git a/rustybot/src/events.rs b/rustybot/src/events.rs index 085b3f8..a8f029c 100644 --- a/rustybot/src/events.rs +++ b/rustybot/src/events.rs @@ -44,7 +44,6 @@ pub enum EventKind { TrailingStopMoved, OrderSubmitted, NewTick, - Any, } #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] @@ -66,124 +65,4 @@ impl Event { fn has_metadata(&self) -> bool { self.metadata.is_some() } - - pub fn kind(&self) -> EventKind { - self.kind - } - pub fn tick(&self) -> u64 { - self.tick - } - pub fn metadata(&self) -> Option { - self.metadata - } } -// -// pub struct Dispatcher { -// event_handlers: HashMap JoinHandle<()>>>>, -// profit_state_handlers: -// HashMap JoinHandle<()>>>>, -// signal_handlers: HashMap JoinHandle<()>>>>, -// -// on_any_event_handlers: Vec JoinHandle<()>>>, -// on_any_profit_state_handlers: Vec JoinHandle<()>>>, -// } -// -// impl Dispatcher { -// pub fn new() -> Self { -// Dispatcher { -// event_handlers: HashMap::new(), -// profit_state_handlers: HashMap::new(), -// signal_handlers: HashMap::new(), -// on_any_event_handlers: Vec::new(), -// on_any_profit_state_handlers: Vec::new(), -// } -// } -// -// pub fn call_signal_handlers(&self, signal: &SignalKind) { -// if let Some(functions) = self.signal_handlers.get(&signal) { -// for f in functions { -// f(signal); -// } -// } -// } -// -// pub fn call_event_handlers(&self, event: &Event, status: &PriceManager) { -// if let Some(functions) = self.event_handlers.get(&event.kind()) { -// for f in functions { -// f(event, status); -// } -// } -// -// for f in &self.on_any_event_handlers { -// f(event, status); -// } -// } -// -// pub fn call_position_state_handlers(&self, position: &Position, status: &PriceManager) { -// if let Some(profit_state) = &position.profit_state() { -// if let Some(functions) = self.profit_state_handlers.get(profit_state) { -// for f in functions { -// f(position, status); -// } -// } -// } -// -// for f in &self.on_any_profit_state_handlers { -// f(position, status); -// } -// } -// -// pub fn register_event_handler(&mut self, event: EventKind, f: F) -// where -// F: Fn(&Event, &PriceManager) -> Fut, -// Fut: Future + Send, -// { -// match event { -// EventKind::Any => self -// .on_any_event_handlers -// .push(Box::new(move |e, s| tokio::spawn(f(&e, s)))), -// _ => self -// .event_handlers -// .entry(event) -// .or_default() -// .push(Box::new(move |e, s| tokio::spawn(f(&e, s)))), -// } -// } -// -// pub fn register_positionstate_handler( -// &mut self, -// state: PositionProfitState, -// f: F, -// ) where -// F: Fn(&Position, &PriceManager) -> Fut, -// Fut: Future + Send, -// { -// match state { -// // PositionProfitState::Any => self -// // .on_any_position_state_handlers -// // .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))), -// _ => self -// .profit_state_handlers -// .entry(state) -// .or_default() -// .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))), -// } -// } -// -// pub fn register_signal_handler(&mut self, signal: SignalKind, f: F) -// where -// F: Fn(&SignalKind) -> Fut, -// Fut: Future + Send, -// { -// match signal { -// // PositionProfitState::Any => self -// // .on_any_position_state_handlers -// // .push(Box::new(move |p, s| tokio::spawn(f(&p, s)))), -// _ => self -// .signal_handlers -// .entry(signal) -// .or_default() -// .push(Box::new(move |s| tokio::spawn(f(s)))), -// } -// } -// } diff --git a/rustybot/src/managers.rs b/rustybot/src/managers.rs index 165d394..f3c7131 100644 --- a/rustybot/src/managers.rs +++ b/rustybot/src/managers.rs @@ -85,9 +85,10 @@ impl PriceManager { _ => {} } - message.respond_to.send((None, None)); - - Ok(()) + Ok(message + .respond_to + .send((None, None)) + .map_err(|_| BoxError::from("Could not send message."))?) } pub fn add_entry(&mut self, entry: PriceEntry) { @@ -224,9 +225,11 @@ impl PositionManager { pub async fn handle_message(&mut self, msg: ActorMessage) -> Result<(), BoxError> { match msg.message { Message::Update { tick } => { - let result = self.update(tick).await?; + self.update(tick).await?; - msg.respond_to.send(result); + msg.respond_to + .send((None, None)) + .map_err(|_| BoxError::from("Could not send message."))?; } _ => {} }; @@ -397,9 +400,10 @@ impl OrderManager { _ => {} }; - msg.respond_to.send((None, None)); - - Ok(()) + Ok(msg + .respond_to + .send((None, None)) + .map_err(|_| BoxError::from("Could not send message."))?) } pub async fn close_position(&mut self, position_id: u64) -> Result<(), BoxError> { diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs index b93b151..1407439 100644 --- a/rustybot/src/strategy.rs +++ b/rustybot/src/strategy.rs @@ -73,8 +73,6 @@ impl TrailingStop { const GOOD_PROFIT_PERC: f64 = TrailingStop::MIN_PROFIT_PERC * 2.5; const MAX_LOSS_PERC: f64 = -0.5; - const TAKER_FEE: f64 = 0.2; - pub fn new() -> Self { TrailingStop { stop_percentages: HashMap::new(), diff --git a/rustybot/src/ticker.rs b/rustybot/src/ticker.rs index 6a4d4c0..b5c036e 100644 --- a/rustybot/src/ticker.rs +++ b/rustybot/src/ticker.rs @@ -1,8 +1,7 @@ -use tokio::time::{Duration, Instant}; +use tokio::time::{Duration}; pub struct Ticker { duration: Duration, - start_time: Instant, current_tick: u64, } @@ -10,7 +9,6 @@ impl Ticker { pub fn new(duration: Duration) -> Self { Ticker { duration, - start_time: Instant::now(), current_tick: 1, } } @@ -18,13 +16,9 @@ impl Ticker { pub fn inc(&mut self) { self.current_tick += 1 } - pub fn duration(&self) -> Duration { self.duration } - pub fn start_time(&self) -> Instant { - self.start_time - } pub fn current_tick(&self) -> u64 { self.current_tick }