use super::{CorporateAction, Ledger, LedgerEntry, LedgerEntryKind};
use crate::error::{QxError, QxResult};
use crate::identity::InstrumentId;
use crate::numeric::{Money, Quantity, SCALE};
impl Ledger {
pub fn apply_corporate_action(
&mut self,
account_id: &str,
instrument: &InstrumentId,
currency: &str,
action: CorporateAction,
ts: u64,
) -> QxResult<Vec<u64>> {
if action.split_num <= 0 || action.split_den <= 0 || action.cash_dividend_raw < 0 {
return Err(QxError::BusinessViolation("公司行为参数非法".into()));
}
let current = self.position_for(account_id, instrument).quantity.raw();
if current == 0 {
return Ok(Vec::new());
}
let mut ids = Vec::new();
let dividend = current
.checked_mul(action.cash_dividend_raw)
.and_then(|value| value.checked_div(SCALE))
.ok_or_else(|| QxError::Invariant("公司行为现金分红溢出".into()))?;
if dividend != 0 {
ids.push(self.append(LedgerEntry {
id: 0,
account_id: account_id.into(),
currency: currency.into(),
kind: LedgerEntryKind::CorporateAction,
amount: Money::from_raw(dividend),
instrument: Some(instrument.clone()),
quantity: Quantity::ZERO,
price: None,
order_id: None,
ts,
multiplier: 1,
position_side: None,
leverage: None,
exact_amounts: None,
})?);
}
if action.split_num != action.split_den {
let next = current
.checked_mul(action.split_num)
.and_then(|value| value.checked_div(action.split_den))
.ok_or_else(|| QxError::Invariant("公司行为拆股数量溢出".into()))?;
let delta = next
.checked_sub(current)
.ok_or_else(|| QxError::Invariant("公司行为拆股增量溢出".into()))?;
if delta != 0 {
let price = self.position_for(account_id, instrument).average_entry;
ids.push(self.append(LedgerEntry {
id: 0,
account_id: account_id.into(),
currency: currency.into(),
kind: LedgerEntryKind::TradePosition,
amount: Money::ZERO,
instrument: Some(instrument.clone()),
quantity: Quantity::from_raw(delta),
price: Some(price),
order_id: None,
ts,
multiplier: 1,
position_side: None,
leverage: None,
exact_amounts: None,
})?);
}
}
Ok(ids)
}
pub fn grant_cash_dividend_entitlement(
&mut self,
account_id: &str,
instrument: &InstrumentId,
currency: &str,
dividend_per_share_raw: i128,
ts: u64,
) -> QxResult<Option<u64>> {
if dividend_per_share_raw < 0 {
return Err(QxError::BusinessViolation("每股现金分红不能为负".into()));
}
let quantity = self.position_for(account_id, instrument).quantity.raw();
if quantity <= 0 || dividend_per_share_raw == 0 {
return Ok(None);
}
let amount = quantity
.checked_mul(dividend_per_share_raw)
.and_then(|value| value.checked_div(SCALE))
.ok_or_else(|| QxError::Invariant("现金分红权益计算溢出".into()))?;
if amount <= 0 {
return Ok(None);
}
self.append(LedgerEntry {
id: 0,
account_id: account_id.into(),
currency: currency.into(),
kind: LedgerEntryKind::CashDividendEntitlement,
amount: Money::ZERO,
instrument: Some(instrument.clone()),
quantity: Quantity::from_raw(amount),
price: None,
order_id: None,
ts,
multiplier: 1,
position_side: None,
leverage: None,
exact_amounts: None,
})
.map(Some)
}
pub fn settle_cash_dividend_entitlement(
&mut self,
account_id: &str,
instrument: &InstrumentId,
currency: &str,
ts: u64,
) -> QxResult<Option<u64>> {
let key = (
account_id.to_string(),
currency.to_string(),
instrument.clone(),
);
let amount = self
.cash_dividend_entitlements
.get(&key)
.copied()
.unwrap_or(0);
if amount <= 0 {
return Ok(None);
}
self.append(LedgerEntry {
id: 0,
account_id: account_id.into(),
currency: currency.into(),
kind: LedgerEntryKind::CashDividendEntitlement,
amount: Money::from_raw(amount),
instrument: Some(instrument.clone()),
quantity: Quantity::from_raw(-amount),
price: None,
order_id: None,
ts,
multiplier: 1,
position_side: None,
leverage: None,
exact_amounts: None,
})
.map(Some)
}
pub fn cash_dividend_entitlement_for(
&self,
account_id: &str,
instrument: &InstrumentId,
currency: &str,
) -> Money {
Money::from_raw(
self.cash_dividend_entitlements
.get(&(
account_id.to_string(),
currency.to_string(),
instrument.clone(),
))
.copied()
.unwrap_or(0),
)
}
}