-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
64 lines (50 loc) · 1.89 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# tidytrading
<!-- badges: start -->
[](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[](https://CRAN.R-project.org/package=tidytrading)
[](https://github.com/quantargo/tidytrading/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
The goal of tidytrading is to ...
## Installation
You can install the development version of tidytrading like so:
``` r
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:
```{r example, eval=FALSE}
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
```