2019-10-05 08:40:59 +00:00
|
|
|
from typing import Dict, List, NamedTuple
|
2018-10-30 19:02:01 +00:00
|
|
|
|
|
|
|
import arrow
|
|
|
|
from pandas import DataFrame
|
|
|
|
|
2019-04-09 09:27:35 +00:00
|
|
|
from freqtrade.exchange import timeframe_to_minutes
|
2018-10-30 19:02:01 +00:00
|
|
|
from freqtrade.strategy.interface import SellType
|
|
|
|
|
|
|
|
ticker_start_time = arrow.get(2018, 10, 3)
|
2019-11-03 09:01:05 +00:00
|
|
|
tests_timeframe = '1h'
|
2018-10-30 19:02:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BTrade(NamedTuple):
|
|
|
|
"""
|
|
|
|
Minimalistic Trade result used for functional backtesting
|
|
|
|
"""
|
|
|
|
sell_reason: SellType
|
|
|
|
open_tick: int
|
|
|
|
close_tick: int
|
|
|
|
|
|
|
|
|
|
|
|
class BTContainer(NamedTuple):
|
|
|
|
"""
|
|
|
|
Minimal BacktestContainer defining Backtest inputs and results.
|
|
|
|
"""
|
2020-02-10 09:35:48 +00:00
|
|
|
data: List[List[float]]
|
2018-10-30 19:02:01 +00:00
|
|
|
stop_loss: float
|
2019-10-05 08:40:59 +00:00
|
|
|
roi: Dict[str, float]
|
2018-10-30 19:02:01 +00:00
|
|
|
trades: List[BTrade]
|
|
|
|
profit_perc: float
|
2019-03-17 14:28:04 +00:00
|
|
|
trailing_stop: bool = False
|
2019-06-13 17:35:20 +00:00
|
|
|
trailing_only_offset_is_reached: bool = False
|
|
|
|
trailing_stop_positive: float = None
|
|
|
|
trailing_stop_positive_offset: float = 0.0
|
|
|
|
use_sell_signal: bool = False
|
2018-10-30 19:02:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _get_frame_time_from_offset(offset):
|
2019-11-03 09:01:05 +00:00
|
|
|
return ticker_start_time.shift(minutes=(offset * timeframe_to_minutes(tests_timeframe))
|
2019-03-23 14:24:11 +00:00
|
|
|
).datetime
|
2018-10-30 19:02:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _build_backtest_dataframe(ticker_with_signals):
|
|
|
|
columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'buy', 'sell']
|
|
|
|
|
|
|
|
frame = DataFrame.from_records(ticker_with_signals, 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')
|
|
|
|
return frame
|