stable/tests/optimize/__init__.py

67 lines
2.0 KiB
Python
Raw Normal View History

from typing import Dict, List, NamedTuple, Optional
import arrow
from pandas import DataFrame
2021-06-08 19:06:47 +00:00
from freqtrade.enums import SellType
from freqtrade.exchange import timeframe_to_minutes
2020-09-28 17:43:15 +00:00
tests_start_time = arrow.get(2018, 10, 3)
tests_timeframe = '1h'
class BTrade(NamedTuple):
"""
Minimalistic Trade result used for functional backtesting
"""
sell_reason: SellType
open_tick: int
close_tick: int
2021-07-23 04:42:43 +00:00
buy_tag: Optional[str] = None
class BTContainer(NamedTuple):
"""
Minimal BacktestContainer defining Backtest inputs and results.
"""
2020-02-10 09:35:48 +00:00
data: List[List[float]]
stop_loss: float
roi: Dict[str, float]
trades: List[BTrade]
profit_perc: float
trailing_stop: bool = False
trailing_only_offset_is_reached: bool = False
trailing_stop_positive: Optional[float] = None
trailing_stop_positive_offset: float = 0.0
use_sell_signal: bool = False
use_custom_stoploss: bool = False
2022-02-05 14:12:29 +00:00
custom_entry_price: Optional[float] = None
def _get_frame_time_from_offset(offset):
minutes = offset * timeframe_to_minutes(tests_timeframe)
return tests_start_time.shift(minutes=minutes).datetime
def _build_backtest_dataframe(data):
2021-07-23 04:42:43 +00:00
columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'buy', 'sell']
columns = columns + ['buy_tag'] if len(data[0]) == 9 else columns
frame = DataFrame.from_records(data, columns=columns)
frame['date'] = frame['date'].apply(_get_frame_time_from_offset)
# Ensure floats are in place
for column in ['open', 'high', 'low', 'close', 'volume']:
frame[column] = frame[column].astype('float64')
if 'buy_tag' not in columns:
frame['buy_tag'] = None
2021-10-20 17:13:34 +00:00
if 'exit_tag' not in columns:
frame['exit_tag'] = None
2021-10-30 14:10:28 +00:00
# Ensure all candles make kindof sense
assert all(frame['low'] <= frame['close'])
assert all(frame['low'] <= frame['open'])
assert all(frame['high'] >= frame['close'])
assert all(frame['high'] >= frame['open'])
return frame