aboutsummaryrefslogtreecommitdiff
path: root/src/app/store/records.rs
blob: 682f676d9e193ad210c330683c8ccf68861d30d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
use bigdecimal::{BigDecimal, Context, RoundingMode};
use std::sync::LazyLock;

pub static ROUNDING_MODE: RoundingMode = RoundingMode::HalfEven;
/// Will use default precision (either 100 or ${RUST_BIGDECIMAL_DEFAULT_PRECISION})
pub static DECIMAL_CONTEXT: LazyLock<Context> = std::sync::LazyLock::new(|| Context::default().with_rounding_mode(ROUNDING_MODE));

type DateTime = chrono::DateTime<chrono::Utc>;

/// Represents an account that holds equity. An account may hold equity with
/// multiple equity symbols.
pub struct FiduciaryAccount {
    pub fiduciary_account_id: u8,
    pub fiduciary_account_number: usize,
    pub fiduciary_account_name: String,
    pub fiduciary_account_description: String,
    pub fiduciary_account_created_timestamp: DateTime,
}

/// An atomic enum to describe for what element of equity a symbol tracks
/// (stock, fund, etc.)
pub enum EquityType {
    STOCK,
    ETF,
    CRYPTO
}

impl std::fmt::Display for EquityType {

    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", match self {
            EquityType::STOCK => "STOCK",
            EquityType::ETF => "ETF",
            EquityType::CRYPTO => "CRYPTO",
        })
    }

}

/// Represents a symbol used to ascribe speculative value
pub struct EquitySymbol {
    pub equity_symbol_id: usize,
    pub equity_symbol_name: String,
    pub equity_symbol_type: EquityType,
    pub equity_symbol_managing_company: String,
    pub equity_symbol_created_timestamp: DateTime,
}

/// An atomic enum to describe the basic action of an equity change event
pub enum ChangeEventType {
    BUY,
    SELL
}

impl ChangeEventType {

    /// Relativizes the effect of a change event for numeration
    pub fn score(&self) -> i8 {
        match self {
            ChangeEventType::BUY => 1,
            ChangeEventType::SELL => -1,
        }
    }

}

impl std::fmt::Display for ChangeEventType {

    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", match self {
            ChangeEventType::BUY => "BUY",
            ChangeEventType::SELL => "SELL",
        })
    }

}

/// An immutable record describing a quantative change for a symbolized
/// equity attributed to an account.
pub struct EquityChangeEvent {
    pub equity_change_event_id: usize,
    pub fiduciary_account_id: u8,
    pub equity_symbol_id: usize,
    pub equity_change_quantity: BigDecimal,
    pub equity_change_cost_basis: BigDecimal,
    pub equity_change_type: ChangeEventType,
    pub equity_change_timestamp: DateTime,
}

impl EquityChangeEvent {

    /// Provides an estimate of the numerated change based on the cost basis
    /// of the equity and the quantity
    pub fn estimated_price(&self) -> BigDecimal {
        DECIMAL_CONTEXT.multiply(&self.equity_change_cost_basis, &self.equity_change_quantity)
    }

    /// Provides the quantity of the event relativized by the event type's score
    pub fn scored_quantity(&self) -> BigDecimal {
        self.equity_change_quantity.clone() * self.equity_change_type.score()
    }

    /// Provides the estimated cost basis of the event relativized by the event type's score
    pub fn scored_cost_basis(&self) -> BigDecimal {
        self.equity_change_cost_basis.clone() * self.equity_change_type.score()
    }

}

mod tests {
    use super::*;
    use std::str::FromStr;

    fn nat(num: BigDecimal) -> BigDecimal {
        num.with_scale_round(2_i64, ROUNDING_MODE)
    }

    #[test]
    fn event_type_score_buy() {
        let event_type = ChangeEventType::BUY;

        assert_eq!(event_type.score(), 1_i8);
    }

    #[test]
    fn event_type_score_sell() {
        let event_type = ChangeEventType::SELL;

        assert_eq!(event_type.score(), -1_i8);
    }

    #[test]
    fn change_event_estimated_price() {
        let event = EquityChangeEvent {
            equity_change_event_id: 0,
            fiduciary_account_id: 0,
            equity_symbol_id: 0,
            equity_change_quantity: BigDecimal::from_str("0.125").unwrap(),
            equity_change_cost_basis: BigDecimal::from_str("82.80").unwrap(),
            equity_change_type: ChangeEventType::BUY,
            equity_change_timestamp: chrono::Local::now().to_utc(),
        };

        assert_eq!(nat(event.estimated_price()).to_plain_string(), "10.35");
    }

    #[test]
    fn change_event_estimated_price_rounded() {
        let event = EquityChangeEvent {
            equity_change_event_id: 0,
            fiduciary_account_id: 0,
            equity_symbol_id: 0,
            equity_change_quantity: BigDecimal::from_str("87.12808").unwrap(),
            equity_change_cost_basis: BigDecimal::from_str("14.91").unwrap(),
            equity_change_type: ChangeEventType::BUY,
            equity_change_timestamp: chrono::Local::now().to_utc(),
        };

        // actual: 1299.0796728
        assert_eq!(nat(event.estimated_price()).to_plain_string(), "1299.08");
    }

    #[test]
    fn change_event_scored_quantity_buy() {
        let event = EquityChangeEvent {
            equity_change_event_id: 0,
            fiduciary_account_id: 0,
            equity_symbol_id: 0,
            equity_change_quantity: BigDecimal::from_str("0.125").unwrap(),
            equity_change_cost_basis: BigDecimal::from_str("82.80").unwrap(),
            equity_change_type: ChangeEventType::BUY,
            equity_change_timestamp: chrono::Local::now().to_utc(),
        };
        
        assert_eq!(event.scored_quantity().to_plain_string(), "0.125");
    }

    #[test]
    fn change_event_scored_quantity_sell() {
        let event = EquityChangeEvent {
            equity_change_event_id: 0,
            fiduciary_account_id: 0,
            equity_symbol_id: 0,
            equity_change_quantity: BigDecimal::from_str("0.125").unwrap(),
            equity_change_cost_basis: BigDecimal::from_str("82.80").unwrap(),
            equity_change_type: ChangeEventType::SELL,
            equity_change_timestamp: chrono::Local::now().to_utc(),
        };
        
        assert_eq!(event.scored_quantity().to_plain_string(), "-0.125");
    }

    #[test]
    fn change_event_scored_estimated_price_buy() {
        let event = EquityChangeEvent {
            equity_change_event_id: 0,
            fiduciary_account_id: 0,
            equity_symbol_id: 0,
            equity_change_quantity: BigDecimal::from_str("0.125").unwrap(),
            equity_change_cost_basis: BigDecimal::from_str("82.80").unwrap(),
            equity_change_type: ChangeEventType::BUY,
            equity_change_timestamp: chrono::Local::now().to_utc(),
        };
        
        assert_eq!(nat(event.scored_cost_basis()).to_plain_string(), "82.80");
    }

    #[test]
    fn change_event_scored_estimated_price_sell() {
        let event = EquityChangeEvent {
            equity_change_event_id: 0,
            fiduciary_account_id: 0,
            equity_symbol_id: 0,
            equity_change_quantity: BigDecimal::from_str("0.125").unwrap(),
            equity_change_cost_basis: BigDecimal::from_str("82.80").unwrap(),
            equity_change_type: ChangeEventType::SELL,
            equity_change_timestamp: chrono::Local::now().to_utc(),
        };
        
        assert_eq!(nat(event.scored_cost_basis()).to_plain_string(), "-82.80");
    }
}