From b02554778e749bc8dc1247f8a0d9d3f0180c8b1e Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Mon, 25 Jan 2021 12:13:49 +0000 Subject: [PATCH] connected actor messages results with actual actor results (duh!) --- rustybot/src/managers.rs | 45 +++++++++++++++++++--------------------- rustybot/src/strategy.rs | 21 ++++++++++++------- 2 files changed, 34 insertions(+), 32 deletions(-) diff --git a/rustybot/src/managers.rs b/rustybot/src/managers.rs index f3c7131..127dc24 100644 --- a/rustybot/src/managers.rs +++ b/rustybot/src/managers.rs @@ -223,18 +223,15 @@ impl PositionManager { } pub async fn handle_message(&mut self, msg: ActorMessage) -> Result<(), BoxError> { - match msg.message { - Message::Update { tick } => { - self.update(tick).await?; - - msg.respond_to - .send((None, None)) - .map_err(|_| BoxError::from("Could not send message."))?; - } - _ => {} + let (events, messages) = match msg.message { + Message::Update { tick } => self.update(tick).await?, + _ => (None, None), }; - Ok(()) + Ok(msg + .respond_to + .send((events, messages)) + .map_err(|_| BoxError::from("Could not send message."))?) } pub async fn update(&mut self, tick: u64) -> Result { @@ -284,6 +281,7 @@ impl PositionManager { .insert(self.current_tick(), pos_post_tick.clone()); self.active_position = Some(pos_post_tick); + println!("Message: {:?}", messages); return Ok((events, messages)); } } @@ -392,21 +390,19 @@ impl OrderManager { } pub async fn handle_message(&mut self, msg: ActorMessage) -> Result<(), BoxError> { - match msg.message { - Message::Update { .. } => { - self.update().await?; - } + let (events, messages) = match msg.message { + Message::Update { .. } => self.update().await?, Message::ClosePosition { position_id } => self.close_position(position_id).await?, - _ => {} + _ => (None, None), }; Ok(msg .respond_to - .send((None, None)) + .send((events, messages)) .map_err(|_| BoxError::from("Could not send message."))?) } - pub async fn close_position(&mut self, position_id: u64) -> Result<(), BoxError> { + pub async fn close_position(&mut self, position_id: u64) -> Result { info!("Closing position #{}", position_id); debug!("Retrieving open orders, positions and current prices..."); @@ -511,18 +507,18 @@ impl OrderManager { } } - Ok(()) + Ok((None, None)) } // TODO: finish me pub async fn update(&self) -> Result { trace!("\tUpdating {} order manager.", self.pair); - - let (open_orders, opt_open_positions) = tokio::join!( - self.client.active_orders(&self.pair), - self.client.active_positions(&self.pair) - ); - let (_open_orders, _opt_open_positions) = (open_orders?, opt_open_positions?); + // + // let (open_orders, opt_open_positions) = tokio::join!( + // self.client.active_orders(&self.pair), + // self.client.active_positions(&self.pair) + // ); + // let (_open_orders, _opt_open_positions) = (open_orders?, opt_open_positions?); Ok((None, None)) } @@ -604,6 +600,7 @@ impl PairManager { messages.merge(opt_pos_messages); messages.merge(opt_order_messages); + println!("Total messages: {:?}", messages); // TODO: to move into Handler? if let Some(messages) = messages { for m in messages { diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs index 1407439..c9edd88 100644 --- a/rustybot/src/strategy.rs +++ b/rustybot/src/strategy.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::fmt::{Debug, Formatter}; use dyn_clone::DynClone; +use log::info; use crate::events::{Event, EventKind, EventMetadata, Message}; use crate::models::{ @@ -68,10 +69,10 @@ pub struct TrailingStop { } impl TrailingStop { - const BREAK_EVEN_PERC: f64 = 0.01; - const MIN_PROFIT_PERC: f64 = TrailingStop::BREAK_EVEN_PERC + 0.03; + const BREAK_EVEN_PERC: f64 = 0.1; + const MIN_PROFIT_PERC: f64 = TrailingStop::BREAK_EVEN_PERC + 0.2; const GOOD_PROFIT_PERC: f64 = TrailingStop::MIN_PROFIT_PERC * 2.5; - const MAX_LOSS_PERC: f64 = -0.5; + const MAX_LOSS_PERC: f64 = -2.25; pub fn new() -> Self { TrailingStop { @@ -88,14 +89,14 @@ impl TrailingStop { if let Some(profit_state) = position.profit_state() { let profit_state_delta = match profit_state { - PositionProfitState::MinimumProfit => Some(Self::MIN_PROFIT_PERC), - PositionProfitState::Profit => Some(Self::GOOD_PROFIT_PERC), + PositionProfitState::MinimumProfit => Some(0.2), + PositionProfitState::Profit => Some(0.1), _ => None, }; if let Some(profit_state_delta) = profit_state_delta { println!( - "PL%: {} | Delta: {}", + "PL%: {:0.2} | Delta: {}", position.pl_perc(), profit_state_delta ); @@ -230,14 +231,18 @@ impl PositionStrategy for TrailingStop { // let's check if we surpassed an existing stop percentage if let Some(existing_stop_percentage) = self.stop_percentages.get(&position.id()) { - if existing_stop_percentage <= &position.pl_perc() { + if &position.pl_perc() <= existing_stop_percentage { + info!("Stop percentage surpassed. Closing position."); return (position, None, Some(vec![close_message])); } } self.update_stop_percentage(&position); - println!("Stop percentages: {:?}", self.stop_percentages); + println!( + "Stop percentage: {:0.2}", + self.stop_percentages.get(&position.id()).unwrap() + ); (position, None, None) }