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