pub const SETPOINT: [f32; 4] = [0.10, 0.00, 0.05, 0.00];
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct AccountBody {
pub margin_stress: f32,
pub drawdown_velocity: f32,
pub slippage_pain: f32,
pub exposure_fatigue: f32,
}
impl Default for AccountBody {
fn default() -> Self {
Self::from_array(SETPOINT)
}
}
impl AccountBody {
pub fn to_array(&self) -> [f32; 4] {
[
self.margin_stress,
self.drawdown_velocity,
self.slippage_pain,
self.exposure_fatigue,
]
}
pub fn from_array(b: [f32; 4]) -> Self {
Self {
margin_stress: b[0],
drawdown_velocity: b[1],
slippage_pain: b[2],
exposure_fatigue: b[3],
}
}
pub fn evolve(&self, alpha: [f32; 4], delta: [f32; 4]) -> Self {
let b = self.to_array();
Self::from_array(std::array::from_fn(|i| {
(b[i] - alpha[i] * (b[i] - SETPOINT[i]) + delta[i]).clamp(0.0, 1.0)
}))
}
pub fn drive(&self, weights: [f32; 4]) -> f32 {
let b = self.to_array();
(0..4)
.map(|i| weights[i] * (b[i] - SETPOINT[i]).powi(2))
.sum::<f32>()
.sqrt()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_body_relaxes_to_its_setpoints_and_stays_in_range() {
let mut body = AccountBody::from_array([1.0, 1.0, 1.0, 1.0]);
for _ in 0..500 {
body = body.evolve([0.1; 4], [0.0; 4]);
}
assert!(body.drive([1.0; 4]) < 1e-4);
let spiked = body.evolve([0.1; 4], [5.0, 5.0, -5.0, 0.0]);
assert_eq!(spiked.margin_stress, 1.0);
assert_eq!(spiked.slippage_pain, 0.0);
}
}