Skip to content

A modern R trading toolkit to develop, backtest and deploy trading strategies.

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

quantargo/tidytrading

Repository files navigation

tidytrading

Lifecycle: experimental CRAN status R-CMD-check

The goal of tidytrading is to …

Installation

You can install the development version of tidytrading like so:

remotes::install_github("tidytrading")

Example

This is a basic example which shows you how to backtest an EMA 50/200 Cross (Long-only) strategy on the S&P 500:

library(tidytrading)
library(recipes)
library(TTR)

port <- get_symbols("^GSPC", from="2007-01-01")
rec <- recipe(port) |>
  step_mutate(ma50 = EMA(close, n = 50)) |>
  step_mutate(ma200 = EMA(close, n = 200))

strat <- function(.data, position, ...) {
  open_orders <- tail(.data, 1) |>
    mutate(target_qty = ifelse(ma50 > ma200), 100, 0) |>
    inner_join(position, by = "symbol") |>
    mutate(order_qty = target_qty - position_qty) |>
    filter(order_qty != 0)
  order(sym = open_orders$symbol,
        qty=open_orders$order_qty, type="market")
}

# Under construction:
samp <- sliding_window(port, lookback = 300)
multi_metric <- metric_set(sharpe, maxdd, ccc_with_bias)
order_txn <- backtest(strat, data = rec, resamples = samp)
order_txn |> multi_metric() # Calculate metrics
order_txn |> autoplot() # Plot similar to blotter::chart.Posn

About

A modern R trading toolkit to develop, backtest and deploy trading strategies.

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages