Can't access tickers with dots using yfinance - python

I cannot access tickers of football clubs that include dots using yfinance (specifically the Tickers function). For example, when looking for BVB.DE, it says "No timezone found, symbol may be delisted". How should I fix this?
Please see my code below. I have seen some answers claiming that removing the dot or including a dash instead should work, but it does not.
import yfinance as yf
import pandas as pd
etiquetas = yf.Tickers('MANU JVTSF GSRAY-IS')
ticker_club1 = 'MANU'
ticker_club2 = 'JVTSF'
ticker_club3 = 'GSRAY-IS'
hist = etiquetas.history(start=fecha_entrada, end=fecha_salida)
precios = hist['Open']
pd.DataFrame.tail(precios)
pd.set_option('display.max_rows', 20)
print(precios)

Related

Received 'ValueError: If using all scalar values, you must pass an index' error while scraping Yahoo Finance data for 10,000+ tickers

Running this code works great with less than a thousand tickers or symbols.
import pandas as pd
import yfinance as yf
import datetime
import time
from yahooquery import Ticker
symbols = ['AAPL','GOOG','MSFT']
faang = Ticker(symbols)
faang.valuation_measures
df = pd.DataFrame(faang.valuation_measures)
df.to_excel('output.xlsx')
But if I put 10,000 or more tickers, I keep getting this error:
ValueError: If using all scalar values, you must pass an index

What is the fastest way to iterate through a list of yfinance tickers?

Im using the python yfinance yahoo API for stock data retrieval. Right now im getting the peg ratio, which is an indicator of a company price related to its growth and earnings. I have a csv downloaded from here: https://www.nasdaq.com/market-activity/stocks/screener.
It has exactly 8000 stocks.
What I do is get the symbol list, and iterate it to access to the yahoo ticker. Then I get a use the ticker.info method which returns a dictionary. I iterate this process through the 8000 symbols. It goes at a speed of 6 symbols per minute, which is not viable. Is there a faster way with another API or another structure? I dont care about the API as long as I can get basic info as the growth, earnings, EPS and those things.
Here is the code:
import pandas as pd
import yfinance as yf
data = pd.read_csv("data/stock_list.csv")
symbols = data['Symbol']
for symbol in symbols:
stock = yf.Ticker(symbol)
try:
if stock.info['pegRatio']:
print(stock.info['shortName'] + " : " + str(stock.info['pegRatio']))
except KeyError:
pass
It seems that when certain data are needed from the Ticker.info attribute, HTTP requests are made to acquire them. Multithreading will help to improve matters. Try this:-
import pandas as pd
import yfinance as yf
import concurrent.futures
data = pd.read_csv('data/stock_list.csv')
def getPR(symbol):
sn = None
pr = None
try:
stock = yf.Ticker(symbol)
pr = stock.info['pegRatio']
sn = stock.info['shortName']
except Exception:
pass
return (sn, pr)
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = {executor.submit(getPR, sym): sym for sym in data['Symbol']}
for future in concurrent.futures.as_completed(futures):
sn, pr = future.result()
if sn:
print(f'{sn} : {pr}')

Real-time data from yfinance

I am trying to get real rime data from yfinance with following code.
But I just get the same close/high/low/open results.
I am starting to think that it is impossible with yfinance. Is there any free and european alternatives?
Thanks a lot in advance!!
#import libraries
import schedule
import time
import alpaca_trade_api as tradeapi
import yfinance as yf
import pandas as pd
# Ask what stocks you want to check
pd = pd.DataFrame()
n = int(input("Enter the size of the list "))
print("\n")
numList = list(num for num in input("Enter the list numbers separated by space ").strip().split())[:n]
print("User List: ", numList)
# Get info for every stock chosen.
def get_data():
for ticker in numList:
ticker_yahoo = yf.Ticker(ticker)
data = ticker_yahoo.history(period = "1d", interval="1m")
data = (data.tail(1).iloc[0])
pd[ticker] = data
print(pd)
get_data()
It is faster to do it with Pandas Datareader by using get_quote_yahoo() method
import pandas_datareader as web
tickers = ["MSFT", "XOM", "KKR"]
current_price = web.get_quote_yahoo(tickers)["regularMarketPrice"]
If you insist on using yfinance, you can use the following code, but beware that it will be MUCH SLOWER This is because 1) instantiating Ticker object and pulling the info property takes time, and 2) it is inconvenient to use Tickers class (as opposed to Ticker), so you have to use a for a loop.
import yfinance as yf
tickers = ["MSFT", "XOM", "KKR"]
current_price = list()
for i in range(len(tickers)):
company = yf.Ticker(ticker[i])
current_price[i] = company.info["regularMarketPrice"]
Obviously, these solutions apply to other types of securities as well. Note though that using this (or any other free) data source for algorithmic trading with real money is impossible at best due to requests limit, high latency, stability issues etc.

Convert columns in dataframe with comas into numeric data to plotting

I'm new in the world of plotting in Python I started learning today doing a mini project by my own, I tried to scrape data and represent here's my code:
import requests
import pandas as pd
from pandas import DataFrame
import numpy as np
import bs4
from bs4 import BeautifulSoup
import matplotlib.pyplot as plot
# Getting the HTML page
URL = "https://www.worldometers.info/coronavirus/#countries"
pag_html = requests.get(URL).text
# Extracting data with BeautifulSoup.
soup = BeautifulSoup(pag_html, 'html.parser')
tabla = soup.find("table", id="main_table_countries_today")
datos_tabla = tabla.tbody.find_all("tr")
Lista = []
for x in range(len(datos_tabla)):
values = [j.string for j in datos_tabla[x].find_all('td')]
Lista.append(values)
df = pd.DataFrame(Lista).iloc[7: , 1:9]
nombre_columna = ["Pais", "Casos totales", "Nuevos Casos", "Muertes totales", "Nuevas Muertes", "Total Recuperados", "Nuevos Recuperados", "Activos"]
df.columns = nombre_columna
df.plot(x="Pais", y="Casos totales", kind ="barh")
plot.show()
The error it's giving me is: "TypeError: no numeric data to plot" I understand that this error is because the column "Casos totales" is a string not a float.
I tried to convert the columns of my Dataframe into floats, but there's no way I got error from everywhere.
Does anyone have any idea how can I represent my DataFrame?
Thanks.
After running the script, as you say the column "Casos Totales" is being interpreted as string due to the commas in the values. You can change this using .str.replace(',','') and then .astype(float), right after renaming the column names in your dataframe:
df['Casos totales'] = df['Casos totales'].str.replace(',','').astype(float)
df.plot(x="Pais", y="Casos totales", kind ="barh")
plot.show()
And this plots the graph (although the visualization is quite poor, but that's another story)

Pandas: 52 week high from yahoo or google finance

Does anyone know if you can get the 52 week high in pandas from either yahoo or google finance? Thanks.
It is possible, please check out pandas documentation. Here's an example:
import pandas.io.data as web
import datetime
symbol = 'aapl'
end = datetime.datetime.now()
start = end - datetime.timedelta(weeks=52)
df = web.DataReader(symbol, 'yahoo', start, end)
highest_high = df['High'].max()
One can also use yfinance(from yahoo)
pip install finance
import yfinance as yf
stock = "JNJ"
dataframe = yf.download(stock, period="1y", auto_adjust=True, prepost=True, threads=True)
max = dataframe['High'].max()
You could also use other libraries such as yahoo_fin. This one works better sometimes, it would depend on what you want to do, but it's good to bear in mind other possibilities : )
import yfinance as yf
import yahoo_fin.stock_info as si
stock = 'AAPL'
df = yf.download(stock, period="1y")
print("$",round(df['High'].max(), 2))
df2 = si.get_data(stock, interval="1mo")
print("$",round(df2['high'].tail(12).max(), 2))
Output:
$ 182.94
$ 182.94
You can use the info keyword to return lots of aggregated data like P/E Ratio, 52-Week High,etc.
import yfinance as yf
data = yf.Ticker(ticker).info
print(data.fiftyTwoWeekHigh)

Categories

Resources