freqtrade_origin/freqtrade/optimize/base_analysis.py

65 lines
1.9 KiB
Python
Raw Permalink Normal View History

2023-09-12 06:42:32 +00:00
import logging
from copy import deepcopy
2023-09-12 10:54:25 +00:00
from datetime import datetime, timezone
from typing import Any, Dict, Optional
2023-09-12 06:42:32 +00:00
from pandas import DataFrame
from freqtrade.configuration import TimeRange
logger = logging.getLogger(__name__)
class VarHolder:
timerange: TimeRange
data: DataFrame
indicators: Dict[str, DataFrame]
result: DataFrame
compared: DataFrame
from_dt: datetime
to_dt: datetime
compared_dt: datetime
timeframe: str
startup_candle: int
class BaseAnalysis:
def __init__(self, config: Dict[str, Any], strategy_obj: Dict):
self.failed_bias_check = True
self.full_varHolder = VarHolder()
2023-09-12 07:14:25 +00:00
self.exchange: Optional[Any] = None
2023-09-12 06:42:32 +00:00
self._fee = None
# pull variables the scope of the lookahead_analysis-instance
self.local_config = deepcopy(config)
2024-05-12 15:20:36 +00:00
self.local_config["strategy"] = strategy_obj["name"]
2023-09-12 06:42:32 +00:00
self.strategy_obj = strategy_obj
@staticmethod
def dt_to_timestamp(dt: datetime):
timestamp = int(dt.replace(tzinfo=timezone.utc).timestamp())
return timestamp
def fill_full_varholder(self):
self.full_varHolder = VarHolder()
# define datetime in human-readable format
2024-05-12 15:20:36 +00:00
parsed_timerange = TimeRange.parse_timerange(self.local_config["timerange"])
2023-09-12 06:42:32 +00:00
if parsed_timerange.startdt is None:
self.full_varHolder.from_dt = datetime.fromtimestamp(0, tz=timezone.utc)
else:
self.full_varHolder.from_dt = parsed_timerange.startdt
if parsed_timerange.stopdt is None:
2024-01-14 19:00:30 +00:00
self.full_varHolder.to_dt = datetime.now(timezone.utc)
2023-09-12 06:42:32 +00:00
else:
self.full_varHolder.to_dt = parsed_timerange.stopdt
2024-05-12 15:20:36 +00:00
self.prepare_data(self.full_varHolder, self.local_config["pairs"])
2023-09-12 06:42:32 +00:00
def start(self) -> None:
# first make a single backtest
self.fill_full_varholder()