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    /// Called when market tick data is received (mainly for VWAP algorithms).
55    fn on_tick(&mut self, tick: &Tick) -> Result<Vec<ChildOrderRequest>>;
56
57    /// Called when a timer event occurs (mainly for TWAP algorithms).
58    fn on_timer(&mut self) -> Result<Vec<ChildOrderRequest>>;
59
60    /// Request cancellation of the algorithm.
61    fn cancel(&mut self) -> Result<()>;
62
63    /// Return the current state for persistence.
64    fn state(&self) -> serde_json::Value;
65
66    /// Restore algorithm from persisted state.
67    fn from_state(state: serde_json::Value) -> Result<Self>
68    where
69        Self: Sized;
70}
71
72pub mod iceberg;
73pub mod pegged;
74pub mod twap;
75pub mod vwap;
76pub use iceberg::IcebergAlgorithm;
77pub use pegged::PeggedBestAlgorithm;
78pub use twap::TwapAlgorithm;
79pub use vwap::VwapAlgorithm;
80pub mod sniper;
81pub use sniper::SniperAlgorithm;