Skip to content

Commit

Permalink
minor: Cleanup hyperopt
Browse files Browse the repository at this point in the history
  • Loading branch information
hroff-1902 committed Sep 16, 2019
1 parent 39f41de commit 5cbc073
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions freqtrade/optimize/hyperopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ def __init__(self, config: Dict[str, Any]) -> None:
self.trials: List = []

# Populate functions here (hasattr is slow so should not be run during "regular" operations)
if hasattr(self.custom_hyperopt, 'populate_indicators'):
self.backtesting.strategy.advise_indicators = \
self.custom_hyperopt.populate_indicators # type: ignore
if hasattr(self.custom_hyperopt, 'populate_buy_trend'):
self.backtesting.advise_buy = self.custom_hyperopt.populate_buy_trend # type: ignore

if hasattr(self.custom_hyperopt, 'populate_sell_trend'):
self.backtesting.advise_sell = self.custom_hyperopt.populate_sell_trend # type: ignore

Expand Down Expand Up @@ -109,7 +111,9 @@ def clean_hyperopt(self):
p.unlink()

def get_args(self, params):
dimensions = self.hyperopt_space()

dimensions = self.dimensions

# Ensure the number of dimensions match
# the number of parameters in the list x.
if len(params) != len(dimensions):
Expand Down Expand Up @@ -322,9 +326,9 @@ def format_results(self, results: DataFrame) -> str:
f'Total profit {total_profit: 11.8f} {stake_cur} '
f'({profit: 7.2f}Σ%). Avg duration {duration:5.1f} mins.')

def get_optimizer(self, cpu_count) -> Optimizer:
def get_optimizer(self, dimensions, cpu_count) -> Optimizer:
return Optimizer(
self.hyperopt_space(),
dimensions,
base_estimator="ET",
acq_optimizer="auto",
n_initial_points=INITIAL_POINTS,
Expand Down Expand Up @@ -370,9 +374,6 @@ def start(self) -> None:
(max_date - min_date).days
)

self.backtesting.strategy.advise_indicators = \
self.custom_hyperopt.populate_indicators # type: ignore

preprocessed = self.backtesting.strategy.tickerdata_to_dataframe(data)

dump(preprocessed, self.tickerdata_pickle)
Expand All @@ -387,7 +388,8 @@ def start(self) -> None:
config_jobs = self.config.get('hyperopt_jobs', -1)
logger.info(f'Number of parallel jobs set as: {config_jobs}')

opt = self.get_optimizer(config_jobs)
self.dimensions = self.hyperopt_space()
self.opt = self.get_optimizer(self.dimensions, config_jobs)

if self.config.get('print_colorized', False):
colorama_init(autoreset=True)
Expand All @@ -398,9 +400,9 @@ def start(self) -> None:
logger.info(f'Effective number of parallel workers used: {jobs}')
EVALS = max(self.total_epochs // jobs, 1)
for i in range(EVALS):
asked = opt.ask(n_points=jobs)
asked = self.opt.ask(n_points=jobs)
f_val = self.run_optimizer_parallel(parallel, asked)
opt.tell(asked, [v['loss'] for v in f_val])
self.opt.tell(asked, [v['loss'] for v in f_val])
for j in range(jobs):
current = i * jobs + j
val = f_val[j]
Expand Down

0 comments on commit 5cbc073

Please sign in to comment.