-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ab4fda
commit 5ceac39
Showing
7 changed files
with
136 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ pydantic==2.7.0 | |
numpy~=1.24 | ||
scipy==1.13.1 | ||
TA-Lib | ||
matplotlib | ||
matplotlib | ||
yfinance~=0.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ invoke==2.2.0 | |
black==24.4.0 | ||
flake8==7.0.0 | ||
blacken-docs==1.16.0 | ||
setuptools==71.1.0 | ||
setuptools==71.1.0 | ||
pandas-stubs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
from typing import Any | ||
from numpy import ndarray, dtype, floating, float64 | ||
from numpy import ndarray, dtype, float64 | ||
|
||
def OBV(close: list[float] | ndarray[Any, dtype[float64]], volume: list[float] | ndarray[Any, dtype[float64]]) -> list[float]: ... | ||
def OBV( | ||
close: list[float] | ndarray[Any, dtype[float64]], | ||
volume: list[float] | ndarray[Any, dtype[float64]], | ||
) -> list[float]: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from typing import Any | ||
import pandas as pd | ||
|
||
def download( | ||
tickers: str, | ||
start: str | None = None, | ||
end: str | None = None, | ||
actions: bool = False, | ||
threads: bool = True, | ||
ignore_tz: bool | None = None, | ||
group_by: str = "column", | ||
auto_adjust: bool = False, | ||
back_adjust: bool = False, | ||
repair: bool = False, | ||
keepna: bool = False, | ||
progress: bool = True, | ||
period: str = "max", | ||
interval: str = "1d", | ||
prepost: bool = False, | ||
proxy: str | None = None, | ||
rounding: bool = False, | ||
timeout: int | float = 10, | ||
session: Any | None = None, | ||
) -> pd.DataFrame: ... |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
"""A client to download Yahoo Finance data | ||
""" | ||
|
||
from datetime import datetime | ||
|
||
import pandas as pd | ||
import yfinance as yf | ||
|
||
from xarizmi.candlestick import CandlestickChart | ||
|
||
|
||
class YahooFinanceDailyDataClient: | ||
|
||
def __init__( | ||
self, | ||
symbol: str, | ||
start_date: str = "1900-01-01", | ||
end_date: str | None = None, | ||
) -> None: | ||
self.symbol = symbol | ||
self.start_date = start_date | ||
self.end_date = end_date | ||
|
||
def extract(self) -> list[dict[str, str | float | pd.Timestamp]] | None: | ||
stock_data: pd.DataFrame = yf.download( | ||
self.symbol, start=self.start_date, end=self.end_date | ||
) | ||
|
||
if stock_data.empty: | ||
print("Error fetching data for symbol:", self.symbol) | ||
return None | ||
|
||
# Reset the index to get 'Date' as a column | ||
stock_data.reset_index(inplace=True) | ||
|
||
return stock_data.to_dict(orient="records") # type: ignore | ||
|
||
def transform( | ||
self, data_list: list[dict[str, str | float | pd.Timestamp]] | ||
) -> list[dict[str, str | float | datetime | dict[str, dict[str, str]]]]: | ||
candles_data = [] | ||
for single_candle_data in data_list: | ||
temp = {} | ||
temp["open"] = single_candle_data["Open"] | ||
temp["high"] = single_candle_data["High"] | ||
temp["low"] = single_candle_data["Low"] | ||
temp["close"] = single_candle_data["Close"] | ||
temp["volume"] = single_candle_data["Volume"] | ||
temp["datetime"] = single_candle_data["Date"].to_pydatetime() # type: ignore # noqa:E501 | ||
temp["interval_type"] = "1day" | ||
temp["symbol"] = { | ||
"base_currency": {"name": self.symbol}, | ||
"quote_currency": {"name": "CAD"}, | ||
"fee_currency": {"name": "CAD"}, | ||
} # type: ignore | ||
candles_data.append(temp) | ||
return candles_data # type: ignore | ||
|
||
def save_file( | ||
self, | ||
candles_data: list[ | ||
dict[str, str | float | datetime | dict[str, dict[str, str]]] | ||
], | ||
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)) | ||
|
||
def etl(self, filepath: str) -> None: | ||
data_list = self.extract() | ||
if data_list: | ||
candles_data = self.transform(data_list=data_list) | ||
self.save_file(candles_data=candles_data, filepath=filepath) |