quantmod

Setting the last date collected to be a Monday

不想你离开。 提交于 2020-01-02 23:00:55
问题 I can download a data set using Quantmod library(quantmod) getSymbols("SPY", from="2013-01-01", to=Sys.Date()) chartSeries(SPY) I would like the last data to be collected to be always on a Monday (so if I collect data on say Thursday, the Tuesday,Wednesday and Thursday data points will be filtered out). Is there a way to do this. Thank you for your help. 回答1: replace your to=Sys.Date() with to=(Sys.Date()-c(6:0))[format((Sys.Date()-c(6:0)),"%w")=="1"] 来源: https://stackoverflow.com/questions

Why Open,High,Low prices are wrong when using quantmod?

淺唱寂寞╮ 提交于 2020-01-02 05:40:13
问题 I use this code on a weekly basis, however, when I tried it today I got wrong results of OHL and SPY.Adjusted, looking at the Close prices and Volume they seem correct so what's wrong? rm(list = ls()) options(scipen=999) require(quantmod) spy<-getSymbols("SPY", src = 'yahoo', from = '2007-05-31', auto.assign = T) spy<-cbind(SPY) dim(SPY) head(SPY) This the outcome from Yahoo: Date Open High Low Close Adj Close* Volume May 31, 2007 153.67 153.89 153.12 153.32 123.86 114,866,700 This is the

Calculate cumulatve growth/drawdown from local min/max

痞子三分冷 提交于 2020-01-01 05:51:17
问题 I'm learning R (and its application to trading tasks via quantmod lib) and looking through the community pretty regularly to get a lot of new knowledge and tricks from here. My impression about R in general and quantmod lib in particular - it's awesome. At this point I need help of seasoned R users. I'm using timeseries downloaded via getSymbols and I need to calculate cumulative growth/drawdown from local minimum/maximum respectively. I can solve my task using FOR cycles as well as I can do

Date information disappears when save to CSV

江枫思渺然 提交于 2019-12-30 07:13:05
问题 I am trying to pull some data in from the internet and then export it to a CSV file, but I am loosing my date information in the CSV file. I can't figure out why. I'm new to R so please keep responses simple. Here is my code: Library(quantmod) getSymbols("SPY", from = "2012-01-01", to = "2012-12-31") write.csv(SPY, "C:/SPY.csv") 回答1: as SPY is a xts/zoo object this will do the trick: replace: write.csv(SPY, "C:/SPY.csv") with write.zoo(SPY,"C:/SPY.csv",index.name="Date",sep=",") 回答2: Try:

addSMA not drawn on graph when called from function

喜夏-厌秋 提交于 2019-12-28 06:37:24
问题 I am a newbie for R and have got stuck here. I am trying to draw a graph with price, sma and ema. When I call the graph from the command line it draws fine including price, sma and ema: tickers = c("BIIB","ISRG","AIG","FITB","GE","JNY","VIAB","WFM","WMB") x= 1 print(paste("Preparing ADX graph for :",paste(tickers[x]))) tmp <- read.csv(paste(tickers[x],".csv", sep=""),as.is=TRUE, header=TRUE, row.names=NULL) tmp$Date<-as.Date(tmp$Date) ydat = xts(tmp[,-1],tmp$Date) names(ydat) <- c("Open",

Merge getSymbols result into one xts object

痴心易碎 提交于 2019-12-28 04:28:24
问题 I have the following code: library(quantmod) tckrs <- c("TLT", "LQD", "HYG", "SPY", "DBC") NumTckrs <- length(tckrs) getSymbols(tckrs, from="1900-01-01", to=Sys.Date()) # merge to allign the start dates MainDF <- merge(Ad(TLT), Ad(LQD), Ad(HYG), Ad(SPY), Ad(DBC), all=FALSE) I would like to not have to repeat the stock symbols in the last line. Does anyone know how this could be done? 回答1: Load all the data into an environment, then call Ad on each, and merge them. Also note that getSymbols

download S&P 500 data with Google as a data source, instead of Yahoo

孤街醉人 提交于 2019-12-25 15:39:32
问题 Not sure if yahoo finances are no longer working, but I have been using google for stock data lately because yahoo public data are unavailable. However, for S&P 500 I used to use the symbol ^GSPC that used to work with yahoo, but doesn't work with google. I tried the following: library(quantmod) getSymbols('GSPC',src="google") getSymbols('^GSPC',src="google") Which generates an error message. Is there a different symbol for google finances that represents S&P 500? 回答1: Yahoo Finance is

For-Loops for functions containing multiple arguments (R)

ぃ、小莉子 提交于 2019-12-25 09:28:18
问题 i have the following problem: I have got a custom defined function in R, which should get finance data (using quantmod); Now that i am thinking about that i maybe want to get several companies' stock prices or so i would it find more convenient if i can have a dataframe which contains the arguments of my function and a loop just goes through all the parts of the data frame and then saves the results into my environment (or a specific new dataframe or whatever). The relevant parts of my code:

Using QuantMod/tseries monthlyReturn with dividend

放肆的年华 提交于 2019-12-24 10:49:24
问题 Is there are way using Monthly Return function to factor in dividends into the monthlyReturn? I have my an xts object with price and dividend columns. 回答1: You can use TTR::adjRatios directly to calculate the adjustment ratios necessary to create a "total-return" price series. Then you can calculate the monthly return using the adjusted series. Note that you might also need to adjust for splits. library(quantmod) # create sample data SPY.Close <- Cl(getSymbols("SPY", auto.assign=FALSE)) SPY

Automatically plot (and save) list of xts objects

北战南征 提交于 2019-12-24 01:44:09
问题 I have a long list of xts objects and I'd like to automatically plot and save them to a folder (because it takes a v long time to do manually with >500 plots). The tricky part seems to be to apply functions to lists of xts objects. For example, getting a generalized naming method for the list elements based on its date (each xts object in the list is a unique day), and plotting each object and saving them to a file path with its own name. The data can be found in a previous post here. Each