Expose position lifecycle fields
This commit is contained in:
@@ -28,6 +28,10 @@ pub struct Position {
|
||||
day_dividend_cash: f64,
|
||||
day_trade_quantity_delta: i32,
|
||||
day_trade_cost: f64,
|
||||
day_buy_quantity: u32,
|
||||
day_sell_quantity: u32,
|
||||
day_buy_value: f64,
|
||||
day_sell_value: f64,
|
||||
lots: Vec<PositionLot>,
|
||||
}
|
||||
|
||||
@@ -48,6 +52,10 @@ impl Position {
|
||||
day_dividend_cash: 0.0,
|
||||
day_trade_quantity_delta: 0,
|
||||
day_trade_cost: 0.0,
|
||||
day_buy_quantity: 0,
|
||||
day_sell_quantity: 0,
|
||||
day_buy_value: 0.0,
|
||||
day_sell_value: 0.0,
|
||||
lots: Vec::new(),
|
||||
}
|
||||
}
|
||||
@@ -69,6 +77,8 @@ impl Position {
|
||||
self.quantity += quantity;
|
||||
self.last_price = price;
|
||||
self.day_trade_quantity_delta += quantity as i32;
|
||||
self.day_buy_quantity += quantity;
|
||||
self.day_buy_value += price * quantity as f64;
|
||||
self.recalculate_average_cost();
|
||||
self.refresh_day_pnl();
|
||||
}
|
||||
@@ -103,6 +113,8 @@ impl Position {
|
||||
self.last_price = price;
|
||||
self.realized_pnl += realized;
|
||||
self.day_trade_quantity_delta -= quantity as i32;
|
||||
self.day_sell_quantity += quantity;
|
||||
self.day_sell_value += price * quantity as f64;
|
||||
self.recalculate_average_cost();
|
||||
self.refresh_day_pnl();
|
||||
Ok(realized)
|
||||
@@ -124,6 +136,54 @@ impl Position {
|
||||
(self.last_price - self.average_cost) * self.quantity as f64
|
||||
}
|
||||
|
||||
pub fn pnl(&self) -> f64 {
|
||||
self.realized_pnl + self.unrealized_pnl()
|
||||
}
|
||||
|
||||
pub fn day_start_quantity(&self) -> u32 {
|
||||
self.day_start_quantity
|
||||
}
|
||||
|
||||
pub fn day_trade_quantity_delta(&self) -> i32 {
|
||||
self.day_trade_quantity_delta
|
||||
}
|
||||
|
||||
pub fn bought_quantity(&self) -> u32 {
|
||||
self.day_buy_quantity
|
||||
}
|
||||
|
||||
pub fn sold_quantity(&self) -> u32 {
|
||||
self.day_sell_quantity
|
||||
}
|
||||
|
||||
pub fn bought_value(&self) -> f64 {
|
||||
self.day_buy_value
|
||||
}
|
||||
|
||||
pub fn sold_value(&self) -> f64 {
|
||||
self.day_sell_value
|
||||
}
|
||||
|
||||
pub fn buy_avg_price(&self) -> f64 {
|
||||
if self.day_buy_quantity == 0 {
|
||||
0.0
|
||||
} else {
|
||||
self.day_buy_value / self.day_buy_quantity as f64
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sell_avg_price(&self) -> f64 {
|
||||
if self.day_sell_quantity == 0 {
|
||||
0.0
|
||||
} else {
|
||||
self.day_sell_value / self.day_sell_quantity as f64
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transaction_cost(&self) -> f64 {
|
||||
self.day_trade_cost
|
||||
}
|
||||
|
||||
pub fn begin_trading_day(&mut self) {
|
||||
self.day_start_quantity = self.quantity;
|
||||
self.day_start_price = self.last_price;
|
||||
@@ -131,6 +191,10 @@ impl Position {
|
||||
self.day_dividend_cash = 0.0;
|
||||
self.day_trade_quantity_delta = 0;
|
||||
self.day_trade_cost = 0.0;
|
||||
self.day_buy_quantity = 0;
|
||||
self.day_sell_quantity = 0;
|
||||
self.day_buy_value = 0.0;
|
||||
self.day_sell_value = 0.0;
|
||||
self.refresh_day_pnl();
|
||||
}
|
||||
|
||||
@@ -235,8 +299,9 @@ impl Position {
|
||||
* (self.last_price - (self.day_start_price / self.day_split_ratio))
|
||||
+ self.day_dividend_cash
|
||||
};
|
||||
self.trading_pnl =
|
||||
(self.day_trade_quantity_delta as f64 * self.last_price) - self.day_trade_cost;
|
||||
self.trading_pnl = (self.day_buy_quantity as f64 * self.last_price - self.day_buy_value)
|
||||
+ (self.day_sell_value - self.day_sell_quantity as f64 * self.last_price)
|
||||
- self.day_trade_cost;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -363,6 +428,7 @@ impl PortfolioState {
|
||||
}
|
||||
|
||||
pub fn holdings_summary(&self, date: NaiveDate) -> Vec<HoldingSummary> {
|
||||
let total_equity = self.total_equity();
|
||||
self.positions
|
||||
.values()
|
||||
.filter(|position| position.quantity > 0)
|
||||
@@ -373,11 +439,26 @@ impl PortfolioState {
|
||||
average_cost: position.average_cost,
|
||||
last_price: position.last_price,
|
||||
market_value: position.market_value(),
|
||||
value_percent: if total_equity > 0.0 {
|
||||
position.market_value() / total_equity
|
||||
} else {
|
||||
0.0
|
||||
},
|
||||
unrealized_pnl: position.unrealized_pnl(),
|
||||
realized_pnl: position.realized_pnl,
|
||||
pnl: position.pnl(),
|
||||
trading_pnl: position.trading_pnl,
|
||||
position_pnl: position.position_pnl,
|
||||
dividend_receivable: position.dividend_receivable,
|
||||
old_quantity: position.day_start_quantity(),
|
||||
bought_quantity: position.bought_quantity(),
|
||||
sold_quantity: position.sold_quantity(),
|
||||
buy_avg_price: position.buy_avg_price(),
|
||||
sell_avg_price: position.sell_avg_price(),
|
||||
bought_value: position.bought_value(),
|
||||
sold_value: position.sold_value(),
|
||||
transaction_cost: position.transaction_cost(),
|
||||
day_trade_quantity_delta: position.day_trade_quantity_delta(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -692,6 +773,52 @@ mod tests {
|
||||
assert!((position.position_pnl - 70.0).abs() < 1e-6);
|
||||
assert!((position.trading_pnl + 5.0).abs() < 1e-6);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn position_tracks_day_lifecycle_fields() {
|
||||
let prev_date = NaiveDate::from_ymd_opt(2025, 1, 2).unwrap();
|
||||
let date = NaiveDate::from_ymd_opt(2025, 1, 3).unwrap();
|
||||
let mut portfolio = PortfolioState::new(10_000.0);
|
||||
portfolio
|
||||
.position_mut("000001.SZ")
|
||||
.buy(prev_date, 100, 10.0);
|
||||
portfolio.begin_trading_day();
|
||||
|
||||
portfolio.position_mut("000001.SZ").buy(date, 50, 11.0);
|
||||
let realized = portfolio
|
||||
.position_mut("000001.SZ")
|
||||
.sell(40, 12.0)
|
||||
.expect("sell");
|
||||
portfolio
|
||||
.position_mut_if_exists("000001.SZ")
|
||||
.expect("position")
|
||||
.record_trade_cost(3.0);
|
||||
|
||||
let position = portfolio.position("000001.SZ").expect("position");
|
||||
assert_eq!(position.day_start_quantity(), 100);
|
||||
assert_eq!(position.bought_quantity(), 50);
|
||||
assert_eq!(position.sold_quantity(), 40);
|
||||
assert_eq!(position.day_trade_quantity_delta(), 10);
|
||||
assert!((position.bought_value() - 550.0).abs() < 1e-6);
|
||||
assert!((position.sold_value() - 480.0).abs() < 1e-6);
|
||||
assert!((position.buy_avg_price() - 11.0).abs() < 1e-6);
|
||||
assert!((position.sell_avg_price() - 12.0).abs() < 1e-6);
|
||||
assert!((position.transaction_cost() - 3.0).abs() < 1e-6);
|
||||
assert!((realized - 80.0).abs() < 1e-6);
|
||||
assert!((position.realized_pnl - 80.0).abs() < 1e-6);
|
||||
assert!((position.position_pnl - 200.0).abs() < 1e-6);
|
||||
assert!((position.trading_pnl - 47.0).abs() < 1e-6);
|
||||
assert!((position.pnl() - (80.0 + position.unrealized_pnl())).abs() < 1e-6);
|
||||
|
||||
let summary = portfolio.holdings_summary(date);
|
||||
assert_eq!(summary[0].old_quantity, 100);
|
||||
assert_eq!(summary[0].bought_quantity, 50);
|
||||
assert_eq!(summary[0].sold_quantity, 40);
|
||||
assert!((summary[0].buy_avg_price - 11.0).abs() < 1e-6);
|
||||
assert!((summary[0].sell_avg_price - 12.0).abs() < 1e-6);
|
||||
assert!((summary[0].transaction_cost - 3.0).abs() < 1e-6);
|
||||
assert!(summary[0].value_percent > 0.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
@@ -703,11 +830,22 @@ pub struct HoldingSummary {
|
||||
pub average_cost: f64,
|
||||
pub last_price: f64,
|
||||
pub market_value: f64,
|
||||
pub value_percent: f64,
|
||||
pub unrealized_pnl: f64,
|
||||
pub realized_pnl: f64,
|
||||
pub pnl: f64,
|
||||
pub trading_pnl: f64,
|
||||
pub position_pnl: f64,
|
||||
pub dividend_receivable: f64,
|
||||
pub old_quantity: u32,
|
||||
pub bought_quantity: u32,
|
||||
pub sold_quantity: u32,
|
||||
pub buy_avg_price: f64,
|
||||
pub sell_avg_price: f64,
|
||||
pub bought_value: f64,
|
||||
pub sold_value: f64,
|
||||
pub transaction_cost: f64,
|
||||
pub day_trade_quantity_delta: i32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
||||
Reference in New Issue
Block a user