SimpleStockData/SimpleStockData.py

96 lines
3.7 KiB
Python
Raw Normal View History

import yfinance as yf
import pandas as pd
class SimpleStockData:
def __init__(self, ticker_list: list, period_start: str, period_end: str, to_currency: str = ""):
"""
:param period_start:
start date (format YYYY-MM-DD)
:param period_end:
end date (format YYYY-MM-DD)
:param ticker_list:
list that contains all stocks/exchange rates (yfinance considers both as "Tickers")
:param to_currency:
currency to convert rates to
"""
self.ticker_list = ticker_list
self.to_currency = to_currency
self._from_currency_list = []
self._period_start = period_start
self._period_end = period_end
def _get_history(self, idx, interval="1d"):
"""
Function for internal use; Just a wrapper around the .history method of the yfinance Ticker class
:param idx:
the index of the share
:param interval:
granularity of data - valid values are 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo
:return: pandas.DataFrame
"""
return yf.Ticker(self.ticker_list[idx]).history(interval=interval, start=self._period_start,
end=self._period_end)
def _generate_exchange_dataframe(self, add_currency=""):
"""
The class has two separate attributes, one to store the plain convert list
(_from_currency_list), and one containing the real mapping needed to convert.
The mapping is recreated by this function following the information in the
_from_currency_list.
:param add_currency:
OPTIONAL. Add a given currency directly to the internal list before regenerating
the mapping (dataframe). Format: CCC (to_currency)
return:
boolean
"""
# just add the currency if add_currency is given
if add_currency != "":
if len(add_currency) != 3:
raise ValueError(
f"The given currency \"{add_currency}\" to add before regenerating is not in the right format.")
self._from_currency_list.append(f"{add_currency}{self.to_currency}=X")
# now the real process begins
tickers = yf.Tickers(
' '.join(self._from_currency_list)) # create a new Ticker instance with all wanted currencies
exchange_rates = []
for i in tickers.tickers: # get all the history of each currency conversion factors
exchange_rates.append(tickers.tickers[i].history(start=self._period_start, end=self._period_end))
self.exchange_df = pd.DataFrame(
exchange_rates).T # transpose the dataframe (imagine just switching rows and columns)
self.exchange_df.columns = self._from_currency_list # set the right names for the columns in the dataframe
return True
def get_info(self, idx, key=""):
"""
:param idx:
the index of the share
:param key:
OPTIONAL. gives which specific datum is wanted
:return:
"""
info = yf.Ticker(self.ticker_list[idx]).info
if key != "": # if just one specific information is wanted
return info[key]
return info
def get_history(self, idx, interval="1d"):
"""
Just a wrapper around the .history method of the yfinance Ticker class
:param idx:
the index of the share
:param interval:
granularity of data - valid values are 1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo
:return: pandas.DataFrame
"""
ret = self._get_history(idx, interval)
return ret