Tools for financial simulation
Efficient quasi-event-driven backtesting functions with opinionated input requirements.
Designed according to the following philosophy:
The easiest way to install and load rsims
is using pacman::p_load_current_gh
which wraps devtools::install_github
and require
:
pacman::p_load_current_gh("Robot-Wealth/rsims", dependencies = TRUE)
Depends on application:
min_commission_backtest
: suitable for backtesting under the assumption of minimum commission, for instance many equities brokers.fixed_commission_backtest
: suitable for backtesting under the assumption of linear (fixed percentage) commission, for instance many crypto exchanges.fixed_commission_futs_backtest
: suitable for backtesting expiring products under the assumption of linear (fixed percentage) commission, for instance CME futures.These functions are optimised for efficiency. They simulate trading into a set of weights (calculated upstream) subject to transaction cost and other constraints. They expect matrixes for prices and target weights.
Example usage:
library(rsims)
results <- min_commission_backtest(prices, theo_weights, trade_buffer = 0., initial_cash = 10000, commission_pct = 0, capitalise_profits = FALSE)
Further details in the respective function documentation.
Examples of wrangling data for input to these functions can be found in the vignettes.
Position deltas are calculated using the trade buffer approach. Positions are rebalanced once they deviate from their target by more than a user-supplied trade_buffer
. Rebalancing happens slightly differently depending on the commission model used:
These are heuristic rules that are theoretically optimal give the different cost models. Here’s a good derivation from @macrocephalopod on Twitter.
Other approaches to trade determination may be implemented in the future.
Currently rsims
implements simplified cost models that only include commission, borrow, and funding costs.
For some applications, the various costs (market impact, spread, and commission) might be reasonably represented by such a model. No attempt is made (yet) to explicitly account for these costs separately.