Skip to main content

df_hyperliquid_core/trade/orders/
trigger.rs

1//! Trigger orders: they rest off the book until the mark crosses the trigger, then fill at
2//! market within a slippage or as a limit. Every take profit and stop loss is one.
3
4use crate::trade::error::TradeError;
5use crate::trade::orders::limit::limit_on;
6use crate::trade::orders::{Order, Side, Tpsl};
7use crate::trade::ticket::{Market, Refusal, positive};
8use crate::utils::rounding::Grid;
9use crate::utils::{Dec, Round};
10
11/// The slippage a market take profit or stop loss takes past its trigger:
12/// the venue's TP/SL page gives market TP/SL orders a tolerance of 10%.
13pub const TRIGGER_SLIPPAGE: Dec = Dec::lit::<1>(1);
14
15/// How a take profit or stop loss fills once its trigger fires.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum ExitPrice {
18    /// At market, no worse than `slippage` past the trigger.
19    Market {
20        /// How far past the trigger it may fill, a fraction: 0.1 is 10%.
21        slippage: Dec,
22    },
23    /// A limit at this price.
24    Limit(Dec),
25}
26
27/// A take profit or stop loss: where it fires and how it fills.
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub struct Exit {
30    /// The mark price that fires it.
31    pub trigger: Dec,
32    /// How it fills once it fires.
33    pub price: ExitPrice,
34}
35
36impl Exit {
37    /// At market, within the venue's own [`TRIGGER_SLIPPAGE`].
38    #[must_use]
39    pub const fn market(trigger: Dec) -> Exit {
40        Exit::market_within(trigger, TRIGGER_SLIPPAGE)
41    }
42
43    /// At market, within `slippage`.
44    #[must_use]
45    pub const fn market_within(trigger: Dec, slippage: Dec) -> Exit {
46        Exit { trigger, price: ExitPrice::Market { slippage } }
47    }
48
49    /// A limit at `price` once `trigger` fires.
50    #[must_use]
51    pub const fn limit(trigger: Dec, price: Dec) -> Exit {
52        Exit { trigger, price: ExitPrice::Limit(price) }
53    }
54}
55
56/// A trigger on the grid: to the nearest tick, since where it fires favours
57/// neither side.
58pub(crate) fn trigger_on(grid: Grid, trigger: Dec) -> Result<Dec, Refusal> {
59    let trigger = positive(trigger, "a trigger price")?;
60    grid.tick(trigger, Round::Nearest).ok_or(Refusal::Trade(TradeError::NoPrice(trigger)))
61}
62
63/// The price a trigger order carries: its bound past the trigger at market,
64/// or its limit.
65pub(crate) fn fill_price(grid: Grid, trigger: Dec, price: ExitPrice, side: Side) -> Result<(Dec, bool), Refusal> {
66    match price {
67        ExitPrice::Market { slippage } => Ok((grid.market_bound(trigger, slippage, side)?, true)),
68        ExitPrice::Limit(price) => Ok((limit_on(grid, price, side, "a limit price")?, false)),
69    }
70}
71
72/// A reduce-only trigger closing `size` on `side`.
73pub(crate) fn exit_order(market: &Market, side: Side, size: Dec, tpsl: Tpsl, exit: Exit) -> Result<Order, Refusal> {
74    let trigger = trigger_on(market.grid(), exit.trigger)?;
75    let (price, is_market) = fill_price(market.grid(), trigger, exit.price, side)?;
76    Ok(Order::trigger(market.asset(), side, price, size, trigger, tpsl, is_market).reduce_only())
77}
78
79#[cfg(test)]
80#[path = "tests/trigger.rs"]
81mod tests;