I have to import some datas from Yahoo! finance to R. For example I have to import in R datas about Roche Holding (AG) (Roche Yahoo! link) and The Goldman Sachs Group, Inc. (GS) (Goldman Yahoo! link).
My problem is that Roche Holding (AG) datas are in euros and datas about The Goldman Sachs Group, Inc. (GS) are in dollars.
Now to import my datas in R I'm using:
Goldman_Sachs.z = get.hist.quote(instrument="GS", start=date.start,
end=date.end, quote="AdjClose",origin="1970-01-01",
provider="yahoo",compression = "m", retclass="zoo")
Is possible to import these datas just in dollars or must I implement a function in R to make this job?
And in the second case how can I choose the exchange rate?
Although Roche Holding is also traded on NASDAQ OTC and you might therefore be able to obtain the current share price in USD, the proper way to handle such situations is to retrieve the data from the main market (which is the Swiss Stock Exchange in Zurich in this case) and calculate the value in dollars using the current exchange rate. The problem with OTC values is their low trading volume which may result in inaccurate prices.
To obtain the exchange rate of the CHF/USD Forex pair you can use the quantmod package:
library(quantmod)
getFX("CHF/USD")
tail(CHFUSD,1)
# CHF.USD
#2016-04-16 1.0332
The price in EUR does not seem to be a suitable choice in this case, but since you mentioned that you are looking at a market where Roche Holding is traded in EUR you may use in the same way getFX("EUR/USD").
To download the EOD data from the Swiss Stock Exchange I would recommend using
Roche_CH <- getSymbols("SWX:RO", src ="google", auto.assign = FALSE)
or
Roche_CH <- getSymbols("RO.SW", src = "yahoo", auto.assign = FALSE)
Hope this helps.
Related
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.
I'm trying to obtain a couple of weeks worth of historical train performance (delay) data on trains arriving at Central Station (id=600016...600024), Brisbane, Australia for research purposes. I found a package called gtfsway developed by SymbolixAU, and some brief code, but I don't know how to specify a date and the station id.
I'm new to GTFS and any help is appreciated.
library(gtfsway)
url <- "https://gtfsrt.api.translink.com.au/Feed/SEQ"
response <- httr::GET(url)
FeedMessage <- gtfs_realtime(response)
## the function gtfs_tripUpdates() extracts the 'trip_update' feed
lst <- gtfs_tripUpdates(FeedMessage)
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 have a text file (0001.txt) which contains the data as below:
<DOC>
<DOCNO>1100101_business_story_11931012.utf8</DOCNO>
<TEXT>
The Telegraph - Calcutta (Kolkata) | Business | Local firms go global
6 Local firms go global
JAYANTA ROY CHOWDHURY
New Delhi, Dec. 31: Indian companies are stepping out of their homes to try their luck on foreign shores.
Corporate India invested $2.7 billion abroad in the first quarter of 2009-2010 on top of $15.9 billion in 2008-09.
Though the first-quarter investment was 15 per cent lower than what was invested in the same period last year, merchant banker Sudipto Bose said, It marks a confidence in a new world order where Indian businesses see themselves as equal to global players.
According to analysts, confidence in global recovery, cheap corporate buys abroad and easier rules governing investment overseas had spurred flow of capital and could see total investment abroad top $12 billion this year and rise to $18-20 billion next fiscal.
For example, Titagarh Wagons plans to expand abroad on the back of the proposed Asian railroad project.
We plan to travel all around the world with the growth of the railroads, said Umesh Chowdhury of Titagarh Wagons.
India is full of opportunities, but we are all also looking at picks abroad, said Gautam Mitra, managing director of Indian Structurals Engineering Company.
Mitra plans to open a holding company in Switzerland to take his business in structurals to other Asian and African countries.
Indian companies created 3 lakh jobs in the US, while contributing $105 billion to the US economy between 2004 and 2007, according to commerce ministry statistics. During 2008-09, Singapore, the Netherlands, Cyprus, the UK, the US and Mauritius together accounted for 81 per cent of the total outward investment.
Bose said, And not all of it is organic growth. Much of our investment abroad reflects takeovers and acquisitions.
In the last two years, Suzlon acquired Portugals Martifers stake in German REpower Systems for $122 million. McNally Bharat Engineering has bought the coal and minerals processing business of KHD Humboldt Wedag. ONGC bought out Imperial Energy for $2 billion.
Indias foreign assets and liabilities today add up to more than 60 per cent of its gross domestic product. By the end of 2008-09, total foreign investment was $67 billion, more than double of that at the end of March 2007.
</TEXT>
</DOC>
Above, all text data is within the HTML code for text i.e.
<TEXT> and </TEXT>.
I want to read it into an R dataframe in a way that there will be four columns and the data should be read as:
Title Author Date Text
The Telegraph - Calcutta (Kolkata) JAYANTA ROY CHOWDHURY Dec. 31 Indian companies are stepping out of their homes to try their luck on foreign shores. Corporate India invested $2.7 billion abroad in the first quarter of 2009-2010 on top of $15.9 billion in 2008-09. Though the first-quarter investment was 15 percent lower than what was invested in the same period last year, merchant banker Sudipto Bose said, It marks a confidence in a new world order where Indian businesses see themselves as equal to global players.
What I was trying to read using dplyr and as shown below:
# read text file
library(dplyr)
library(readr)
dat <- read_csv("0001.txt") %>% slice(-8)
# print part of data frame
head(dat, n=2)
In above code, I tried to skip first few lines (which are not important) from the text file that contains the above text and then read it into dataframe.
But I could not get what I was looking for and got confused what I am doing is wrong.
Could someone please help?
To be able to read data into R as a data frame or table, the data needs to have a consistent structure maintained by separators. One of the most common formats is a file with comma separated values (CSV).
The data you're working with doesn't have separators though. It's essentially a string with minimally enforced structure. Because of this, it sounds like the question is more related to regular expressions (regex) and data mining than it is to reading text files into R. So I'd recommend looking into those two things if you do this task often.
That aside, to do what you're wanting in this example, I'd recommend reading the text file into R as a single string of text first. Then you can parse the data you want using regex. Here's a basic, rough draft of how to do that:
fileName <- "Path/to/your/data/0001.txt"
string <- readChar(fileName, file.info(fileName)$size)
df <- data.frame(
Title=sub("\\s+[|]+(.*)","",string),
Author=gsub("(.*)+?([A-Z]{2,}.*[A-Z]{2,})+(.*)","\\2",string),
Date=gsub("(.*)+([A-Z]{1}[a-z]{2}\\.\\s[0-9]{1,2})+(.*)","\\2",string),
Text=gsub("(.*)+([A-Z]{1}[a-z]{2}\\.\\s[0-9]{1,2})+[: ]+(.*)","\\3",string))
Output:
str(df)
'data.frame': 1 obs. of 4 variables:
$ Title : chr "The Telegraph - Calcutta (Kolkata)"
$ Author: chr "JAYANTA ROY CHOWDHURY"
$ Date : chr "Dec. 31"
$ Text : chr "Indian companies are stepping out of their homes to"| __truncated__
The reason why regex can be useful is that it allows for very specific patterns in strings. The downside is when you're working with strings that keep changing formats. That will likely mean some slight adjustments to the regex used.
read.table( file = ... , sep = "|") will solve your issue.
I am trying to write a small program in R that will randomly select a stock from the NYSE and display that stock's YTD information in a data.frame.
I currently have:
Stock.List<-data.frame(StockListNew)
colnames(Stock.List)<-c("Number", "Ticker", "Company Name")
# Renamed Columns to number, ticker and company name
random.stock<-sample.rows(data.frame(Stock.List),1,replace=TRUE)
Stock.List[sample.int(3284,size=1,replace=TRUE),2]
#Selects a Random Stock Ticker and Name from Stock.List
So far this has worked for me. When I run...
Stock.List[sample.int(3284,size=1,replace=TRUE),2]
I successfully return the random ticket in the console, for example:
> Stock.List[sample.int(3284,size=1,replace=TRUE),2]
[1] WPP
3284 Levels: A AA AA-B AAC AAN AAP AAT AAV AB ABB ABBV ... ZX
I end up getting the second line which I don't understand why R is relisting the Stock.List as levels.
Ultimately my goal is to then have R open quantmod and use the getSymbols() function to grab the random stock I selected and download the data.frame from January 1st, 2015 to Dec 18th, 2015.
I know that I need to get the randomly selected stock into a string(?) to properly use the getSymbols function.
Sorry if my language is not proper I am brand new to R and programming languages.