derivs/
lib.rs

1mod strategy {
2    use crate::{OptionType, Position};
3
4    struct Commission {
5        long: f64,
6        short: f64,
7    }
8    struct Option {
9        option_type: OptionType,
10        ins_code: String,
11        name: String,
12        symbol: String,
13        contract_size: f64,
14        begin_date: String,
15        end_date: String,
16        k: f64,
17        t: f64,
18        bid_price: f64,
19        bid_vol: f64,
20        ask_price: f64,
21        ask_vol: f64,
22        commission: Commission,
23    }
24    impl Option {
25        fn net_bid_price(&self) -> f64 {
26            self.bid_price * (1. + self.commission.long)
27        }
28        fn net_ask_price(&self) -> f64 {
29            self.bid_price * (1. - self.commission.short)
30        }
31        fn profit(&self, st: f64, position: Position) -> f64 {
32            let distance = st - self.k;
33            let p = match position {
34                Position::Long => {
35                    let net_premium = self.bid_price * (1. + self.commission.long);
36                    match self.option_type {
37                        OptionType::Call => f64::max(distance, 0.) - net_premium,
38                        OptionType::Put => f64::max(-distance, 0.) - net_premium,
39                    }
40                }
41                Position::Short => {
42                    let net_premium = self.ask_price * (1. - self.commission.short);
43                    match self.option_type {
44                        OptionType::Call => -f64::max(distance, 0.) + net_premium,
45                        OptionType::Put => -f64::max(-distance, 0.) + net_premium,
46                    }
47                }
48            };
49            p
50        }
51    }
52    struct UnderlyingAsset {
53        ins_code: String,
54        symbol: String,
55        ask_price: f64,
56        ask_vol: f64,
57        bid_price: f64,
58        bid_vol: f64,
59        commission: Commission,
60    }
61    impl UnderlyingAsset {
62        fn net_ask_price(&self) -> f64 {
63            self.bid_price * (1. - self.commission.short)
64        }
65    }
66    struct CoveredCall {
67        max_pot_profit: f64,
68        max_pot_loss: f64,
69        break_even: f64,
70        current_profit: f64,
71    }
72    fn covered_call(call: Option, ua: UnderlyingAsset) -> CoveredCall {
73        let max_pot_profit = call.k - ua.net_ask_price() + call.net_bid_price();
74        let max_pot_loss = call.net_bid_price() - ua.net_ask_price();
75        let break_even = ua.net_ask_price() - call.net_bid_price();
76        let current_profit = call.profit(ua.net_ask_price(), Position::Short);
77        CoveredCall {
78            max_pot_profit,
79            max_pot_loss,
80            break_even,
81            current_profit,
82        }
83    }
84}
85enum OptionType {
86    Call,
87    Put,
88}
89
90enum Position {
91    Long,
92    Short,
93}