# rs-backtester
[](https://crates.io/crates/rs-backtester)
[](https://github.com/nicferrari/rs-backtester/blob/master/LICENSE-APACHE-2.0)
[](https://docs.rs/rs-backtester)
rs-backtester is a financial backtesting library entirely written in Rust with the purpose of being
easy-to-use yet flexible enough to allow a quick implementation of different strategies
## Get started
To get started:
- Import the necessary modules:
```rust
use std::error::Error;
use rs_backtester::backtester::Backtest;
use rs_backtester::data::Data;
use rs_backtester::strategies::sma_cross;
use rs_backtester::report::report_horizontal;
```
- Define an instance of the Data class. Market data can be retrieved either through yahoo-finance or read from
a CSV file
```rust
let quotes = Data::load("test_data//NVDA.csv","NVDA")?;
```
- As an alternative, you can retrieve data directly from yahoo finance with the following
which makes use of the crate yahoo-finance-api
```rust
let quotes = Data::new_from_yahoo("GOOG","1d","6mo")?;
```
- Create a function which returns a Strategy or use one provided by the library.<BR>
A Strategy is basically a vector of Choices (e.g. BUY, SHORTSELL, ...)
and the indicator(s) used
```rust
let sma_cross_strategy = sma_cross(quotes.clone(), 10, 20);
```
- Create an instance of the Backtest class
```rust
let sma_cross_bt = Backtest::new(sma_cross_strategy,100_000f64);
```
- Now:
- you can read a report of the backtest
```rust
report_horizontal(sma_cross_tester);
```
- you can see the list of trades
```rust
sma_cross_tester.print_all_trades();
```
- you can chart it (with indicators)
```rust
plot(sma_cross_tester,plot_config)?;
```
<img src="https://raw.githubusercontent.com/nicferrari/rs-backtester/master/plot.png" width="400"/><BR>
- you can save it to CSV for inspection
```rust
sma_cross_bt.to_csv("bt.csv")?;
```
- you can also compare multiple strategies at once
```rust
report_vertical(&[&buynhold_bt, &sma_bt, &sma_cross_bt, &rsi_bt]);
```
- and you can also play with your strategy modifying it by inverting it or transform it in long or short-only
```rust
let sma_cross_long = sma_cross_strategy.long_only();
```
### Examples
- <B>backtesting</B>: how to test a strategy, with and without commission fees
- <B>compare</B>: how to compare different strategies
- <B>modify_strategy</B>: how to modify a strategy in long-only, short-only, reverse
- <B>trades</B>: how to obtain list of trades (with stats) in a Backtest or see details of a single trade
- <B>strategy</B>: how to build a custom strategy
- <B>log</B>: how to log (to csv) parts or whole Backtest
- <B>chart</B>: how to save a plot (.png) of the backtest
### What's new
- 0.1.4 introduces several reworks, optimizations and clean-ups
- More details in the changelog
## Disclaimer <br>
This software is provided for educational and research purposes only.<br>
The results generated by this library do not guarantee future performance and should not be interpreted as financial advice.<br>
Users are solely responsible for any decisions made based on the outputs of this software.<br>
The authors and contributors of this project disclaim any liability for losses or damages arising from the use of this library.