fix: commision bug fix

This commit is contained in:
KhizarImran
2026-06-25 21:48:49 +01:00
parent e88dc28b2f
commit 53fe8e0e9d
+11 -5
View File
@@ -5,6 +5,7 @@ use pyo3::prelude::*;
pub struct Broker { pub struct Broker {
pub cash: f64, pub cash: f64,
pub initial_cash: f64, pub initial_cash: f64,
next_id: u64,
pub positions: Vec<Position>, pub positions: Vec<Position>,
pub trade_history: Vec<Trade>, pub trade_history: Vec<Trade>,
pub commission: f64, pub commission: f64,
@@ -62,7 +63,7 @@ impl Broker {
* self.contract_size * self.contract_size
* self.quote_to_account * self.quote_to_account
}; };
self.cash += pnl; self.cash += pnl - self.commission * position.lot_size;
self.trade_history.push(Trade { self.trade_history.push(Trade {
entry_price: position.entry_price, entry_price: position.entry_price,
exit_price: close_price, exit_price: close_price,
@@ -92,6 +93,7 @@ impl Broker {
Broker { Broker {
cash: initial_cash, cash: initial_cash,
initial_cash, initial_cash,
next_id: 0,
positions: Vec::new(), positions: Vec::new(),
trade_history: Vec::new(), trade_history: Vec::new(),
commission, commission,
@@ -111,8 +113,10 @@ impl Broker {
) { ) {
let fill_price = price + self.spread; let fill_price = price + self.spread;
self.cash -= self.commission * lot_size; self.cash -= self.commission * lot_size;
let id = self.next_id;
self.next_id += 1;
self.positions.push(Position { self.positions.push(Position {
id: self.positions.len() as u64, id,
entry_price: fill_price, entry_price: fill_price,
lot_size, lot_size,
is_long: true, is_long: true,
@@ -132,8 +136,10 @@ impl Broker {
) { ) {
let fill_price = price - self.spread; let fill_price = price - self.spread;
self.cash -= self.commission * lot_size; self.cash -= self.commission * lot_size;
let id = self.next_id;
self.next_id += 1;
self.positions.push(Position { self.positions.push(Position {
id: self.positions.len() as u64, id,
entry_price: fill_price, entry_price: fill_price,
lot_size, lot_size,
is_long: false, is_long: false,
@@ -165,7 +171,7 @@ impl Broker {
* self.quote_to_account * self.quote_to_account
}; };
self.cash += pnl; self.cash += pnl - self.commission * position.lot_size;
self.trade_history.push(Trade { self.trade_history.push(Trade {
entry_price: position.entry_price, entry_price: position.entry_price,
lot_size: position.lot_size, lot_size: position.lot_size,
@@ -197,7 +203,7 @@ impl Broker {
* self.contract_size * self.contract_size
* self.quote_to_account * self.quote_to_account
}; };
self.cash += pnl; self.cash += pnl - self.commission * position.lot_size;
self.trade_history.push(Trade { self.trade_history.push(Trade {
entry_price: position.entry_price, entry_price: position.entry_price,
lot_size: position.lot_size, lot_size: position.lot_size,