bybit/models/time_in_force.rs
1use crate::prelude::*;
2
3/// Specifies the time-in-force policy for an order, controlling how long it remains active.
4///
5/// Time-in-force (TIF) dictates the duration an order stays open before it is executed
6/// or canceled. In perpetual futures trading, TIF is critical for managing order
7/// execution in volatile markets. For example:
8/// - **GTC (Good Till Canceled)**: Remains active until filled or manually canceled, suitable for patient strategies.
9/// - **IOC (Immediate or Cancel)**: Executes immediately, with any unfilled portion canceled, ideal for high-frequency trading.
10/// - **FOK (Fill or Kill)**: Must be fully filled immediately or canceled, used for large orders needing guaranteed execution.
11/// - **PostOnly**: Ensures the order adds liquidity (maker order) and cancels if it would take liquidity, useful for fee optimization.
12///
13/// Bots must carefully select TIF based on strategy. For instance, a PostOnly order
14/// can reduce fees but may fail in fast-moving markets, impacting profitability.
15#[derive(Serialize, Deserialize, Clone, Debug, Default)]
16pub enum TimeInForce {
17 /// Good Till Canceled: Order remains active until filled or canceled.
18 #[default]
19 GTC,
20
21 /// Immediate or Cancel: Executes immediately, with unfilled portion canceled.
22 IOC,
23
24 /// Fill or Kill: Must be fully filled immediately or canceled.
25 FOK,
26
27 /// Post Only: Ensures the order is a maker order, cancels if it would be a taker.
28 PostOnly,
29}
30
31impl TimeInForce {
32 /// Converts the `TimeInForce` enum to its string representation.
33 ///
34 /// Returns the string format expected by Bybit's API, such as "GTC" or "PostOnly".
35 /// Bots should use this method to ensure API compatibility when setting TIF.
36 pub fn as_str(&self) -> &str {
37 match self {
38 TimeInForce::GTC => "GTC",
39 TimeInForce::IOC => "IOC",
40 TimeInForce::FOK => "FOK",
41 TimeInForce::PostOnly => "PostOnly",
42 }
43 }
44}