Robot-Wealth / rsims

R package for financial simulation
Other
38 stars 16 forks source link

rsims

Tools for financial simulation

Description

Efficient quasi-event-driven backtesting functions with opinionated input requirements.

Designed according to the following philosophy:

Install and load

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)

Usage

Depends on application:

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.

Approach to calculating position deltas

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.

Cost model

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.

Examples and vignettes

TODO: