scuriolus 0.2.0

Scuriolus is a modular trading bot platform.
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
mod core_error;
mod kline_store;
mod storage;

use chrono::Utc;
pub use core_error::{CoreError, CoreResult};
pub use kline_store::KlineStore;
use std::cmp::min;

#[cfg(test)]
use crate::data::SpecificOrderDetails;
use crate::{
    clock::ClockBase,
    core::storage::Storage,
    data::{Crypto, Order, OrderDraft, OrderStatus},
    market::{Balance, Kline, KlinesParams, Market},
};

/// Number of maximum consecutive requests to get klines
const MAX_KLINES_REQUESTS: u32 = 5;

/// An interface with the market and different data sources.
pub struct Core<M: Market> {
    market: M,
    storage: Storage,
    kline_store: KlineStore<M>,
    clock: <M as Market>::_Clock,
}

impl<M: Market> Core<M> {
    pub async fn new(
        name: String,
        market: M,
        kline_store: &KlineStore<M>,
        clock: <M as Market>::_Clock,
    ) -> CoreResult<Self> {
        let storage = Storage::new(&name).await?;
        let kline_store = kline_store.clone();
        Ok(Self {
            market,
            storage,
            kline_store,
            clock,
        })
    }

    /// Send an order to the market and save it to the database.
    ///
    /// First, it creates an `Order` from the given `OrderDraft` and sends it to the market.
    /// If the sending is successful, it sets the market's ID of the order and saves the order
    /// to the database. If the sending fails, it sets the status of the order to `Failed` and logs
    /// the error.
    ///
    /// # Arguments
    ///
    /// * `draft` - The draft of the order to be sent.
    ///
    /// | Order Type | Side | `price`     | `quantity::Asset` | `quantity::Quote`| Notes                         |
    /// | ---------- | ---- | ----------- | ----------------- | ---------------- | ----------------------------- |
    /// | Limit      | Buy  | ✅ required | ✅ required      | ❌ not allowed   | Buy at a specific price.      |
    /// | Limit      | Sell | ✅ required | ✅ required      | ❌ not allowed   | Sell at a specific price.     |
    /// | Market     | Buy  | ❌ ignored  | ❌ not allowed   | ✅ required      | Buy for a given quote amount. |
    /// | Market     | Sell | ❌ ignored  | ✅ required      | ❌ not allowed   | Sell a given asset quantity.  |
    ///
    /// # Returns
    ///
    /// * `Result<ORder, Error>` - On success, returns the order. On failure, returns an `Error`.
    pub async fn send_order(&self, draft: OrderDraft) -> CoreResult<Order<M::_OrderDetails>> {
        let mut order: Order<M::_OrderDetails> = Order::new(
            draft.asset,
            draft.quote,
            draft.side,
            draft.order_type,
            draft.amount,
            draft.price,
        )?;

        tracing::info!(
            "{} sending order {}, {:#?} {} at {}",
            self.storage.get_name(),
            order.id(),
            order.side(),
            order.asset(),
            self.clock.now()
        );

        match self.market.send_order(&mut order).await {
            Ok(()) => {
                tracing::debug!("Order {} sent successfully", order.id());
                self.storage.add_order(order.clone()).await?;
                //TODO if order at market price : update soon
                Ok(order)
            }
            Err(e) => {
                order.set_status(OrderStatus::Failed);
                tracing::warn!("Failed to send order {} : {}", order.id(), e);
                self.storage.add_order(order).await?;
                Err(e)
            }
        }
    }

    pub fn clock(&self) -> &M::_Clock {
        &self.clock
    }

    /// Get the list of opened orders on the market and update their status with the information from the market.
    ///
    /// # Returns
    ///
    /// Returns the list of updated orders.
    pub async fn update_opened_orders(&self) -> CoreResult<Vec<Order<M::_OrderDetails>>> {
        tracing::debug!("Updating opened orders");
        let mut orders: Vec<Order<M::_OrderDetails>> = self.storage.opened_orders().await;
        for order in &mut orders {
            tracing::trace!("Updating order {}", order.id());
            self.update_order(order).await?;
        }
        Ok(orders)
    }

    /// Cancel all opened orders on the market and update their status accordingly.
    ///
    /// # Returns
    ///
    /// Returns the list of orders with their updated statuses after being canceled.
    pub async fn cancel_opened_orders(&self) -> CoreResult<Vec<Order<M::_OrderDetails>>> {
        let mut orders: Vec<Order<M::_OrderDetails>> = self.storage.opened_orders().await;
        for order in &mut orders {
            self.cancel_order(order).await?;
        }
        Ok(orders)
    }

    /// Get an order by its id from the database.
    ///
    /// # Arguments
    ///
    /// * `id` - The id of the order to retrieve.
    ///
    /// # Returns
    ///
    /// Returns the order with the given id if it exists
    pub async fn get_order(&self, id: &str) -> Option<Order<M::_OrderDetails>> {
        self.storage.get_order(id).await
    }

    /// Returns the list of opened orders from the database.
    ///
    /// # Returns
    ///
    /// Returns a vector of opened orders.
    pub async fn common_get_opened_orders(&self) -> Vec<Order<M::_OrderDetails>> {
        self.storage.opened_orders().await
    }

    /// Updates an order's status and details based on the market's response.
    ///
    /// This function sends an update request for the specified order to the market. If the order's
    /// status or details have changed after the update, it updates the local order accordingly and saves the changes in the
    /// database.
    ///
    /// # Arguments
    ///
    /// * `order` - A mutable reference to the `Order` object to be updated.
    ///
    /// # Returns
    ///
    /// Returns `Ok(true)` if the order's status or details changed
    pub async fn update_order(&self, order: &mut Order<M::_OrderDetails>) -> CoreResult<bool> {
        tracing::debug!("Updating order {}", order.id());
        let query_output = self.market.update_order(order).await?;
        if *order.status() != query_output.status.into()
            || *order.price() != Some(query_output.price)
            || *order.executed_qty() != query_output.executed_quantity
            || *order.cummulative_quote_qty() != query_output.cummulative_quote_quantity
        {
            order.set_status(query_output.status.into());
            if *order.price() != Some(query_output.price) {
                order.set_price(query_output.price)?;
            }
            order.set_executed(
                query_output.executed_quantity,
                query_output.cummulative_quote_quantity,
            );

            self.storage.update_order(order.clone()).await?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    /// Cancels an order and updates its status and details based on the market's response.
    ///
    /// This function sends a cancellation request for the specified order to the market. If the order's
    /// status or details have changed after the cancellation, it updates the local order accordingly and saves the changes in the
    /// database.
    ///
    /// # Arguments
    ///
    /// * `order` - A mutable reference to the `Order` object to be canceled.
    ///
    /// # Returns
    ///
    /// Returns `Ok(true)` if the order's status or details changed (by update meanwhile or cancellation)
    pub async fn cancel_order(&self, order: &mut Order<M::_OrderDetails>) -> CoreResult<bool> {
        tracing::debug!("Canceling order {}", order.id());
        let cancel_output = self.market.cancel_order(order).await?;

        if *order.status() == cancel_output.status.into() {
            return Ok(false);
        }

        order.set_status(cancel_output.status.into());
        if *order.price() != Some(cancel_output.price) {
            order.set_price(cancel_output.price)?;
        }
        if *order.executed_qty() != cancel_output.executed_quantity
            || *order.cummulative_quote_qty() != cancel_output.cummulative_quote_quantity
        {
            order.set_executed(
                cancel_output.executed_quantity,
                cancel_output.cummulative_quote_quantity,
            );
        }
        self.storage.update_order(order.clone()).await?;
        Ok(true)
    }

    /// Get klines from the market.
    ///
    /// # Arguments
    ///
    /// * `params`: The parameters to query the klines with.
    ///
    /// # Returns
    ///
    /// Returns the list of klines that were requested.
    pub async fn get_klines(&self, mut params: KlinesParams) -> CoreResult<Vec<Kline>> {
        let mut safety = 0;

        tracing::debug!("Core : Getting klines");
        tracing::trace!("Params: {:#?}", params);

        loop {
            tracing::trace!("Core klines loop round {}", safety);
            let (local_klines, complete) = self.kline_store.try_get_klines(&params).await?;

            if complete {
                tracing::debug!("Klines acquired");
                return Ok(local_klines);
            }

            if safety > MAX_KLINES_REQUESTS {
                return Err(CoreError::param_error("Max klines requests reached"));
            }

            if !local_klines.is_empty() {
                params = KlinesParams::new(
                    *params.asset(),
                    *params.quote(),
                    *params.interval(),
                    local_klines.last().unwrap().close_time,
                    *params.end_time(),
                );
            }

            tracing::debug!("Getting klines from Market");

            let max_endtime = *params.start_time()
                + params
                    .interval()
                    .time_delta()
                    .checked_mul(self.market.klines_limit() - 1)
                    .ok_or(CoreError::comput_error("TimeDelta overflow"))?;

            let now = Utc::now();

            let klines_params_extended = KlinesParams::new(
                *params.asset(),
                *params.quote(),
                *params.interval(),
                *params.start_time(),
                min(max_endtime, now),
            );

            tracing::trace!(
                "Params modified end_date: {:#?}",
                klines_params_extended.end_time()
            );

            let klines: Vec<Kline> = self.market.klines(klines_params_extended).await?;

            if klines.is_empty() {
                return Err(CoreError::UnavailableData);
            }

            self.kline_store.inject_klines(&params, klines).await?;

            safety += 1;
        }
    }

    /// Get the current balance of a crypto on the market.
    ///
    /// # Arguments
    ///
    /// * `crypto`: The crypto to get the balance of.
    ///
    /// # Returns
    ///
    /// Returns the balance of the given crypto.
    pub async fn get_balance(&self, crypto: Crypto) -> CoreResult<Balance> {
        self.market.get_balance(crypto).await
    }

    #[cfg(test)]
    pub async fn clear_storage<D: SpecificOrderDetails>(&self) -> CoreResult<()> {
        self.storage.clear::<D>().await
    }
}

#[cfg(test)]
mod tests {

    use std::future::ready;

    use chrono::{DateTime, TimeDelta, TimeZone as _, Utc};
    use rust_decimal::Decimal;

    use crate::{
        clock::{ClockFactory as _, RunningCheatClockFactory},
        core::{Core, KlineStore},
        data::{
            Crypto, EmptySpecificOrderDetails, KlineInterval, OrderDraft, OrderStatus, Quantity,
        },
        market::{
            mexc_enums, mock_context, CancelOrderOutput, Kline, KlinesParams, MockMarket,
            QueryOrderOutput,
        },
    };

    pub const ASSET: Crypto = Crypto::BTC;
    pub const QUOTE: Crypto = Crypto::USDT;

    fn klines_start() -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap()
    }

    fn klines_end() -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2024, 1, 1, 0, 1, 0).unwrap()
    }

    #[tokio::test]
    async fn reload_and_update_orders() {
        let name = "core-reload_and_update_orders".to_string();

        let mut market1 = MockMarket::new();

        market1
            .expect_send_order()
            .returning(|_| Box::pin(ready(Ok(()))));

        let mut market2 = MockMarket::new();

        market2.expect_update_order().returning(|order| {
            Box::pin(ready(Ok(QueryOrderOutput {
                symbol: "BTCUSDT".to_string(),
                original_client_order_id: None,
                order_id: order.id().to_string(),
                client_order_id: None,
                price: order.price().unwrap(),
                original_quantity: order.quantity().get_amount(),
                executed_quantity: *order.executed_qty(),
                cummulative_quote_quantity: *order.cummulative_quote_qty(),
                status: mexc_enums::OrderStatus::New,
                time_in_force: None,
                order_type: *order.order_type(),
                side: *order.side(),
                stop_price: order.price().unwrap(),
                time: Utc::now(),
                update_time: Utc::now(),
                is_working: true,
            })))
        });

        let _ctx = mock_context();
        let kline_store = KlineStore::test_new(&name).await;

        let clock = RunningCheatClockFactory { date: Utc::now() }
            .build()
            .unwrap()
            .1;

        let order = {
            let core = Core::new(name.clone(), market1, &kline_store, clock.clone())
                .await
                .unwrap();
            core.clear_storage::<EmptySpecificOrderDetails>()
                .await
                .unwrap();

            let order = core
                .send_order(OrderDraft {
                    asset: ASSET,
                    quote: QUOTE,
                    side: mexc_enums::OrderSide::Buy,
                    order_type: mexc_enums::OrderType::Limit,
                    amount: Quantity::Asset(Decimal::ONE),
                    price: Some(Decimal::from(10000)),
                })
                .await
                .unwrap();

            drop(core);
            order
        };

        // wait for db to fully close
        tokio::time::sleep(std::time::Duration::from_millis(100)).await;

        let core = Core::new(name.clone(), market2, &kline_store, clock.clone())
            .await
            .unwrap();

        let updated_orders = core.update_opened_orders().await.unwrap();

        assert_eq!(updated_orders.len(), 1);
        assert_eq!(core.get_order(order.id()).await.unwrap(), updated_orders[0]);
    }

    #[tokio::test]
    async fn cancel_orders() {
        let name = "core-cancel_orders".to_string();
        let mut market = MockMarket::new();
        market
            .expect_send_order()
            .returning(|_| Box::pin(ready(Ok(()))));

        market.expect_cancel_order().returning(|order| {
            Box::pin(ready(Ok(CancelOrderOutput {
                symbol: "BTCUSDT".to_string(),
                original_client_order_id: None,
                order_id: order.id().to_string(),
                client_order_id: None,
                price: order.price().unwrap(),
                original_quantity: order.quantity().get_amount(),
                executed_quantity: *order.executed_qty(),
                cummulative_quote_quantity: *order.cummulative_quote_qty(),
                status: mexc_enums::OrderStatus::Canceled,
                time_in_force: None,
                order_type: *order.order_type(),
                side: *order.side(),
            })))
        });

        let _ctx = mock_context();
        let kline_store = KlineStore::test_new(&name).await;
        let clock = RunningCheatClockFactory { date: Utc::now() }
            .build()
            .unwrap()
            .1;
        let core = Core::new(name, market, &kline_store, clock).await.unwrap();
        core.clear_storage::<EmptySpecificOrderDetails>()
            .await
            .unwrap();

        let mut order = core
            .send_order(OrderDraft {
                asset: ASSET,
                quote: QUOTE,
                side: mexc_enums::OrderSide::Buy,
                order_type: mexc_enums::OrderType::Limit,
                amount: Quantity::Asset(Decimal::ONE),
                price: Some(Decimal::from(10000)),
            })
            .await
            .unwrap();

        let canceled_orders = core.cancel_opened_orders().await.unwrap();

        assert_eq!(canceled_orders.len(), 1);
        order.set_status(OrderStatus::Canceled);
        assert_eq!(order, canceled_orders[0]);
    }

    #[tokio::test]
    async fn get_klines() {
        let name = "core-get_klines".to_string();
        let _ctx = mock_context();
        let kline_store = KlineStore::test_new(&name).await;
        kline_store.clear(ASSET, QUOTE).await.unwrap();

        let mut market = MockMarket::new();
        market.expect_klines_limit().return_const(100);

        market.expect_klines().once().returning(move |params| {
            Box::pin(ready(Kline::vec_over(
                *params.interval(),
                *params.start_time(),
                *params.end_time(),
                Kline {
                    open_time: klines_start(),
                    open: Decimal::ZERO,
                    high: Decimal::from(42),
                    low: Decimal::ZERO,
                    close: Decimal::ONE,
                    volume: Decimal::ONE,
                    close_time: klines_end(),
                    quote_asset_volume: Decimal::ONE,
                },
            )))
        });

        let clock = RunningCheatClockFactory { date: Utc::now() }
            .build()
            .unwrap()
            .1;

        let core = Core::new(name, market, &kline_store, clock).await.unwrap();
        core.clear_storage::<EmptySpecificOrderDetails>()
            .await
            .unwrap();

        let start = klines_start();
        let end = klines_end();

        //TODO : do twice to check cache
        let klines = core
            .get_klines(KlinesParams::new(
                ASSET,
                QUOTE,
                KlineInterval::OneMinute,
                start + TimeDelta::minutes(3),
                end + TimeDelta::minutes(3),
            ))
            .await
            .unwrap();

        let kline = // no clone
            Kline {
                open_time: start+ TimeDelta::minutes(3),
                open: Decimal::ZERO,
                high: Decimal::from(42),
                low: Decimal::ZERO,
                close: Decimal::ONE,
                volume: Decimal::ONE,
                close_time: end+ TimeDelta::minutes(3),
                quote_asset_volume: Decimal::ONE,
            };

        assert_eq!(klines.len(), 1);
        assert_eq!(klines[0].high, kline.high);
    }
}