2

Wondering if someone has some R capability to take a company name and output it's exchange and ticker symbol. For instance, could take a character vector input:

company <- c("Google", "General Motors Company", "singtei")

and return

stockinfo <- ("NASDAQ: GOOGL", "NYSE: GM", "SGX: Z74")

There may not be anything this straightforward (with a package like ggmap doing the heavy lifting), but as an example of a similar capability, this code returns geographic coordinates given city names:

# Cities needing geocodes
cities <- c("Phoenix", "Los Angeles", "Portland")
# Geocode function
library(ggmap)
coord <- geocode(cities)
# Geographic coordinates
coord  

Output:

        lon      lat
1 -112.0740 33.44838
2 -118.2437 34.05223
3 -122.6765 45.52306
> 
dmb
  • 567
  • 5
  • 17
  • `library(TTR)` has `stockSymbols(exchange="NASDAQ")` that provides the ticker information. Reference: https://stackoverflow.com/a/25635136/8382207 – Sagar Sep 12 '17 at 17:43
  • Hello Sagar. That is great -- thank you very much. One subtle detail though -- from testing just now the TTR package is limited to "AMEX", "NASDAQ", and "NYSE" exchanges. Not sure if there may be a way to incorporate other international exchanges as well? – dmb Sep 12 '17 at 18:02
  • I realized this does not include international exchanges, trying to find more but no luck yet. Please see my answer below for the current symbols. – Sagar Sep 12 '17 at 18:11

1 Answers1

2
> company <- "Microsoft"
> symbolData <- stockSymbols(exchange = c("AMEX", "NASDAQ", "NYSE"))
Fetching AMEX symbols...
Fetching NASDAQ symbols...
Fetching NYSE symbols...

> exc <- symbolData[agrep(company, symbolData[,2]), 8]
> sym <- symbolData[agrep(company, symbolData[,2]), 1]
> STK <- paste(exc,":",sym, sep = "")

> STK
[1] "NASDAQ:MSFT"
Sagar
  • 2,778
  • 1
  • 8
  • 16
  • Hello Sagar. Your code works when using one company, but produces this error with a vector of companies: "argument 'pattern' has length > 1 and only the first element will be used". Perhaps you can take a quick look? – dmb Sep 13 '17 at 18:10
  • If you loop in, it will work for each element of a data object. – Sagar Sep 13 '17 at 18:18