Skip to main content

trailing_stop_calculate

Function trailing_stop_calculate 

Source
pub fn trailing_stop_calculate(
    price_increment: Price,
    trigger_px: Option<Price>,
    order: &OrderAny,
    bid: Option<Price>,
    ask: Option<Price>,
    last: Option<Price>,
) -> Result<(Option<Price>, Option<Price>), TrailingStopError>
Expand description

Calculates the new trigger and limit prices for a trailing stop order.

trigger_px is an optional override for the trigger price already carried inside order. If Some(_), it takes priority over the value on the order itself, otherwise the function falls back to the value stored on the order.

Default, BidAsk, and LastOrBidAsk require both bid and ask. LastOrBidAsk evaluates the quote first, then requires last. LastPrice and MarkPrice use last as their reference. Other trigger types are unsupported.

§Returns

A tuple with the newly-set trigger-price and limit-price (if any). None in either position means the respective price did not improve.

§Errors

Returns an error if:

  • the order type, trigger type, or trailing offset type is invalid.
  • a required bid, ask, or last price is missing.
  • the calculated price cannot be represented as a [Price].