2017-08-08 62 views

回答

0

我不確定爲什麼在近1個月內沒有得到任何答覆。也許你需要考慮你發佈的特定鏈接以外的選項。

library(BatchGetSymbols) 

first.date <- Sys.Date()-365 
last.date <- Sys.Date() 

df.SP500 <- GetSP500Stocks() 
tickers <- df.SP500$tickers 

l.out <- BatchGetSymbols(tickers = tickers, 
         first.date = first.date, 
         last.date = last.date) 

print(l.out$df.control) 
print(l.out$df.tickers) 

此外,您知道,您可以使用quantmod獲取各種歷史庫存數據。

以下是一些示例。

############################################################################### 
# Load Systematic Investor Toolbox (SIT) 
############################################################################### 
con = gzcon(url('http://www.systematicportfolio.com/sit.gz', 'rb')) 
    source(con) 
close(con) 

    #***************************************************************** 
    # Load historical data 
    #****************************************************************** 
    load.packages('quantmod') 

    stock.folder = 'c:\\Stocks\\Data\\' 
    tickers = spl('UUP,EMB,HYG') 

    data <- new.env() 


    # load historical data, select data load method 
    data.load.method = 'basic' 

    if(data.load.method == 'basic') {  
     # quantmod - getSymbols 
     getSymbols(tickers, src = 'yahoo', from = '1970-01-01', env = data, auto.assign = T) 
    }else if(data.load.method == 'basic.local') { 
     # if you saved yahoo historical price files localy 
     getSymbols.sit(tickers, src = 'yahoo', from = '1980-01-01', env = data, auto.assign = T, stock.folder = stock.folder) 
    }else if(data.load.method == 'custom.local') { 
     # custom format historical price files 
     for(n in tickers) { 
      data[[n]] = read.xts(paste(stock.folder, n, '.csv', sep=''), format='%m/%d/%Y') 
     } 
    }else if(data.load.method == 'custom.one.file') { 
     # read from one csv file, column headers are tickers 
     filename = 'hex.csv' 
     all.data = read.xts(paste(stock.folder, filename, sep=''), format='%m/%d/%Y') 
     for(n in names(all.data)) { 
      data[[n]] = all.data[,n] 
      colnames(data[[n]]) = 'Close' 
      data[[n]]$Adjusted = data[[n]]$Open = data[[n]]$High = data[[n]]$Low = data[[n]]$Close 
     } 
    }  


    # prepare data for back test 
     for(i in ls(data)) data[[i]] = adjustOHLC(data[[i]], use.Adjusted=T)        
    bt.prep(data, align='remove.na') 


    #***************************************************************** 
    # Code Strategies 
    #****************************************************************** 
    prices = data$prices 
    n = ncol(prices) 

    models = list() 

    # find period ends 
    period.ends = endpoints(prices, 'months') 
     period.ends = period.ends[period.ends > 0] 

    obj = portfolio.allocation.helper(data$prices, period.ends=period.ends, lookback.len = 250, 
     min.risk.fns = list(EW=equal.weight.portfolio, 
         RP=risk.parity.portfolio, 
         MV=min.var.portfolio, 
         MC=min.corr.portfolio) 
    ) 

    models = create.strategies(obj, data)$models 

    #***************************************************************** 
    # Create Report 
    #******************************************************************  

    strategy.performance.snapshoot(models, T) 

https://systematicinvestor.wordpress.com/2013/06/01/loading-historical-stock-data/

https://chrisconlan.com/download-daily-data-every-sp-500-stock-r/