For some reason getSymbols is returning inaccurate data for the below symbol. Example for this data in the high column the price went from 35 to 3515 between 2021 feb and 2021 march 16th.
d <- as.data.frame(
getSymbols(
paste("USHAMART" , ".NS", sep = ""),
from ="2000-01-01",
periodicity = "weekly" ,
return.class = 'zoo',
env = NULL
)
)
Not sure if this is occurring for other symbols but I doubt it may.
Crapy data on yahoo finance. What a surprise. You get "free" data and this is an example of errors that can occur. Looks like you need to divide the numbers that are in the thousands by 100 to get the "correct" data.
See the data disclaimers on Yahoo:
All data provided on Yahoo Finance is provided for informational
purposes only, and is not intended for trading or investing purposes.
Related
I have a script to download data from yahoo-finance into R. It works well for every stock, but has a hard time with indexes. I am trying to run the index TNX, but It only gives me data from 5 consecutive days.
I've tried putting a "^" before the index, because that is what yahoo finance uses as a variable for indexes, and it doesn't work.
ticker <- "TNX"
start.date <- as.Date('2016-09-01')
getSymbols(ticker, src='yahoo', from=start.date)
Adj.Close <- get(ticker)[,6]
daily.returns <- ROC(Adj.Close, n=1, type='continuous')
When I put this in I get no errors, but when I view daily. returns I get this:
2019-04-22 NA
2019-04-23 -0.03306086
2019-04-24 0.00000000
2019-04-25 -0.03419136
2019-04-26 0.00000000
That's all. Of course, this code works very well on any other stocks, but I just can't figure out this one.
Thank you for your time, and even if you can't help, your desire to help is appreciated.
You're getting all the data that Yahoo has:
https://finance.yahoo.com/quote/TNX/history
You are using a ticker symbol that has been delisted that Yahoo hasn't completely unplugged.
I'm using IBM Data Science Experience (DSX), https://datascience.ibm.com/. I use R with RStudio.
What's the lowest level of data available (say, seconds or minutes or hourly, ...)?
Looking for an example code to access lowest level of data say for the period 1 January 2016 to 31 November 2017 for a certain location.
As per API documentation historical data only gives last 24 hours of data
https://twcservice.mybluemix.net/rest-api/#!/Historical_Data/v1geotimeseriesobs
Anyhow here is the implementation for R and Rstudio to get data for last 23 hours in a dataframe for latitude and longitude(33.40/-83.42):-
library(jsonlite)
username <- "<PUT-YOUR-WEATHERDATA-USERNAME>"
password <- "<PUT-YOUR-WEATHERDATA-PASSWORD>"
base <- "https://twcservice.mybluemix.net/api/weather/v1/geocode/33.40/-83.42/observations/timeseries.json?hours=23"
library(httr)
get_data <- GET(base, authenticate(username,password, type = "basic"))
get_data
get_data_text <- content(get_data, "text")
get_data_text
get_data_json <- fromJSON(get_data_text,flatten = TRUE)
get_data_json
get_data_df <- as.data.frame(get_data_json)
View(get_data_df)
I'm learning to use R's capability in technical trading with Technical Trading Rules (TTR) package. Assume a crypto portfolio and BTC its reference currency. Historical hourly data (60 period) is collected using cryptocompare.com API and converted to zoo object. The aim is to create a 14-period RSI for each crypto (and possibly visualize all in one canvas). For each crypto, I expect RSI output to be 14 NA followed by 46 calculated values. But I'm getting 360 outputs. What am I missing here?
require(jsonlite)
require(dplyr)
require(TTR)
portfolio <- c("ETH", "XMR", "IOT")
for(i in 1:length(portfolio)) {
hour_data <- fromJSON(paste0("https://min-api.cryptocompare.com/data/histohour?fsym=", portfolio[i], "&tsym=BTC&limit=60", collapse = ""))
read.zoo(hour_data$Data) %>%
RSI(n = 14) %>%
print()
}
Also, my time series data is in the following form (first column timestamp):
close high low open volumefrom volumeto
1506031200 261.20 264.97 259.78 262.74 4427.84 1162501.8
1506034800 258.80 261.20 255.68 261.20 2841.67 735725.4
Does TTR use more conventional OHLC (open, high, low, close) order?
The RSI() function expects a univariate price series. You passed it an object with 6 columns, so it converted that to a univariate vector. You need to subset the output of read.zoo() so that only the "close" column is passed to RSI().
Is there way I can extract the data for last 10 historic periods?
As of now I am getting 5 historic quarterly reports and 4 historic Annual reports via this code.
Please help me.
library(quantmod)
getFin('AAPL') # returns AAPL.f to "env"
viewFin(AAPL.f, "IS", "Q") # Quarterly Income Statement
viewFin(AAPL.f, "CF", "A") # Annual Cash Flows
BTW, if you want 10 years worth of metrics and ratios, you can use this methodology.
read.csv("http://financials.morningstar.com/ajax/exportKR2CSV.html?&t=AAPL",header=T,stringsAsFactors = F,skip = 2)[,-c(12)]->spreadsheet
#str(spreadsheet)
View(spreadsheet)
You can use this, but I think the limitation is 4 years of historical data.
require(quantmod)
setwd("C:/Users/your_path_here/Desktop")
stocks <- c("AAVL",
"ACAD",
"ACHN")
# equityList <- read.csv("EquityList.csv", header = FALSE, stringsAsFactors = FALSE)
# names(equityList) <- c ("Ticker")
for (i in 1 : length(stocks)) {
temp<-getFinancials(stocks[i],src="google",auto.assign=FALSE)
write.csv(temp$IS$A,paste(stocks[i],"_Income_Statement(Annual).csv",sep=""))
write.csv(temp$BS$A,paste(stocks[i],"_Balance_Sheet(Annual).csv",sep=""))
write.csv(temp$CF$A,paste(stocks[i],"_Cash_Flow(Annual).csv",sep=""))
}
I have a column of 84 monthly expenditures from 1/2004 - 12/2010, which in Excel looks like...
12247815.55
11812697.14
13741176.13
21372260.37
27412419.28
42447077.96
55563235.3
45130678.8
54579583.53
43406197.32
34318334.64
25321371.4
...(74 more entries)
I am trying to run an stl() from the forecast package on this series, and so I load the data:
d <- ts(read.csv("deseason_vVectForTS.csv",
header = TRUE),
start=c(2004,1),
end=c(2010,12),
frequency = 12)
(If I do header=FALSE it will absorb the first entry - 122...- as the header for the second column, and name the first column's header 'X')
But instead of my environment being populated with a Time Series Object from 2004 to 2011 (as it has said before) it simply says ts[1:84, 1].
Probably related is the fact that,
fit <- stl(d)
throws
Error in stl(d) : only univariate series are allowed.
despite the fact that
head(d)
[1] 12247816 11812697 13741176 21372260 27412419 42447078
and
d
Jan Feb Mar Apr May Jun Jul Aug Sep Oct
2004 12247816 11812697 13741176 21372260 27412419 42447078 55563235 45130679 54579584 43406197
("years 2005-2010 look exactly the same, and all rows have columns for Jan-Dec; it just doesn't fit on here neatly - just trying to show the object has taken the ts labeling structure.")
What am I doing wrong? As far as I know this is the same way I have been building my time series objects in the past...
read.csv reads in a matrix. If it only has one column, it is still a matrix. To make it a vector use
d <- ts(read.csv("deseason_vVectForTS.csv",
header = TRUE)[,1],
start=c(2004,1),
end=c(2010,12),
frequency = 12)
Also, please check your facts. stl is in the stats package, not the forecast package. This is easily checked by using help(stl).