implement fallback loading for load_backtest_data
This commit is contained in:
parent
1339479882
commit
c13ec4a1d4
@ -70,29 +70,34 @@ def load_backtest_data(filename: Union[Path, str]) -> pd.DataFrame:
|
|||||||
Load backtest data file.
|
Load backtest data file.
|
||||||
:param filename: pathlib.Path object, or string pointing to the file.
|
:param filename: pathlib.Path object, or string pointing to the file.
|
||||||
:return: a dataframe with the analysis results
|
:return: a dataframe with the analysis results
|
||||||
|
:raise: ValueError if loading goes wrong.
|
||||||
"""
|
"""
|
||||||
if isinstance(filename, str):
|
data = load_backtest_stats(filename)
|
||||||
filename = Path(filename)
|
if not isinstance(data, list):
|
||||||
|
# new format
|
||||||
|
if 'strategy' not in data:
|
||||||
|
raise ValueError("Unknown dataformat")
|
||||||
|
if len(data['strategy']) != 1:
|
||||||
|
raise ValueError("Detected new Format with more than one strategy")
|
||||||
|
strategy = list(data['strategy'].keys())[0]
|
||||||
|
data = data['strategy'][strategy]['trades']
|
||||||
|
df = pd.DataFrame(data)
|
||||||
|
|
||||||
if not filename.is_file():
|
else:
|
||||||
raise ValueError(f"File {filename} does not exist.")
|
# old format - only with lists.
|
||||||
|
df = pd.DataFrame(data, columns=BT_DATA_COLUMNS)
|
||||||
|
|
||||||
with filename.open() as file:
|
df['open_date'] = pd.to_datetime(df['open_date'],
|
||||||
data = json_load(file)
|
unit='s',
|
||||||
|
utc=True,
|
||||||
df = pd.DataFrame(data, columns=BT_DATA_COLUMNS)
|
infer_datetime_format=True
|
||||||
|
)
|
||||||
df['open_date'] = pd.to_datetime(df['open_date'],
|
df['close_date'] = pd.to_datetime(df['close_date'],
|
||||||
unit='s',
|
unit='s',
|
||||||
utc=True,
|
utc=True,
|
||||||
infer_datetime_format=True
|
infer_datetime_format=True
|
||||||
)
|
)
|
||||||
df['close_date'] = pd.to_datetime(df['close_date'],
|
df['profit_abs'] = df['close_rate'] - df['open_rate']
|
||||||
unit='s',
|
|
||||||
utc=True,
|
|
||||||
infer_datetime_format=True
|
|
||||||
)
|
|
||||||
df['profit'] = df['close_rate'] - df['open_rate']
|
|
||||||
df = df.sort_values("open_date").reset_index(drop=True)
|
df = df.sort_values("open_date").reset_index(drop=True)
|
||||||
return df
|
return df
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ def backtest_result_to_list(results: DataFrame) -> List[List]:
|
|||||||
# Return 0 as "index" for compatibility reasons (for now)
|
# Return 0 as "index" for compatibility reasons (for now)
|
||||||
# TODO: Evaluate if we can remove this
|
# TODO: Evaluate if we can remove this
|
||||||
return [[t.pair, t.profit_percent, t.open_date.timestamp(),
|
return [[t.pair, t.profit_percent, t.open_date.timestamp(),
|
||||||
t.open_date.timestamp(), 0, t.trade_duration,
|
t.close_date.timestamp(), 0, t.trade_duration,
|
||||||
t.open_rate, t.close_rate, t.open_at_end, t.sell_reason.value]
|
t.open_rate, t.close_rate, t.open_at_end, t.sell_reason.value]
|
||||||
for index, t in results.iterrows()]
|
for index, t in results.iterrows()]
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user