<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Tidyquant on 格物堂</title><link>https://gewutang.cn/tags/tidyquant/</link><description>Recent content in Tidyquant on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Thu, 22 May 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/tidyquant/index.xml" rel="self" type="application/rss+xml"/><item><title>用R中的eTTR包和tidyquant包绘制布林带</title><link>https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/</link><pubDate>Thu, 22 May 2025 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/</guid><description>&#10;&#10;&#10;&lt;div id="引言" class="section level1"&gt;&#10;&lt;h1&gt;引言&lt;/h1&gt;&#10;&lt;p&gt;俗话说，巧妇难为无米之炊。绘图的前提是得有数据在手。下面是获取数据的代码：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 定义股票代码&#10;tickers &amp;lt;- c(&amp;quot;TSLA&amp;quot;, &amp;quot;AAPL&amp;quot;, &amp;quot;NVDA&amp;quot;)&#10;&#10;# 批量下载股票数据并进行预处理&#10;stock_list &amp;lt;- map(tickers, function(ticker) {&#10; # 下载数据&#10; stock_data &amp;lt;- getSymbols(ticker, auto.assign = FALSE)&#10; # 重命名列&#10; colnames(stock_data) &amp;lt;- c(&amp;quot;Open&amp;quot;, &amp;quot;High&amp;quot;, &amp;quot;Low&amp;quot;, &amp;quot;Close&amp;quot;, &amp;quot;Volume&amp;quot;, &amp;quot;Adjusted&amp;quot;)&#10; # 转化为tibble&#10; tibble_data &amp;lt;- as_tibble(fortify.zoo(stock_data))&#10; # 标准化列命名&#10; renamed_data &amp;lt;- tibble_data %&amp;gt;%&#10; rename(&#10; Date = Index,&#10; open = Open,&#10; high = High,&#10; low = Low,&#10; close = Close,&#10; volume = Volume,&#10; adjusted = Adjusted&#10; )&#10; # 动态命名&#10; dyn_name &amp;lt;- paste0(ticker, &amp;quot;_data&amp;quot;)&#10; assign(dyn_name, renamed_data, envir = globalenv())&#10; return(get(dyn_name))&#10;})&#10;# 重新命名列表&#10;names(stock_list) &amp;lt;- tickers&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;有了基础数据之后，接下来是根据布林带定义计算布林带值，并将布林带数据整合进原始数据备用。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 使用dplyr生成包含high, low, close的tibble对象并计算布林带&#10;stock_list_with_bbands &amp;lt;- map(stock_list, function(data) {&#10; # 确认输入参数为tibble对象&#10; if (is.list(data) &amp;amp;&amp;amp; length(data) == 1) {&#10; data &amp;lt;- data[[1]]&#10; }&#10; # 提取eTTR::BBands所需参数&#10; hlc_tibble &amp;lt;- data %&amp;gt;% select(high, low, close)&#10; # 计算布林带&#10; bbands &amp;lt;- BBands(hlc_tibble, n = 20, sd = 2)&#10; # 整合进原始数据&#10; data %&amp;gt;%&#10; mutate(&#10; middle = bbands[, &amp;quot;mavg&amp;quot;],&#10; upper = bbands[, &amp;quot;up&amp;quot;],&#10; lower = bbands[, &amp;quot;dn&amp;quot;]&#10; )&#10;})&#10;&#10;# 将列表中的tibble行合并，并增加公司名作为列&#10;combined_data &amp;lt;- bind_rows(stock_list_with_bbands,.id = &amp;quot;company_name&amp;quot;)&#10;&#10;# 确保相关列是数值型&#10;num_cols &amp;lt;- c(&amp;quot;open&amp;quot;, &amp;quot;high&amp;quot;, &amp;quot;low&amp;quot;, &amp;quot;close&amp;quot;, &amp;quot;middle&amp;quot;, &amp;quot;upper&amp;quot;, &amp;quot;lower&amp;quot;)&#10;combined_data &amp;lt;- combined_data %&amp;gt;% &#10; mutate(across(all_of(num_cols), as.numeric))&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;数据准备好之后，用&lt;code&gt;ggplot2&lt;/code&gt;包几行命令就可以打造一个叠加布林带的蜡烛图，代码如下：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 利用分面功能绘制多支股票的蜡烛图和布林带&#10;ggplot(combined_data, aes(x = Date)) +&#10; geom_candlestick(aes(open = open, high = high, low = low, close = close)) +&#10; geom_line(aes(y = middle), color = &amp;quot;blue&amp;quot;) +&#10; geom_line(aes(y = upper), color = &amp;quot;red&amp;quot;) +&#10; geom_line(aes(y = lower), color = &amp;quot;green&amp;quot;) +&#10; facet_wrap(~ company_name, scales = &amp;quot;free_y&amp;quot;) +&#10; labs(title = &amp;quot;多股票布林带对比&amp;quot;, x = &amp;quot;日期&amp;quot;, y = &amp;quot;价格&amp;quot;) +&#10; theme_minimal()&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-3-1.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;当然，还可以单独绘制每支股票的蜡烛图叠加布林带。这里我们使用&lt;code&gt;purrr&lt;/code&gt;包中的&lt;code&gt;walk2&lt;/code&gt;函数来遍历列表中每一个对象。代码如下：&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 单独绘制每支股票的蜡烛图叠加布林带&#10;walk2(stock_list_with_bbands, names(stock_list_with_bbands), function(data, name) {&#10; if (is.list(data) &amp;amp;&amp;amp; length(data) == 1) {&#10; data &amp;lt;- data[[1]]&#10; }&#10; p &amp;lt;- ggplot(data, aes(x = Date)) +&#10; geom_candlestick(aes(open = open, high = high, low = low, close = close)) +&#10; geom_line(aes(y = middle), color = &amp;quot;blue&amp;quot;) +&#10; geom_line(aes(y = upper), color = &amp;quot;red&amp;quot;) +&#10; geom_line(aes(y = lower), color = &amp;quot;green&amp;quot;) +&#10; labs(title = paste0(&amp;quot;布林带 - &amp;quot;, name), x = &amp;quot;日期&amp;quot;, y = &amp;quot;价格&amp;quot;) +&#10; theme_minimal()&#10; print(p)&#10;})&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-4-1.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-4-2.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-4-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;其实，tidyquant包中提供了一个直接绘制布林带的函数叫 &lt;code&gt;tidyquant::geom_bbands&lt;/code&gt;，可以在绘图时直接基于&lt;code&gt;HLC&lt;/code&gt;数据计算布林带。这样一来，可以免去单独计算布林带数据的过程。&lt;/p&gt;&#10;&lt;pre class="r"&gt;&lt;code&gt;# 基于geom_bbands 单独绘制每个对象的蜡烛图叠加布林带&#10;walk2(stock_list, names(stock_list), function(data, name) {&#10; if (is.list(data) &amp;amp;&amp;amp; length(data) == 1) {&#10; data &amp;lt;- data[[1]]&#10; }&#10; p &amp;lt;- ggplot(data, aes(x = Date)) +&#10; geom_candlestick(aes(open = open, high = high, low = low, close = close)) +&#10; geom_bbands(aes(high = high, low = low, close = close),ma_fun = SMA, n = 20) +&#10; labs(title = paste0(&amp;quot;布林带 - &amp;quot;, name), x = &amp;quot;日期&amp;quot;, y = &amp;quot;价格&amp;quot;) +&#10; theme_minimal()&#10; print(p)&#10;})&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-5-1.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-5-2.png" width="672" /&gt;&lt;img src="https://gewutang.cn/2025/05/22/plot-bbands-via-ettr-and-tidyquant/index_files/figure-html/unnamed-chunk-5-3.png" width="672" /&gt;&lt;/p&gt;&#10;&lt;p&gt;呐，有了R语言，尤其是eTTR包和ggplot2包之后，计算技术指标并进行可视化就是这么简单。欢迎收藏本网站，关注微信公众号&lt;strong&gt;ruijiao007&lt;/strong&gt;。&lt;/p&gt;&#10;&lt;/div&gt;&#10;</description></item></channel></rss>