tesser_execution/algorithm/mod.rs
1//! Execution algorithms for algorithmic order placement.
2
3use anyhow::Result;
4use serde::{Deserialize, Serialize};
5use tesser_core::{Fill, Order, OrderRequest, OrderUpdateRequest, Tick};
6use uuid::Uuid;
7
8/// Actions generated by algorithms for managing their child orders.
9#[derive(Clone, Debug, Deserialize, Serialize)]
10pub enum ChildOrderAction {
11 /// Place a brand new child order.
12 Place(OrderRequest),
13 /// Amend an existing child order in-place.
14 Amend(OrderUpdateRequest),
15}
16
17/// Represents a child order request from an execution algorithm.
18#[derive(Clone, Debug, Deserialize, Serialize)]
19pub struct ChildOrderRequest {
20 /// ID of the parent algorithm that generated this request.
21 pub parent_algo_id: Uuid,
22 /// The action that should be performed.
23 pub action: ChildOrderAction,
24}
25
26/// Current status of an execution algorithm.
27#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
28pub enum AlgoStatus {
29 /// Algorithm is actively working.
30 Working,
31 /// Algorithm has completed successfully.
32 Completed,
33 /// Algorithm has been cancelled.
34 Cancelled,
35 /// Algorithm failed with an error message.
36 Failed(String),
37}
38
39/// Trait defining the behavior of an execution algorithm.
40///
41/// Each execution algorithm is a stateful entity that responds to various events
42/// and generates child orders as needed. The algorithm maintains its own internal
43/// state and can be persisted and restored.
44pub trait ExecutionAlgorithm: Send + Sync {
45 /// Human-readable identifier for persistence/logging.
46 fn kind(&self) -> &'static str;
47
48 /// Returns the unique ID of this algorithm instance.
49 fn id(&self) -> &Uuid;
50
51 /// Returns the current status of the algorithm.
52 fn status(&self) -> AlgoStatus;
53
54 /// Start the algorithm and return any initial child orders.
55 fn start(&mut self) -> Result<Vec<ChildOrderRequest>>;
56
57 /// Called when a child order has been successfully placed.
58 fn on_child_order_placed(&mut self, order: &Order);
59
60 /// Called when a fill is received for one of this algorithm's child orders.
61 fn on_fill(&mut self, fill: &Fill) -> Result<Vec<ChildOrderRequest>>;
62
63 /// Bind a previously placed child order to the algorithm (used during recovery).
64 fn bind_child_order(&mut self, _order: Order) -> Result<()> {
65 Ok(())
66 }
67
68 /// Called when market tick data is received (mainly for VWAP algorithms).
69 fn on_tick(&mut self, tick: &Tick) -> Result<Vec<ChildOrderRequest>>;
70
71 /// Called when a timer event occurs (mainly for TWAP algorithms).
72 fn on_timer(&mut self) -> Result<Vec<ChildOrderRequest>>;
73
74 /// Request cancellation of the algorithm.
75 fn cancel(&mut self) -> Result<()>;
76
77 /// Return the current state for persistence.
78 fn state(&self) -> serde_json::Value;
79
80 /// Restore algorithm from persisted state.
81 fn from_state(state: serde_json::Value) -> Result<Self>
82 where
83 Self: Sized;
84}
85
86pub mod iceberg;
87pub mod pegged;
88pub mod twap;
89pub mod vwap;
90pub use iceberg::IcebergAlgorithm;
91pub use pegged::PeggedBestAlgorithm;
92pub use twap::TwapAlgorithm;
93pub use vwap::VwapAlgorithm;
94pub mod sniper;
95pub use sniper::SniperAlgorithm;
96pub mod trailing_stop;
97pub use trailing_stop::TrailingStopAlgorithm;