damn calculations

This commit is contained in:
Giulio De Pasquale 2021-01-25 13:16:45 +00:00
parent 3a38c20f56
commit a2eae0ac13

View File

@ -384,16 +384,21 @@ impl Position {
} }
pub fn update_profit_loss(&mut self, best_offer: f64, fee_perc: f64) { pub fn update_profit_loss(&mut self, best_offer: f64, fee_perc: f64) {
let best_offer = best_offer.abs(); let (base_price, delta) = {
let base_price = self.base_price * (1.0 + fee_perc / 100.0); 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, delta)
base_price - best_offer
} else { } else {
best_offer - base_price 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 = let profit_loss_percentage =
((1.0 - (base_price + delta) / base_price) * 100.0).abs() * profit_loss.signum(); ((1.0 - (base_price + delta) / base_price) * 100.0).abs() * profit_loss.signum();