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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use crate::fraction;
use crate::support::rational::Rounding;
use crate::to_u128_wrapper;
use crate::transcendental::saturating_powi_high_precision;
use crate::types::{Balance, Fraction, Ratio};
use num_traits::{One, Zero};
use primitive_types::{U128, U256, U512};
pub type EmaPrice = Ratio;
pub type EmaVolume = (Balance, Balance, Balance, Balance);
pub type EmaLiquidity = (Balance, Balance);
pub fn calculate_new_by_integrating_incoming(
previous: (EmaPrice, EmaVolume, EmaLiquidity),
incoming: (EmaPrice, EmaVolume, EmaLiquidity),
smoothing: Fraction,
) -> (EmaPrice, EmaVolume, EmaLiquidity) {
let (prev_price, prev_volume, prev_liquidity) = previous;
let (incoming_price, incoming_volume, incoming_liquidity) = incoming;
let new_price = price_weighted_average(prev_price, incoming_price, smoothing);
let new_volume = volume_weighted_average(prev_volume, incoming_volume, smoothing);
let new_liquidity = liquidity_weighted_average(prev_liquidity, incoming_liquidity, smoothing);
(new_price, new_volume, new_liquidity)
}
pub fn update_outdated_to_current(
iterations: u32,
outdated: (EmaPrice, EmaVolume, EmaLiquidity),
update_with: (EmaPrice, EmaLiquidity),
smoothing: Fraction,
) -> (EmaPrice, EmaVolume, EmaLiquidity) {
let (prev_price, prev_volume, prev_liquidity) = outdated;
let (incoming_price, incoming_liquidity) = update_with;
let smoothing = exp_smoothing(smoothing, iterations);
let new_price = price_weighted_average(prev_price, incoming_price, smoothing);
let new_volume = volume_weighted_average(prev_volume, (0, 0, 0, 0), smoothing);
let new_liquidity = liquidity_weighted_average(prev_liquidity, incoming_liquidity, smoothing);
(new_price, new_volume, new_liquidity)
}
pub fn iterated_price_ema(iterations: u32, prev: EmaPrice, incoming: EmaPrice, smoothing: Fraction) -> EmaPrice {
price_weighted_average(prev, incoming, exp_smoothing(smoothing, iterations))
}
pub fn iterated_balance_ema(iterations: u32, prev: Balance, incoming: Balance, smoothing: Fraction) -> Balance {
balance_weighted_average(prev, incoming, exp_smoothing(smoothing, iterations))
}
pub fn iterated_volume_ema(iterations: u32, prev: EmaVolume, smoothing: Fraction) -> EmaVolume {
volume_weighted_average(prev, (0, 0, 0, 0), exp_smoothing(smoothing, iterations))
}
pub fn iterated_liquidity_ema(
iterations: u32,
prev: EmaLiquidity,
incoming: EmaLiquidity,
smoothing: Fraction,
) -> EmaLiquidity {
liquidity_weighted_average(prev, incoming, exp_smoothing(smoothing, iterations))
}
pub fn exp_smoothing(smoothing: Fraction, iterations: u32) -> Fraction {
debug_assert!(smoothing <= Fraction::one());
let complement = Fraction::one() - smoothing;
let exp_complement: Fraction = saturating_powi_high_precision(complement, iterations);
debug_assert!(exp_complement <= Fraction::one());
Fraction::one() - exp_complement
}
pub fn smoothing_from_period(period: u64) -> Fraction {
fraction::frac(2, u128::from(period.max(1)).saturating_add(1))
}
pub fn price_weighted_average(prev: EmaPrice, incoming: EmaPrice, weight: Fraction) -> EmaPrice {
debug_assert!(weight <= Fraction::one(), "weight must be <= 1");
if incoming >= prev {
rounding_add(prev, multiply(weight, saturating_sub(incoming, prev)), Rounding::Down)
} else {
rounding_sub(prev, multiply(weight, saturating_sub(prev, incoming)), Rounding::Up)
}
}
pub fn balance_weighted_average(prev: Balance, incoming: Balance, weight: Fraction) -> Balance {
debug_assert!(weight <= Fraction::one(), "weight must be <= 1");
if incoming >= prev {
prev + fraction::multiply_by_balance(weight, incoming - prev)
} else {
prev - fraction::multiply_by_balance(weight, prev - incoming)
}
}
pub fn volume_weighted_average(prev: EmaVolume, incoming: EmaVolume, weight: Fraction) -> EmaVolume {
debug_assert!(weight <= Fraction::one(), "weight must be <= 1");
let (prev_a_in, prev_b_out, prev_a_out, prev_b_in) = prev;
let (a_in, b_out, a_out, b_in) = incoming;
(
balance_weighted_average(prev_a_in, a_in, weight),
balance_weighted_average(prev_b_out, b_out, weight),
balance_weighted_average(prev_a_out, a_out, weight),
balance_weighted_average(prev_b_in, b_in, weight),
)
}
pub fn liquidity_weighted_average(
prev: (Balance, Balance),
incoming: (Balance, Balance),
weight: Fraction,
) -> (Balance, Balance) {
debug_assert!(weight <= Fraction::one(), "weight must be <= 1");
let (prev_a, prev_b) = prev;
let (a, b) = incoming;
(
balance_weighted_average(prev_a, a, weight),
balance_weighted_average(prev_b, b, weight),
)
}
pub(super) fn saturating_sub(l: EmaPrice, r: EmaPrice) -> (U256, U256) {
if l.n.is_zero() || r.n.is_zero() {
return (l.n.into(), l.d.into());
}
let (l_n, l_d, r_n, r_d) = to_u128_wrapper!(l.n, l.d, r.n, r.d);
let n = l_n.full_mul(r_d).saturating_sub(r_n.full_mul(l_d));
let d = l_d.full_mul(r_d);
(n, d)
}
pub(super) fn multiply(f: Fraction, (r_n, r_d): (U256, U256)) -> (U512, U512) {
debug_assert!(f <= Fraction::ONE);
if f.is_zero() || r_n.is_zero() {
return (U512::zero(), U512::one());
} else if f.is_one() {
return (r_n.into(), r_d.into());
}
let n = r_n.full_mul(U256::from(f.to_bits()));
let d = r_d.full_mul(U256::from(crate::fraction::DIV));
(n, d)
}
pub(super) fn round((n, d): (U512, U512), rounding: Rounding) -> (U512, U512) {
let shift = n.bits().max(d.bits()).saturating_sub(383); if shift > 0 {
let min_n = if n.is_zero() { U512::zero() } else { U512::one() };
let (bias_n, bias_d) = rounding.to_bias(1);
(
(n >> shift).saturating_add(bias_n.into()).max(min_n),
(d >> shift).saturating_add(bias_d.into()).max(U512::one()),
)
} else {
(n, d)
}
}
pub(super) fn round_to_rational((n, d): (U512, U512), rounding: Rounding) -> EmaPrice {
let shift = n.bits().max(d.bits()).saturating_sub(128);
let (n, d) = if shift > 0 {
let min_n = if n.is_zero() { 0 } else { 1 };
let (bias_n, bias_d) = rounding.to_bias(1);
let shifted_n = (n >> shift).low_u128();
let shifted_d = (d >> shift).low_u128();
(
shifted_n.saturating_add(bias_n).max(min_n),
shifted_d.saturating_add(bias_d).max(1),
)
} else {
(n.low_u128(), d.low_u128())
};
EmaPrice::new(n, d)
}
pub(super) fn rounding_add(l: EmaPrice, (r_n, r_d): (U512, U512), rounding: Rounding) -> EmaPrice {
if l.is_zero() {
return round_to_rational((r_n, r_d), Rounding::Nearest);
} else if r_n.is_zero() {
return l;
}
let (l_n, l_d) = (U512::from(l.n), U512::from(l.d));
let (r_n, r_d) = round((r_n, r_d), rounding);
let n = l_n.saturating_mul(r_d).saturating_add(r_n.saturating_mul(l_d));
let d = l_d.saturating_mul(r_d);
round_to_rational((n, d), rounding)
}
pub(super) fn rounding_sub(l: EmaPrice, (r_n, r_d): (U512, U512), rounding: Rounding) -> EmaPrice {
if l.is_zero() || r_n.is_zero() {
return l;
}
let (l_n, l_d) = (U512::from(l.n), U512::from(l.d));
let (r_n, r_d) = round((r_n, r_d), rounding);
let n = l_n.saturating_mul(r_d).saturating_sub(r_n.saturating_mul(l_d));
let d = l_d.saturating_mul(r_d);
round_to_rational((n, d), rounding)
}