R创建交易模拟

时间:2019-05-16 12:57:50

标签: r trading

我想编写一个代表交易模拟的函数。我有一个金融资产价格的数据框,我想创建一个基于信号的交易策略。

这是我的数据框:

(开盘价=开盘价,收盘价=收盘价,回报率=变化率,%)

    date        open close return signal
    <date>     <dbl> <dbl>  <dbl>  <dbl>
  1 2015-01-20  213.  211.     -1      0
  2 2015-01-21  211.  227.      7      1
  3 2015-01-22  227.  233.      3      0
  4 2015-01-23  234.  233.      0      0
  5 2015-01-24  233.  248.      7      1
  6 2015-01-25  247.  254.      3      0
  7 2015-01-26  254.  273.      8      1
  8 2015-01-27  273.  263.     -4      0
  9 2015-01-28  263.  234.    -11      0
 10 2015-01-29  233.  234.      0      0

我的交易策略描述如下:如果day t上有信号,则用以下day t+1买入开盘价,然后卖出至收盘价。在我的示例数据集中,第2天有一个信号,所以我在第3天买入,并以第3天的收盘价卖出。我投资100 $,因此在第3天有3 $的收益。下一个信号出现在第5天,因此我在第6天投资了103美元,然后我又得到了3美元。下表说明了我的交易策略:

   date        open close return signal  trading.strategy capital    
   <date>     <dbl> <dbl>  <dbl>  <dbl>     
 1 2015-01-20  213.  211.     -1      0     -             100
 2 2015-01-21  211.  227.      7      1     -             100
 3 2015-01-22  227.  233.      3      0     buy+sell      103
 4 2015-01-23  234.  233.      0      0     -             103
 5 2015-01-24  233.  248.      7      1     -             103
 6 2015-01-25  247.  254.      3      0     buy+sell      106
 7 2015-01-26  254.  273.      8      1     -             106
 8 2015-01-27  273.  263.     -4      0     buy+sell      102
 9 2015-01-28  263.  234.    -11      0     -             102
10 2015-01-29  233.  234.      0      0     -             102

有人可以帮我编写执行我的交易策略的功能吗?

这是我的数据:

structure(list(date = structure(c(16455, 16456, 16457, 16458, 
16459, 16460, 16461, 16462, 16463, 16464), class = "Date"), open = c(212.91, 
211.38, 227.32, 233.52, 232.7, 247.35, 254.08, 273.17, 263.35, 
233.35), close = c(211.32, 226.9, 233.41, 232.88, 247.85, 253.72, 
273.47, 263.48, 233.91, 233.51), return = c(-1, 7, 3, 0, 7, 3, 
8, -4, -11, 0), signal = c(0, 1, 0, 0, 1, 0, 1, 0, 0, 0)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -10L))

3 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

library(tidyverse)

df %>% 
  mutate(trading.strategy = if_else(lag(signal) == 1, "buy+sell", "-")) %>% 
  filter(trading.strategy == "buy+sell") %>% 
  mutate(capital = 100 + cumsum(return)) %>% 
  right_join(df) %>% 
  fill(capital) %>% 
  mutate(capital = if_else(is.na(capital), 100, capital),
         trading.strategy = if_else(is.na(trading.strategy), "-", trading.strategy))

   date        open close return signal trading.strategy capital
   <date>     <dbl> <dbl>  <dbl>  <dbl> <chr>              <dbl>
 1 2015-01-20  213.  211.     -1      0 -                    100
 2 2015-01-21  211.  227.      7      1 -                    100
 3 2015-01-22  227.  233.      3      0 buy+sell             103
 4 2015-01-23  234.  233.      0      0 -                    103
 5 2015-01-24  233.  248.      7      1 -                    103
 6 2015-01-25  247.  254.      3      0 buy+sell             106
 7 2015-01-26  254.  273.      8      1 -                    106
 8 2015-01-27  273.  263.     -4      0 buy+sell             102
 9 2015-01-28  263.  234.    -11      0 -                    102
10 2015-01-29  233.  234.      0      0 -                    102

答案 1 :(得分:2)

library(dplyr)

initial_capital <- 10000

df %>%
  mutate(
    trade = ifelse(lag(signal, default = 0), 1, 0),
    trading.strategy = ifelse(trade, "buy+sell", "-"),
    days_return = trade * (close - open) / (open),
    cum_return = cumsum(days_return),
    capital = initial_capital * (1 + cum_return)
  ) %>%
  select(-trade,-return)

# A tibble: 10 x 8
   date        open close signal trading.strategy days_return cum_return capital
   <date>     <dbl> <dbl>  <dbl> <chr>                  <dbl>      <dbl>   <dbl>
 1 2015-01-20  213.  211.      0 -                     0          0       10000 
 2 2015-01-21  211.  227.      1 -                     0          0       10000 
 3 2015-01-22  227.  233.      0 buy+sell              0.0268     0.0268  10268.
 4 2015-01-23  234.  233.      0 -                     0          0.0268  10268.
 5 2015-01-24  233.  248.      1 -                     0          0.0268  10268.
 6 2015-01-25  247.  254.      0 buy+sell              0.0258     0.0525  10525.
 7 2015-01-26  254.  273.      1 -                     0          0.0525  10525.
 8 2015-01-27  273.  263.      0 buy+sell             -0.0355     0.0171  10171.
 9 2015-01-28  263.  234.      0 -                     0          0.0171  10171.
10 2015-01-29  233.  234.      0 -                     0          0.0171  10171.

答案 2 :(得分:0)

仅开发@Lennyy策略,请勿将其视为独立解决方案:

library(tidyverse)

CPTL = 100

right_join(
  filter(dat, lag(signal == 1)) %>%
  mutate(
    trading.strategy = 'buy+sell',
    capital = CPTL + cumsum(return / 100) * CPTL
    ),
  dat
  ) %>%
  mutate(
    capital = replace_na(fill(., capital)$capital, CPTL),
    trading.strategy = replace_na(trading.strategy, '-')
    )

# A tibble: 10 x 7
#   date        open close return signal trading.strategy capital
#   <date>     <dbl> <dbl>  <dbl>  <dbl> <chr>              <dbl>
# 1 2015-01-20  213.  211.     -1      0 -                    100
# 2 2015-01-21  211.  227.      7      1 -                    100
# 3 2015-01-22  227.  233.      3      0 buy+sell             103
# 4 2015-01-23  234.  233.      0      0 -                    103
# 5 2015-01-24  233.  248.      7      1 -                    103
# 6 2015-01-25  247.  254.      3      0 buy+sell             106
# 7 2015-01-26  254.  273.      8      1 -                    106
# 8 2015-01-27  273.  263.     -4      0 buy+sell             102
# 9 2015-01-28  263.  234.    -11      0 -                    102
#10 2015-01-29  233.  234.      0      0 -                    102