Skip to content

Commit 83c0bdc

Browse files
committed
fix cache implementation
1 parent 42421af commit 83c0bdc

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/isbtchot/model.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66
from isbtchot.schemas.args import TypeTime
77
from sklearn.linear_model import LinearRegression
88

9-
USE_CACHE = True
10-
119
CACHE_BTC_PATH = isbtchot.root_path / "cache" / "btc.csv"
1210
BTC_API = (
1311
"https://min-api.cryptocompare.com/data/v2/histoday?fsym=BTC&tsym=USD&allData=true"
1412
)
1513

16-
17-
def btc_historical_daily() -> pd.DataFrame:
18-
# Ensure the directory exists
19-
CACHE_BTC_PATH.parent.mkdir(parents=True, exist_ok=True)
14+
def btc_historical_daily(options=None) -> pd.DataFrame:
15+
16+
if not options:
17+
options = {}
18+
19+
use_cache = options.get("disable_cache") is None
20+
21+
if use_cache:
22+
# Ensure the directory exists
23+
CACHE_BTC_PATH.parent.mkdir(parents=True, exist_ok=True)
2024

2125
# Check if the file exists and was modified today
2226
if (
23-
USE_CACHE and
27+
use_cache and
2428
CACHE_BTC_PATH.is_file()
2529
and datetime.datetime.fromtimestamp(CACHE_BTC_PATH.stat().st_mtime).date()
2630
== datetime.datetime.today().date()
@@ -44,8 +48,8 @@ def btc_historical_daily() -> pd.DataFrame:
4448
return df
4549

4650

47-
def btc_pi(time_grouping: TypeTime, periods_back: int | None = None):
48-
df = btc_historical_daily()[["close"]].rename({"close": "price"}, axis=1)
51+
def btc_pi(time_grouping: TypeTime, periods_back: int | None = None, **kwargs):
52+
df = btc_historical_daily(**kwargs)[["close"]].rename({"close": "price"}, axis=1)
4953

5054
df["sma111"] = df["price"].rolling(window=111).mean()
5155
df["sma350x2"] = df["price"].rolling(window=350).mean() * 2
@@ -84,8 +88,8 @@ def btc_pi(time_grouping: TypeTime, periods_back: int | None = None):
8488
return df
8589

8690

87-
def btc_hist(time_grouping: TypeTime | None = None, periods_back: int | None = None):
88-
df = btc_historical_daily()
91+
def btc_hist(time_grouping: TypeTime | None = None, periods_back: int | None = None, **kwargs):
92+
df = btc_historical_daily(**kwargs)
8993
df = df.rename(
9094
{"open": "Open", "close": "Close", "high": "High", "low": "Low"}, axis=1
9195
)
@@ -107,8 +111,9 @@ def btc_power_law(
107111
time_grouping: TypeTime | None = None,
108112
periods_back: int | None = None,
109113
years_to_predict: int = 20,
114+
**kwargs
110115
) -> pd.DataFrame:
111-
df = btc_hist(time_grouping=time_grouping)[["Close"]].rename(
116+
df = btc_hist(time_grouping=time_grouping, **kwargs)[["Close"]].rename(
112117
{"Close": "price"}, axis=1
113118
)
114119

0 commit comments

Comments
 (0)