Update some usages of timerange to new, simplified method

This commit is contained in:
Matthias 2022-11-10 18:11:39 +01:00
parent 3e676dbaa4
commit 57313dd961
6 changed files with 18 additions and 25 deletions

View File

@ -33,11 +33,13 @@ class TimeRange:
def startdt(self) -> Optional[datetime]: def startdt(self) -> Optional[datetime]:
if self.startts: if self.startts:
return datetime.fromtimestamp(self.startts, tz=timezone.utc) return datetime.fromtimestamp(self.startts, tz=timezone.utc)
return None
@property @property
def stopdt(self) -> Optional[datetime]: def stopdt(self) -> Optional[datetime]:
if self.stopts: if self.stopts:
return datetime.fromtimestamp(self.stopts, tz=timezone.utc) return datetime.fromtimestamp(self.stopts, tz=timezone.utc)
return None
def __eq__(self, other): def __eq__(self, other):
"""Override the default Equals behavior""" """Override the default Equals behavior"""

View File

@ -3,7 +3,6 @@ Functions to convert data from one format to another
""" """
import itertools import itertools
import logging import logging
from datetime import datetime, timezone
from operator import itemgetter from operator import itemgetter
from typing import Dict, List from typing import Dict, List
@ -137,11 +136,9 @@ def trim_dataframe(df: DataFrame, timerange, df_date_col: str = 'date',
df = df.iloc[startup_candles:, :] df = df.iloc[startup_candles:, :]
else: else:
if timerange.starttype == 'date': if timerange.starttype == 'date':
start = datetime.fromtimestamp(timerange.startts, tz=timezone.utc) df = df.loc[df[df_date_col] >= timerange.startdt, :]
df = df.loc[df[df_date_col] >= start, :]
if timerange.stoptype == 'date': if timerange.stoptype == 'date':
stop = datetime.fromtimestamp(timerange.stopts, tz=timezone.utc) df = df.loc[df[df_date_col] <= timerange.stopdt, :]
df = df.loc[df[df_date_col] <= stop, :]
return df return df

View File

@ -1,6 +1,6 @@
import logging import logging
import operator import operator
from datetime import datetime, timezone from datetime import datetime
from pathlib import Path from pathlib import Path
from typing import Dict, List, Optional, Tuple from typing import Dict, List, Optional, Tuple
@ -160,9 +160,9 @@ def _load_cached_data_for_updating(
end = None end = None
if timerange: if timerange:
if timerange.starttype == 'date': if timerange.starttype == 'date':
start = datetime.fromtimestamp(timerange.startts, tz=timezone.utc) start = timerange.startdt
if timerange.stoptype == 'date': if timerange.stoptype == 'date':
end = datetime.fromtimestamp(timerange.stopts, tz=timezone.utc) end = timerange.stopdt
# Intentionally don't pass timerange in - since we need to load the full dataset. # Intentionally don't pass timerange in - since we need to load the full dataset.
data = data_handler.ohlcv_load(pair, timeframe=timeframe, data = data_handler.ohlcv_load(pair, timeframe=timeframe,

View File

@ -366,13 +366,11 @@ class IDataHandler(ABC):
""" """
if timerange.starttype == 'date': if timerange.starttype == 'date':
start = datetime.fromtimestamp(timerange.startts, tz=timezone.utc) if pairdata.iloc[0]['date'] > timerange.startdt:
if pairdata.iloc[0]['date'] > start:
logger.warning(f"{pair}, {candle_type}, {timeframe}, " logger.warning(f"{pair}, {candle_type}, {timeframe}, "
f"data starts at {pairdata.iloc[0]['date']:%Y-%m-%d %H:%M:%S}") f"data starts at {pairdata.iloc[0]['date']:%Y-%m-%d %H:%M:%S}")
if timerange.stoptype == 'date': if timerange.stoptype == 'date':
stop = datetime.fromtimestamp(timerange.stopts, tz=timezone.utc) if pairdata.iloc[-1]['date'] < timerange.stopdt:
if pairdata.iloc[-1]['date'] < stop:
logger.warning(f"{pair}, {candle_type}, {timeframe}, " logger.warning(f"{pair}, {candle_type}, {timeframe}, "
f"data ends at {pairdata.iloc[-1]['date']:%Y-%m-%d %H:%M:%S}") f"data ends at {pairdata.iloc[-1]['date']:%Y-%m-%d %H:%M:%S}")

View File

@ -432,8 +432,8 @@ class FreqaiDataKitchen:
timerange_train.stopts = timerange_train.startts + train_period_days timerange_train.stopts = timerange_train.startts + train_period_days
first = False first = False
start = datetime.fromtimestamp(timerange_train.startts, tz=timezone.utc) start = timerange_train.startdt
stop = datetime.fromtimestamp(timerange_train.stopts, tz=timezone.utc) stop = timerange_train.stopdt
tr_training_list.append(start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d")) tr_training_list.append(start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d"))
tr_training_list_timerange.append(copy.deepcopy(timerange_train)) tr_training_list_timerange.append(copy.deepcopy(timerange_train))
@ -446,8 +446,8 @@ class FreqaiDataKitchen:
if timerange_backtest.stopts > config_timerange.stopts: if timerange_backtest.stopts > config_timerange.stopts:
timerange_backtest.stopts = config_timerange.stopts timerange_backtest.stopts = config_timerange.stopts
start = datetime.fromtimestamp(timerange_backtest.startts, tz=timezone.utc) start = timerange_backtest.startdt
stop = datetime.fromtimestamp(timerange_backtest.stopts, tz=timezone.utc) stop = timerange_backtest.stopdt
tr_backtesting_list.append(start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d")) tr_backtesting_list.append(start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d"))
tr_backtesting_list_timerange.append(copy.deepcopy(timerange_backtest)) tr_backtesting_list_timerange.append(copy.deepcopy(timerange_backtest))
@ -490,11 +490,9 @@ class FreqaiDataKitchen:
it is sliced down to just the present training period. it is sliced down to just the present training period.
""" """
start = datetime.fromtimestamp(timerange.startts, tz=timezone.utc) df = df.loc[df["date"] >= timerange.startdt, :]
stop = datetime.fromtimestamp(timerange.stopts, tz=timezone.utc)
df = df.loc[df["date"] >= start, :]
if not self.live: if not self.live:
df = df.loc[df["date"] < stop, :] df = df.loc[df["date"] < timerange.stopdt, :]
return df return df
@ -1057,8 +1055,8 @@ class FreqaiDataKitchen:
backtest_timerange.startts = ( backtest_timerange.startts = (
backtest_timerange.startts - backtest_period_days * SECONDS_IN_DAY backtest_timerange.startts - backtest_period_days * SECONDS_IN_DAY
) )
start = datetime.fromtimestamp(backtest_timerange.startts, tz=timezone.utc) start = backtest_timerange.startdt
stop = datetime.fromtimestamp(backtest_timerange.stopts, tz=timezone.utc) stop = backtest_timerange.stopdt
full_timerange = start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d") full_timerange = start.strftime("%Y%m%d") + "-" + stop.strftime("%Y%m%d")
config_path = Path(self.config["config_files"][0]) config_path = Path(self.config["config_files"][0])

View File

@ -230,7 +230,5 @@ def get_timerange_backtest_live_models(config: Config) -> str:
dk = FreqaiDataKitchen(config) dk = FreqaiDataKitchen(config)
models_path = dk.get_full_models_path(config) models_path = dk.get_full_models_path(config)
timerange, _ = dk.get_timerange_and_assets_end_dates_from_ready_models(models_path) timerange, _ = dk.get_timerange_and_assets_end_dates_from_ready_models(models_path)
start_date = datetime.fromtimestamp(timerange.startts, tz=timezone.utc) tr = f"{timerange.startdt.strftime('%Y%m%d')}-{timerange.stopdt.strftime('%Y%m%d')}"
end_date = datetime.fromtimestamp(timerange.stopts, tz=timezone.utc)
tr = f"{start_date.strftime('%Y%m%d')}-{end_date.strftime('%Y%m%d')}"
return tr return tr