#exchange #trading #simulation #backest #leverage


A leveraged perpetual futures exchange for simulated trading and backtesting

98 releases (25 breaking)

new 0.32.3 Mar 19, 2023
0.31.0 Mar 5, 2023
0.23.4 Dec 28, 2022
0.23.3 Oct 18, 2022
0.5.7 Mar 29, 2021

#5 in Finance

Download history 3/week @ 2022-12-02 85/week @ 2022-12-09 2/week @ 2022-12-16 22/week @ 2022-12-23 9/week @ 2022-12-30 2/week @ 2023-01-06 7/week @ 2023-01-13 2/week @ 2023-01-20 6/week @ 2023-01-27 8/week @ 2023-02-03 175/week @ 2023-02-10 235/week @ 2023-02-17 61/week @ 2023-02-24 116/week @ 2023-03-03 28/week @ 2023-03-10 30/week @ 2023-03-17

241 downloads per month

Custom license


Leveraged Futures Exchange for Simulated Trading (LFEST)

☢️ This is a personal project, use a your own risk.

lfest-rs is a simulated futures exchange capable of leveraged positions.
You fed it external market data through the MarketUpdate enum to update the internal state.
Where you either provide bid and ask price or information derived from a candle.
Macros (bba, candle) make it easy to construct the concrete variant.
For simplicity's sake (and performance) the exchange does not use an order book.
The exchange can be configured using Config


  • 💱 Fixed point arithmetic using fpdec crate, for super fast and precise numeric calculations.
  • 🧠 Use of newtype pattern to enforce the correct types at function boundaries.
    Examples include BaseCurrency, QuoteCurrency, Fee and Leverage.
    This makes it impossible to mistakenly input for example a USD denoted value into a function that expects a BTC denoted value.
  • 📡 Flexible market data integration through the MarketUpdate type and associated macros.
  • 💹 Integrated performance tracking.
    Use the existing FullAccountTracker
    or implement your own using the AccountTracker trait.
  • ✔️ Broad test coverage, to get closer to ensured correctness.
  • 🔍 Auditable due to its small and consice codebase. < 8k LOC
  • 📃 Supports both linear and inverse futures contracts.
  • ⛔ Order filtering to make sure the price and quantity follow certain rules. See PriceFilter and QuantityFilter

Order Types

The supported order types are:

  • Market: aggressively execute against the best bid / ask
  • Limit: passively place an order into the orderbook

Performance Metrics:

The following performance metrics are available when using the FullTrack AccountTracker,
but you may define any performance metric by implementing the AccountTracker trait.

  • win_ratio: wins / total_trades
  • profit_loss_ratio: avg_win_amnt / avg_loss_amnt
  • total_rpnl: Total realized profit and loss
  • sharpe
  • sortino
  • cumulative fees: Sum total of fees payed to the exchange
  • max_drawdown_wallet_balance: Maximum fraction the wallet balance has decreased from its high.
  • max_drawdown_total: Drawdown including unrealized profit and loss
  • num_trades: The total number of trades executed
  • turnover: The total quantity executed
  • trade_percentage: trades / total_trade_opportunities
  • buy_ratio: buys / total_trades
  • limit_order_fill_ratio
  • limit_order_cancellation_ratio
  • historical_value_at_risk
  • cornish_fisher_value_at_risk
  • d_ratio

Some of these metric may behave differently from what you would expect, so make sure to take a look at the code.

How to use

To use this crate in your project, add the following to your Cargo.toml:

lfest = "0.32.0"

Then proceed to use it in your code. For an example see examples


  • proper liquidations
  • Rework some internal components for greater clarity and simplicity
  • Order filters (price and quantity)


If you find a bug or would like to help out, feel free to create a pull-request.

Donations 💰 💸

I you would like to support the development of this crate, feel free to send over a donation:

Monero (XMR) address:




Copyright (C) 2020 <Mathis Wellmann wellmannmathis@gmail.com>

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.



~95K SLoC