Skip to main content

py_get_exchange_rate

Function py_get_exchange_rate 

Source
pub fn py_get_exchange_rate(
    from_currency: &str,
    to_currency: &str,
    price_type: PriceType,
    quotes_bid: HashMap<String, f64>,
    quotes_ask: HashMap<String, f64>,
) -> PyResult<Option<Decimal>>
Expand description

Calculates the exchange rate between two currencies using provided bid and ask quotes.

This function builds a graph of direct conversion rates from the quotes and uses a DFS to accumulate the conversion rate along a valid conversion path. While a full Floyd-Warshall algorithm could compute all-pairs conversion rates, the DFS approach here provides a quick solution for a single conversion query.

ยงErrors

For conversions between distinct currencies (an identical from_currency and to_currency returns a rate of one without inspecting the quotes), returns an error if:

  • quotes_bid or quotes_ask is empty.
  • quotes_bid and quotes_ask lengths are not equal.
  • price_type is equal to Last or Mark (cannot calculate from quotes).
  • The bid or ask side of a pair is missing.