Skip to content

Commit

Permalink
Handle end_date with CCXT source when loading crypto (#4296)
Browse files Browse the repository at this point in the history
* Handle `end_date` with `CCXT` source when loading crypto

* Fix linting

* Fix linting

* Improve code readability

* Final linting

---------

Co-authored-by: James Maslek <jmaslek11@gmail.com>
  • Loading branch information
northern-64bit and jmaslek authored Feb 20, 2023
1 parent 33e79a2 commit 1c84da9
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion openbb_terminal/cryptocurrency/cryptocurrency_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ def load_from_ccxt(
interval: str = "1440",
exchange: str = "binance",
to_symbol: str = "usdt",
end_date: datetime = datetime.now(),
) -> pd.DataFrame:
"""Load crypto currency data [Source: https://github.com/ccxt/ccxt]
Expand All @@ -341,6 +342,8 @@ def load_from_ccxt(
The exchange to get data from.
to_symbol: str
Quote Currency (Defaults to usdt)
end_date: datetime
The datetime to stop at
Returns
-------
Expand All @@ -351,6 +354,11 @@ def load_from_ccxt(
pair = f"{symbol.upper()}/{to_symbol.upper()}"

try:
if interval not in list(CCXT_INTERVAL_MAP):
console.print(
f"\nInvalid interval chosen for source, "
f"available intervals: {', '.join(list(CCXT_INTERVAL_MAP))}\n"
)
df = fetch_ccxt_ohlc(
exchange,
3,
Expand All @@ -362,6 +370,19 @@ def load_from_ccxt(
if df.empty:
console.print(f"\nPair {pair} not found in {exchange}\n")
return pd.DataFrame()
i = 0
for date in df.index:
if date > end_date:
break
i += 1
first_date = pd.to_datetime(str(df.index.values[0]))
df = df.iloc[0:i]
if df.empty:
console.print(
f"\nThe first data point retrieved was {first_date.strftime('%Y-%m-%d')}, "
f"thus the end date ({end_date.strftime('%Y-%m-%d')}) filtered out all of the data.\n"
)
return pd.DataFrame()
except Exception:
console.print(f"\nPair {pair} not found on {exchange}\n")
return df
Expand Down Expand Up @@ -537,7 +558,9 @@ def load(
end_date = check_datetime(end_date, start=False)

if source == "CCXT":
return load_from_ccxt(symbol, start_date, interval, exchange, to_symbol)
return load_from_ccxt(
symbol, start_date, interval, exchange, to_symbol, end_date
)
if source == "CoinGecko":
return load_from_coingecko(symbol, start_date, to_symbol)
if source == "YahooFinance":
Expand Down

0 comments on commit 1c84da9

Please sign in to comment.