discretionary_engine 1.0.0

LaTeX of trading
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
mod approaching_limit;
mod dummy_market;
mod sar;
mod trailing_stop;
use std::{collections::HashSet, str::FromStr};

use approaching_limit::{ApproachingLimit, ApproachingLimitWrapper};
use color_eyre::eyre::{bail, Result};
use dummy_market::DummyMarketWrapper;
use sar::{Sar, SarWrapper};
use tokio::{sync::mpsc, task::JoinSet};
use tracing::instrument;
use trailing_stop::{TrailingStop, TrailingStopWrapper};
use uuid::Uuid;
use v_utils::{io::Percent, trades::Side};

use crate::exchange_apis::order_types::{ConceptualOrder, ConceptualOrderPercents, ProtocolOrderId};

/// Used when determining sizing or the changes in it, in accordance to the current distribution of rm on types of algorithms.
///
/// Size is by default equally distributed amongst the protocols of the same `ProtocolType`, to total 101% for each type with at least one representative.
/// Note that total size is is 101% for both the stop and normal orders (because they are on the different sides of the price).
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy, derive_new::new)]
pub enum ProtocolType {
	Momentum,
	TP,
	SL,
	StopEntry,
}

pub trait ProtocolTrait {
	type Params;
	/// Requested orders are being sent over the mspc with uuid of the protocol on each batch, as we want to replace the previous requested batch if any.
	fn attach(&self, set: &mut JoinSet<Result<()>>, tx_orders: mpsc::Sender<ProtocolOrders>, asset: String, protocol_side: Side) -> Result<()>;
	fn update_params(&self, params: Self::Params) -> Result<()>;
	fn get_type(&self) -> ProtocolType;
}

// HACK: Protocol enum. Seems suboptimal {\{{
#[derive(Debug, Clone)]
pub enum Protocol {
	TrailingStop(TrailingStopWrapper),
	Sar(SarWrapper),
	ApproachingLimit(ApproachingLimitWrapper),
	DummyMarket(DummyMarketWrapper),
}
impl FromStr for Protocol {
	type Err = eyre::Report;

	fn from_str(spec: &str) -> Result<Self> {
		if let Ok(ts) = TrailingStopWrapper::from_str(spec) {
			Ok(Protocol::TrailingStop(ts))
		} else if let Ok(sar) = SarWrapper::from_str(spec) {
			Ok(Protocol::Sar(sar))
		} else if let Ok(al) = ApproachingLimitWrapper::from_str(spec) {
			Ok(Protocol::ApproachingLimit(al))
		} else if let Ok(dm) = DummyMarketWrapper::from_str(spec) {
			Ok(Protocol::DummyMarket(dm))
		} else {
			bail!("Could not convert string to any Protocol\nString: {spec}")
		}
	}
}
impl Protocol {
	pub fn attach(&self, position_set: &mut JoinSet<Result<()>>, tx_orders: mpsc::Sender<ProtocolOrders>, asset: String, protocol_side: Side) -> Result<()> {
		match self {
			Protocol::TrailingStop(ts) => ts.attach(position_set, tx_orders, asset, protocol_side),
			Protocol::Sar(sar) => sar.attach(position_set, tx_orders, asset, protocol_side),
			Protocol::ApproachingLimit(al) => al.attach(position_set, tx_orders, asset, protocol_side),
			Protocol::DummyMarket(dm) => dm.attach(position_set, tx_orders, asset, protocol_side),
		}
	}

	pub fn update_params(&self, params: ProtocolParams) -> Result<()> {
		match self {
			Protocol::TrailingStop(ts) => match params {
				ProtocolParams::TrailingStop(ts_params) => ts.update_params(ts_params),
				_ => Err(eyre::Report::msg("Mismatched params")),
			},
			Protocol::Sar(sar) => match params {
				ProtocolParams::Sar(sar_params) => sar.update_params(sar_params),
				_ => Err(eyre::Report::msg("Mismatched params")),
			},
			Protocol::ApproachingLimit(al) => match params {
				ProtocolParams::ApproachingLimit(al_params) => al.update_params(al_params),
				_ => Err(eyre::Report::msg("Mismatched params")),
			},
			Protocol::DummyMarket(_) => Ok(()),
		}
	}

	pub fn get_type(&self) -> ProtocolType {
		match self {
			Protocol::TrailingStop(ts) => ts.get_type(),
			Protocol::Sar(sar) => sar.get_type(),
			Protocol::ApproachingLimit(al) => al.get_type(),
			Protocol::DummyMarket(dm) => dm.get_type(),
		}
	}

	pub fn signature(&self) -> String {
		match self {
			Protocol::TrailingStop(ts) => ts.signature(),
			Protocol::Sar(sar) => sar.signature(),
			Protocol::ApproachingLimit(al) => al.signature(),
			Protocol::DummyMarket(dm) => dm.signature(),
		}
	}
}

#[derive(Debug, Clone, derive_new::new)]
pub enum ProtocolParams {
	TrailingStop(TrailingStop),
	Sar(Sar),
	ApproachingLimit(ApproachingLimit),
}
impl From<TrailingStop> for ProtocolParams {
	fn from(ts: TrailingStop) -> Self {
		ProtocolParams::TrailingStop(ts)
	}
}
impl From<Sar> for ProtocolParams {
	fn from(sar: Sar) -> Self {
		ProtocolParams::Sar(sar)
	}
}
impl From<ApproachingLimit> for ProtocolParams {
	fn from(al: ApproachingLimit) -> Self {
		ProtocolParams::ApproachingLimit(al)
	}
}
//,}}}

#[instrument]
pub fn interpret_protocol_specs(protocol_specs: Vec<String>) -> Result<Vec<Protocol>> {
	let protocol_specs: Vec<String> = protocol_specs.into_iter().filter(|s| s != "").collect();
	if protocol_specs.len() == 0 {
		bail!("No protocols specified");
	}
	assert_eq!(protocol_specs.len(), protocol_specs.iter().collect::<HashSet<&String>>().len()); // protocol specs are later used as their IDs
	let mut protocols = Vec::new();
	for spec in protocol_specs {
		let protocol = Protocol::from_str(&spec)?;
		protocols.push(protocol);
	}
	Ok(protocols)
}

#[derive(Clone, Debug, Default, derive_new::new)]
pub struct ProtocolFill {
	pub id: ProtocolOrderId,
	pub qty: f64,
}

#[derive(Clone, Debug, Default, derive_new::new)]
pub struct ProtocolFills {
	pub key: Uuid,
	pub fills: Vec<ProtocolFill>,
}

/// Position's knowledge of the protocols in use.
#[derive(Clone, Debug, Default)]
pub struct ProtocolDynamicInfo {
	pub fills: Vec<f64>,
	pub protocol_orders: ProtocolOrders,
}
impl ProtocolDynamicInfo {
	pub fn new(protocol_orders: ProtocolOrders) -> Self {
		let fills = protocol_orders.empty_fills_mask();
		Self { fills, protocol_orders }
	}

	pub fn update_fills(&mut self, fills: Vec<f64>) {
		self.fills = fills;
	}

	pub fn update_fill_at(&mut self, i: usize, fill: f64) {
		self.fills[i] += fill;
	}

	pub fn update_orders(&mut self, orders: ProtocolOrders) {
		self.protocol_orders = orders;
	}
}

#[derive(Clone, Debug, Default, derive_new::new)]
pub struct RecalculatedAllocation {
	pub orders: Vec<ConceptualOrder<ProtocolOrderId>>,
	/// None -> protocol is in play
	/// Some -> the remaining value should be redistributed amongst the remaining protocols (of same type if any, otherwise all). Negative value means we overdid it; same rules apply.
	pub leftovers: Option<f64>,
}

#[derive(Clone, Debug, Default, derive_new::new, Copy)]
pub struct RecalculateOrdersPerOrderInfo {
	pub filled: f64,
	pub min_possible_qty: f64,
}

/// Wrapper around Orders, which allows for updating the target after a partial fill, without making a new request to the protocol.
///
/// NB: the protocol itself must internally uphold the equality of ids attached to orders to corresponding fields of ProtocolOrders, as well as to ensure that all possible orders the protocol can ether request are initialized in every ProtocolOrders instance it outputs.
#[derive(Debug, Clone, Default)]
pub struct ProtocolOrders {
	pub protocol_id: String,
	pub __orders: Vec<Option<ConceptualOrderPercents>>, // pub for testing purposes
}
impl ProtocolOrders {
	#[instrument(skip(orders))]
	pub fn new(protocol_id: String, orders: Vec<Option<ConceptualOrderPercents>>) -> Self {
		assert_ne!(
			orders.len(),
			0,
			"Semantically makes no sense. Protocol must always send Vec<Some<Order>> for all possible orders it will ever send, them being None if at current iteration they are ignored"
		);

		let mut symbols_set = HashSet::new();
		for order in &orders.iter().flatten().collect::<Vec<&ConceptualOrderPercents>>() {
			symbols_set.insert(order.symbol.clone());
		}

		assert_eq!(symbols_set.len(), 1, "Different symbols in return of the same protocol are not yet implemented");
		Self { protocol_id, __orders: orders }
	}

	pub fn empty_fills_mask(&self) -> Vec<f64> {
		vec![0.0; self.__orders.len()]
	}

	/// Order is *NOT* preserved. Orders with no remaining size are completely excluded from the output.
	///
	//HACK: doesn't yet work with multiple symbols.
	// Matter of fact, none of this does. Currently all Positions assume working with specific asset.
	#[instrument(skip(self))]
	pub fn recalculate_protocol_orders_allocation(
		&self,
		per_order_infos: &[RecalculateOrdersPerOrderInfo],
		protocol_controlled_notional: f64,
		min_qty_any_ordertype: f64,
	) -> RecalculatedAllocation {
		assert_eq!(self.__orders.len(), per_order_infos.len());

		let mut left_controlled_notional = protocol_controlled_notional - per_order_infos.iter().map(|info| info.filled).sum::<f64>();
		// Must be comparing against the largest of min_qties, as we can't force protocols to send their largest order always of the order_type with smallest min_qty.
		if left_controlled_notional < min_qty_any_ordertype {
			return RecalculatedAllocation {
				orders: Vec::new(),
				leftovers: Some(left_controlled_notional),
			};
		}
		let mut per_order_additional_percents_from_skipped = Percent::new(0.0);

		let orders: Vec<ConceptualOrder<ProtocolOrderId>> = self
			.__orders
			.iter()
			.enumerate()
			.filter_map(|(i, order)| match order {
				Some(order) => {
					let desired_notional_i = *(order.qty_percent_of_controlled + per_order_additional_percents_from_skipped) * left_controlled_notional;
					if desired_notional_i > per_order_infos[i].min_possible_qty {
						let order = ConceptualOrder::new(
							ProtocolOrderId::new(self.protocol_id.clone(), i),
							order.order_type,
							order.symbol.clone(),
							order.side,
							desired_notional_i,
						);
						left_controlled_notional -= desired_notional_i;
						Some(order)
					} else {
						per_order_additional_percents_from_skipped += *order.qty_percent_of_controlled / (self.__orders.len() - (i + 1)) as f64;
						None
					}
				}
				None => None,
			})
			.collect();

		RecalculatedAllocation { orders, leftovers: None }
	}
}

#[cfg(test)]
mod tests {
	use insta::assert_debug_snapshot;
	use lazy_static::lazy_static;
	use v_utils::{io::Percent, trades::Side};

	use super::*;
	use crate::exchange_apis::{
		order_types::{ConceptualMarket, ConceptualOrderType},
		Market, Symbol,
	};

	mod recalculate_protocol_orders_allocation {
		use super::*;

		#[test]
		fn test_apply_mask() {
			let orders = ProtocolOrders::new(
				"test".to_string(),
				vec![Some(ConceptualOrderPercents::new(
					ConceptualOrderType::Market(ConceptualMarket::new(Percent(1.0))),
					Symbol::new("BTC".to_string(), "USDT".to_string(), Market::BinanceFutures),
					Side::Buy,
					Percent::new(1.0),
				))],
			);

			let protocol_controlled_notional = 2.0;
			let per_order_infos = vec![RecalculateOrdersPerOrderInfo::new(1.1, 0.007)];
			let min_qty_any_ordertype = 0.007;
			let got = orders.recalculate_protocol_orders_allocation(&per_order_infos, protocol_controlled_notional, min_qty_any_ordertype);
			assert_debug_snapshot!(got, @r###"
   RecalculatedAllocation {
       orders: [
           ConceptualOrder {
               id: ProtocolOrderId {
                   protocol_signature: "test",
                   ordinal: 0,
               },
               order_type: Market(
                   ConceptualMarket {
                       maximum_slippage_percent: Percent(
                           1.0,
                       ),
                   },
               ),
               symbol: Symbol {
                   base: "BTC",
                   quote: "USDT",
                   market: BinanceFutures,
               },
               side: Buy,
               qty_notional: 0.8999999999999999,
           },
       ],
       leftovers: None,
   }
   "###);
		}

		#[test]
		fn nones() {
			let orders = ProtocolOrders::new(
				"test".to_string(),
				vec![
					None,
					Some(ConceptualOrderPercents::new(
						ConceptualOrderType::Market(ConceptualMarket::new(Percent(2.0))),
						Symbol::new("ADA".to_string(), "USDT".to_string(), Market::BinanceFutures),
						Side::Buy,
						Percent::new(1.0),
					)),
				],
			);

			let protocol_controlled_notional = 100.0;
			let per_order_infos = vec![RecalculateOrdersPerOrderInfo::new(0.0, 10.0), RecalculateOrdersPerOrderInfo::new(0.0, 10.0)];
			let min_qty_any_ordertype = 10.0;
			let recalculated_allocation = orders.recalculate_protocol_orders_allocation(&per_order_infos, protocol_controlled_notional, min_qty_any_ordertype);

			let qties = recalculated_allocation.orders.into_iter().map(|co| co.qty_notional).collect::<Vec<f64>>();
			assert_debug_snapshot!(qties, @r###"
  [
      100.0,
  ]
  "###);
		}

		mod two_diff_orders {
			use super::*;

			lazy_static! {
				static ref ORDERS: ProtocolOrders = ProtocolOrders::new(
					"test".to_string(),
					vec![
						Some(ConceptualOrderPercents::new(
							ConceptualOrderType::Market(ConceptualMarket::new(Percent(1.0))),
							Symbol::new("ADA".to_string(), "USDT".to_string(), Market::BinanceFutures),
							Side::Sell,
							Percent::new(0.25),
						)),
						Some(ConceptualOrderPercents::new(
							ConceptualOrderType::Market(ConceptualMarket::new(Percent(1.0))),
							Symbol::new("ADA".to_string(), "USDT".to_string(), Market::BinanceFutures),
							Side::Buy,
							Percent::new(0.75),
						)),
					],
				);
				static ref MIN_QTY_ANY_ORDERTYPE: f64 = 10.0;
			}

			#[test]
			fn full_fill() {
				let protocol_controlled_notional = 100.0;
				let per_order_infos = vec![RecalculateOrdersPerOrderInfo::new(75.0, 10.0), RecalculateOrdersPerOrderInfo::new(25.0, 10.0)];
				let got = ORDERS.recalculate_protocol_orders_allocation(&per_order_infos, protocol_controlled_notional, *MIN_QTY_ANY_ORDERTYPE);
				assert_debug_snapshot!(got, @r###"
    RecalculatedAllocation {
        orders: [],
        leftovers: Some(
            0.0,
        ),
    }
    "###);
			}

			#[test]
			fn overfill() {
				let protocol_controlled_notional = 2.0;
				let per_order_infos = vec![RecalculateOrdersPerOrderInfo::new(25.0, 10.0), RecalculateOrdersPerOrderInfo::new(25.0, 10.0)];
				let got = ORDERS.recalculate_protocol_orders_allocation(&per_order_infos, protocol_controlled_notional, *MIN_QTY_ANY_ORDERTYPE);
				//TODO!!!: start returning by how much we were off. In the next snapshot we overdo it by 48.0, yet all we get is a success with empty vec of orders to deploy.
				assert_debug_snapshot!(got, @r###"
    RecalculatedAllocation {
        orders: [],
        leftovers: Some(
            -48.0,
        ),
    }
    "###);
			}

			#[test]
			fn size_redistribution_on_hitting_min_qty() {
				let protocol_controlled_notional = 15.0;
				let per_order_infos = vec![RecalculateOrdersPerOrderInfo::new(0.0, 10.0), RecalculateOrdersPerOrderInfo::new(0.0, 10.0)];
				let got = ORDERS.recalculate_protocol_orders_allocation(&per_order_infos, protocol_controlled_notional, *MIN_QTY_ANY_ORDERTYPE);
				//TODO!!!: start returning by how much we were off. In the next snapshot we overdo it by 48.0, yet all we get is a success with empty vec of orders to deploy.
				assert_debug_snapshot!(got, @r###"
    RecalculatedAllocation {
        orders: [
            ConceptualOrder {
                id: ProtocolOrderId {
                    protocol_signature: "test",
                    ordinal: 1,
                },
                order_type: Market(
                    ConceptualMarket {
                        maximum_slippage_percent: Percent(
                            1.0,
                        ),
                    },
                ),
                symbol: Symbol {
                    base: "ADA",
                    quote: "USDT",
                    market: BinanceFutures,
                },
                side: Buy,
                qty_notional: 15.0,
            },
        ],
        leftovers: None,
    }
    "###);
			}
		}
	}
}