Help from R experienced user(trader), to download daily close

Discussion in 'App Development' started by jk90029, Dec 25, 2014.

  1. Dear-

    Let c:\\aa.txt have

    BAC
    DD
    MSFT
    ZTS

    It is well-known that, in R,

    install.packages("quantmod")
    library(quantmod)
    WatchList <- as.vector(as.matrix(read.csv(file="c:\\aa.txt", header=FALSE)))
    getSymbols(WatchList,from ="2014-01-01")

    gives us 4 daily prices (roughly 250) for this year.

    *********************************************************

    Now, when we like to make a matrix (250 row and 4 columns) with ONLY daily closes of the 4 stocks

    Obviously cbind(BAC[,4], DD[,4], MSFT[,4], ZTS[,4]) gives us an answer.

    ********************************************************

    However, when our WatchList contains 500 symbols (similar to SnP500), instead of the above 4, what is the appropriate R commands?

    Please do NOT tell me to type the time-consuming 500 symbols with [,4].
     
  2. tdazio

    tdazio

    ...
    getSymbols(WatchList,from ="2014-01-01")
    Close <- do.call(merge,lapply(WatchList,function(x) Cl(get(x))))
    # head(Close)
     
  3. To [tdazio]

    Would you please help me more for explaining

    1) do.call
    2) merge
    3) function?
    4) x?
    5) Cl
    6) get

    ***************************

    And furthermore, are you successful to use R and API to BUY order delivery to IB server?
     
  4. tdazio

    tdazio

    jk,
    you can get good explains using r-help function (?merge, ?do.call..) or googoling R-Sig Archive (or stackoverflow).
    Sorry, I don't use IB.

    Another way to retrive yahoo data, using 'tawny' pachage (search CRAN packages repository):

    #
    library(tawny)
    WatchList <- c("BAC","DD","MSFT","ZTS")
    Close <- getPortfolioReturns(WatchList, start = "2014-01-01", fun = function(x) Cl(x))
    # head(Close)

    using tawny::getPortfolioReturns you can also download directly prices, returns, log-returns or other modifying 'fun'
    happy new year
     
  5. Here is error message. Please provide me detail on function() and Cl().
    I am a R beginner.

    ****************************************************

    > install.packages("tawny")
    > library(tawny)

    > WatchList <- c("BAC","DD","MSFT","ZTS")
    > a <- getPortfolioReturns(WatchList, start = "2014-01-01", fun = function(x) Cl(x))

    As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
    auto.assign=TRUE by default.

    This behavior will be phased out in 0.5-0 when the call will
    default to use auto.assign=FALSE. getOption("getSymbols.env") and
    getOptions("getSymbols.auto.assign") are now checked for alternate defaults

    This message is shown once per session and may be disabled by setting
    options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
    Error in fun(asset) : could not find function "Cl"
    In addition: Warning message:
    In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m, :
    downloaded length 12511 != reported length 200
     
  6. tdazio

    tdazio

    Sorry, I haven't error message, but only a warnig about next release of getSymbols function. Script works for me with R version 3.1.2 (2014-10-31).

    Code:
    Welcome at Sat Dec 27 03:35:05 2014
    > library(tawny)
    > WatchList <- c("BAC","DD","MSFT","ZTS")
    > Close <- getPortfolioReturns(WatchList, start = "2014-01-01", fun = function(x) Cl(x))
        As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
    auto.assign=TRUE by default.
    
    This  behavior  will be  phased out in 0.5-0  when the call  will
    default to use auto.assign=FALSE. getOption("getSymbols.env") and
    getOptions("getSymbols.auto.assign") are now checked for alternate defaults
    
    This message is shown once per session and may be disabled by setting
    options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
    INFO [2014-12-27 03:35:14] Binding BAC for [2014-01-02,2014-12-26]
    INFO [2014-12-27 03:35:15] Binding DD for [2014-01-02,2014-12-26]
    INFO [2014-12-27 03:35:15] Binding MSFT for [2014-01-02,2014-12-26]
    INFO [2014-12-27 03:35:15] Binding ZTS for [2014-01-02,2014-12-26]
    > head(Close)
                 BAC    DD  MSFT   ZTS
    2014-01-03 16.41 63.78 36.91 32.05
    2014-01-06 16.66 62.96 36.13 31.98
    2014-01-07 16.50 62.33 36.41 32.10
    2014-01-08 16.58 63.13 35.76 31.74
    2014-01-09 16.83 63.94 35.53 31.96
    2014-01-10 16.77 63.54 36.04 32.54
    >
    
     
  7. Today, I found that your code works fine in R, even with version 3.0.2

    However your code does NOT work in RStudio,

    I do NOT know why.
     
  8. Furthermore, those code seems ONLY daily close.

    How about during the 9:30 and 4:00, can we download intraday price?

    1) I do NOT need tick or every seconds but 1-min or 5-min or 15-min data are fine.
    2) I heard that 15-min delayed data is free in some place.
    3) I can pay some money for the data that I want.
    4) Hope to use similar code as above / Hope to use R preferably, or Python possibly if not.
    5) I heard IB provides ONLY 100 stocks to the trader, a lot smaller than my personal watchlist with about 200~300.