use super::{define_signed_value_type, CashFlow, ParamKind, PositionSize};
define_signed_value_type!(
Pnl,
ParamKind::Pnl
);
impl Pnl {
pub fn to_cash_flow(self) -> CashFlow {
CashFlow::from_pnl(self)
}
pub fn to_position_size(self) -> super::position_size::PositionSize {
PositionSize::new(self.to_decimal())
}
}
#[cfg(test)]
mod tests {
use super::Pnl;
use crate::param::{CashFlow, PositionSize};
use rust_decimal::Decimal;
fn d(value: &str) -> Decimal {
value
.parse()
.expect("decimal literal in tests must be valid")
}
#[test]
fn converts_to_cash_flow() {
let pnl = Pnl::new(d("-10"));
assert_eq!(pnl.to_cash_flow(), CashFlow::new(d("-10")));
}
#[test]
fn converts_to_position_size() {
let profit = Pnl::new(d("10"));
let loss = Pnl::new(d("-10"));
assert_eq!(profit.to_position_size(), PositionSize::new(d("10")));
assert_eq!(loss.to_position_size(), PositionSize::new(d("-10")));
}
}