mirror of
https://github.com/KhizarImran/backtestingfx.git
synced 2026-07-27 20:17:44 +00:00
feat: spread calculated on exit (close position & all)
This commit is contained in:
+16
-2
@@ -51,6 +51,13 @@ impl Broker {
|
||||
if let Some(index) = self.positions.iter().position(|p| p.id == id) {
|
||||
let position = self.positions.remove(index);
|
||||
|
||||
|
||||
let close_price = if position.is_long {
|
||||
price - self.spread // sell at the bif whilst closing
|
||||
} else {
|
||||
price + self.spread
|
||||
};
|
||||
|
||||
let pnl = if position.is_long {
|
||||
(price - position.entry_price) * position.lot_size
|
||||
} else {
|
||||
@@ -65,13 +72,20 @@ impl Broker {
|
||||
pnl,
|
||||
entry_timestamp: position.entry_timestamp,
|
||||
exit_timestamp: timestamp,
|
||||
exit_price: price
|
||||
exit_price: close_price
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close_all (&mut self, price:f64, timestamp: i64) { // Instead of .push it uses drain to calculate the close all positions
|
||||
for position in self.positions.drain(..) {
|
||||
|
||||
let close_price = if position.is_long {
|
||||
price - self.spread // sell at the bif whilst closing
|
||||
} else {
|
||||
price + self.spread
|
||||
};
|
||||
|
||||
let pnl = if position.is_long {
|
||||
(price - position.entry_price) * position.lot_size
|
||||
} else {
|
||||
@@ -85,7 +99,7 @@ impl Broker {
|
||||
pnl,
|
||||
entry_timestamp: position.entry_timestamp,
|
||||
exit_timestamp: timestamp,
|
||||
exit_price: price
|
||||
exit_price: close_price
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user