Flake8 compliance and documentation for hyperopt in strategy file

This commit is contained in:
Stephen Dade 2018-03-01 19:58:37 +11:00
parent 41b9165495
commit e4d29e473b
4 changed files with 44 additions and 40 deletions

View File

@ -19,8 +19,8 @@ your strategy file located into [user_data/strategies/](https://github.com/gcarq
### 1. Configure your Guards and Triggers ### 1. Configure your Guards and Triggers
There are two places you need to change in your strategy file to add a There are two places you need to change in your strategy file to add a
new buy strategy for testing: new buy strategy for testing:
- Inside [populate_buy_trend()](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py#L278-L294). - Inside [populate_buy_trend()](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py#L273-L294).
- Inside [hyperopt_space()](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py#L244-L297) known as `SPACE`. - Inside [indicator_space()](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py#L251-L67).
There you have two different type of indicators: 1. `guards` and 2. There you have two different type of indicators: 1. `guards` and 2.
`triggers`. `triggers`.
@ -55,10 +55,14 @@ Your hyperopt file must contain `guards` to find the right value for
`(dataframe['adx'] > 65)` & and `(dataframe['plus_di'] > 0.5)`. That `(dataframe['adx'] > 65)` & and `(dataframe['plus_di'] > 0.5)`. That
means you will need to enable/disable triggers. means you will need to enable/disable triggers.
In our case the `SPACE` and `populate_buy_trend` in your strategy file In our case the `indicator_space` and `populate_buy_trend` in your strategy file
will look like: will look like:
```python ```python
space = { def indicator_space(self) -> Dict[str, Any]:
"""
Define your Hyperopt space for searching strategy parameters
"""
return {
'rsi': hp.choice('rsi', [ 'rsi': hp.choice('rsi', [
{'enabled': False}, {'enabled': False},
{'enabled': True, 'value': hp.quniform('rsi-value', 20, 40, 1)} {'enabled': True, 'value': hp.quniform('rsi-value', 20, 40, 1)}
@ -77,7 +81,7 @@ space = {
{'type': 'stochf_cross'}, {'type': 'stochf_cross'},
{'type': 'ht_sine'}, {'type': 'ht_sine'},
]), ]),
} }
... ...
@ -100,7 +104,13 @@ def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame:
'stochf_cross': (crossed_above(dataframe['fastk'], dataframe['fastd'])), 'stochf_cross': (crossed_above(dataframe['fastk'], dataframe['fastd'])),
'ht_sine': (crossed_above(dataframe['htleadsine'], dataframe['htsine'])), 'ht_sine': (crossed_above(dataframe['htleadsine'], dataframe['htsine'])),
} }
... conditions.append(triggers.get(params['trigger']['type']))
dataframe.loc[
reduce(lambda x, y: x & y, conditions),
'buy'] = 1
return dataframe
``` ```
@ -116,7 +126,7 @@ The Hyperopt configuration is located in
## Advanced notions ## Advanced notions
### Understand the Guards and Triggers ### Understand the Guards and Triggers
When you need to add the new guards and triggers to be hyperopt When you need to add the new guards and triggers to be hyperopt
parameters, you do this by adding them into the [hyperopt_space()](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py#L244-L297). parameters, you do this by adding them into the [indicator_space()](https://github.com/gcarq/freqtrade/blob/develop/user_data/strategies/test_strategy.py#L251-L267).
If it's a trigger, you add one line to the 'trigger' choice group and that's it. If it's a trigger, you add one line to the 'trigger' choice group and that's it.

View File

@ -7,18 +7,14 @@ import os
import pickle import pickle
import signal import signal
import sys import sys
from functools import reduce
from math import exp from math import exp
from operator import itemgetter from operator import itemgetter
from typing import Dict, Any, Callable from typing import Dict, Any
import numpy from hyperopt import STATUS_FAIL, STATUS_OK, Trials, fmin, space_eval, tpe
import talib.abstract as ta
from hyperopt import STATUS_FAIL, STATUS_OK, Trials, fmin, hp, space_eval, tpe
from hyperopt.mongoexp import MongoTrials from hyperopt.mongoexp import MongoTrials
from pandas import DataFrame from pandas import DataFrame
import freqtrade.vendor.qtpylib.indicators as qtpylib
# Monkey patch config # Monkey patch config
from freqtrade import main # noqa; noqa from freqtrade import main # noqa; noqa
from freqtrade import exchange, misc, optimize from freqtrade import exchange, misc, optimize

View File

@ -4,7 +4,7 @@ import talib.abstract as ta
from pandas import DataFrame from pandas import DataFrame
from typing import Dict, Any, Callable from typing import Dict, Any, Callable
import freqtrade.vendor.qtpylib.indicators as qtpylib import freqtrade.vendor.qtpylib.indicators as qtpylib
from hyperopt import STATUS_FAIL, STATUS_OK, Trials, fmin, hp, space_eval, tpe from hyperopt import hp
from functools import reduce from functools import reduce
from freqtrade.strategy.interface import IStrategy from freqtrade.strategy.interface import IStrategy
from freqtrade.indicator_helpers import fishers_inverse from freqtrade.indicator_helpers import fishers_inverse
@ -385,7 +385,6 @@ class DefaultStrategy(IStrategy):
'roi_p3': hp.quniform('roi_p3', 0.01, 0.20, 0.01), 'roi_p3': hp.quniform('roi_p3', 0.01, 0.20, 0.01),
} }
def stoploss_space(self) -> Dict[str, Any]: def stoploss_space(self) -> Dict[str, Any]:
return { return {
'stoploss': hp.quniform('stoploss', -0.5, -0.02, 0.02), 'stoploss': hp.quniform('stoploss', -0.5, -0.02, 0.02),

View File

@ -197,4 +197,3 @@ class Strategy(object):
def stoploss_space(self) -> Dict[str, Any]: def stoploss_space(self) -> Dict[str, Any]:
return self.custom_strategy.stoploss_space() return self.custom_strategy.stoploss_space()