Function to export multiple yfinance stocks to csv - python

I'm trying to define a function to allow me to extract information on stocks over the past 12 months and export it to a CSV file. I'm not sure where it's going wrong as it prints 'bad'. Any thoughts?
Thanks.
import pandas as py
import numpy as np
import yfinance as yf
import datetime as dt
from pandas_datareader import data as pdr
from yahoofinancials import YahooFinancials
yf.pdr_override()
now_time=dt.datetime.now()
start_time = dt.datetime(now_time.year - 1, now_time.month , now_time.day)
bad_names=[]
def download_stock(stock):
try:
print(stock)
stock_df = pdr.get_yahoo_data(stock, start_time, now_time)
stock_df['Name'] = stock
output_name = stock + '_data.csv'
stock_df.to_csv("./stocks/"+output_name)
except:
bad_names.append(stock)
print('bad: %s' % (stock))
download_stock('AAPL')

A try - except block will handle any exception and simply execute what follows after except.
You could try running the code without the try-except block and see what the error is.
Alternatively, you could use
except Exception as e:
print(e)
So you can know what is going wrong exactly. Looking at it now, I would guess that you are missing one dot in the filepath "../stocks/"+output_name

Related

Python error "Unable to read URL" even though "Requirement already satisfied" in command prompt

I've been trying to work on some beginner/entry level python projects for a resume (working with WIN10) and I've been running into the same constant error. I've been trying to pull info from yahoo finance and I get this error, no matter how I rework my code:
import pandas_datareader as web
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
start = dt.datetime(2018,1,1)
end = dt.datetime.now()
tickers = ["FB", "GS", "NVDA", "MSFT", "TSLA", "AAPL", "CCL", "BA"]
colnames = []
for ticker in tickers:
data = web.DataReader(ticker, "yahoo", start, end)
if len(colnames) == 0:
combined = data[['Adj Close']].copy()
else:
combined = combined.join(data['Adj Close'])
colnames.append(ticker)
combined.columns = colnames
print(combined)
RemoteDataError: Unable to read URL: https://finance.yahoo.com/quote/FB/history?period1=1514779200&period2=1652673599&interval=1d&frequency=1d&filter=history
I looked around and saw that yahoo finance changed it's structure for python, so I then tried changing the format or my code, still got the error. I tried installing suggested libraries to fix it, and while it says that their installed, I can't use the libraries because it says that module isn't found.
I've looked at other Stack posts and none of the solutions are helping.

Accessing nested dictionary from a JSON, with variable headers

I am trying to use json_normalize to parse data from the yahoo financials package. Seem to be running into an issue when trying to separate the columns out from the last object, a variable date. Each date I believe is a dictionary, which contains various balance sheet line items.
My code is:
import json
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import yfinance as yf
from yahoofinancials import YahooFinancials
tickerinput = "AAPL"
ticker = yf.Ticker(tickerinput)
tickerfin = YahooFinancials(tickerinput)
balancesheet = tickerfin.get_financial_stmts('annual', 'balance')
''' Flattening w json_normalize'''
balsheet = pd.json_normalize(balancesheet, record_path=['balanceSheetHistory', tickerinput])
I have also tried using this below code but receive a key error, despite it being in the original JSON output.
balsheet = pd.json_normalize(balancesheet, record_path=['balanceSheetHistory', tickerinput], meta=['2021-09-25', ['totalLiab','totalStockholderEquity','totalAssets','commonStock','otherCurrentAssets','retainedEarnings','otherLiab','treasuryStock','otherAssets','cash','totalCurrentLiabilities','shortLongTermDebt','otherStockholderEquity','propertyPlantEquipment','totalCurrentAssets','longTermInvestments','netTangibleAssets','shortTermInvestments','netReceivables','longTermDebt','inventory','accountsPayable']], errors='ignore')
The main issue is that I am returned the below data frame:
Returned dataframe from balsheet
Sample Output of the JSON file:
JSON Output (balancesheet variable)

How to get IEX cloud API Key and make it available to my python code?

# Import DataReader
from pandas_datareader.data import DataReader
# Import date
from datetime import date
# Set start and end dates
start = date(2016,1,1)
end = date(2016,12,31)
# Set the ticker
ticker = "AAPL"
# Set the data source
data_source = "iex"
# Import the stock prices
stock_prices = DataReader(ticker, data_source, start, end)
# Display and inspect the result
print(stock_prices.head())
stock_prices.info()
I can import DataReader and use 'iex' in a virtual environment easily but when I'm trying to do the same in my python interpreter I got a ValueError Exception also it asks for the IEX cloud API key. I haven't heard the term before even from my instructor. Please explain me what it is, also how to implement so that I can get rid of this error.
Error I have got!

Where am I going wrong retrieving stock data from Quandl?

ValueError: The Quandl API key must be provided either through the api_key variable or through the environmental variable QUANDL_API_KEY.
I am trying to retrieve some simple stock data from Quandl. I have put in the actual API key instead of the x in the below example code below but I am still getting errors. Am I missing out on something?
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
style.use('ggplot')
symbol = 'AAPL'
api_key = 'x'
start = dt.datetime(2015, 1, 1)
end = dt.datetime.now()
df = web.DataReader(symbol, 'quandl', start, end, api_key)
print(df.head())
From the quandl docs:
AUTHENTICATION The Quandl Python module is free but you must have a
Quandl API key in order to download data. To get your own API key, you
will need to create a free Quandl account and set your API key.
After importing the Quandl module, you can set your API key with the
following command: quandl.ApiConfig.api_key = "YOURAPIKEY"
So you will need to pip install and import quandl. Then you can set the api_key attribute as above.
If you only want to get the data from Quandl, maybe you can try another approach.
import pandas as pd
import Quandl
api_key = 'yoursuperamazingquandlAPIkey'
df = Quandl.get('heregoesthequandlcode', authtoken = api_key)
print(df.head())

Python retrieving data from web HTTP 400: Bad Request Error (Too many Requests?)

I am using a python module (googlefinance) to retrieve stock information. In my code, I create a symbols list which then gets sent into a loop to collect the information for each symbol.
The symbols list contains about 3000 indexes which is why I think I am getting this error. When I try shortening the range of the loop (24 requests), it works fine. I have tried also tried using a time delay in between requests but no luck. How can I make it so that I can retrieve the information for all specified symbols without getting the HTTP 400 Error?
from googlefinance import getQuotes
import pandas as pd
import pymysql
import time
import threading
import urllib.request
def createSymbolList(csvFile):
df = pd.read_csv(csvFile)
saved_column = df['Symbol']
return saved_column
def getSymbolInfo(symbolList):
newList=[]
for i in range(int(24)):
newList.append(getQuotes(symbolList[i]))
return newList
nyseList = createSymbolList("http://www.nasdaq.com/screening/companies-by-name.aspx?letter=0&exchange=nyse&render=download")
try:
l=(getSymbolInfo(nyseList))
print(l)
print(len(l))
except urllib.error.HTTPError as err:
print(err)

Categories

Resources