diff --git a/rustybot/src/connectors.rs b/rustybot/src/connectors.rs index c71cc6a..8fbdff9 100644 --- a/rustybot/src/connectors.rs +++ b/rustybot/src/connectors.rs @@ -54,7 +54,25 @@ impl Client { &self, pair: &SymbolPair, ) -> Result>, BoxError> { - self.inner.active_positions(pair).await + // retrieving open positions and order book to calculate effective profit/loss + let (positions, order_book) = tokio::join!( + self.inner.active_positions(pair), + self.inner.order_book(pair) + ); + + let (mut positions, order_book) = (positions?, order_book?); + let (best_ask, best_bid) = (order_book.lowest_ask(), order_book.highest_bid()); + + // updating positions with effective profit/loss + positions.iter_mut().flatten().for_each(|mut x| { + if x.is_short() { + x.update_profit_loss(best_ask, 0.2); + } else { + x.update_profit_loss(best_bid, 0.2); + } + }); + + Ok(positions) } pub async fn current_prices(&self, pair: &SymbolPair) -> Result { diff --git a/rustybot/src/managers.rs b/rustybot/src/managers.rs index c3998e7..40095fb 100644 --- a/rustybot/src/managers.rs +++ b/rustybot/src/managers.rs @@ -268,6 +268,7 @@ impl PositionManager { None => return Ok((None, None)), Some(positions) => { + println!("Open positions: {:?}", positions); // checking if there are positions open for our pair match positions .into_iter() diff --git a/rustybot/src/models.rs b/rustybot/src/models.rs index 1c5713d..70e0cfe 100644 --- a/rustybot/src/models.rs +++ b/rustybot/src/models.rs @@ -386,6 +386,28 @@ impl Position { self } + pub fn update_profit_loss(&mut self, best_offer: f64, fee_perc: f64) { + let best_offer = best_offer.abs(); + let base_price = self.base_price * (1.0 + fee_perc / 100.0); + + let delta = if self.is_short() { + base_price - best_offer + } else { + best_offer - base_price + }; + + let profit_loss_percentage = + ((1.0 - (base_price + delta) / base_price) * 100.0).abs() * delta.signum(); + + self.pl = delta * self.amount; + self.pl_perc = profit_loss_percentage; + } + + pub fn with_profit_loss(mut self, profit_loss: f64) -> Self { + self.pl = profit_loss; + self + } + pub fn pair(&self) -> &SymbolPair { &self.pair } diff --git a/rustybot/src/strategy.rs b/rustybot/src/strategy.rs index b89fbce..e7c0890 100644 --- a/rustybot/src/strategy.rs +++ b/rustybot/src/strategy.rs @@ -96,7 +96,7 @@ impl PositionStrategy for TrailingStop { let mut messages = vec![]; let events = vec![]; - let pl_perc = TrailingStop::net_pl_percentage(position.pl_perc(), TrailingStop::TAKER_FEE); + let pl_perc = position.pl_perc(); let state = { if pl_perc > TrailingStop::GOOD_PROFIT_PERC {