Giter Club home page Giter Club logo

rsims's Introduction

rsims

Tools for financial simulation

Description

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

Designed according to the following philosophy:

  • Prioritise backtesting efficiency
  • Function inputs should be close to the natural outputs of the quant research process: dataframes/matrixes of prices and target weights.
  • Include helper functions for efficiently wrangling these research outputs into formats suitable for fast backtesting

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:

  • 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.

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:

  • for minimum commission backtesting, rebalance back to the target weight
  • for fixed commission backtesting, rebalance back to the target weight plus/minus the trade buffer

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. 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. Borrow and funding costs are not yet implemented.

Examples and vignettes

TODO

Backtesting ETF strategies with min_commission_backtest

Finding the historically sharpe-optimal trade buffer parameter for a crypto momentum strategy with fixed_commission_backtest

Backtesting CME futures with fixed_commission_futs_backtest

rsims's People

Contributors

robotmasterkris avatar mark1lah avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.