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