pub trait AssetType {
fn amount(&self, price: f64, qty: f64) -> f64;
fn equity(&self, price: f64, balance: f64, position: f64, fee: f64) -> f64;
}
#[derive(Clone)]
pub struct LinearAsset {
contract_size: f64,
}
impl LinearAsset {
pub fn new(contract_size: f64) -> Self {
Self { contract_size }
}
}
impl AssetType for LinearAsset {
fn amount(&self, exec_price: f64, qty: f64) -> f64 {
self.contract_size * exec_price * qty
}
fn equity(&self, price: f64, balance: f64, position: f64, fee: f64) -> f64 {
balance + self.contract_size * position * price - fee
}
}
#[derive(Clone)]
pub struct InverseAsset {
contract_size: f64,
}
impl InverseAsset {
pub fn new(contract_size: f64) -> Self {
Self { contract_size }
}
}
impl AssetType for InverseAsset {
fn amount(&self, exec_price: f64, qty: f64) -> f64 {
self.contract_size * qty / exec_price
}
fn equity(&self, price: f64, balance: f64, position: f64, fee: f64) -> f64 {
-balance - self.contract_size * position / price - fee
}
}