ITPub博客

首页 > > > R语言金融基础:tidyquant数据整理(日行情转月行情、MACD的使用)

R语言金融基础:tidyquant数据整理(日行情转月行情、MACD的使用)

作者:浪花小雨滴 时间:2019-05-06 15:56:54 0 删除 编辑

作者:黄天元,复旦大学博士在读,热爱数据科学与开源工具(R/Python),致力于利用数据科学迅速积累行业经验优势和科学知识发现,涉猎内容包括但不限于信息计量、机器学习、数据可视化、应用统计建模、知识图谱等。知乎专栏:R语言数据挖掘邮箱:huang.tian-yuan@qq.com.欢迎合作交流。

前文:

HopeR:R语言金融基础:tidyquant获取数据(标普500与纳斯达克)

HopeR:R语言金融基础:tidyquant获取数据(股票每日行情)

HopeR:R语言金融基础:tidyquant获取数据(宏观经济数据)


根据已有的数据进行新的计算,然后得到新的具有启发性的数据,这是数据分析中很基本的操作。tidyquant包给这个操作带来了便利,下面举几个官网了例子进行学习,具体的官网网址如下:

Core Functions in tidyquant​business-science.github.io

首先展示的,是对于股市行情时间尺度的调整:通过下载的每日股市行情,整合为以月为尺度的数据。开始我们需要搞点数据:

library(tidyquant)
data(FANG)
FANG

# A tibble: 4,032 x 8
   symbol date        open  high   low close    volume adjusted
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>
 1 FB     2013-01-02  27.4  28.2  27.4  28    69846400     28  
 2 FB     2013-01-03  27.9  28.5  27.6  27.8  63140600     27.8
 3 FB     2013-01-04  28.0  28.9  27.8  28.8  72715400     28.8
 4 FB     2013-01-07  28.7  29.8  28.6  29.4  83781800     29.4
 5 FB     2013-01-08  29.5  29.6  28.9  29.1  45871300     29.1
 6 FB     2013-01-09  29.7  30.6  29.5  30.6 104787700     30.6
 7 FB     2013-01-10  30.6  31.5  30.3  31.3  95316400     31.3
 8 FB     2013-01-11  31.3  32.0  31.1  31.7  89598000     31.7
 9 FB     2013-01-14  32.1  32.2  30.6  31.0  98892800     31.0
10 FB     2013-01-15  30.6  31.7  29.9  30.1 173242600     30.1
# ... with 4,022 more rows

可以看到,这是很典型的OHLC数据。这份数据包含了4支股票2013年到2016年的每日行情。

现在我们要看看这些股票的每月行情:

FANG %>%
    group_by(symbol) %>%
    tq_transmute(select = adjusted, mutate_fun = to.monthly, indexAt = "lastof")

# A tibble: 192 x 3
# Groups:   symbol [4]
   symbol date       adjusted
   <chr>  <date>        <dbl>
 1 FB     2013-01-31     31.0
 2 FB     2013-02-28     27.2
 3 FB     2013-03-31     25.6
 4 FB     2013-04-30     27.8
 5 FB     2013-05-31     24.4
 6 FB     2013-06-30     24.9
 7 FB     2013-07-31     36.8
 8 FB     2013-08-31     41.3
 9 FB     2013-09-30     50.2
10 FB     2013-10-31     50.2
# ... with 182 more rows

这里首先对股票代号(symbol)进行分组,然后针对矫正后的收盘股价(select = adjusted),从日数据整合为月数据(mutate_fun = to.monthly)。最后,显示的日期为当月最后一天的日期(indexAt = "lastof")。

如果不是OHLC这种每日的行情数据,也可以做时间尺度的调整。下面举个例子:

wti_prices <- tq_get("DCOILWTICO", get = "economic.data") 
wti_prices

# A tibble: 2,693 x 2
   date       price
   <date>     <dbl>
 1 2009-01-01  NA  
 2 2009-01-02  46.2
 3 2009-01-05  48.6
 4 2009-01-06  48.6
 5 2009-01-07  42.8
 6 2009-01-08  41.7
 7 2009-01-09  40.7
 8 2009-01-12  37.6
 9 2009-01-13  37.8
10 2009-01-14  37.4
# ... with 2,683 more rows

wti_prices %>%    
    tq_transmute(mutate_fun = to.period,
                 period = "months", 
                 col_rename = "WTI Price")

# A tibble: 124 x 2
   date       `WTI Price`
   <date>           <dbl>
 1 2009-01-30        41.7
 2 2009-02-27        44.2
 3 2009-03-31        49.6
 4 2009-04-30        50.4
 5 2009-05-29        66.3
 6 2009-06-30        69.8
 7 2009-07-31        69.3
 8 2009-08-31        70.0
 9 2009-09-30        70.5
10 2009-10-30        77.0
# ... with 114 more rows
Warning message:
In fun_transmute(., OHLC = FALSE, ...) : missing values removed from data

警告信息中提示,缺失值已经自动被忽略。此外我么你知道,col_rename参数可以控制结果的列名称。

使用过dplyr的用户应该知道,transmute是完全忽略过去的数据来构造列,而mutate则可以在原有数据的基础上增加列。基于这个理念,tidyquant中也有相对应的函数。

比如我们要在收盘价的基础上,求MACD,可以这样操作:

FANG %>%
    group_by(symbol) %>%
    tq_mutate(select     = close, 
              mutate_fun = MACD)

# A tibble: 4,032 x 10
# Groups:   symbol [4]
   symbol date        open  high   low close    volume adjusted  macd signal
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl> <dbl>  <dbl>
 1 FB     2013-01-02  27.4  28.2  27.4  28    69846400     28      NA     NA
 2 FB     2013-01-03  27.9  28.5  27.6  27.8  63140600     27.8    NA     NA
 3 FB     2013-01-04  28.0  28.9  27.8  28.8  72715400     28.8    NA     NA
 4 FB     2013-01-07  28.7  29.8  28.6  29.4  83781800     29.4    NA     NA
 5 FB     2013-01-08  29.5  29.6  28.9  29.1  45871300     29.1    NA     NA
 6 FB     2013-01-09  29.7  30.6  29.5  30.6 104787700     30.6    NA     NA
 7 FB     2013-01-10  30.6  31.5  30.3  31.3  95316400     31.3    NA     NA
 8 FB     2013-01-11  31.3  32.0  31.1  31.7  89598000     31.7    NA     NA
 9 FB     2013-01-14  32.1  32.2  30.6  31.0  98892800     31.0    NA     NA
10 FB     2013-01-15  30.6  31.7  29.9  30.1 173242600     30.1    NA     NA
# ... with 4,022 more rows

稍微介绍一下MACD的概念:MACD称为异同移动平均线,是从双指数移动平均线发展而来的,由快的指数移动平均线(EMA12)减去慢的指数移动平均线(EMA26)得到快线DIF,再用2×(快线DIF-DIF的9日加权移动均线DEA)得到MACD柱。MACD的意义和双移动平均线基本相同,即由快、慢均线的离散、聚合表征当前的多空状态和股价可能的发展变化趋势,但阅读起来更方便。当MACD从负数转向正数,是买的信号。当MACD从正数转向负数,是卖的信号。当MACD以大角度变化,表示快的移动平均线和慢的移动平均线的差距非常迅速的拉开,代表了一个市场大趋势的转变。

详见:MACD指标_百度百科

我觉得时刻记住所有概念真的是非常困难,但是我们应该知道,MACD是技术分析中一种基于滑动平均判断买入卖出的一种方法。使用短期趋势和长期趋势的差值,我们能够判断一只股票在最近的时间内趋势如何。这里找到一个我认为比较优秀的解释:

macd指标的内在逻辑是什么?​图标

即MACD是对过去行情趋势的总结,能够在一定程度上探知长期趋势,但是只能回顾过去,对未来的预测能力相当有限。在波动大的背景下,这个指标甚至有误导作用。其实质为:MACD是对当前股价走势的描述。

也许大家还是不一定知道代码里面出来的结果是什么,这里给出解释(来自The MACD indicator - How To Use The MACD Correctly - Tradeciety Trading Academy):

MACD Line: The MACD line is the heart of the indicator and by default it’s the difference between the 12-period EMA and the 26-period EMA. This means that the MACD line is basically a complete moving average crossover system by itself.

Signal Line: The Signal line is the 9-period EMA of MACD Line

MACD Histogram: MACD Line – Signal Line


最后给大家实质的指导,就是买还是不买,买的话买多少,风险有多大。这里水很深,暂时不探索讨论了。不跑题,我们还是回到这个计算的本身。大家看都是NA,因为滑动平均是12日和26日的差值,所以如果当日之前还没有26天,是肯定没有结果的。我们来看看后面的结果,同时给两个指标改个名字:

FANG %>%
  group_by(symbol) %>%
  tq_mutate(select     = close, 
            mutate_fun = MACD, 
            col_rename = c("MACD", "Signal")) %>% 
  print(n = 50)

# A tibble: 4,032 x 10
# Groups:   symbol [4]
   symbol date        open  high   low close    volume adjusted   MACD Signal
   <chr>  <date>     <dbl> <dbl> <dbl> <dbl>     <dbl>    <dbl>  <dbl>  <dbl>
 1 FB     2013-01-02  27.4  28.2  27.4  28    69846400     28    NA     NA   
 2 FB     2013-01-03  27.9  28.5  27.6  27.8  63140600     27.8  NA     NA   
 3 FB     2013-01-04  28.0  28.9  27.8  28.8  72715400     28.8  NA     NA   
 4 FB     2013-01-07  28.7  29.8  28.6  29.4  83781800     29.4  NA     NA   
 5 FB     2013-01-08  29.5  29.6  28.9  29.1  45871300     29.1  NA     NA   
 6 FB     2013-01-09  29.7  30.6  29.5  30.6 104787700     30.6  NA     NA   
 7 FB     2013-01-10  30.6  31.5  30.3  31.3  95316400     31.3  NA     NA   
 8 FB     2013-01-11  31.3  32.0  31.1  31.7  89598000     31.7  NA     NA   
 9 FB     2013-01-14  32.1  32.2  30.6  31.0  98892800     31.0  NA     NA   
10 FB     2013-01-15  30.6  31.7  29.9  30.1 173242600     30.1  NA     NA   
11 FB     2013-01-16  30.2  30.4  29.5  29.8  75332700     29.8  NA     NA   
12 FB     2013-01-17  30.1  30.4  30.0  30.1  40256700     30.1  NA     NA   
13 FB     2013-01-18  30.3  30.4  29.3  29.7  49631500     29.7  NA     NA   
14 FB     2013-01-22  29.8  30.9  29.7  30.7  55243300     30.7  NA     NA   
15 FB     2013-01-23  31.1  31.5  30.8  30.8  48899800     30.8  NA     NA   
16 FB     2013-01-24  31.3  31.5  30.8  31.1  43845100     31.1  NA     NA   
17 FB     2013-01-25  31.4  31.9  31.1  31.5  54363600     31.5  NA     NA   
18 FB     2013-01-28  31.9  32.5  31.8  32.5  59682500     32.5  NA     NA   
19 FB     2013-01-29  32    32.1  30.7  30.8  72976500     30.8  NA     NA   
20 FB     2013-01-30  31.0  31.5  30.9  31.2  87682100     31.2  NA     NA   
21 FB     2013-01-31  29.2  31.5  28.7  31.0 190744900     31.0  NA     NA   
22 FB     2013-02-01  31.0  31.0  29.6  29.7  85856700     29.7  NA     NA   
23 FB     2013-02-04  29.1  29.2  28.0  28.1  92362200     28.1  NA     NA   
24 FB     2013-02-05  28.3  29.0  28.0  28.6  47948200     28.6  NA     NA   
25 FB     2013-02-06  28.7  29.3  28.7  29.0  38375900     29.0  NA     NA   
26 FB     2013-02-07  29.1  29.2  28.3  28.6  34540100     28.6  -1.18  NA   
27 FB     2013-02-08  28.9  29.2  28.5  28.5  37708800     28.5  -1.40  NA   
28 FB     2013-02-11  28.6  28.7  28.0  28.3  37361800     28.3  -1.64  NA   
29 FB     2013-02-12  27.7  28.2  27.1  27.4  93498700     27.4  -2.05  NA   
30 FB     2013-02-13  27.4  28.3  27.3  27.9  50164000     27.9  -2.21  NA   
31 FB     2013-02-14  28.0  28.6  28.0  28.5  35615800     28.5  -2.15  NA   
32 FB     2013-02-15  28.5  28.8  28.1  28.3  33109300     28.3  -2.12  NA   
33 FB     2013-02-19  28.2  29.1  28.1  28.9  49396400     28.9  -1.91  NA   
34 FB     2013-02-20  28.9  29.0  28.3  28.5  42098200     28.5  -1.85  -1.84
35 FB     2013-02-21  28.3  28.5  27.2  27.3  49642300     27.3  -2.12  -1.89
36 FB     2013-02-22  27.6  27.6  26.8  27.1  36350200     27.1  -2.34  -1.98
37 FB     2013-02-25  27.2  27.6  27.2  27.3  34652000     27.3  -2.46  -2.08
38 FB     2013-02-26  27.4  27.5  26.7  27.4  31611700     27.4  -2.49  -2.16
39 FB     2013-02-27  27.3  27.3  26.6  26.9  44319700     26.9  -2.63  -2.25
40 FB     2013-02-28  26.8  27.3  26.3  27.2  83027800     27.2  -2.61  -2.33
41 FB     2013-03-01  27.0  28.1  26.8  27.8  54064800     27.8  -2.41  -2.34
42 FB     2013-03-04  27.8  28.1  27.4  27.7  32400700     27.7  -2.25  -2.32
43 FB     2013-03-05  27.9  28.2  27.2  27.5  40622200     27.5  -2.15  -2.29
44 FB     2013-03-06  28.1  28.1  27.4  27.5  33532600     27.5  -2.07  -2.25
45 FB     2013-03-07  27.6  28.7  27.5  28.6  74540200     28.6  -1.66  -2.13
46 FB     2013-03-08  28.4  28.5  27.7  28.0  44198900     28.0  -1.49  -2.00
47 FB     2013-03-11  28.0  28.6  27.8  28.1  35642100     28.1  -1.30  -1.86
48 FB     2013-03-12  28.1  28.3  27.6  27.8  27569600     27.8  -1.22  -1.73
49 FB     2013-03-13  27.6  27.6  26.9  27.1  39619500     27.1  -1.36  -1.66
50 FB     2013-03-14  27.1  27.4  26.8  27.0  27646400     27.0  -1.46  -1.62
# ... with 3,982 more rows

现在,大家知道MACD的计算原理和实现方法了,至于怎么用,还请各位自行思考。

原文作者:;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论

注册时间:2018-05-16

  • 博文量
    4259
  • 访问量
    39904