architect_api/algo/
common_params.rs

1use rust_decimal::Decimal;
2use schemars::JsonSchema;
3use serde_with::{DeserializeFromStr, SerializeDisplay};
4
5#[derive(Debug, Clone, SerializeDisplay, DeserializeFromStr, JsonSchema)]
6pub enum TakeThrough {
7    Fraction(Decimal), // same as percent but 100x
8    Percent(Decimal),
9    Price(Decimal),
10    Ticks(Decimal),
11}
12
13impl std::fmt::Display for TakeThrough {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            TakeThrough::Fraction(frac) => write!(f, "{frac}f"),
17            TakeThrough::Percent(pct) => write!(f, "{pct}%"),
18            TakeThrough::Price(price) => write!(f, "{price}"),
19            TakeThrough::Ticks(ticks) => write!(f, "{ticks}t"),
20        }
21    }
22}
23
24impl std::str::FromStr for TakeThrough {
25    type Err = anyhow::Error;
26
27    fn from_str(s: &str) -> Result<Self, Self::Err> {
28        if s.ends_with('f') {
29            let num_str = s.strip_suffix('f').unwrap();
30            let num = num_str.parse::<Decimal>()?;
31            Ok(TakeThrough::Fraction(num))
32        } else if s.ends_with('%') {
33            let num_str = s.strip_suffix('%').unwrap();
34            let num = num_str.parse::<Decimal>()?;
35            Ok(TakeThrough::Percent(num))
36        } else if s.ends_with('t') {
37            let num_str = s.strip_suffix('t').unwrap();
38            let num = num_str.parse::<Decimal>()?;
39            Ok(TakeThrough::Ticks(num))
40        } else {
41            let num = s.parse::<Decimal>()?;
42            Ok(TakeThrough::Price(num))
43        }
44    }
45}