From 7d639d1b4ccef3ebb011b4b80cd489a8fbb8adc3 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Sun, 24 Jan 2021 20:50:19 +0000 Subject: [PATCH] renamed orderstrategy methods, debug in position manager --- rustybot/src/strategy.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs index 14d79e3..bcf8b30 100644 --- a/rustybot/src/strategy.rs +++ b/rustybot/src/strategy.rs @@ -45,10 +45,10 @@ pub trait OrderStrategy: DynClone + Send + Sync { fn name(&self) -> String; /// This method is called when the OrderManager checks the open orders on a new tick. /// It should manage if some orders have to be closed or keep open. - fn on_update(&self); + fn on_open_order(&self); /// This method is called when the OrderManager is requested to close /// a position that has an open order associated to it. - fn on_position_close( + fn on_position_order( &self, order: &ActiveOrder, open_position: &Position, @@ -75,7 +75,7 @@ impl TrailingStop { const BREAK_EVEN_PERC: f64 = 0.01; const MIN_PROFIT_PERC: f64 = TrailingStop::BREAK_EVEN_PERC + 0.03; const GOOD_PROFIT_PERC: f64 = TrailingStop::MIN_PROFIT_PERC * 2.5; - const MAX_LOSS_PERC: f64 = -4.0; + const MAX_LOSS_PERC: f64 = -0.5; const TAKER_FEE: f64 = 0.2; @@ -86,6 +86,12 @@ impl TrailingStop { } fn update_stop_percentage(&mut self, position: &Position) { + println!( + "State: {:?} | PL%: {:0.2}", + position.profit_state(), + position.pl_perc() + ); + if let Some(profit_state) = position.profit_state() { let profit_state_delta = match profit_state { PositionProfitState::MinimumProfit => Some(Self::MIN_PROFIT_PERC), @@ -94,6 +100,12 @@ impl TrailingStop { }; if let Some(profit_state_delta) = profit_state_delta { + println!( + "PL%: {} | Delta: {}", + position.pl_perc(), + profit_state_delta + ); + let current_stop_percentage = position.pl_perc() - profit_state_delta; match profit_state { @@ -224,7 +236,7 @@ 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 existing_stop_percentage <= &position.pl_perc() { return (position, None, Some(vec![close_message])); } } @@ -261,14 +273,14 @@ impl OrderStrategy for FastOrderStrategy { "Fast order strategy".into() } - fn on_update(&self) { + fn on_open_order(&self) { unimplemented!() } - fn on_position_close( + fn on_position_order( &self, order: &ActiveOrder, - active_position: &Position, + _: &Position, order_book: &OrderBook, ) -> Result<(Option>, Option>), BoxError> { let mut messages = vec![];