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
#![deny(missing_docs, rustdoc::missing_crate_level_docs)]
#![warn(clippy::all)]
extern crate pretty_env_logger;
#[macro_use]
extern crate log;
#[macro_use]
extern crate serde;
mod acc_tracker;
mod account;
mod config;
mod cornish_fisher;
mod errors;
mod exchange;
mod futures_type;
mod limit_order_margin;
mod margin;
mod orders;
mod position;
mod utils;
mod validator;
mod welford_online;
pub use acc_tracker::AccTracker;
pub use acc_tracker::ReturnsSource;
pub use account::Account;
pub use config::Config;
pub use errors::{Error, OrderError, Result};
pub use exchange::Exchange;
pub use futures_type::FuturesTypes;
pub use margin::Margin;
pub use orders::Order;
pub use position::Position;
use std::fmt::Formatter;
pub(crate) use utils::{max, min};
pub(crate) use validator::Validator;
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum Side {
Buy,
Sell,
}
impl Side {
#[inline(always)]
pub fn as_integer(&self) -> u64 {
match self {
Side::Buy => 0,
Side::Sell => 1,
}
}
#[inline(always)]
pub fn from_integer(val: u64) -> Result<Self> {
match val {
0 => Ok(Side::Buy),
1 => Ok(Side::Sell),
_ => Err(Error::ParseError),
}
}
}
impl std::fmt::Display for Side {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum OrderType {
Market,
Limit,
}
impl OrderType {
#[inline(always)]
pub fn as_integer(&self) -> u64 {
match self {
OrderType::Market => 0,
OrderType::Limit => 1,
}
}
#[inline(always)]
pub fn from_integer(val: u64) -> Result<Self> {
match val {
0 => Ok(Self::Market),
1 => Ok(Self::Limit),
_ => Err(Error::ParseError),
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum FeeType {
Maker,
Taker,
}
#[inline(always)]
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);
}
}