我想将中国公司(在美国股票市场,例如纳斯达克(NASDAQ)上上市)的历史股价(每日)下载到R中。
因此,我在纳斯达克找到了公司列表
stockprice <- c("YI","VNET","QFIN","JOBS","ATV","AMCN","BABA","ACH","AMBO","ATAI","JG","BIDU","BZUN","BI","LI","BCAC","BCACR","BCACU","BCACW","BITA","BRQS","BEDU","CSIQ","CBAT","CYOU","CADC","CAAS","GLG","CBPO","CCCL","CCRC","DL","CEA","JRJC","CGA","HGSH","CIFS","CJJD","LFC","BORN","COE","SNP","CPHI","CREG","ZNH","SXTC","CNTF","CHA","CXDC","CCIH","CNET","CLPS","CMSS","CMSSR","CMSSU","CMSSW","CCM","CTRP","DQ","DTSS","DOGZ","LYL","DXF","EHIC","EVK","SFUN","FANH","FAMI","FORK","FTFT","FFHL","GDS","DNJR","GLAC","GLACR","GLACU","GLACW","GRNQ","GSUM","GSH","GURE","HLG","HEBT","HX","HPJ","HOLI","HMI","HNP","HTHT","IQ","ITP","JD","JKS","JMU","KNDI","KBSF","KGJI","LX","LLIT","LITB","LOAC","LOACR","LOACU","LOACW","LKCO","MDJH","MTC","MOMO","MOXC","NTP","NTES","EDU","NEWA","NIU","NOAH","SEED","OSN","PTR","FENG","PDD","PME","PT","PLAG","PHCF","QD","QTT","RCON","SOL","BIQI","RENN","RETO","REDU","SECO","SMI","AIHS","SGOC","TYHT","SINA","SHI","SVA","SOHU","SORL","SPI","SSLJ","TAL","TANH","TAOP","TEDU","PETZ","NCTY","TMSR","TC","TOUR","UTSI","UXIN","VIOT","VIPS","WB","WINS","XIN","XNET","YIN","YRD","YY","ZLAB","ZKIN","ZTO") %>%
tq_get(get = "stock.prices",
from = "2014-01-01",
to = "2018-12-31") %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
col_rename = "stockprice")
问题1 我想按日期将表排序,并将每个公司的表放在单独的列(而不是行!)中。
问题2 这些公司未在同一时间范围内上市(其中一些公司的上市时间少于5年)。 如何“清理”表并为缺失值添加“零”?
(也许)问题3 目的是要获得应该 100 x(log(P_t)-log(P_t-1)) 因此,今天的价格对数减去昨天的价格对数乘以100。
我不知道这是否由“ mutate_fun = periodReturn ”发生。
我也尝试了其他代码,但是以上代码最接近输出。
答案 0 :(得分:0)
如果X是问题代码的结果(在修正后–当前无法按发布方式工作),则可以全部在一条语句中完成(给出多变量动物园序列):
read.zoo(X, index = "date", split = "symbol")