Skip to content

Commit

Permalink
Fixed issue with yahoo API
Browse files Browse the repository at this point in the history
Use temporary fix from 'quantmod' pkg, hopefully to be included by
default soon.

Ref: joshuaulrich/quantmod#157
  • Loading branch information
ceschi committed May 30, 2017
1 parent b51961a commit 45bdee9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
6 changes: 3 additions & 3 deletions Processed data/US_data.txt
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,6 @@ NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA
0.36;0.24;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;2.47065056746884;0.15;-3.69686989545131;-1.40685408811035;3.38;-0.0333177482896838;3854.546;3127.3;12511.3
0.38;0.36;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;2.42369090126012;0.15;-3.8124280000411;-1.45355574439764;2.91;0.367154492587323;3844.973;3230.8;12740.6
0.4;0.38;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;2.32550101274048;0.16;-3.80682960131093;-0.990860876976995;2.69;0.784507754204544;3775.053;3295;12947.3
0.54;0.4;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;2.43443757878857;-0.04;-3.48530809014828;-0.870236838847008;2.5;0.637460634274031;3577.822;3333.8;13131.2
0.79;0.54;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;2.03472412846555;-0.04;-3.31466882012588;-1.08532230783687;2.22;1.20533475495637;3732.705;3398.9;13313.3
NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA
0.54;0.4;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;2.43443757878857;-0.04;-3.48530809014828;-0.870236838847008;2.5;0.637460634274031;3577.822;3333.7;13131.1
0.79;0.54;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;2.03469620235791;-0.04;-3.31466882012588;-0.972561548581107;2.22;1.20533475495637;3732.705;3398.9;13313.4
0.9;0.79;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;NA;-3.50939803737809;NA;NA;NA;NA;NA;NA
79 changes: 45 additions & 34 deletions USdatacoll.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,30 +219,39 @@ tbill_rate_10y <- as.xts(fredr_series(series_id='DGS10',frequency='q'))

## Scraping Yahoo! Finance

# determine current date, adapt the Yahoo! URL
sp_ret <- read_csv(paste0('http://chart.finance.yahoo.com/table.csv?s=^GSPC&a=0&b=3&c=1950&d=',
as.numeric(format(Sys.Date(), '%m')),
'&e=', as.numeric(format(Sys.Date(), '%d')),
'&f=', as.numeric(format(Sys.Date(), '%Y')),
'&g=m&ignore=.csv'),
col_names=T, col_types = cols(
Date = col_date(format = "%Y-%m-%d"),
Open = col_double(),
High = col_double(),
Low = col_double(),
Close = col_double(),
Volume = col_double(),
`Adj Close` = col_double()
))

# # determine current date, adapt the Yahoo! URL
# sp_ret <- read_csv(paste0('http://chart.finance.yahoo.com/table.csv?s=^GSPC&a=0&b=3&c=1950&d=',
# as.numeric(format(Sys.Date(), '%m')),
# '&e=', as.numeric(format(Sys.Date(), '%d')),
# '&f=', as.numeric(format(Sys.Date(), '%Y')),
# '&g=m&ignore=.csv'),
# col_names=T, col_types = cols(
# Date = col_date(format = "%Y-%m-%d"),
# Open = col_double(),
# High = col_double(),
# Low = col_double(),
# Close = col_double(),
# Volume = col_double(),
# `Adj Close` = col_double()
# ))

# downloads daily prices time series through new Yahoo! API
# to be fixed sooner than later
sp_ret <- getSymbols(src='yahoo', Symbols='^GSPC',
from='1950-01-03',
to=format(Sys.Date(), '%Y-%m-%d'))
# adapts the order of the observations
sp_ret <- sp_ret[order(-1:-nrow(sp_ret)),]
sp_ret <- data.frame(sp_ret$Close)
sp_ret <- as.xts(ts(sp_ret[1:(nrow(sp_ret)-1),], start=c(1950, 01), frequency=12))
sp_ret <- diff(log(sp_ret))*100
to=format(Sys.Date(), '%Y-%m-%d'),
auto.assign = F)

# aggregating up to quarterly data
# through monthly upscaling
sp_ret <- to.monthly(sp_ret)

# adapts the order of the observations <- code for old API
# sp_ret <- sp_ret[order(-1:-nrow(sp_ret)),]
# sp_ret <- data.frame(sp_ret$sp_ret.Close)
# sp_ret <- as.xts(ts(sp_ret[1:(nrow(sp_ret)-1),], start=c(1950, 01), frequency=12))

sp_ret <- diff(log(sp_ret$sp_ret.Close))*100
sp_ret <- as.xts(aggregate(sp_ret, as.yearqtr(as.yearmon(time(sp_ret))), mean))

# one_year <- as.xts(fredr_series(series_id='DGS1', frequency='q'))
Expand All @@ -267,18 +276,20 @@ names(spreads) <- c('spread_baa', 'spread_sp_3m')
# plot(sa_surplus)
#
# # debt lvl
# # money aggregates
#
# base <- as.xts(fredr_series(series_id='BOGMBASE', frequency='q'))/1000
# m1 <- as.xts(fredr_series(series_id='M1SL', frequency='q'))
# m2 <- as.xts(fredr_series(series_id='M2SL', frequency='q'))
#
# money <- merge(base, m1, m2)
# names(money) <- c('base', 'm1', 'm2')
#
# # monetary aggregates growth rates
# money_g <- diff(log(money))
# names(money_g) <- c('base_g', 'm1_g', 'm2_g')


#### MONEY AGGREGATES ####

base <- as.xts(fredr_series(series_id='BOGMBASE', frequency='q'))/1000
m1 <- as.xts(fredr_series(series_id='M1SL', frequency='q'))
m2 <- as.xts(fredr_series(series_id='M2SL', frequency='q'))

money <- merge(base, m1, m2)
names(money) <- c('base', 'm1', 'm2')

# monetary aggregates growth rates
money_g <- diff(log(money))
names(money_g) <- c('base_g', 'm1_g', 'm2_g')
#
# # spf data

Expand Down
9 changes: 8 additions & 1 deletion functs.R
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,17 @@ pkgs <- c('vars', 'MSwM','tidyverse',
'dyn', 'strucchange', 'xts',
'MASS', 'car', 'ggplot2',
'mFilter', 'fredr', 'xlsx',
'dplyr', 'readr', 'quantmod')
'dplyr', 'readr', 'quantmod',
'devtools')
# fill pkgs with names of the packages to install

instant_pkgs(pkgs)

## part needed for handling Yahoo! finance data,
## supposed to change w/ next release of 'quantmod' pkg
## requires installation of 'Rtools' to compile

devtools::install_github("joshuaulrich/quantmod", ref="157_yahoo_502")
library(quantmod)

rm(pkgs)
6 changes: 3 additions & 3 deletions mainTaylor.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# for US (Trulyfinal.R) and for the panel building

#### Functions #####
source('functs.R')
source('functs.R', verbose=F, echo=F)

#### Directories
working_directory <- getwd()
Expand All @@ -25,13 +25,13 @@ source("USdatacoll.R", verbose=F, echo=F)

##### VISUALIZATION ####

source('visuals.R')
source('visuals.R', verbose=F, echo=F)


#### REGRESSIONS - SIMPLE ONES ####

# US Data
source('USreg.R')
source('USreg.R', verbose=F, echo=F)


#### REGRESSIONS - MORE SOPHISTICATED ####
Expand Down

0 comments on commit 45bdee9

Please sign in to comment.