<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>股票 on 格物堂</title><link>https://gewutang.cn/tags/%E8%82%A1%E7%A5%A8/</link><description>Recent content in 股票 on 格物堂</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><lastBuildDate>Mon, 12 Dec 2011 00:00:00 +0000</lastBuildDate><atom:link href="https://gewutang.cn/tags/%E8%82%A1%E7%A5%A8/index.xml" rel="self" type="application/rss+xml"/><item><title>从股票代码到公司名</title><link>https://gewutang.cn/2011/12/12/from-symbol-to-company-name/</link><pubDate>Mon, 12 Dec 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/12/12/from-symbol-to-company-name/</guid><description>&lt;p&gt;Ruser也有Ruser的恶习，经常弄的什么事情都想用R来完成：用R玩儿扫雷、用R玩汉诺塔、用R玩儿黑白棋、用R获取股票数据、用R获取财务数据。&lt;/p&gt;&#10;&lt;p&gt;现在想的是，如果已经知道一个股票代码，比如上交所的000061，如何知道其对应的公司名称呢？如果能找到一个数据库，其包含公司名称、公司股票代码、公司简称就可以完成这样的事情了。&lt;/p&gt;&#10;&lt;p&gt;最典型的金融数据库有什么呢？google?yahoo? yahoo是可以的。代码：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(quantmod)&#10;paste(getQuote(&amp;quot;000061.ss&amp;quot;, what=yahooQF(&amp;quot;Name&amp;quot;))[,2])&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;返回结果如下：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;[1] &amp;quot;SSE CORPORATE BON&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;</description></item><item><title>用R检验股票的协整关系</title><link>https://gewutang.cn/2011/10/27/use-r-to-test-cointegration/</link><pubDate>Thu, 27 Oct 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/10/27/use-r-to-test-cointegration/</guid><description>&lt;h2 id="需要用到的r包"&gt;&lt;a href="#%e9%9c%80%e8%a6%81%e7%94%a8%e5%88%b0%e7%9a%84r%e5%8c%85" class="header-anchor"&gt;&lt;/a&gt;需要用到的R包&#10;&lt;/h2&gt;&lt;p&gt;xts和zoo是两个强大的处理时序对象的R包。xts包本身又相当于是一个加强版的zoo包。zoo包处理的对象是zoo，zoo对象与数据框（dataframe)类似，由行列构成。不同的是，zoo对象比数据框多了一个时间戳。&lt;/p&gt;&#10;&lt;p&gt;假设我们有一个zoo对象t，index(t)返回时间戳，也就是一个表示日期的向量。start(t)和end（t）可以查看zoo对象的起始值和末端值。&lt;/p&gt;&#10;&lt;h2 id="数据预处理"&gt;&lt;a href="#%e6%95%b0%e6%8d%ae%e9%a2%84%e5%a4%84%e7%90%86" class="header-anchor"&gt;&lt;/a&gt;数据预处理&#10;&lt;/h2&gt;&lt;p&gt;分四个步骤：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;读入数据（data frame格式）&lt;/li&gt;&#10;&lt;li&gt;将日期转化为Date对象&lt;/li&gt;&#10;&lt;li&gt;将data frame数据转换为zoo对象&lt;/li&gt;&#10;&lt;li&gt;合并数据&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;h2 id="相应的r代码"&gt;&lt;a href="#%e7%9b%b8%e5%ba%94%e7%9a%84r%e4%bb%a3%e7%a0%81" class="header-anchor"&gt;&lt;/a&gt;相应的R代码：&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;library(quantmod) # 金融数据获取&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：xts&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## 载入程序包：'zoo'&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## The following objects are masked from 'package:base':&#10;## &#10;## as.Date, as.Date.numeric&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 载入需要的程序包：TTR&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Registered S3 method overwritten by 'quantmod':&#10;## method from&#10;## as.zoo.data.frame zoo&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(urca) # 协整检验&#10;library(ggplot2) # 可视化&#10;&#10;# 获取2010-2011年股票数据&#10;getSymbols(c(&amp;quot;AAPL&amp;quot;,&amp;quot;MSFT&amp;quot;))&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## [1] &amp;quot;AAPL&amp;quot; &amp;quot;MSFT&amp;quot;&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 提取收盘价并转换为对数序列&#10;aapl &amp;lt;- log(Ad(AAPL$AAPL.Adjusted))&#10;msft &amp;lt;- log(Ad(MSFT$MSFT.Adjusted))&#10;&#10;# 创建合并数据集&#10;prices &amp;lt;- merge(aapl, msft)&#10;colnames(prices) &amp;lt;- c(&amp;quot;log_AAPL&amp;quot;, &amp;quot;log_MSFT&amp;quot;)&#10;&#10;# 绘制对数价格趋势&#10;ggplot(prices, aes(x=Index)) +&#10; geom_line(aes(y=log_AAPL, color=&amp;quot;苹果&amp;quot;)) +&#10; geom_line(aes(y=log_MSFT, color=&amp;quot;微软&amp;quot;)) +&#10; labs(title=&amp;quot;苹果与微软对数收盘价序列&amp;quot;, y=&amp;quot;log(Price)&amp;quot;, x=&amp;quot;日期&amp;quot;) +&#10; scale_color_manual(values=c(&amp;quot;#FF6B6B&amp;quot;, &amp;quot;#4ECDC4&amp;quot;)) +&#10; theme_minimal(base_family = &amp;quot;PingFang SC&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2011/10/27/use-r-to-test-cointegration/index_files/figure-html/unnamed-chunk-1-1.png" width="672" /&gt;&#10;&lt;h2 id="平稳性检验"&gt;&lt;a href="#%e5%b9%b3%e7%a8%b3%e6%80%a7%e6%a3%80%e9%aa%8c" class="header-anchor"&gt;&lt;/a&gt;平稳性检验&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 原始序列检验&#10;# 苹果价格ADF检验&#10;summary(ur.df(prices$log_AAPL, type=&amp;quot;drift&amp;quot;, lags=5)) #P-value大于0.05则序列不平稳&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ############################################### &#10;## # Augmented Dickey-Fuller Test Unit Root Test # &#10;## ############################################### &#10;## &#10;## Test regression drift &#10;## &#10;## &#10;## Call:&#10;## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -0.197346 -0.008942 0.000164 0.010330 0.141513 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## (Intercept) 0.0016271 0.0008139 1.999 0.0457 *&#10;## z.lag.1 -0.0002047 0.0002258 -0.907 0.3646 &#10;## z.diff.lag1 -0.0321993 0.0147383 -2.185 0.0290 *&#10;## z.diff.lag2 -0.0155993 0.0147150 -1.060 0.2892 &#10;## z.diff.lag3 -0.0090648 0.0147162 -0.616 0.5379 &#10;## z.diff.lag4 0.0250380 0.0147149 1.702 0.0889 .&#10;## z.diff.lag5 0.0145020 0.0147113 0.986 0.3243 &#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.01992 on 4600 degrees of freedom&#10;## Multiple R-squared: 0.00234,&#9;Adjusted R-squared: 0.001039 &#10;## F-statistic: 1.798 on 6 and 4600 DF, p-value: 0.09538&#10;## &#10;## &#10;## Value of test-statistic is: -0.9067 5.4705 &#10;## &#10;## Critical values for test statistics: &#10;## 1pct 5pct 10pct&#10;## tau2 -3.43 -2.86 -2.57&#10;## phi1 6.43 4.59 3.78&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 微软价格ADF检验&#10;summary(ur.df(prices$log_MSFT, type=&amp;quot;drift&amp;quot;, lags=5)) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ############################################### &#10;## # Augmented Dickey-Fuller Test Unit Root Test # &#10;## ############################################### &#10;## &#10;## Test regression drift &#10;## &#10;## &#10;## Call:&#10;## lm(formula = z.diff ~ z.lag.1 + 1 + z.diff.lag)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -0.148299 -0.008044 0.000121 0.008836 0.160776 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## (Intercept) 0.0001374 0.0010067 0.137 0.89142 &#10;## z.lag.1 0.0001571 0.0002344 0.670 0.50269 &#10;## z.diff.lag1 -0.1088675 0.0147498 -7.381 1.86e-13 ***&#10;## z.diff.lag2 -0.0421217 0.0148530 -2.836 0.00459 ** &#10;## z.diff.lag3 0.0054135 0.0148661 0.364 0.71576 &#10;## z.diff.lag4 -0.0431566 0.0148530 -2.906 0.00368 ** &#10;## z.diff.lag5 -0.0185213 0.0147761 -1.253 0.21010 &#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.01756 on 4600 degrees of freedom&#10;## Multiple R-squared: 0.01478,&#9;Adjusted R-squared: 0.0135 &#10;## F-statistic: 11.5 on 6 and 4600 DF, p-value: 8.16e-13&#10;## &#10;## &#10;## Value of test-statistic is: 0.6703 4.8403 &#10;## &#10;## Critical values for test statistics: &#10;## 1pct 5pct 10pct&#10;## tau2 -3.43 -2.86 -2.57&#10;## phi1 6.43 4.59 3.78&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 一阶差分平稳性验证&#10;# 差分处理&#10;diff_aapl &amp;lt;- diff(prices$log_AAPL)[-1]&#10;diff_msft &amp;lt;- diff(prices$log_MSFT)[-1]&#10;&#10;# 差分序列ADF检验&#10;summary(ur.df(diff_aapl, type=&amp;quot;none&amp;quot;)) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ############################################### &#10;## # Augmented Dickey-Fuller Test Unit Root Test # &#10;## ############################################### &#10;## &#10;## Test regression none &#10;## &#10;## &#10;## Call:&#10;## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -0.197910 -0.007903 0.001180 0.011336 0.140719 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## z.lag.1 -1.04076 0.02112 -49.286 &amp;lt;2e-16 ***&#10;## z.diff.lag 0.01349 0.01473 0.916 0.36 &#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.01999 on 4608 degrees of freedom&#10;## Multiple R-squared: 0.5134,&#9;Adjusted R-squared: 0.5131 &#10;## F-statistic: 2430 on 2 and 4608 DF, p-value: &amp;lt; 2.2e-16&#10;## &#10;## &#10;## Value of test-statistic is: -49.2864 &#10;## &#10;## Critical values for test statistics: &#10;## 1pct 5pct 10pct&#10;## tau1 -2.58 -1.95 -1.62&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;summary(ur.df(diff_msft, type=&amp;quot;none&amp;quot;)) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## ############################################### &#10;## # Augmented Dickey-Fuller Test Unit Root Test # &#10;## ############################################### &#10;## &#10;## Test regression none &#10;## &#10;## &#10;## Call:&#10;## lm(formula = z.diff ~ z.lag.1 - 1 + z.diff.lag)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -0.149197 -0.007368 0.000777 0.009507 0.165463 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## z.lag.1 -1.14697 0.02190 -52.367 &amp;lt; 2e-16 ***&#10;## z.diff.lag 0.03991 0.01475 2.705 0.00685 ** &#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.01758 on 4608 degrees of freedom&#10;## Multiple R-squared: 0.5521,&#9;Adjusted R-squared: 0.5519 &#10;## F-statistic: 2840 on 2 and 4608 DF, p-value: &amp;lt; 2.2e-16&#10;## &#10;## &#10;## Value of test-statistic is: -52.3667 &#10;## &#10;## Critical values for test statistics: &#10;## 1pct 5pct 10pct&#10;## tau1 -2.58 -1.95 -1.62&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;h2 id="协整检验"&gt;&lt;a href="#%e5%8d%8f%e6%95%b4%e6%a3%80%e9%aa%8c" class="header-anchor"&gt;&lt;/a&gt;协整检验&#10;&lt;/h2&gt;&lt;p&gt;我们用Engle-Granger两步法进行协整检验。&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 第一步：构建回归模型&#10;coint_model &amp;lt;- lm(log_AAPL ~ log_MSFT, data=prices)&#10;summary(coint_model) # 显示回归系数与R-squared&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## &#10;## Call:&#10;## lm(formula = log_AAPL ~ log_MSFT, data = prices)&#10;## &#10;## Residuals:&#10;## Min 1Q Median 3Q Max &#10;## -1.20523 -0.17828 0.03798 0.22297 0.77306 &#10;## &#10;## Coefficients:&#10;## Estimate Std. Error t value Pr(&amp;gt;|t|) &#10;## (Intercept) -1.343406 0.020650 -65.06 &amp;lt;2e-16 ***&#10;## log_MSFT 1.131850 0.004805 235.54 &amp;lt;2e-16 ***&#10;## ---&#10;## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1&#10;## &#10;## Residual standard error: 0.3607 on 4611 degrees of freedom&#10;## Multiple R-squared: 0.9233,&#9;Adjusted R-squared: 0.9233 &#10;## F-statistic: 5.548e+04 on 1 and 4611 DF, p-value: &amp;lt; 2.2e-16&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;# 提取残差序列&#10;residuals &amp;lt;- residuals(coint_model)&#10;&#10;# 第二步：残差平稳性检验&#10;adf_test &amp;lt;- ur.df(residuals, type=&amp;quot;none&amp;quot;, lags=5)&#10;print(adf_test@teststat) # ADF统计量&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## tau1&#10;## statistic -2.893062&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;print(adf_test@cval) # 临界值&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## 1pct 5pct 10pct&#10;## tau1 -2.58 -1.95 -1.62&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;ADF统计量为-2.893062,小于1%置信水平对应的临界值-2.58，故拒绝原假设，说明残差序列平稳，两股票存在协整关系。&lt;/p&gt;&#10;&lt;h2 id="对协整关系进行可视化"&gt;&lt;a href="#%e5%af%b9%e5%8d%8f%e6%95%b4%e5%85%b3%e7%b3%bb%e8%bf%9b%e8%a1%8c%e5%8f%af%e8%a7%86%e5%8c%96" class="header-anchor"&gt;&lt;/a&gt;对协整关系进行可视化&#10;&lt;/h2&gt;&lt;pre&gt;&lt;code class="language-r"&gt;# 计算价差序列&#10;spread &amp;lt;- prices$log_AAPL - coint_model$coefficients[2] * prices$log_MSFT&#10;&#10;# 绘制价差通道&#10;ggplot(spread, aes(x=Index)) +&#10; geom_line(aes(y=spread), color=&amp;quot;#2C3E50&amp;quot;) +&#10; geom_hline(yintercept=mean(spread), linetype=&amp;quot;dashed&amp;quot;, color=&amp;quot;#E74C3C&amp;quot;) +&#10; geom_hline(yintercept=mean(spread)+2*sd(spread), color=&amp;quot;#27AE60&amp;quot;, alpha=0.5) +&#10; geom_hline(yintercept=mean(spread)-2*sd(spread), color=&amp;quot;#27AE60&amp;quot;, alpha=0.5) +&#10; labs(title=&amp;quot;协整价差序列与2σ通道&amp;quot;, y=&amp;quot;标准化价差&amp;quot;)+&#10; theme_minimal(base_family = &amp;quot;PingFang SC&amp;quot;)&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;pre&gt;&lt;code&gt;## Don't know how to automatically pick scale for object of type &amp;lt;xts/zoo&amp;gt;.&#10;## Defaulting to continuous.&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;img src="https://gewutang.cn/2011/10/27/use-r-to-test-cointegration/index_files/figure-html/unnamed-chunk-4-1.png" width="672" /&gt;&#10;</description></item><item><title>R获取股票数据</title><link>https://gewutang.cn/2011/04/14/use-r-to-get-trading-data/</link><pubDate>Thu, 14 Apr 2011 00:00:00 +0000</pubDate><guid>https://gewutang.cn/2011/04/14/use-r-to-get-trading-data/</guid><description>&lt;p&gt;R中有多个包都提供了从线上数据库获取股票数据的方法。如果非得在其中找出一个最好的，quantmod当之无愧。譬如下载沪市大盘数据，代码可以是：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(quantmod) &#10;SSE &amp;lt;- getSymbols(&amp;quot;000001.SS&amp;quot;,auto.assign=FALSE) &#10;head(SSE) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;或者：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(quantmod) &#10;setSymbolLookup(SSE=list(name=&amp;quot;000001.SS&amp;quot;, src=&amp;quot;yahoo&amp;quot;)) &#10;getSymbols(&amp;quot;SSE&amp;quot;) &#10;head(SSE) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;以上是Jeff Ryan（quantmod作者）推荐过的方法。不过，后来JoshuaUlrich在他的邮件中给出了一个更简洁的方法：&lt;/p&gt;&#10;&lt;pre&gt;&lt;code class="language-r"&gt;library(quantmod) &#10;getSymbols(&amp;quot;^SSEC&amp;quot;) &#10;head(SSEC) &#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;虽然quantmod的包出自Jeff Ryan之手，显而易见，在这一点上Joshua Ulrich是青出于蓝而胜于蓝。&lt;/p&gt;&#10;</description></item></channel></rss>