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
use crate::FuturesTypes;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
pub struct Position {
size: f64,
entry_price: f64,
leverage: f64,
unrealized_pnl: f64,
}
impl Position {
#[must_use]
#[inline]
pub fn new_init(leverage: f64) -> Self {
debug_assert!(leverage >= 1.0);
Position {
size: 0.0,
entry_price: 0.0,
leverage,
unrealized_pnl: 0.0,
}
}
#[must_use]
#[inline]
pub fn new(size: f64, entry_price: f64, leverage: f64, unrealized_pnl: f64) -> Self {
debug_assert!(size.is_finite());
debug_assert!(entry_price.is_finite());
debug_assert!(leverage.is_finite());
debug_assert!(unrealized_pnl.is_finite());
debug_assert!(leverage >= 1.0);
debug_assert!(entry_price >= 0.0);
Position {
size,
entry_price,
leverage,
unrealized_pnl,
}
}
#[inline(always)]
pub fn size(&self) -> f64 {
self.size
}
#[inline(always)]
pub fn entry_price(&self) -> f64 {
self.entry_price
}
#[inline(always)]
pub fn leverage(&self) -> f64 {
self.leverage
}
#[inline(always)]
pub fn unrealized_pnl(&self) -> f64 {
self.unrealized_pnl
}
pub(crate) fn change_size(&mut self, size_delta: f64, price: f64, futures_type: FuturesTypes) {
debug!("change_size({}, {}, {})", size_delta, price, futures_type);
debug_assert!(price > 0.0);
if self.size > 0.0 {
if self.size + size_delta < 0.0 {
self.entry_price = price;
} else if (self.size + size_delta).abs() > self.size {
self.entry_price = ((self.size.abs() * self.entry_price)
+ (size_delta.abs() * price))
/ (self.size.abs() + size_delta.abs());
}
} else if self.size < 0.0 {
if self.size + size_delta > 0.0 {
self.entry_price = price;
} else if self.size + size_delta < self.size {
self.entry_price = ((self.size.abs() * self.entry_price)
+ (size_delta.abs() * price))
/ (self.size.abs() + size_delta.abs());
}
} else {
self.entry_price = price;
}
self.size += size_delta;
self.update_state(price, futures_type);
}
#[inline]
pub(crate) fn update_state(&mut self, price: f64, futures_type: FuturesTypes) {
self.unrealized_pnl = if self.size != 0.0 {
futures_type.pnl(self.entry_price, price, self.size)
} else {
0.0
};
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::round;
#[test]
fn position_change_size() {
let mut pos = Position::new_init(1.0);
let futures_type = FuturesTypes::Inverse;
pos.change_size(100.0, 100.0, futures_type);
assert_eq!(pos.size, 100.0);
assert_eq!(pos.entry_price, 100.0);
assert_eq!(pos.leverage, 1.0);
assert_eq!(pos.unrealized_pnl, 0.0);
pos.change_size(-50.0, 150.0, futures_type);
assert_eq!(pos.size, 50.0);
assert_eq!(pos.entry_price, 100.0);
assert_eq!(pos.leverage, 1.0);
assert_eq!(round(pos.unrealized_pnl, 2), 0.17);
pos.change_size(50.0, 150.0, futures_type);
assert_eq!(pos.size, 100.0);
assert_eq!(pos.entry_price, 125.0);
assert_eq!(pos.leverage, 1.0);
assert_eq!(round(pos.unrealized_pnl, 2), 0.13);
pos.change_size(-150.0, 150.0, futures_type);
assert_eq!(pos.size, -50.0);
assert_eq!(pos.entry_price, 150.0);
assert_eq!(pos.leverage, 1.0);
assert_eq!(pos.unrealized_pnl, 0.0);
pos.change_size(50.0, 150.0, futures_type);
assert_eq!(pos.size, 0.0);
assert_eq!(pos.entry_price, 150.0);
assert_eq!(pos.leverage, 1.0);
assert_eq!(pos.unrealized_pnl, 0.0);
}
#[test]
fn position_update_state_inverse_futures() {
let mut pos = Position::new(100.0, 100.0, 1.0, 0.0);
pos.update_state(110.0, FuturesTypes::Inverse);
assert_eq!(round(pos.unrealized_pnl, 2), 0.09);
}
#[test]
fn position_update_state_linear_futures() {
let mut pos = Position::new(1.0, 100.0, 1.0, 0.0);
pos.update_state(110.0, FuturesTypes::Linear);
assert_eq!(round(pos.unrealized_pnl, 2), 10.0);
}
}