Leveraged Futures Exchange for Simulated Trading (LFEST)
:warning: This is a personal project, use a your own risk.
:warning: The results may not represent real trading results on any given exchange. Currently there is no correct implementation of order margining, which is fine for me but may not work for your purposes. Also automatic liquidation is terrible.
lfest-rs is a blazingly fast simulated exchange capable of leveraged positions. It gets fed external data either as a trade or a candle to update the internal state and check for order execution. For simplicity's sake (and performance) the exchange does not use an order book
Order Types
The supported order types are:
- market - aggressively execute against the best bid / ask
- limit - passively place an order into the orderbook
- stop_market - A protective but aggressive market order which is triggered at a specific price
External data types
To use raw trade data to update the exchanges state, the Trade struct from trade-aggregation-rs is used. Each data point must have the following fields:
/// Defines a taker trade
When constructing the Trade struct pay careful attention to have a timestamp in milliseconds and a size denoted in QUOTE currency. Sell taker trades have a negative size. Then, update exchange state using consume_trade(&trade) method.
To use candle data to update the exchange state, the Candle struct from trade-aggregation-rs is used. To aggregate candles from a &Vec:
// aggregate candles using a volume threshold of 100 BTC, therefore By::Base is used
let mut aggregator = new;
let candles = aggregate_all_trades;
Use exchange.consume_candle(&candle) method to update the exchanges state
Performance Metrics:
The following performance metrics are available through AccTracker struct:
- win_ratio
- profit_loss_ratio
- total_rpnl
- sharpe
- sharpe_daily_returns
- sortino
- cumulative fees
- sharpe_sterling_ratio
- max_drawdown
- max_upnl_drawdown
- num_trades
- turnover
- trade_percentage
- buy_ratio
- limit_order_fill_ratio
- limit_order_cancellation_ratio
How to use
To use this crate in your project, add the following to your Cargo.toml:
[dependencies]
lfest = "0.5.2"
Then proceed to use it in your code.
The following example uses trades to update the bid and ask price of the exchange. See examples folder for all the examples. Run the following example using:
cargo run --example use_trades --release
//! Example usage of Exchange using external trade data.
//! A randomly acting agent places market buy / sell orders every 100 candles
extern crate log;
use ;
use load_trades_from_csv;
use ;
use Instant;
/// analyze the resulting performance metrics of the traded orders
Benchmark
See the example use_trades.rs and compile in release mode to see that the Exchange is capable of simulating 1 million historical trades and executing ~40k market orders in ~470ms.
Dependencies
A non trivial dependency is trade_aggregation as the exchange relies on the Trade and Candle struct.
TODOs:
- proper order margin mechanism
- proper liquidations
Contributions
If you find a bug or would like to help out, feel free to create a pull-request.
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/.