From a2eae0ac13b138d63cdada81ef8807215d589f97 Mon Sep 17 00:00:00 2001 From: Giulio De Pasquale Date: Mon, 25 Jan 2021 13:16:45 +0000 Subject: [PATCH] damn calculations --- rustybot/src/models.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/rustybot/src/models.rs b/rustybot/src/models.rs index 9c12914..a5fb122 100644 --- a/rustybot/src/models.rs +++ b/rustybot/src/models.rs @@ -384,16 +384,21 @@ impl Position { } 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 (base_price, delta) = { + if self.is_short() { + let base_price = self.base_price * (1.0 - fee_perc / 100.0); + let delta = base_price - best_offer; - let delta = if self.is_short() { - base_price - best_offer - } else { - best_offer - base_price + (base_price, delta) + } else { + let base_price = self.base_price * (1.0 - fee_perc / 100.0); + let delta = best_offer - base_price; + + (base_price, delta) + } }; - let profit_loss = delta * self.amount; + let profit_loss = delta * self.amount.abs(); let profit_loss_percentage = ((1.0 - (base_price + delta) / base_price) * 100.0).abs() * profit_loss.signum();