Skip to main content

df_hyperliquid_core/trade/orders/
entry.rs

1//! An entry: one order at market or at a limit, with its take profit and stop loss grouped
2//! `normalTpsl` when it has them. Each order is worth at least [`MINIMUM_ORDER_VALUE`].
3
4use crate::trade::builder::BuilderSetting;
5use crate::trade::orders::trigger::Exit;
6use crate::trade::orders::{Cloid, Grouping, Order, PlaceOrder, Side, Tif, limit, market, tpsl};
7use crate::trade::ticket::{Amount, Market, Refusal, at_least, routed};
8use crate::utils::Dec;
9use crate::utils::rounding::MINIMUM_ORDER_VALUE;
10
11/// At what price an order goes.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum Price {
14    /// `FrontendMarket`, bounded `slippage` (a fraction, 0.01 for 1%) past
15    /// `reference`: the mid, or else the mark.
16    Market {
17        /// The price the bound is measured from: the mid, or else the mark.
18        reference: Dec,
19        /// How far past `reference` the order may fill, a fraction.
20        slippage: Dec,
21    },
22    /// A limit at `price`, resting as `tif` says. Never `FrontendMarket`.
23    Limit {
24        /// The limit price.
25        price: Dec,
26        /// How long it rests.
27        tif: Tif,
28    },
29}
30
31/// An order to place: a market or limit entry, with its take profit and stop
32/// loss when it has them.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub struct OrderIntent {
35    /// Buy or sell.
36    pub side: Side,
37    /// How much: a size, or a notional sized at the order's price.
38    pub amount: Amount,
39    /// At market or at a limit.
40    pub price: Price,
41    /// Whether it may only reduce a position.
42    pub reduce_only: bool,
43    /// The client order id; `None` for none.
44    pub cloid: Option<Cloid>,
45    /// Placed with the entry, the other way, once it fills; `None` for none.
46    pub take_profit: Option<Exit>,
47    /// Placed with the entry, the other way, once it fills; `None` for none.
48    pub stop_loss: Option<Exit>,
49}
50
51impl OrderIntent {
52    pub(super) const fn at(side: Side, amount: Amount, price: Price) -> OrderIntent {
53        OrderIntent { side, amount, price, reduce_only: false, cloid: None, take_profit: None, stop_loss: None }
54    }
55
56    /// Sent with this client order id.
57    #[must_use]
58    pub const fn with_cloid(mut self, cloid: Cloid) -> OrderIntent {
59        self.cloid = Some(cloid);
60        self
61    }
62
63    /// Placed with the entry, the other way, once it fills.
64    #[must_use]
65    pub const fn take_profit(mut self, exit: Exit) -> OrderIntent {
66        self.take_profit = Some(exit);
67        self
68    }
69
70    /// Placed with the entry, the other way, once it fills.
71    #[must_use]
72    pub const fn stop_loss(mut self, exit: Exit) -> OrderIntent {
73        self.stop_loss = Some(exit);
74        self
75    }
76}
77
78/// An `order`: the entry, and its take profit and stop loss grouped `normalTpsl` when it has them.
79/// Each is worth at least [`MINIMUM_ORDER_VALUE`] in the quote, valued at its limit, at the
80/// reference at market, or at the trigger.
81pub fn order(market: &Market, intent: &OrderIntent, builder: Option<&BuilderSetting>) -> Result<PlaceOrder, Refusal> {
82    let has_exits = intent.take_profit.is_some() || intent.stop_loss.is_some();
83    if market.is_spot() && intent.reduce_only {
84        return Err(Refusal::Spot("reduce only"));
85    }
86    if market.is_spot() && has_exits {
87        return Err(Refusal::Spot("a take profit or stop loss"));
88    }
89    let grid = market.grid();
90    let side = intent.side;
91    let (price, valued_at, tif) = match intent.price {
92        Price::Market { reference, slippage } => market::bound(grid, reference, slippage, side)?,
93        Price::Limit { price, tif } => limit::resting(grid, price, tif, side)?,
94    };
95    let size = intent.amount.size_on(grid, valued_at)?;
96    at_least(size, valued_at, MINIMUM_ORDER_VALUE)?;
97    let mut entry = match tif {
98        Tif::FrontendMarket => Order::market(market.asset(), side, price, size),
99        tif => Order::limit(market.asset(), side, price, size, tif),
100    };
101    if intent.reduce_only {
102        entry = entry.reduce_only();
103    }
104    if let Some(cloid) = intent.cloid {
105        entry = entry.with_cloid(cloid);
106    }
107    let mut orders = vec![entry];
108    orders.extend(tpsl::attach(market, side, size, valued_at, intent.take_profit, intent.stop_loss)?);
109    let grouping = match has_exits {
110        true => Grouping::NormalTpsl,
111        false => Grouping::Na,
112    };
113    routed(PlaceOrder::new(orders).grouped(grouping), builder)
114}
115
116#[cfg(test)]
117#[path = "tests/entry.rs"]
118mod tests;