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
use crate::execution::fill::{FillEvent, Fees};
use crate::execution::error::ExecutionError;
use crate::portfolio::order::OrderEvent;
use chrono::Utc;
use serde::Deserialize;
pub trait FillGenerator {
fn generate_fill(&self, order: &OrderEvent) -> Result<FillEvent, ExecutionError>;
}
#[derive(Debug, Deserialize)]
pub struct Config {
pub simulated_fees_pct: Fees,
}
pub struct SimulatedExecution {
fees_pct: Fees,
}
impl FillGenerator for SimulatedExecution {
fn generate_fill(&self, order: &OrderEvent) -> Result<FillEvent, ExecutionError> {
let fill_value_gross = SimulatedExecution::calculate_fill_value_gross(order);
Ok(FillEvent {
event_type: FillEvent::EVENT_TYPE,
trace_id: order.trace_id,
timestamp: Utc::now(),
exchange: order.exchange.clone(),
symbol: order.symbol.clone(),
market_meta: order.market_meta.clone(),
decision: order.decision.clone(),
quantity: order.quantity,
fill_value_gross,
fees: self.calculate_fees(&fill_value_gross),
})
}
}
impl SimulatedExecution {
pub fn new(cfg: &Config) -> Self {
Self {
fees_pct: cfg.simulated_fees_pct,
}
}
fn calculate_fill_value_gross(order: &OrderEvent) -> f64 {
order.quantity.abs() * order.market_meta.close
}
fn calculate_fees(&self, fill_value_gross: &f64) -> Fees {
Fees {
exchange: self.fees_pct.exchange * fill_value_gross,
slippage: self.fees_pct.slippage * fill_value_gross,
network: self.fees_pct.network * fill_value_gross
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_generate_ok_fill_event_with_valid_order_event_provided() {
let simulated_execution = SimulatedExecution::new(&Config {
simulated_fees_pct: Fees {
exchange: 0.1,
slippage: 0.05,
network: 0.0,
}
});
let mut input_order = OrderEvent::default();
input_order.quantity = 10.0;
input_order.market_meta.close = 10.0;
let actual_result = simulated_execution.generate_fill(&input_order);
let expected_fill_value_gross = 100.0;
let expected_fees = Fees {
exchange: 10.0,
slippage: 5.0,
network: 0.0
};
assert!(actual_result.is_ok());
let actual_result = actual_result.unwrap();
assert_eq!(actual_result.fill_value_gross, expected_fill_value_gross);
assert_eq!(actual_result.fees, expected_fees);
}
#[test]
fn should_calculate_fill_value_gross_correctly() {
let mut input_order = OrderEvent::default();
input_order.quantity = 100.0;
input_order.market_meta.close = 10.0;
let actual = SimulatedExecution::calculate_fill_value_gross(&input_order);
let expected = 100.0 * 10.0;
assert_eq!(actual, expected)
}
#[test]
fn should_calculate_fill_value_gross_correctly_with_negative_order_quantity_provided() {
let mut input_order = OrderEvent::default();
input_order.quantity = -(100.0);
input_order.market_meta.close = 10.0;
let actual = SimulatedExecution::calculate_fill_value_gross(&input_order);
let expected = (100.0 * 10.0) as f64;
assert_eq!(actual, expected)
}
#[test]
fn should_calculate_simulated_fees_correctly() {
let simulated_execution = SimulatedExecution::new(&Config {
simulated_fees_pct: Fees {
exchange: 0.5,
slippage: 0.1,
network: 0.001,
}
});
let input_fill_value_gross = 100.0;
let actual_result = simulated_execution.calculate_fees(&input_fill_value_gross);
let expected = Fees {
exchange: 50.0,
slippage: 10.0,
network: 0.1,
};
assert_eq!(actual_result, expected)
}
}