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
#![deny(missing_docs, missing_crate_level_docs)]
extern crate pretty_env_logger;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde;
mod acc_tracker;
mod account;
mod exchange;
mod futures_type;
mod margin;
mod orders;
mod position;
mod utils;
mod validator;
mod welford_online;
pub use exchange::Exchange;
pub use futures_type::FuturesType;
pub use margin::Margin;
pub use orders::Order;
pub use position::Position;
pub(crate) use account::Account;
pub(crate) use utils::{max, min};
pub(crate) use validator::Validator;
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum Side {
Buy,
Sell,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum OrderError {
MaxActiveOrders,
InvalidLimitPrice,
InvalidTriggerPrice,
InvalidOrderSize,
NotEnoughAvailableBalance,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum OrderType {
Market,
Limit,
StopMarket,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum FeeType {
Maker,
Taker,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct Config {
pub fee_maker: f64,
pub fee_taker: f64,
pub starting_balance: f64,
pub use_candles: bool,
pub leverage: f64,
pub futures_type: FuturesType,
}
pub fn round(val: f64, prec: i32) -> f64 {
((val * 10.0_f64.powi(prec)).round()) / 10.0_f64.powi(prec)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_round() {
assert_eq!(round(0.111111, 0), 0.0);
assert_eq!(round(0.111111, 1), 0.1);
assert_eq!(round(0.111111, 2), 0.11);
assert_eq!(round(0.111111, 3), 0.111);
assert_eq!(round(0.111111, 4), 0.1111);
assert_eq!(round(0.111111, 5), 0.11111);
assert_eq!(round(0.111111, 6), 0.111111);
}
}