Charting with tidyquant

Charting with tidyquant

本文是 Charting with tidyquant 的学习笔记。介绍了 tidyquant 包中的绘图方法。

获取数据

R
1
2
3
4
5
6
7
8
9
pingan <- tq_get("000001.SZ",
get = "stock.prices",
from = "2018-09-01",
to = "2018-12-31")
wanke <- tq_get("000002.SZ",
get = "stock.prices",
from = "2001-01-01",
to = "2018-12-31")
end <- as_date("2018-12-31")

线图

R
1
2
3
4
5
pingan %>%
ggplot(aes(x = date, y = close)) +
geom_line() +
labs(title = "Ping An Bank Co. Line Chart", y = "Closing Price", x = "") +
theme_tq(base_family = "RobotoSlab-Regular")

柱形图

如果你的 ggplot2 版本>3.0.0,绘制这幅图可能会出问题,可以参考 https://github.com/business-science/tidyquant/issues/112 解决。

R
1
2
3
4
5
6
7
8
pingan %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open,
high = high,
low = low,
close = close)) +
labs(title = "Ping An Bank Co. Bar Chart", y = "Closing Price", x = "") +
theme_tq(base_family = enfont)

R
1
2
3
4
5
6
7
8
9
10
11
pingan %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open,
close = close,
high = high,
low = low)) +
labs(title = "Ping An Bank Co. Bar Chart", y = "Closing price", x = "") +
coord_x_date(
xlim = c(end - weeks(6), end),
ylim = c(9, 11)
) + theme_tq(base_family = enfont)

蜡烛图

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
pingan %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(
open = open,
close = close,
low = low,
high = high
),
color_down = '#00D1B2', fill_down = '#00D1B2',
color_up = '#FF3860', fill_up = '#FF3860') +
labs(title = "Ping An Bank Co. Bar Chart", y = "Closing price", x = "") +
coord_x_date(
xlim = c(end - weeks(6), end),
ylim = c(9, 11)
) + theme_tq(base_family = enfont)

Charting Multiple Securities

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
end <- as_date("2016-12-31")
start = end - weeks(6)
FANG %>%
dplyr::filter(date >= start - days(2 * 15)) %>%
ggplot(aes(x = date, y = close,
group = symbol)) +
geom_candlestick(aes(
open = open,
close = close,
high = high,
low = low
),
color_down = '#00D1B2', fill_down = '#00D1B2',
color_up = '#FF3860', fill_up = '#FF3860') +
labs(title = "FANG Candlestick Chart",
subtitle = "Experimenting with Mulitple Stocks",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, ncol = 2,
scales = 'free_y') +
theme_tq(base_family = enfont)

添加均线

可以添加以下几种类型的均线:

  1. Simple moving averages (SMA)
  2. Exponential moving averages (EMA)
  3. Weighted moving averages (WMA)
  4. Double exponential moving averages (DEMA)
  5. Zero-lag exponential moving averages (ZLEMA)
  6. Volume-weighted moving averages (VWMA) (also known as VWAP)
  7. Elastic, volume-weighted moving averages (EVWMA) (also known as MVWAP)
R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
FANG %>%
dplyr::filter(date >= start - days(30)) %>%
ggplot(aes(x = date, y = close,
group = symbol)) +
geom_candlestick(
aes(
open = open,
close = close,
high = high,
low = low
),
color_down = '#00D1B2', fill_down = '#00D1B2',
color_up = '#FF3860', fill_up = '#FF3860'
) +
geom_ma(ma_fun = SMA, n = 15, color = "#3273DC", size = 0.8) +
labs(title = "FANG Candlestick Chart",
subtitle = "Experimenting with Mulitple Stocks",
y = "Closing Price", x = "") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, ncol = 2, scale = "free_y") +
theme_tq(base_family = enfont)

示例 1: 绘制 50 天和 200 天的 SMA

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
wanke %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(
aes(
open = open,
close = close,
high = high,
low = low
),
color_down = '#00D1B2', fill_down = '#00D1B2',
color_up = '#FF3860', fill_up = '#FF3860'
) +
geom_ma(ma_fun = SMA,
n = 50,
linetype = 5) +
geom_ma(ma_fun = SMA,
n = 200,
color = "#209CEE") +
labs(title = "Ping An Bank Co. Candlestick Chart",
subtitle = "50 and 200-Day SMA",
y = "Closing Price", x = "") +
theme_tq(base_family = enfont)

示例 2: Charting exponential moving averages

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
wanke %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(
aes(
open = open,
close = close,
high = high,
low = low
),
color_down = '#00D1B2', fill_down = '#00D1B2',
color_up = '#FF3860', fill_up = '#FF3860'
) +
geom_ma(ma_fun = EMA,
n = 50, wilder = TRUE,
linetype = 5) +
geom_ma(ma_fun = EMA,
n = 200, wilder = TRUE,
color = "#209CEE") +
labs(title = "Ping An Bank Co. Candlestick Chart",
subtitle = "50 and 200-Day EMA",
y = "Closing Price", x = "") +
theme_tq(base_family = enfont)

示例三:Charting MA for multipule stocks at once

R
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
FANG %>%
dplyr::filter(date >= start - days(100)) %>%
ggplot(aes(x = date, y = close,
volume = volume,
group = symbol)) +
geom_candlestick(
aes(
open = open,
close = close,
high = high,
low = low
),
color_down = '#00D1B2', fill_down = '#00D1B2',
color_up = '#FF3860', fill_up = '#FF3860'
) +
geom_ma(ma_fun = VWMA,
n = 15, wilder = TRUE,
linetype = 5) +
geom_ma(ma_fun = VWMA,
n = 50, wilder = TRUE,
color = "#209CEE") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, ncol = 2,
scales = "free_y") +
theme_tq(base_family = enfont)

布林带

R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
wanke %>%
dplyr::filter(
date >= as_date('2018-01-01')
) %>%
ggplot(aes(x = date,
y = close,
open = open,
close = close,
high = high,
low = low)) +
geom_candlestick(
color_down = '#00D1B2', fill_down = '#00D1B2',
color_up = '#FF3860', fill_up = '#FF3860') +
geom_bbands(
ma_fun = SMA, sd = 2, n = 20,
linetype = 4,
alpha = 0.2,
fill = palette_light()[[11]],
color_bands = palette_light()[[12]],
color_ma = palette_light()[[10]]) +
labs(title = "Wan Ke A. Candlestick Chart",
subtitle = "BBands with SMA Applied",
y = "Closing Price", x = "") +
theme_tq(base_family = enfont)

ggplot2 函数

示例 1: 使用 scale_y_log10()

R
1
2
3
4
5
6
7
wanke %>%
ggplot(aes(x = date, y = close)) +
geom_line(color = palette_light()[[1]]) +
scale_y_log10() +
labs(title = "Wan Ke A. Line Chart",
y = "Closing Price", x = "") +
theme_tq(base_family = enfont)

R
1
2
3
4
5
6
7
8
wanke %>%
ggplot(aes(x = date, y = close)) +
geom_line(color = palette_light()[[1]]) +
scale_y_log10() +
geom_smooth(method = "lm") +
labs(title = "Wan Ke A. Line Chart",
y = "Closing Price", x = "") +
theme_tq(base_family = enfont)

示例 2: 使用 geom_segment() 绘制交易量

R
1
2
3
4
5
6
7
8
9
10
wanke %>%
ggplot(aes(x = date, y = close)) +
geom_segment(aes(xend = date,
yend = 0,
color = volume)) +
geom_smooth(method = "loess", se = FALSE) +
labs(title = "Wan Ke A. Line Chart",
y = "Closing Price", x = "") +
theme_tq(base_family = enfont) +
theme(legend.position = "none")

# R

Comments

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×