lfest 0.77.0

A leveraged perpetual futures exchange for simulated trading and backtesting
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use std::cmp::Ordering;

use fpdec::{Dec, Decimal};
use getset::{CopyGetters, Getters};
use tracing::{debug, trace};

use crate::{
    prelude::{
        Transaction, TransactionAccounting, EXCHANGE_FEE_ACCOUNT, TREASURY_ACCOUNT,
        USER_POSITION_MARGIN_ACCOUNT, USER_WALLET_ACCOUNT,
    },
    quote,
    types::{Currency, MarginCurrency, QuoteCurrency},
};

/// Describes the position information of the account.
/// It assumes isolated margining mechanism, because the margin is directly associated with the position.
// TODO: change generic to `M`.
#[derive(Debug, Clone, Default, Eq, PartialEq, Getters, CopyGetters)]
pub struct PositionInner<Q>
where
    Q: Currency,
    Q::PairedCurrency: MarginCurrency,
{
    /// The number of futures contracts making up the position.
    #[getset(get_copy = "pub")]
    quantity: Q,

    /// The total cost paid for the position (not margin though).
    #[getset(get_copy = "pub")]
    total_cost: Q::PairedCurrency,

    /// The outstanding fees of the position that will be payed when reducing the position.
    #[getset(get_copy = "pub")]
    outstanding_fees: Q::PairedCurrency,
}

impl<Q> PositionInner<Q>
where
    Q: Currency,
    Q::PairedCurrency: MarginCurrency,
{
    /// Create a new instance.
    ///
    /// # Panics:
    /// if `quantity` or `entry_price` are invalid.
    pub fn new<T>(
        quantity: Q,
        entry_price: QuoteCurrency,
        accounting: &mut T,
        init_margin_req: Decimal,
        fees: Q::PairedCurrency,
    ) -> Self
    where
        T: TransactionAccounting<Q::PairedCurrency>,
    {
        trace!("new position: qty {quantity} @ {entry_price}");
        assert!(quantity > Q::new_zero());
        assert!(entry_price > quote!(0));

        let margin = quantity.convert(entry_price) * init_margin_req;
        let transaction =
            Transaction::new(USER_POSITION_MARGIN_ACCOUNT, USER_WALLET_ACCOUNT, margin);
        accounting
            .create_margin_transfer(transaction)
            .expect("margin transfer for opening a new position works.");

        Self {
            quantity,
            total_cost: quantity.convert(entry_price),
            outstanding_fees: fees,
        }
    }

    /// The average price at which this position was entered into.
    pub fn entry_price(&self) -> QuoteCurrency {
        self.total_cost.price_paid_for_qty(self.quantity)
    }

    /// Return the positions unrealized profit and loss
    /// denoted in QUOTE when using linear futures,
    /// denoted in BASE when using inverse futures
    pub fn unrealized_pnl(&self, mark_to_market_price: QuoteCurrency) -> Q::PairedCurrency {
        Q::PairedCurrency::pnl(self.entry_price(), mark_to_market_price, self.quantity)
    }

    /// Add contracts to the position.
    pub(crate) fn increase_contracts<T>(
        &mut self,
        qty: Q,
        entry_price: QuoteCurrency,
        accounting: &mut T,
        init_margin_req: Decimal,
        fees: Q::PairedCurrency,
    ) where
        T: TransactionAccounting<Q::PairedCurrency>,
    {
        debug!(
            "increase_contracts: qty: {qty} @ {entry_price}; self: {:?}",
            self
        );
        assert!(qty > Q::new_zero());
        assert!(entry_price > quote!(0));

        let value = qty.convert(entry_price);

        self.quantity += qty;
        self.outstanding_fees += fees;
        self.total_cost += value;

        let margin = value * init_margin_req;
        let transaction =
            Transaction::new(USER_POSITION_MARGIN_ACCOUNT, USER_WALLET_ACCOUNT, margin);
        accounting
            .create_margin_transfer(transaction)
            .expect("is an internal call and must work");
    }

    /// Decrease the position.
    pub(crate) fn decrease_contracts<T>(
        &mut self,
        qty: Q,
        liquidation_price: QuoteCurrency,
        accounting: &mut T,
        init_margin_req: Decimal,
        direction_multiplier: Decimal,
        fees: Q::PairedCurrency,
    ) where
        T: TransactionAccounting<Q::PairedCurrency>,
    {
        debug!(
            "decrease_contracts: qty: {qty} @ {liquidation_price}; self: {:?}",
            self
        );
        assert!(qty > Q::new_zero());
        assert!(qty <= self.quantity);
        debug_assert!(direction_multiplier == Dec!(1) || direction_multiplier == Dec!(-1));

        let entry_price = self.entry_price();

        self.quantity -= qty;
        self.outstanding_fees += fees;
        self.total_cost -= qty.convert(entry_price);

        debug_assert!(self.quantity >= Q::new_zero());
        if *self.quantity.as_ref() == Dec!(0) {
            assert_eq!(*self.total_cost.as_ref(), Dec!(0));
        }

        let pnl =
            Q::PairedCurrency::pnl(entry_price, liquidation_price, qty * direction_multiplier);
        match pnl.cmp(&Q::PairedCurrency::new_zero()) {
            Ordering::Greater => {
                let transaction = Transaction::new(USER_WALLET_ACCOUNT, TREASURY_ACCOUNT, pnl);
                accounting
                    .create_margin_transfer(transaction)
                    .expect("margin transfer must work");
            }
            Ordering::Less => {
                let transaction =
                    Transaction::new(TREASURY_ACCOUNT, USER_WALLET_ACCOUNT, pnl.abs());
                accounting
                    .create_margin_transfer(transaction)
                    .expect("margin transfer must work");
            }
            Ordering::Equal => {}
        }
        let margin_to_free = qty.convert(entry_price) * init_margin_req;
        let transaction = Transaction::new(
            USER_WALLET_ACCOUNT,
            USER_POSITION_MARGIN_ACCOUNT,
            margin_to_free,
        );
        accounting
            .create_margin_transfer(transaction)
            .expect("margin transfer must work");

        let transaction = Transaction::new(
            EXCHANGE_FEE_ACCOUNT,
            USER_WALLET_ACCOUNT,
            self.outstanding_fees,
        );
        accounting
            .create_margin_transfer(transaction)
            .expect("margin transfer must work");
        self.outstanding_fees = Q::PairedCurrency::new_zero();
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        base,
        prelude::{BaseCurrency, InMemoryTransactionAccounting, Side},
        TEST_FEE_MAKER,
    };

    #[test_case::test_matrix([1, 2, 5])]
    fn position_inner_new(leverage: u32) {
        let mut ta = InMemoryTransactionAccounting::new(quote!(1000));
        let init_margin_req = Dec!(1) / Decimal::from(leverage);
        let qty = base!(0.5);
        let entry_price = quote!(100);
        let fees = qty.convert(entry_price) * TEST_FEE_MAKER;
        let pos = PositionInner::new(qty, entry_price, &mut ta, init_margin_req, fees);
        assert_eq!(
            pos,
            PositionInner {
                quantity: qty,
                total_cost: quote!(50),
                outstanding_fees: fees,
            }
        );
        assert_eq!(pos.entry_price(), quote!(100));
        assert_eq!(
            ta.margin_balance_of(USER_POSITION_MARGIN_ACCOUNT).unwrap(),
            quote!(50) * init_margin_req
        );
        assert_eq!(
            ta.margin_balance_of(USER_WALLET_ACCOUNT).unwrap(),
            quote!(1000) - quote!(50) * init_margin_req
        );
    }

    #[test_case::test_matrix([1, 2, 5])]
    fn position_inner_increase_contracts(leverage: u32) {
        let mut ta = InMemoryTransactionAccounting::new(quote!(1000));
        let init_margin_req = Dec!(1) / Decimal::from(leverage);
        let qty = base!(0.5);
        let entry_price = quote!(100);
        let fee_0 = qty.convert(entry_price) * TEST_FEE_MAKER;
        let mut pos = PositionInner::new(qty, entry_price, &mut ta, init_margin_req, fee_0);

        let entry_price = quote!(150);
        let fee_1 = qty.convert(entry_price) * TEST_FEE_MAKER;
        pos.increase_contracts(qty, entry_price, &mut ta, init_margin_req, fee_1);
        assert_eq!(
            pos,
            PositionInner {
                quantity: base!(1),
                total_cost: quote!(125),
                outstanding_fees: fee_0 + fee_1
            }
        );
        assert_eq!(pos.entry_price(), quote!(125));
        assert_eq!(
            ta.margin_balance_of(USER_POSITION_MARGIN_ACCOUNT).unwrap(),
            quote!(125) * init_margin_req
        );
        assert_eq!(
            ta.margin_balance_of(USER_WALLET_ACCOUNT).unwrap(),
            quote!(1000) - quote!(125) * init_margin_req
        );
    }

    #[test_case::test_matrix([1, 2, 5])]
    fn position_inner_decrease_contracts_basic(leverage: u32) {
        let mut ta = InMemoryTransactionAccounting::new(quote!(1000));
        let init_margin_req = Dec!(1) / Decimal::from(leverage);
        let qty = base!(5);
        let entry_price = quote!(100);
        let fees = qty.convert(entry_price) * TEST_FEE_MAKER;
        let mut pos = PositionInner::new(qty, entry_price, &mut ta, init_margin_req, fees);
        pos.decrease_contracts(
            qty / Dec!(2),
            entry_price,
            &mut ta,
            init_margin_req,
            Dec!(1),
            fees / Dec!(2),
        );
        assert_eq!(
            pos,
            PositionInner {
                quantity: base!(2.5),
                total_cost: quote!(250),
                outstanding_fees: quote!(0),
            }
        );
        assert_eq!(pos.entry_price(), quote!(100));
        let margin = quote!(250) * init_margin_req;
        assert_eq!(
            ta.margin_balance_of(USER_POSITION_MARGIN_ACCOUNT).unwrap(),
            margin,
        );
        assert_eq!(
            ta.margin_balance_of(USER_WALLET_ACCOUNT).unwrap(),
            quote!(1000) - margin - fees * Dec!(1.5)
        );

        pos.decrease_contracts(
            qty / Dec!(2),
            entry_price,
            &mut ta,
            init_margin_req,
            Dec!(1),
            fees / Dec!(2),
        );
        assert_eq!(
            pos,
            PositionInner {
                quantity: base!(0),
                total_cost: quote!(0),
                outstanding_fees: quote!(0),
            }
        );
        assert_eq!(pos.entry_price(), quote!(0));
        assert_eq!(
            ta.margin_balance_of(USER_POSITION_MARGIN_ACCOUNT).unwrap(),
            quote!(0)
        );
        assert_eq!(
            ta.margin_balance_of(USER_WALLET_ACCOUNT).unwrap(),
            quote!(1000) - fees * Dec!(2)
        );
    }

    #[test_case::test_matrix(
        [1, 2, 5],
        [Side::Buy, Side::Sell]
    )]
    fn position_inner_decrease_contracts_win(leverage: u32, position_side: Side) {
        let mut ta = InMemoryTransactionAccounting::new(quote!(1000));
        let init_margin_req = Dec!(1) / Decimal::from(leverage);
        let qty = base!(5);
        let entry_price = quote!(100);
        let fees = qty.convert(entry_price) * TEST_FEE_MAKER;
        let mut pos = PositionInner::new(qty, entry_price, &mut ta, init_margin_req, fees);

        let exit_price = quote!(110);
        let side_mult = match position_side {
            Side::Buy => Dec!(1),
            Side::Sell => Dec!(-1),
        };
        pos.decrease_contracts(
            qty / Dec!(2),
            exit_price,
            &mut ta,
            init_margin_req,
            side_mult,
            fees / Dec!(2),
        );

        assert_eq!(pos.quantity(), base!(2.5));
        assert_eq!(pos.entry_price(), quote!(100));
        assert_eq!(pos.total_cost(), quote!(250));
        let margin = quote!(250) * init_margin_req;
        assert_eq!(
            ta.margin_balance_of(USER_POSITION_MARGIN_ACCOUNT).unwrap(),
            margin
        );
        let profit = quote!(25) * side_mult;
        assert_eq!(
            ta.margin_balance_of(USER_WALLET_ACCOUNT).unwrap(),
            quote!(1000) + profit - margin - fees * Dec!(1.5)
        );
    }

    #[test_case::test_matrix(
        [1, 2, 5],
        [Side::Buy, Side::Sell]
    )]
    fn position_inner_decrease_contracts_2(leverage: u32, position_side: Side) {
        let mut ta = InMemoryTransactionAccounting::new(quote!(1000));
        let init_margin_req = Dec!(1) / Decimal::from(leverage);
        let qty = base!(5);
        let entry_price = quote!(100);
        let fees = qty.convert(entry_price) * TEST_FEE_MAKER;
        let mut pos = PositionInner::new(qty, entry_price, &mut ta, init_margin_req, fees);

        let exit_price = quote!(90);
        let side_mult = match position_side {
            Side::Buy => Dec!(1),
            Side::Sell => Dec!(-1),
        };
        pos.decrease_contracts(
            qty / Dec!(2),
            exit_price,
            &mut ta,
            init_margin_req,
            side_mult,
            fees / Dec!(2),
        );

        assert_eq!(pos.quantity(), base!(2.5));
        assert_eq!(pos.entry_price(), quote!(100));
        assert_eq!(pos.total_cost(), quote!(250));
        let margin = quote!(250) * init_margin_req;
        assert_eq!(
            ta.margin_balance_of(USER_POSITION_MARGIN_ACCOUNT).unwrap(),
            margin
        );
        let loss = quote!(25) * side_mult;
        assert_eq!(
            ta.margin_balance_of(USER_WALLET_ACCOUNT).unwrap(),
            quote!(1000) - loss - margin - fees * Dec!(1.5)
        );
    }

    #[tracing_test::traced_test]
    #[test_case::test_matrix([1, 2, 5])]
    fn position_inner_decrease_contracts_inverse(leverage: u32) {
        let mut ta = InMemoryTransactionAccounting::new(base!(10));
        let init_margin_req = Dec!(1) / Decimal::from(leverage);
        let qty = quote!(500);
        let entry_price = quote!(100);
        let fees = qty.convert(entry_price) * TEST_FEE_MAKER;
        let mut pos = PositionInner::new(qty, entry_price, &mut ta, init_margin_req, fees);

        let exit_price = quote!(200);
        pos.decrease_contracts(
            qty / Dec!(2),
            exit_price,
            &mut ta,
            init_margin_req,
            Dec!(1),
            fees / Dec!(2),
        );

        assert_eq!(pos.quantity(), quote!(250));
        assert_eq!(pos.entry_price(), quote!(100));
        assert_eq!(pos.total_cost(), base!(2.5));
        let margin = base!(2.5) * init_margin_req;
        assert_eq!(
            ta.margin_balance_of(USER_POSITION_MARGIN_ACCOUNT).unwrap(),
            margin
        );
        assert_eq!(
            ta.margin_balance_of(USER_WALLET_ACCOUNT).unwrap(),
            base!(11.25) - margin - fees * Dec!(1.5)
        );
    }

    #[test_case::test_matrix([1, 2, 5, 9])]
    fn position_inner_entry_price_linear(qty: u32) {
        let qty = BaseCurrency::from(Decimal::from(qty));
        let mut ta = InMemoryTransactionAccounting::new(quote!(1000));
        let init_margin_req = Dec!(1);
        let fees = quote!(0);
        let pos = PositionInner::new(qty, quote!(100), &mut ta, init_margin_req, fees);
        assert_eq!(pos.entry_price(), quote!(100));
    }

    #[test_case::test_matrix([10, 20, 50, 90])]
    fn position_inner_entry_price_inverse(qty: u32) {
        let qty = QuoteCurrency::from(Decimal::from(qty));
        let mut ta = InMemoryTransactionAccounting::new(base!(10));
        let init_margin_req = Dec!(1);
        let fees = base!(0);
        let pos = PositionInner::new(qty, quote!(100), &mut ta, init_margin_req, fees);
        assert_eq!(pos.entry_price(), quote!(100));
    }
}