# Download stock price time series from alphavantage # Save the series in .csv files, one for each ticker # For the class # https://www.math.nyu.edu/faculty/goodman/teaching/MathFin2019/MathFinance.html # For use with Assignment 7, posted there APIKey = " " # get this from https://www.alphavantage.co/documentation/ # Click on "Claim your free API key" # Then copy/paste to inside the quotes URLbase = 'https://www.alphavantage.co/query?' func = 'TIME_SERIES_DAILY' # "function" is a reserved word # the "tickers" for asset price time series you want to get. tickerList = c( "T", # ATT (phone company) "F", # Ford (car company) "IBM", # IBM (computers, mostly software) "MSFT", # Microsoft (software) "BA", # Boeing (planes, civilian and military) "GM", # General Motors (cars) "AAPL", # Apple (computers, phones, software) "GE", # General Electric (jet engines and other things) "AMD", # AMD (chips) "JPM", # J P Morgan Chase (bank) "NVDA", # Nvidia (chips) "SPY") # S & P ETF tickerCount = 0 # how many tickers you've received so far. SeventySeconds = 70 # how long to wait every five requests for ( ticker in tickerList){ tickerCount = tickerCount + 1 # Get only five per minute if ( ( tickerCount > 1 ) & ( (tickerCount %% 5) == 1 ) ){ cat("sleeping\n") flush.console() # get the message without waiting Sys.sleep(SeventySeconds) # Wait (sleep) a minute } URL = paste( c( URLbase, # create the request ... "function=", func, # ... see documentation for the format. "&symbol=", ticker, "&apikey=", APIKey, "&outputsize=full", # get the whole series, not just 100 days "&datatype=csv"), collapse = "") cat("Fetching with url ") cat(URL) cat("\n") timeSeriesCSVfile = read.csv(URL) # send the request cat("Got ticker ") cat(ticker) cat("\n") # # write the time series to a .csv file csvFileName = paste( c("StockPriceTimeSeries_", ticker, ".csv"), collapse = "") write.csv( timeSeriesCSVfile, file = csvFileName) }