use crate::error::{QxError, QxResult};
use crate::identity::InstrumentId;
use crate::numeric::{ExactDecimal, Money, Price};
use crate::trading::{MarginState, TradingInstrumentSpec};
use std::collections::BTreeMap;
#[derive(Clone, Copy, Debug)]
pub struct ValuationContext<'a> {
pub account_id: &'a str,
pub marks: &'a BTreeMap<InstrumentId, Price>,
pub reporting_currency: &'a str,
pub spec: Option<&'a TradingInstrumentSpec>,
pub market_specs: Option<&'a BTreeMap<InstrumentId, TradingInstrumentSpec>>,
pub multiplier: i128,
pub fx_rates: &'a BTreeMap<String, Price>,
pub margin_required: Money,
pub maintenance_required: Money,
}
impl<'a> ValuationContext<'a> {
pub fn spot(
account_id: &'a str,
marks: &'a BTreeMap<InstrumentId, Price>,
reporting_currency: &'a str,
) -> Self {
Self {
account_id,
marks,
reporting_currency,
spec: None,
market_specs: None,
multiplier: 1,
fx_rates: empty_fx_rates(),
margin_required: Money::ZERO,
maintenance_required: Money::ZERO,
}
}
pub fn with_spec(mut self, spec: Option<&'a TradingInstrumentSpec>) -> Self {
self.spec = spec;
self.market_specs = None;
self
}
pub fn with_market_specs(
mut self,
specs: &'a BTreeMap<InstrumentId, TradingInstrumentSpec>,
) -> Self {
self.spec = None;
self.market_specs = Some(specs);
self
}
pub fn with_multiplier(mut self, multiplier: i128) -> Self {
self.multiplier = multiplier;
self
}
pub fn with_fx(mut self, fx_rates: &'a BTreeMap<String, Price>) -> Self {
self.fx_rates = fx_rates;
self
}
}
#[derive(Clone, Copy, Debug)]
pub struct ExactValuationContext<'a> {
pub valuation: ValuationContext<'a>,
pub exact_marks: Option<&'a BTreeMap<InstrumentId, ExactDecimal>>,
}
impl<'a> ExactValuationContext<'a> {
pub fn new(valuation: ValuationContext<'a>) -> Self {
Self {
valuation,
exact_marks: None,
}
}
pub fn with_exact_marks(mut self, marks: &'a BTreeMap<InstrumentId, ExactDecimal>) -> Self {
self.exact_marks = Some(marks);
self
}
}
#[derive(Clone, Copy, Debug)]
pub struct PaperValuationContext<'a> {
pub account_id: &'a str,
pub marks: &'a BTreeMap<InstrumentId, Price>,
pub reporting_currency: &'a str,
pub market_specs: &'a BTreeMap<InstrumentId, TradingInstrumentSpec>,
pub fx_rates: &'a BTreeMap<String, Price>,
}
impl<'a> PaperValuationContext<'a> {
pub fn new(
account_id: &'a str,
marks: &'a BTreeMap<InstrumentId, Price>,
reporting_currency: &'a str,
market_specs: &'a BTreeMap<InstrumentId, TradingInstrumentSpec>,
fx_rates: &'a BTreeMap<String, Price>,
) -> Self {
Self {
account_id,
marks,
reporting_currency,
market_specs,
fx_rates,
}
}
}
fn empty_fx_rates() -> &'static BTreeMap<String, Price> {
static EMPTY: std::sync::OnceLock<BTreeMap<String, Price>> = std::sync::OnceLock::new();
EMPTY.get_or_init(BTreeMap::new)
}
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct ValuationResult {
pub equity: Money,
pub initial_margin: Money,
pub maintenance_margin: Money,
pub available: Money,
pub currency: String,
}
impl ValuationResult {
pub fn new(
equity: Money,
margin_required: Money,
maintenance_required: Money,
currency: impl Into<String>,
) -> QxResult<Self> {
let available = equity
.raw()
.checked_sub(margin_required.raw())
.ok_or_else(|| QxError::Invariant("可用保证金计算溢出".into()))?;
Ok(Self {
equity,
initial_margin: margin_required,
maintenance_margin: maintenance_required,
available: Money::from_raw(available),
currency: currency.into(),
})
}
}
impl MarginState {
pub fn valuate(self, currency: impl Into<String>) -> Option<ValuationResult> {
let equity = self
.collateral
.raw()
.checked_add(self.unrealized_pnl.raw())?
.checked_sub(self.funding.raw())?
.checked_sub(self.interest.raw())?;
ValuationResult::new(
Money::from_raw(equity),
self.initial_margin,
self.maintenance_margin,
currency,
)
.ok()
}
}