freqtrade / freqtrade-strategies

Free trading strategies for Freqtrade bot
GNU General Public License v3.0
3.29k stars 1.11k forks source link

HO-SwingHighToSky.py space trailing error #136

Closed Pascal66 closed 3 years ago

Pascal66 commented 3 years ago

Using --space default is working --space alltoo But --space trailing alone give an error :

2021-03-19 08:37:16,830 - freqtrade - ERROR - Fatal exception!
joblib.externals.loky.process_executor._RemoteTraceback:
"""
Traceback (most recent call last):
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 4554, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 4562, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'cci-buy'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/joblib/externals/loky/process_executor.py", line 431, in _process_worker
    r = call_item()
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/joblib/externals/loky/process_executor.py", line 285, in __call__
    return self.fn(*self.args, **self.kwargs)
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/joblib/_parallel_backends.py", line 595, in __call__
    return self.func(*args, **kwargs)
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/joblib/parallel.py", line 262, in __call__
    return [func(*args, **kwargs)
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/joblib/parallel.py", line 262, in <listcomp>
    return [func(*args, **kwargs)
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/joblib/externals/loky/cloudpickle_wrapper.py", line 38, in __call__
    return self._obj(*args, **kwargs)
  File "/home/speedoo/freqtrade/freqtrade/optimize/hyperopt.py", line 283, in generate_optimizer
    backtesting_results = self.backtesting.backtest(
  File "/home/speedoo/freqtrade/freqtrade/optimize/backtesting.py", line 335, in backtest
    data: Dict = self._get_ohlcv_as_lists(processed)
  File "/home/speedoo/freqtrade/freqtrade/optimize/backtesting.py", line 196, in _get_ohlcv_as_lists
    self.strategy.advise_buy(pair_data, {'pair': pair}), {'pair': pair})[headers].copy()
  File "/home/speedoo/freqtrade/freqtrade/strategy/interface.py", line 702, in advise_buy
    return self.populate_buy_trend(dataframe, metadata)
  File "/home/speedoo/freqtrade/user_data/strategies/SwingHighToSky.py", line 70, in populate_buy_trend
    (dataframe['cci-buy'] <= -200.0) # Replace with value from hyperopt.
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/pandas/core/frame.py", line 3024, in __getitem__
    indexer = self.columns.get_loc(key)
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3082, in get_loc
    raise KeyError(key) from err
KeyError: 'cci-buy'
"""

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/speedoo/freqtrade/freqtrade/main.py", line 37, in main
    return_code = args['func'](args)
  File "/home/speedoo/freqtrade/freqtrade/commands/optimize_commands.py", line 86, in start_hyperopt
    hyperopt.start()
  File "/home/speedoo/freqtrade/freqtrade/optimize/hyperopt.py", line 449, in start
    f_val = self.run_optimizer_parallel(parallel, asked, i)
  File "/home/speedoo/freqtrade/freqtrade/optimize/hyperopt.py", line 366, in run_optimizer_parallel
    return parallel(delayed(
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/joblib/parallel.py", line 1054, in __call__
    self.retrieve()
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/joblib/parallel.py", line 933, in retrieve
    self._output.extend(job.get(timeout=self.timeout))
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/joblib/_parallel_backends.py", line 542, in wrap_future_result
    return future.result(timeout=timeout)
  File "/usr/lib/python3.8/concurrent/futures/_base.py", line 439, in result
    return self.__get_result()
  File "/usr/lib/python3.8/concurrent/futures/_base.py", line 388, in __get_result
    raise self._exception
KeyError: 'cci-buy'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "/usr/lib/python3.8/logging/__init__.py", line 2115, in shutdown
    h.close()
  File "/home/speedoo/freqtrade/.env/lib/python3.8/site-packages/absl/logging/__init__.py", line 945, in close
    self.stream.close()
AttributeError: 'WrappingIO' object has no attribute 'close'
xmatthias commented 3 years ago

it should work if you optimize all spaces.

When using only one space (for example --spaces trailing) - in the SwingHighToSky example, hyperopt will use:

The problem is that the strategy methods use the column 'cci-buy' - which the hyperopt does not populate. It's also not quite easy to do correctly, as i assume you first optimized for buy / sell spaces, and are now moving to the "next" space.

Assuming you ONLY optimize the trailing space, i'd rename the populate_indicators method in hyperopt so it's no longer used. This will ensure that the (previously) optimized buy / sell values will be used from the strategy.

I wouldn't know a better solution to this at the moment, as the combination of strategy / hyperopt assumes that results from buy/sell optimization are inserted into the strategy before running a reduced space.

maybe @OtenMoten as some other tips / opinions about this ... ?

Hope this helps

OtenMoten commented 3 years ago

Hello @xmatthias and @Pascal66,

thanks for sharing your issue. I will comment this in future directly in the file, the problem is that the hyperopt uses the ranges for the indicators you define at the top of the file:

cciTimeMin = 10
cciTimeMax = 80
cciValueMin = -200
cciValueMax = 200
cciTimeRange = range(cciTimeMin, cciTimeMax)

rsiTimeMin = 10
rsiTimeMax = 80
rsiValueMin = 10
rsiValueMax = 90
rsiTimeRange = range(rsiTimeMin, rsiTimeMax)

As you can see in the method populate_indicators(...) in the hyperopt file cciName = "cci-buy-" + str(cciTime) that the variable name will be cci-buy-55 for example. Therefore, if you use the hyperopt together with the strategy file, then you need to change the name if your variable of the strategy file to cci-buy-55 for example, depends on what timeperiod you use on this indicator. cci-buy-55 means cci with 55 timeperiod for buy signals.

Try it, I will be sure it will work :-) Because I experienced the same issue.

Kind regards @OtenMoten

PS: In one sentence: Indicator names in strategy file must be equal to the indiactor names of the hyperopt file.