Expand description
§Leveraged Futures Exchange for Simulated Trading (LFEST)
:radioactive: This is a personal project, use a your own risk.
lfest-rs is a simulated perpetual futures exchange capable of leveraged positions.
You fed it external market data through the MarketUpdate
enum to update the MarketState
.
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
and ContractSpecification
§Features:
- :currency_exchange: Fixed point arithmetic using
fpdec
crate, for super fast and precise numeric calculations. - :brain: Use of newtype pattern to enforce the correct types at function boundaries.
Examples include:
BaseCurrency
,
QuoteCurrency
,
Fee
,
Leverage
.
This makes it impossible to mistakenly input for example aUSD
denoted value into a function that expects aBTC
denoted value. - :satellite: Flexible market data integration through the
MarketUpdate
type and associated macros. - :chart: Integrated performance tracking.
Use the existingFullAccountTracker
or implement your own using theAccountTracker
trait. - :heavy_check_mark: good test coverage and heavy use of assertions, to get to ensure correctness.
- :mag: Auditable due to its small and consice codebase.
- :page_with_curl: Supports both
linear
andinverse
futures contracts. - :no_entry: Order filtering to make sure the price and quantity follow certain rules. See:
PriceFilter
QuantityFilter
IsolatedMarginRiskEngine
- Double-Entry Bookkeeping is used to ensure the accounting-equation always holds.
§Order Types
The supported order types are:
Market
: aggressively execute against the best bid / askLimit
: 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_tradesprofit_loss_ratio
: avg_win_amnt / avg_loss_amnttotal_rpnl
: Total realized profit and losssharpe
: The annualized sharpe ratiosortino
: The annualized sortino ratiocumulative fees
: Sum total of fees payed to the exchangemax_drawdown_wallet_balance
: Maximum fraction the wallet balance has decreased from its high.max_drawdown_total
: Drawdown including unrealized profit and lossmax_drawdown_duration
: The duration of the longest drawdownnum_trades
: The total number of trades executedturnover
: The total quantity executedtrade_percentage
: trades / total_trade_opportunitiesbuy_ratio
: buys / total_tradeslimit_order_fill_ratio
limit_order_cancellation_ratio
historical_value_at_risk
cornish_fisher_value_at_risk
d_ratio
There probably are some more metrics that I missed. 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:
[dependencies]
lfest = "*" # or lookup newest version on `crates.io`
Then proceed to use it in your code. For an example see examples
§TODOs:
- proper liquidations (see
update_state
inExchange
) - Orderbook support (with
MatchingEngine
) - Funding rate (support
settle_funding_period
inClearingHouse
) - Multiple accounts (low priority)
- Multiple markets (low priority)
- Portfolio
RiskEngine
for multiple markets akin toSPAN
- Support for updating leverage of a position.
- Support auto-deleveraging
- Benchmarking
- more unit tests.
- fuzz testing?
§Contributions
Would love to see you use and contribute to this project. Even just adding more tests is welcome.
§Donations :moneybag: :money_with_wings:
I you would like to support the development of this crate, feel free to send over a donation:
Monero (XMR) address:
47xMvxNKsCKMt2owkDuN1Bci2KMiqGrAFCQFSLijWLs49ua67222Wu3LZryyopDVPYgYmAnYkSZSz9ZW2buaDwdyKTWGwwb
§License
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/.
lfest - leveraged futures exchange for simulated trading
Modules§
- Provides trait and implementations to track accounts performance
- Exports common types
Macros§
- Allows the quick construction of
BaseCurrency
- Creates the
Bba
struct used as aMarketUpdate
. - Creates the
Candle
struct used as aMarketUpdate
. - Allows the quick construction of
Leverage
- Allows the quick construction of
QuoteCurrency
- Creates the
Trade
struct used as aMarketUpdate
.
Structs§
- Mocks
TransactionAccounting
for testing purposes.
Constants§
- The maker fee used in tests.
- The taker fee used in tests.
Functions§
- Constructs a mock exchange (for inverse futures) for testing.
- Constructs a mock exchange (for linear futures) for testing. The size is denoted in
BaseCurrency
and the margin currency isQuoteCurency
- Constructs a mock exchange (for linear futures) for testing. The size is denoted in
BaseCurrency
and the margin currency isQuoteCurency
Type Aliases§
- This is defined as a convenience.