Now using the new method (renamed to _create_exchange_dataframe).
Also, some optimizations and fixes were added.
This commit is contained in:
parent
ac87844b9b
commit
baf6614491
@ -10,17 +10,19 @@ class SimpleStockData:
|
|||||||
:param period_end:
|
:param period_end:
|
||||||
end date (format YYYY-MM-DD)
|
end date (format YYYY-MM-DD)
|
||||||
:param ticker_list:
|
:param ticker_list:
|
||||||
list that contains all stocks/exchange rates (yfinance considers both as "Tickers")
|
list containing all stocks/exchange rates (yfinance considers both as "Tickers")
|
||||||
:param to_currency:
|
:param to_currency:
|
||||||
currency to convert rates to
|
currency to convert rates to
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.ticker_list = ticker_list
|
self.ticker_list = ticker_list
|
||||||
self.to_currency = to_currency
|
self.to_currency = to_currency
|
||||||
self._from_currency_list = []
|
|
||||||
self._period_start = period_start
|
self._period_start = period_start
|
||||||
self._period_end = period_end
|
self._period_end = period_end
|
||||||
|
|
||||||
|
self._exchange_df = None # Mapping: time mapped to conversion factor, to get the right converted value per date
|
||||||
|
self._create_exchange_dataframe() # initialize self.exchange_df attribute
|
||||||
|
|
||||||
def _get_history(self, idx, interval="1d"):
|
def _get_history(self, idx, interval="1d"):
|
||||||
"""
|
"""
|
||||||
Function for internal use; Just a wrapper around the .history method of the yfinance Ticker class
|
Function for internal use; Just a wrapper around the .history method of the yfinance Ticker class
|
||||||
@ -34,36 +36,42 @@ class SimpleStockData:
|
|||||||
return yf.Ticker(self.ticker_list[idx]).history(interval=interval, start=self._period_start,
|
return yf.Ticker(self.ticker_list[idx]).history(interval=interval, start=self._period_start,
|
||||||
end=self._period_end)
|
end=self._period_end)
|
||||||
|
|
||||||
def _generate_exchange_dataframe(self, add_currency=""):
|
def _create_exchange_dataframe(self):
|
||||||
"""
|
"""
|
||||||
The class has two separate attributes, one to store the plain convert list
|
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.
|
(_from_currency_list), and one containing the real mapping needed to convert.
|
||||||
The mapping is recreated by this function following the information in the
|
The mapping is recreated by this function following the information in the
|
||||||
_from_currency_list.
|
_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:
|
return:
|
||||||
boolean
|
boolean
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# just add the currency if add_currency is given
|
# check if a to_currency is even given
|
||||||
if add_currency != "":
|
if self.to_currency == "":
|
||||||
if len(add_currency) != 3:
|
return False
|
||||||
raise ValueError(
|
|
||||||
f"The given currency \"{add_currency}\" to add before regenerating is not in the right format.")
|
# create the list of currencies based on all the stocks of the class
|
||||||
self._from_currency_list.append(f"{add_currency}{self.to_currency}=X")
|
_from_currency_list = []
|
||||||
|
for i in range(len(self.ticker_list)): # to get all indexes; this adds an entry for each currency
|
||||||
|
add_currency = f"{self.get_info(i, 'currency')}{self.to_currency}=X" # Format: "fffttt=X" f=from, t=to
|
||||||
|
# for the case that FROM and TO are equal, just don't download the data (as conversion factor's 1)
|
||||||
|
if add_currency == f"{self.to_currency}{self.to_currency}=X":
|
||||||
|
pass
|
||||||
|
elif add_currency not in _from_currency_list: # add a new item if not already there
|
||||||
|
_from_currency_list.append(add_currency)
|
||||||
|
|
||||||
|
print(_from_currency_list)
|
||||||
|
|
||||||
# now the real process begins
|
# now the real process begins
|
||||||
tickers = yf.Tickers(
|
tickers = yf.Tickers(" ".join(_from_currency_list)) # create a new Ticker instance with all wanted currencies
|
||||||
' '.join(self._from_currency_list)) # create a new Ticker instance with all wanted currencies
|
|
||||||
exchange_rates = []
|
exchange_rates = []
|
||||||
for i in tickers.tickers: # get all the history of each currency conversion factors
|
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))
|
# for simplicity: using the conversion factor of closing (.Close at the end)
|
||||||
|
exchange_rates.append(tickers.tickers[i].history(start=self._period_start, end=self._period_end).Close)
|
||||||
|
|
||||||
self.exchange_df = pd.DataFrame(
|
self._exchange_df = pd.DataFrame(exchange_rates).T # transpose the dataframe (imagine just switching rows and columns)
|
||||||
exchange_rates).T # transpose the dataframe (imagine just switching rows and columns)
|
self._exchange_df.columns = _from_currency_list # set the right names for the columns in the dataframe
|
||||||
self.exchange_df.columns = self._from_currency_list # set the right names for the columns in the dataframe
|
self._exchange_df[f"{self.to_currency}{self.to_currency}=X"] = 1.0 # for FROM and TO being equal: set factor to 1
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from SimpleStockData import SimpleStockData
|
from SimpleStockData import SimpleStockData
|
||||||
|
|
||||||
ssd = SimpleStockData(["BAS.DE", "AMZN"], "2023-11-24", "2023-11-25")
|
ssd = SimpleStockData(["BAS.DE", "AMZN"], "2023-11-20", "2023-11-24", "EUR")
|
||||||
|
|
||||||
print(ssd.get_info(0))
|
print(ssd.get_info(0))
|
||||||
print(ssd.get_info(0, "address1"))
|
print(ssd.get_info(0, "address1"))
|
||||||
@ -12,3 +12,4 @@ print(ssd.get_info(1))
|
|||||||
print(ssd.get_info(1, "currency"))
|
print(ssd.get_info(1, "currency"))
|
||||||
print(ssd.get_info(1, "website"))
|
print(ssd.get_info(1, "website"))
|
||||||
print(ssd.get_history(0, "1h"))
|
print(ssd.get_history(0, "1h"))
|
||||||
|
print(ssd._exchange_df)
|
Loading…
Reference in New Issue
Block a user