diff --git a/xarizmi/examples/__init__.py b/xarizmi/examples/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/xarizmi/examples/download_yahoo_finance_data.py b/xarizmi/examples/download_yahoo_finance_data.py new file mode 100644 index 0000000..cab4dcb --- /dev/null +++ b/xarizmi/examples/download_yahoo_finance_data.py @@ -0,0 +1,44 @@ +# Download sample data for several stocks traded in Toronto Stock Exchange data + +import os +import pathlib + +from xarizmi.opendata.yahoo_finance import YahooFinanceDailyDataClient + +SYMBOLS_LIST: list[str] = [ + "T.TO", + "SHOP.TO", + "CNR.TO", + "CNQ.TO", + "ENB.TO", + "AC.TO", + "OTEX.TO", + "TRP.TO", + "TFII.TO", +] + + +def setup_data_directory() -> pathlib.Path: + + data_directory = ( + pathlib.Path(".") + / "data" + # / datetime.date.today().strftime("%Y-%m-%d") + ) + if not data_directory.exists(): + data_directory.mkdir(parents=True) + + return data_directory + + +def download_daily_symbols(parent_directory: pathlib.Path) -> None: + for symbol in SYMBOLS_LIST: + YahooFinanceDailyDataClient.download_full_data( + symbol=symbol, + filepath=os.path.join(parent_directory, symbol + ".json"), + ) + + +def main() -> None: + data_directory = setup_data_directory() + download_daily_symbols(data_directory) diff --git a/xarizmi/opendata/yahoo_finance.py b/xarizmi/opendata/yahoo_finance.py index fdf0872..0079a14 100644 --- a/xarizmi/opendata/yahoo_finance.py +++ b/xarizmi/opendata/yahoo_finance.py @@ -1,8 +1,6 @@ """A client to download Yahoo Finance data """ -from datetime import datetime - import pandas as pd import yfinance as yf @@ -37,7 +35,7 @@ def extract(self) -> list[dict[str, str | float | pd.Timestamp]] | None: def transform( self, data_list: list[dict[str, str | float | pd.Timestamp]] - ) -> list[dict[str, str | float | datetime | dict[str, dict[str, str]]]]: + ) -> CandlestickChart: candles_data = [] for single_candle_data in data_list: temp = {} @@ -54,22 +52,30 @@ def transform( "fee_currency": {"name": "CAD"}, } # type: ignore candles_data.append(temp) - return candles_data # type: ignore + return CandlestickChart.model_validate({"candles": candles_data}) def save_file( self, - candles_data: list[ - dict[str, str | float | datetime | dict[str, dict[str, str]]] - ], + candlestick_chart: CandlestickChart, filepath: str, indent: int = 4, ) -> None: - candles = CandlestickChart.model_validate({"candles": candles_data}) with open(filepath, "w") as f: - f.write(candles.model_dump_json(indent=indent)) + f.write(candlestick_chart.model_dump_json(indent=indent)) - def etl(self, filepath: str) -> None: + def etl(self, filepath: str) -> CandlestickChart: data_list = self.extract() if data_list: - candles_data = self.transform(data_list=data_list) - self.save_file(candles_data=candles_data, filepath=filepath) + candlestick_chart = self.transform(data_list=data_list) + self.save_file( + candlestick_chart=candlestick_chart, filepath=filepath + ) + return candlestick_chart + else: + return CandlestickChart(candles=[]) + + @staticmethod + def download_full_data(symbol: str, filepath: str) -> CandlestickChart: + client = YahooFinanceDailyDataClient(symbol=symbol) + candlestick_chart = client.etl(filepath=filepath) + return candlestick_chart