I am currently downloading currency pairs from Oanda using getSymbols and here is my code:
Currency.Pairs <- as_tibble(c("EUR/USD", "USD/JPY", "GBP/USD", "EUR/GBP"))
colnames(Currency.Pairs) <- "Pairs"
getSymbols(Currency.Pairs$Pairs, src = "oanda", from = "2012-01-01", to = "2016-12-20")
output:
[1] "EURUSD" "USDJPY" "GBPUSD" "EURGBP"
Is there a way that I can download all the prices of currency prices in one single list or data frame, instead of dataframes for each currency pair? Thanks in advance!
The env (environment) parameter of getSymbols allows creation of a secure location to store
and manipulate the price data as below:
#tibble library is not required (tibble::as_tibble)
Currency.Pairs <- c("EUR/USD", "USD/JPY", "GBP/USD", "EUR/GBP")
#create new environment to house all price data
newEnv = new.env()
#source data
getSymbols(Currency.Pairs, src = "oanda",env=newEnv, from = "2012-01-01", to = "2016-12-20")
#merge all price series
allTimeSeries = do.call(merge.xts,as.list(newEnv))
#convert column names to original form EUR.USD => EUR/USD
colnames(allTimeSeries) = gsub("[.]","\\/",colnames(allTimeSeries))
#reorder columns as per original order
allTimeSeries = allTimeSeries[,Currency.Pairs]
head(allTimeSeries)
# EUR/USD USD/JPY GBP/USD EUR/GBP
#2012-01-01 1.29590 76.8892 1.55384 0.833490
#2012-01-02 1.29375 76.9584 1.55049 0.834425
#2012-01-03 1.30038 76.7861 1.55779 0.834755
#2012-01-04 1.30036 76.6969 1.56310 0.831920
#2012-01-05 1.28717 76.8753 1.55586 0.827300
#2012-01-06 1.27698 77.1381 1.54737 0.825255
tail(allTimeSeries)
# EUR/USD USD/JPY GBP/USD EUR/GBP
#2016-12-15 1.04682 117.772 1.25015 0.837350
#2016-12-16 1.04349 118.106 1.24366 0.839055
#2016-12-17 1.04510 117.915 1.24884 0.836935
#2016-12-18 1.04512 117.913 1.24887 0.836930
#2016-12-19 1.04458 117.339 1.24500 0.839030
#2016-12-20 1.03906 117.695 1.23718 0.839870
Related
I'm looking to use the getCensus command to grab information from a series ID for all tracts in every state in the US. The Census API doesn't let you do this automatically (only lets you get all tracts from one state at a time), so I am looking for a way to loop through every state to gather all tracts, append each result, and then export to one large csv.
I currently have code that exports each state's result to a csv:
library(censusapi)
list<- listCensusMetadata("acs/acs5/profile", vintage=2017, group="DP04", type="variables")
tract5yrunits1<- getCensus(name = "acs/acs5/profile",
vintage=2017,
vars=c("NAME", "DP04_0011E", "DP04_0012E","DP04_0013E"),
region = "tract:*",
regionin = "state:01")
View(tract5yrunits1)
write.csv(tract5yrunits1,'tract5yrunits1.csv')
Where 1 is the first state (AK). But I would like to loop through a vector of states, append, and export the appended file.
Any help would be appreciated!
Try this
tract5yrunits<- getCensus(name = "acs/acs5/profile",
vintage=2017,
vars=c("NAME", "DP04_0011E", "DP04_0012E","DP04_0013E"),
region = "tract:*",
regionin = "state:01")
# states vector
states <- as.character(seq(2,50))
states[1:8] <- paste("0",states[1:8],sep="")
# loop through states
for (state in states) {
tract5yrunitstemp<- getCensus(name = "acs/acs5/profile",
vintage=2017,
vars=c("NAME", "DP04_0011E", "DP04_0012E","DP04_0013E"),
region = "tract:*",
regionin = paste("state:",state,sep=""))
# add rows
tract5yrunits <- rbind(tract5yrunits,tract5yrunitstemp)
}
write.csv(tract5yrunits1,'tract5yrunits1.csv')
I have created a RasterStack from NDVI layers of the MODIS data. Now i want to extract time series data from different locations of this data so that i can use BFAST/greenbrown package to estimate trend and breakpoints.
Here is how i have created the stack:
#runGdal(Job="testJob","MYD13Q1",begin = "2018.01.09", end = "2018.12.27",
# tileH = 26:29, tileV = 4:7
# , SDSstring = "1000000000000000000000")
###NDVI files path
NDVI_files_path <- "/media/MyData/Data/MODIS/PROCESSED/MYD13Q1.006_20190527193158"
all_NDVI_files <- list.files(NDVI_files_path,
full.names = TRUE,
pattern = ".tif$")
all_NDVI_files
### Raster Stack
NDVI_stack <- stack(all_NDVI_files)
How can i extract time series data for any specific area in Raster stack ?
You can use lubridate and rts to create a RasterStackTS object as follows:
#Load libraries
library(rts)
library(lubridate)
#Reproducible example (use your files here)
all_NDVI_files = c('MOD13Q1.006__250m_16_days_EVI_doy2000049_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000065_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000081_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000097_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000113_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000129_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000145_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000161_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000177_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000193_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000209_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000225_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000241_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000257_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000273_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000289_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000305_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000321_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000337_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2000353_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001001_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001017_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001033_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001049_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001065_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001081_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001097_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001113_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001129_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001145_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001161_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001177_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001193_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001209_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001225_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001241_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001257_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001273_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001289_aid0001.tif',
'MOD13Q1.006__250m_16_days_EVI_doy2001305_aid0001.tif')
#Depending on file format, extract dates (this example uses MODIS 16-day composite NDVI)
ndvi.time = data.frame(year=substr(basename(all_NDVI_files),34,37),
julD=substr(basename(all_NDVI_files),38,40))
ndvi.time$dateJ = paste(ndvi.time$year,ndvi.time$julD,sep='-')
ndvi.time$julD = parse_date_time(ndvi.time$dateJ,'y-j')
# create your RasterStackTS object
# Use your actual rasterstack here i.e. it's not reproducible with above code
ndvi.rts = rts(NDVI_stack ,ndvi.time$julD)
ndvi.rts
plot(ndvi.rts)
Hope this helps!
I am trying my best at a simple event study in R, with some data retrieved from the Wharton Research Data Service (WRDS). I am not completely new to R, but I would describe my expertise level as intermediate. So, here is the problem. I am using the eventstudies package and one of the steps is converting the physical dates to event time frame dates with the phys2eventtime(..) function. This function takes multiple arguments:
z : time series data for which event frame is to be generated. In the form of an xts object.
Events : it is a data frame with two columns: unit and when. unit has column name of which response is to measured on the event date, while when has the event date.
Width : width corresponds to the number of days on each side of the event date. For a given width, if there is any NA in the event window then the last observation is carried forward.
The authors of the package have provided an example for the xts object (StockPriceReturns) and for Events (SplitDates). This looks like the following:
> data(StockPriceReturns)
> data(SplitDates)
> head(SplitDates)
unit when
5 BHEL 2011-10-03
6 Bharti.Airtel 2009-07-24
8 Cipla 2004-05-11
9 Coal.India 2010-02-16
10 Dr.Reddy 2001-10-10
11 HDFC.Bank 2011-07-14
> head(StockPriceReturns)
Mahindra.&.Mahindra
2000-04-03 -8.3381609
2000-04-04 0.5923550
2000-04-05 6.8097616
2000-04-06 -0.9448889
2000-04-07 7.6843828
2000-04-10 4.1220462
2000-04-11 -1.9078480
2000-04-12 -8.3286900
2000-04-13 -3.8876847
2000-04-17 -8.2886060
So I have constructed my data in the same way, an xts object (DS_xts) and a data.frame (cDS) with the columns "unit" and "when". This is how it looks:
> head(DS_xts)
61241
2011-01-03 0.024247
2011-01-04 0.039307
2011-01-05 0.010589
2011-01-06 -0.022172
2011-01-07 0.018057
2011-01-10 0.041488
> head(cDS)
unit when
1 11754 2012-01-05
2 10104 2012-01-24
3 61241 2012-01-31
4 13928 2012-02-07
5 14656 2012-02-08
6 60097 2012-02-14
These are similar in my opinion, but how it looks does not tell the whole story. I am quite certain that my problem is in how I have constructed these two objects. Below is my R code:
#install.packages("eventstudies")
library("eventstudies")
DS = read.csv("ReturnData.csv")
cDS = read.csv("EventData.csv")
#Calculate Abnormal Returns
DS$AR = DS$RET - DS$VWRETD
#Clean up and let only necessary columns remain
DS = DS[, c("PERMNO", "DATE", "AR")]
cDS = cDS[, c("PERMNO", "DATE")]
#Generate correct date format according to R's as.Date
for (i in 1:nrow(DS)) {
DS$DATE[i] = format(as.Date(toString(DS$DATE[i]), format = "%Y %m %d"), format = "%Y-%m-%d")
}
for (i in 1:nrow(cDS)) {
cDS$DATE[i] = format(as.Date(toString(cDS$DATE[i]), format = "%Y %m %d"), format = "%Y-%m-%d")
}
#Rename cDS columns according to phys2eventtime format
colnames(cDS)[1] = "unit"
colnames(cDS)[2] = "when"
#Create list of unique PERMNO's
PERMNO <- unique(DS$PERMNO)
for (i in 1:length(PERMNO)) {
#Subset based on PERMNO
DStmp <- DS[DS$PERMNO == PERMNO[i], ]
#Remove PERMNO column and rename AR to PERMNO
DStmp <- DStmp[, c("DATE", "AR")]
colnames(DStmp)[2] = as.character(PERMNO[i])
dates <- as.Date(DStmp$DATE)
DStmp <- DStmp[, -c(1)]
#Create a temporary XTS object
DStmp_xts <- xts(DStmp, order.by = dates)
#If first iteration, just create new variable, otherwise merge
if (i == 1) {
DS_xts <- DStmp_xts
} else {
DS_xts <- merge(DS_xts, DStmp_xts, all = TRUE)
}
}
#Renaming columns for matching
colnames(DS_xts) <- c(PERMNO)
#Making sure classes are the same
cDS$unit <- as.character(cDS$unit)
eventList <- phys2eventtime(z = DS_xts, events = cDS, width = 10)
So, if I run phys2eventtime(..) it returns:
> eventList <- phys2eventtime(z = DS_xts, events = cDS, width = 10)
Error in if ((location <= 1) | (location >= length(x))) { :
missing value where TRUE/FALSE needed
In addition: Warning message:
In findInterval(when, index(x)) : NAs introduced by coercion
I have looked at the original function (it is available at their GitHub, can't use more than two links yet) to figure out this error, but I ran out of ideas how to debug it. I hope someone can help me sort it out. As a final note, I have also looked at another (magnificent) answer related to this R package (question: "format a zoo object with “dimnames”=List of 2"), but it wasn't enough to help me solve it (or I couldn't yet comprehend it).
Here is the link for the two CSV files if you would like to reproduce my error (or solve it!).
I stuck in a problem with R Programming.
My aim is to randomly select 2 stocks out of the Swiss Market Index, which contains of 30 stocks.
Until now I solved the random pick of the 2 stocks with the following code:
SMI_components <- cbind("ABB (ABBN.VX)", "ADECCO (ADEN.VX)", "ACTELION (ATLN.VX)", "JULIUS BAER GRP (BAER.VX)", "RICHEMONT (CFR.VX)", "CREDIT SUISSE (CSGN.VX)", "GEBERIT (GEBN.VX)", "GIVAUDAN (GIVN.VX)", "HOLCIM (HOLN.VX)", "NESTLE (NESN.VX)", "NOVARTIS (NOVN.VX)", "TRANSOCEAN (RIGN.VX)", "ROCHE HOLDING (ROG.VX)", "SWISSCOM (SCMN.VX)", "SGS (SGSN.VX)", "SWISS RE (SREN.VX)", "SYNGENTA (SYNN.VX)", "UBS (UBSG.VX)", "SWATCH GROUP (UHR.VX)", "ZURICH INSURANCE GROUP (ZURN.VX)")
for(i in 1:1){
print(sample(SMI_components, 2))
}
How do I continue my code, if I want to download the historical data from these two random picked stocks?
For example, the random selection is:
"NOVARTIS (NOVN.VX)" and "ZURICH INSURANCE GROUP (ZURN.VX)"
how to continue that ...
SMI_NOVARTIS <- yahooSeries ("NOVN.VX", from = "2005-01-01", to = "2015-07-30", frequency = "daily")
SMI_ZURICH <- yahooSeries ("ZURN.VX", from = "2005-01-01", to = "2015-07-30", frequency = "daily")
I would really appreciate your help
Regards
print outputs to the console but doesn't store anything. So the first thing to do is assign the output of sample into a variable.
my_picks <- sample(SMI_components, 2)
Extract ticker symbol between parens (courtesy the comment below):
my_picks <- sub(".*\\((.*)\\).*", "\\1", my_picks)
Then you can use lapply, to call a function (yahooSeries) for each value in my_picks.
series_list <- lapply(my_picks, yahooSeries, from = "2005-01-01", to = "2015-07-30", frequency = "daily")
Then you'll get the output in a list. series_list[[1]] will have the output of yahooSeries for the first value of my_picks, and series_list[[2]] for the second
Lastly, not sure why you bothered with the single-iteration for loop, but you don't need that
I am stuck with the following code.
For reference the code it is taken from the following website (http://gekkoquant.com/2013/01/21/statistical-arbitrage-trading-a-cointegrated-pair/), I am also compiling the code through R Studio.
library("quantmod")
startDate = as.Date("2013-01-01")
symbolLst<-c("WPL.AX","BHP.AX")
symbolData <- new.env()
getSymbols(symbolLst, env = symbolData, src = "yahoo", from = startDate)
stockPair <- list(
a =coredata(Cl(eval(parse(text=paste("symbolData$\"",symbolLst[1],"\"",sep="")))))
,b = coredata(Cl(eval(parse(text=paste("symbolData$\"",symbolLst[2],"\"",sep="")))))
,hedgeRatio = 0.70 ,name=title)
spread <- stockPair$a - stockPair$hedgeRatio*stockPair$b
I am getting the following error.
Error in stockPair$a - stockPair$hedgeRatio * stockPair$b :
non-conformable arrays
The reason these particular series don't match is because "WPL.AX" has an extra value (date:19-05-2014 - the matrix lengths are different) compared to "BHP". How can I solve this issue when loading data?
I have also tested other stock pairs such as "ANZ","WBC" with the source = "google" which produces two of the same length arrays.
> length(stockPair$a)
[1] 360
> length(stockPair$b)
[1] 359
Add code such as this prior to the stockPair computation, to trim each xts set to the intersection of dates:
common_dates <- as.Date(Reduce(intersect, eapply(symbolData, index)))
symbolData <- eapply(symbolData, `[`, i=common_dates)
Your code works fine if you don't convert your xts object to matrix via coredata. Then Ops.xts will ensure that only the rows with the same index will be subtracted. And fortune(106) applies.
fortunes::fortune(106)
# If the answer is parse() you should usually rethink the question.
# -- Thomas Lumley
# R-help (February 2005)
stockPair <- list(
a = Cl(symbolData[[symbolLst[1]]])
,b = Cl(symbolData[[symbolLst[2]]])
,hedgeRatio = 0.70
,name = "title")
spread <- stockPair$a - stockPair$hedgeRatio*stockPair$b
Here's an alternative approach:
# merge stocks into a single xts object
stockPair <- do.call(merge, eapply(symbolData, Cl))
# ensure stockPair columns are in the same order as symbolLst, since
# eapply may loop over the environment in an order you don't expect
stockPair <- stockPair[,pmatch(symbolLst, colnames(stockPair))]
colnames(stockPair) <- c("a","b")
# add hedgeRatio and name as xts attributes
xtsAttributes(stockPair) <- list(hedgeRatio=0.7, name="title")
spread <- stockPair$a - attr(stockPair,'hedgeRatio')*stockPair$b