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
use chrono::{DateTime, Utc};
use uuid::Uuid;
use crate::data::MarketMeta;
use crate::portfolio::OrderEvent;
use crate::execution::error::ExecutionError;
use serde::{Deserialize, Serialize};
use crate::strategy::Decision;
pub mod error;
pub mod simulated;
pub trait ExecutionClient {
fn generate_fill(&self, order: &OrderEvent) -> Result<FillEvent, ExecutionError>;
}
#[derive(Clone, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
pub struct FillEvent {
pub event_type: &'static str,
pub trace_id: Uuid,
pub timestamp: DateTime<Utc>,
pub exchange: &'static str,
pub symbol: String,
pub market_meta: MarketMeta,
pub decision: Decision,
pub quantity: f64,
pub fill_value_gross: f64,
pub fees: Fees,
}
impl FillEvent {
pub const EVENT_TYPE: &'static str = "Fill";
pub fn builder() -> FillEventBuilder {
FillEventBuilder::new()
}
}
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug, Default, Deserialize, Serialize)]
pub struct Fees {
pub exchange: FeeAmount,
pub slippage: FeeAmount,
pub network: FeeAmount,
}
impl Fees {
pub fn calculate_total_fees(&self) -> f64 {
self.exchange + self.network + self.slippage
}
}
pub type FeeAmount = f64;
#[derive(Debug, Default)]
pub struct FillEventBuilder {
pub trace_id: Option<Uuid>,
pub timestamp: Option<DateTime<Utc>>,
pub exchange: Option<&'static str>,
pub symbol: Option<String>,
pub market_meta: Option<MarketMeta>,
pub decision: Option<Decision>,
pub quantity: Option<f64>,
pub fill_value_gross: Option<f64>,
pub fees: Option<Fees>,
}
impl FillEventBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn trace_id(self, value: Uuid) -> Self {
Self {
trace_id: Some(value),
..self
}
}
pub fn timestamp(self, value: DateTime<Utc>) -> Self {
Self {
timestamp: Some(value),
..self
}
}
pub fn exchange(self, value: &'static str) -> Self {
Self {
exchange: Some(value),
..self
}
}
pub fn symbol(self, value: &'static str) -> Self {
Self {
exchange: Some(value),
..self
}
}
pub fn market_meta(self, value: MarketMeta) -> Self {
Self {
market_meta: Some(value),
..self
}
}
pub fn decision(self, value: Decision) -> Self {
Self {
decision: Some(value),
..self
}
}
pub fn quantity(self, value: f64) -> Self {
Self {
quantity: Some(value),
..self
}
}
pub fn fill_value_gross(self, value: f64) -> Self {
Self {
fill_value_gross: Some(value),
..self
}
}
pub fn fees(self, value: Fees) -> Self {
Self {
fees: Some(value),
..self
}
}
pub fn build(self) -> Result<FillEvent, ExecutionError> {
Ok(FillEvent {
event_type: FillEvent::EVENT_TYPE,
trace_id: self.trace_id.ok_or(ExecutionError::BuilderIncomplete)?,
timestamp: self.timestamp.ok_or(ExecutionError::BuilderIncomplete)?,
exchange: self.exchange.ok_or(ExecutionError::BuilderIncomplete)?,
symbol: self.symbol.ok_or(ExecutionError::BuilderIncomplete)?,
market_meta: self.market_meta.ok_or(ExecutionError::BuilderIncomplete)?,
decision: self.decision.ok_or(ExecutionError::BuilderIncomplete)?,
quantity: self.quantity.ok_or(ExecutionError::BuilderIncomplete)?,
fill_value_gross: self.fill_value_gross.ok_or(ExecutionError::BuilderIncomplete)?,
fees: self.fees.ok_or(ExecutionError::BuilderIncomplete)?,
})
}
}