Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何检索特定日期和股票代码的股票价格?_R_Quantmod - Fatal编程技术网

R 如何检索特定日期和股票代码的股票价格?

R 如何检索特定日期和股票代码的股票价格?,r,quantmod,R,Quantmod,我有一张桌子 Ticker. Date AAPL. 1/1/2020 AAPL. 1/2/2020 KO. 1/1/2020 KO. 1/20/2020 我想在同一个表中再添加两列,并给出收盘价和调整价 Ticker. Date. Adj_Price. Closing_Price AAPL. 1/1/2020. 234 235 AAPL. 1/2/202

我有一张桌子

Ticker.    Date
AAPL.      1/1/2020
AAPL.      1/2/2020
KO.        1/1/2020
KO.        1/20/2020
我想在同一个表中再添加两列,并给出收盘价和调整价

Ticker.  Date.            Adj_Price.    Closing_Price
AAPL.      1/1/2020.          234           235
AAPL.      1/2/2020.          245           249
KO.        1/1/2020            30            32
KO.        1/20/2020           33            44

我认为我们可以使用quant mod library来实现这一点。这是否有效:

> tickers = c('AAPL', 'KO')
> portfolio <- NULL
> for(t in tickers){
+   portfolio <- cbind(portfolio, getSymbols(t, from = '2020-01-01', to = '2020-01-21', auto.assign = 0)[,c(4,6)])
+ }
> portfolio
           AAPL.Close AAPL.Adjusted KO.Close KO.Adjusted
2020-01-02    75.0875      74.57304    54.99    53.58738
2020-01-03    74.3575      73.84803    54.69    53.29503
2020-01-06    74.9500      74.43647    54.67    53.27554
2020-01-07    74.5975      74.08639    54.25    52.86625
2020-01-08    75.7975      75.27816    54.35    52.96370
2020-01-09    77.4075      76.87714    55.34    53.92844
2020-01-10    77.5825      77.05093    55.53    54.11360
2020-01-13    79.2400      78.69707    56.13    54.69830
2020-01-14    78.1700      77.63441    56.00    54.57161
2020-01-15    77.8350      77.30170    56.70    55.25376
2020-01-16    78.8100      78.27002    56.82    55.37069
2020-01-17    79.6825      79.13655    56.94    55.48764
> portfolio[index(portfolio) %in% as.Date(c('2020-01-15','2020-01-16'))]
           AAPL.Close AAPL.Adjusted KO.Close KO.Adjusted
2020-01-15     77.835      77.30170    56.70    55.25376
2020-01-16     78.810      78.27002    56.82    55.37069
> 
tickers=c('AAPL','KO') >证券投资组合(t股){ +投资组合 AAPL.关闭AAPL.已调整KO.关闭KO.已调整 2020-01-02 75.0875 74.57304 54.99 53.58738 2020-01-03 74.3575 73.84803 54.69 53.29503 2020-01-06 74.9500 74.43647 54.67 53.27554 2020-01-07 74.5975 74.08639 54.25 52.86625 2020-01-08 75.7975 75.27816 54.35 52.96370 2020-01-09 77.4075 76.87714 55.34 53.92844 2020-01-10 77.5825 77.05093 55.53 54.11360 2020-01-13 79.2400 78.69707 56.13 54.69830 2020-01-14 78.1700 77.63441 56.00 54.57161 2020-01-15 77.8350 77.30170 56.70 55.25376 2020-01-16 78.8100 78.27002 56.82 55.37069 2020-01-17 79.6825 79.13655 56.94 55.48764 >投资组合[指数(投资组合)%以截至日期的百分比表示(c('2020-01-15','2020-01-16'))] AAPL.关闭AAPL.已调整KO.关闭KO.已调整 2020-01-15 77.835 77.30170 56.70 55.25376 2020-01-16 78.810 78.27002 56.82 55.37069 >
您可以使用任何日期进行子集设置。

也许可以阅读手册,然后带着更具体的问题回来?请查看
tidyquant::tq_get
。如果Fund是数据集名称,那么它是否有效?
Fund$adjClose谢谢Karthik。不,因为它是一个庞大的数据集,并且无法手动输入日期。Retr中是否有逻辑eving,如中所示,您只需要周一的价格或两天之间的具体差距?只需要当天的收盘价和当天调整后的收盘价。我是编程新手,使用此
Fund$adjClose通常是日期范围内的价格,不确定为什么要随机选择中间几天?下面是一个示例为什么,这是一个包含收入、利润等以及财务结果日期的基本数据集。我希望添加截至财务结果状态的收盘价。