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
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
/// Describes the margin information of the account
pub struct Margin {
    /// The wallet balance of account
    /// denoted in QUOTE currency when using linear futures
    /// denoted in BASE currency when using inverse futures
    wallet_balance: f64,
    /// The position margin of account, same denotation as wallet_balance
    position_margin: f64,
    /// The order margin of account, same denotation as wallet_balance
    order_margin: f64,
    /// The available balance of account, same denotation as wallet_balance
    available_balance: f64,
}

impl Margin {
    /// Create a new margin account with an initial balance
    /// # Panics
    //  In debug mode, if the input values don't make sense
    #[must_use]
    pub fn new_init(init_balance: f64) -> Self {
        debug_assert!(init_balance > 0.0);
        debug_assert!(init_balance.is_finite());

        Margin {
            wallet_balance: init_balance,
            position_margin: 0.0,
            order_margin: 0.0,
            available_balance: init_balance,
        }
    }

    /// Create a new Margin with all fields custom
    /// # Panics
    /// In debug mode, if the input values don't make sense
    #[must_use]
    pub fn new(
        wallet_balance: f64,
        position_margin: f64,
        order_margin: f64,
        available_balance: f64,
    ) -> Self {
        debug_assert!(wallet_balance.is_finite());
        debug_assert!(position_margin.is_finite());
        debug_assert!(order_margin.is_finite());
        debug_assert!(available_balance.is_finite());

        debug_assert!(wallet_balance > 0.0);
        debug_assert!(position_margin >= 0.0);
        debug_assert!(order_margin >= 0.0);
        debug_assert!(available_balance >= 0.0);

        debug_assert!(position_margin <= wallet_balance);
        debug_assert!(order_margin <= wallet_balance);
        debug_assert!(available_balance <= wallet_balance);

        Margin {
            wallet_balance,
            position_margin,
            order_margin,
            available_balance,
        }
    }

    /// Return the wallet balance of account
    /// denoted in QUOTE currency when using linear futures
    /// denoted in BASE currency when using inverse futures
    #[inline(always)]
    pub fn wallet_balance(&self) -> f64 {
        self.wallet_balance
    }

    /// Return the position margin of account, same denotation as wallet_balance
    #[inline(always)]
    pub fn position_margin(&self) -> f64 {
        self.position_margin
    }

    /// Return the used order margin of account, same denotation as wallet_balance
    #[inline(always)]
    pub fn order_margin(&self) -> f64 {
        self.order_margin
    }

    /// Return the available balance of account, same denotation as wallet_balance
    #[inline(always)]
    pub fn available_balance(&self) -> f64 {
        self.available_balance
    }

    /// Set a new order margin
    pub(crate) fn set_order_margin(&mut self, order_margin: f64) {
        debug!("set_order_margin: {}", order_margin);
        debug_assert!(order_margin >= 0.0);

        self.order_margin = order_margin;
        self.available_balance = self.wallet_balance - self.position_margin - self.order_margin;

        debug!("set_order_margin: self: {:?}", self);

        debug_assert!(self.available_balance >= 0.0);
    }

    /// Set the position margin by a given delta and adjust available balance accordingly
    pub(crate) fn set_position_margin(&mut self, val: f64) {
        debug!("set_position_margin({})", val);

        debug_assert!(val.is_finite());
        debug_assert!(val >= 0.0);

        self.position_margin = val;
        self.available_balance = self.wallet_balance - self.order_margin - self.position_margin;

        debug!("set_position_margin: self: {:?}", self);

        debug_assert!(self.position_margin >= 0.0);
        debug_assert!(self.position_margin <= self.wallet_balance);
        debug_assert!(self.available_balance >= 0.0);
        debug_assert!(self.available_balance <= self.wallet_balance);
    }

    /// Change the balance by a given delta e.g. from realizing profit or loss
    pub(crate) fn change_balance(&mut self, delta: f64) {
        debug_assert!(delta.is_finite());

        self.wallet_balance += delta;
        self.available_balance += delta;

        debug!("change_balance: self: {:?}", self);

        debug_assert!(self.wallet_balance >= 0.0);
        debug_assert!(self.available_balance >= 0.0);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::round;

    #[test]
    fn margin_set_order_margin() {
        let mut margin = Margin::new_init(1.0);
        margin.set_order_margin(1.0);
        assert_eq!(margin.wallet_balance, 1.0);
        assert_eq!(margin.position_margin, 0.0);
        assert_eq!(margin.order_margin, 1.0);
        assert_eq!(margin.available_balance, 0.0);
    }

    #[test]
    #[should_panic]
    fn margin_set_order_margin_panic_0() {
        let mut margin = Margin::new_init(1.0);
        margin.set_order_margin(1.01);
    }

    #[test]
    #[should_panic]
    fn margin_set_order_margin_panic_1() {
        let mut margin = Margin::new_init(1.0);
        margin.set_order_margin(-0.1);
    }

    #[test]
    fn margin_set_position_margin() {
        let mut margin = Margin::new_init(1.0);
    }

    #[test]
    fn margin_change_balance() {
        let mut margin = Margin::new_init(1.0);

        margin.change_balance(0.05);
        assert_eq!(margin.wallet_balance, 1.05);
        assert_eq!(margin.position_margin, 0.0);
        assert_eq!(margin.order_margin, 0.0);
        assert_eq!(margin.available_balance, 1.05);

        margin.change_balance(-0.1);
        assert_eq!(round(margin.wallet_balance, 2), 0.95);
        assert_eq!(margin.position_margin, 0.0);
        assert_eq!(margin.order_margin, 0.0);
        assert_eq!(round(margin.available_balance, 2), 0.95);
    }

    #[test]
    #[should_panic]
    fn margin_change_balance_panic_0() {
        let mut margin = Margin::new_init(1.0);

        margin.change_balance(-1.01);
    }
}