Attempting to extract stock prices using python - python

I am currently trying to extract stock prices from a list of stock codes contained on a csv file by using pandas and yfinance.
I have 145 companies I need to do this for, is there a way of doing it? As I have tried over a period of 5 days without success.
I just need to know if its possible and what would you recommend to achieve this.

yfinance.Ticker(ticker).history(start=start_date) gets you the data that you desire.
if you have a giant csv, with a field "ticker", you can create a pandas dataframe with the below:
import pandas as pd
import yfinance
def read_create_giant_df(file_in):
df = pd.read_csv(file_in)
out = []
for item in df["ticker"]:
ticker_df = yfinance.Ticker(item).history(start="1930-01-01")
ticker_df["ticker"] = item
out.append(ticker_df)
return pd.concat(out)

below code should work , if any module missing use command to install it .
pip install yfinance
pip install yahoofinancials
Run below code to get the data for Amazon-AMZN
import pandas as pd
import yfinance as yf
from yahoofinancials import YahooFinancials
amzn_df = yf.download('AMZN',
start='2019-01-01',
end='2019-12-31',
progress=False)
amzn_df.head()

Related

PyCharm not showing dataframe when I press ctrl+alt+f10

I'm trying to backtest a simple strategy of mine and the first step is to retrieve historical data from yfinance. However, whenver I run this, I can't see the contents of hist. Instead, it just has this outputthis output
# import all the libraries
import nsetools as ns
import pandas as pd
import numpy
from datetime import datetime
import yfinance as yf
import matplotlib.pyplot as plot
plot.style.use('classic')
a = input("Enter the ticker name you wish to apply strategy to")
ticker = yf.Ticker(a)
hist = ticker.history(period="1mo", interval="5m")
hist
I really just want to see the historical prices against the time but can't get the dataframe to appear. I would appreciate any input on this.

problem with 1m interval in get_data_yahoo(pandas_datareader)

I wrote
from pandas_datareader import data as pdr
import yfinance as yf
data = pdr.get_data_yahoo("AAPL", start='2020-04-12', end='2020-04-13', interval="m")
And I got a error
KeyError 'Date'
I heard people saying Yahoo finance API is down but I still can get the following code working correctly
data = pdr.get_data_yahoo("AAPL", start='2020-04-12', end='2020-04-13')
pandas_datareader interval="m" means 1 month you need yfinance.
pip download yfinance
import yfinance
a = yfinance.download(ticker="your symbol",period="5d",interval="1m")

Scanning Finviz Stocks

Hello here is a screener tool for Finviz but my stock_list result returns object of type Screener I am trying to put that into a data frame but I am having issues as the data is one long string divided by pipes. I tried to use str but method does not exists in the screener class. I am new to python this looks easy but I just dont know the proper syntax here can anyone help. Thank you!
import pandas as pd
import nest_asyncio
from finviz.screener import Screener
import csv
import sys
from datetime import datetime
nest_asyncio.apply()
filters = ['idx_sp500'] # Shows companies in NASDAQ which are in the S&P500
stock_list = Screener(filters=filters, order='price')
You could output the data to a csv file and read the file using pandas:
stock_list = Screener(filters=filters, order='price')
stock_list.to_csv(filename="stocks.csv")
df = pd.read_csv("stocks.csv")

Yahoo Financial Stock info data loading into Pandas

I would like to load stock info data into pandas. Code
import yfinance as yf
abb = yf.Ticker("ABB.NS")
# get stock info
abb.info
How do i load abb.info data into pandas. Please throw some lights on this?
If you only want to convert the dict to pandas dataframe you can do it as follows:
pd.DataFrame.from_dict(abb.info, orient='index')
Generally I would recommend you to use the pandas_datareader pibrary for this task.
check the following code taken from [1].
pandas dataframe available at [2]
from pandas_datareader import data as pdr
import yfinance as yf
yf.pdr_override() # <== that's all it takes :-)
# download dataframe
data = pdr.get_data_yahoo("SPY", start="2017-01-01", end="2017-04-30")
[1] https://github.com/ranaroussi/yfinance
[2] https://github.com/pydata/pandas-datareader

Pandas DataFrame not working as intended

I am new to Python and I'm trying to use it for finance, specifically plotting stock prices. I am using pandas and its DataFrame object, but for some reason I cannot obtain the data I need. The web.DataReader method works, as I tried it in another program, but my code does not. Here is my code:
import numpy as np
import pandas as pd
import pandas.io.data as web
symbols = ['AAPL', 'MSFT', 'GLD']
data=pd.DataFrame()
for sym in symbols:
data[sym] = web.DataReader(sym, data_source='yahoo', start='4/14/2014',end='01/30/2015')['Adj Close']
data.columns=symbols
print(data['AAPL'])
The output is an empty dataframe and I am not sure why, because DataReader does work as I tried it elsewhere.
An update of pandas to version >=0.17.1 should solve your problem. If you use conda (recommended) :
conda update pandas
will do.
After the update you will get a deprecation warning.
To avoid this install pandas-datareader:
conda install pandas-datareader
and change:
import pandas.io.data as web
into:
from pandas_datareader import data as web

Categories

Resources