Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from freqtrade/AverageHyperopt
hyperopt for AverageStrategy.py
- Loading branch information
Showing
2 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import talib.abstract as ta | ||
from pandas import DataFrame | ||
from typing import Dict, Any, Callable, List | ||
from functools import reduce | ||
|
||
from skopt.space import Categorical, Dimension, Integer, Real | ||
|
||
import freqtrade.vendor.qtpylib.indicators as qtpylib | ||
from freqtrade.optimize.hyperopt_interface import IHyperOpt | ||
|
||
shortRangeBegin = 10 | ||
shortRangeEnd = 20 | ||
mediumRangeBegin = 100 | ||
mediumRangeEnd = 120 | ||
|
||
|
||
class AverageHyperopt(IHyperOpt): | ||
""" | ||
Hyperopt file for optimizing AverageStrategy. | ||
Uses ranges of EMA periods to find the best parameter combination. | ||
""" | ||
|
||
@staticmethod | ||
def populate_indicators(dataframe: DataFrame, metadata: dict) -> DataFrame: | ||
|
||
for short in range(shortRangeBegin, shortRangeEnd): | ||
dataframe[f'maShort({short})'] = ta.EMA(dataframe, timeperiod=short) | ||
|
||
for medium in range(mediumRangeBegin, mediumRangeEnd): | ||
dataframe[f'maMedium({medium})'] = ta.EMA(dataframe, timeperiod=medium) | ||
|
||
return dataframe | ||
|
||
@staticmethod | ||
def buy_strategy_generator(params: Dict[str, Any]) -> Callable: | ||
""" | ||
Define the buy strategy parameters to be used by hyperopt | ||
""" | ||
def populate_buy_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: | ||
""" | ||
Buy strategy Hyperopt will build and use | ||
""" | ||
conditions = [] | ||
# TRIGGERS | ||
if 'trigger' in params: | ||
conditions.append(qtpylib.crossed_above( | ||
dataframe[f"maShort({params['trigger'][0]})"], | ||
dataframe[f"maMedium({params['trigger'][1]})"]) | ||
) | ||
|
||
if conditions: | ||
dataframe.loc[ | ||
reduce(lambda x, y: x & y, conditions), | ||
'buy'] = 1 | ||
|
||
return dataframe | ||
|
||
return populate_buy_trend | ||
|
||
@staticmethod | ||
def indicator_space() -> List[Dimension]: | ||
""" | ||
Define your Hyperopt space for searching strategy parameters | ||
""" | ||
buyTriggerList = [] | ||
for short in range(shortRangeBegin, shortRangeEnd): | ||
for medium in range(mediumRangeBegin, mediumRangeEnd): | ||
# The output will be (short, long) | ||
buyTriggerList.append( | ||
(short, medium) | ||
) | ||
return [ | ||
Categorical(buyTriggerList, name='trigger') | ||
] | ||
|
||
@staticmethod | ||
def sell_strategy_generator(params: Dict[str, Any]) -> Callable: | ||
""" | ||
Define the sell strategy parameters to be used by hyperopt | ||
""" | ||
def populate_sell_trend(dataframe: DataFrame, metadata: dict) -> DataFrame: | ||
""" | ||
Sell strategy Hyperopt will build and use | ||
""" | ||
# print(params) | ||
conditions = [] | ||
|
||
# TRIGGERS | ||
if 'sell-trigger' in params: | ||
conditions.append(qtpylib.crossed_above( | ||
dataframe[f"maMedium({params['sell-trigger'][1]})"], | ||
dataframe[f"maShort({params['sell-trigger'][0]})"]) | ||
) | ||
|
||
if conditions: | ||
dataframe.loc[ | ||
reduce(lambda x, y: x & y, conditions), | ||
'sell'] = 1 | ||
|
||
return dataframe | ||
|
||
return populate_sell_trend | ||
|
||
@staticmethod | ||
def sell_indicator_space() -> List[Dimension]: | ||
""" | ||
Define your Hyperopt space for searching sell strategy parameters | ||
""" | ||
sellTriggerList = [] | ||
for short in range(shortRangeBegin, shortRangeEnd): | ||
for medium in range(mediumRangeBegin, mediumRangeEnd): | ||
# The output will be (short, long) | ||
sellTriggerList.append( | ||
(short, medium) | ||
) | ||
|
||
return [ | ||
Categorical(sellTriggerList, name='sell-trigger') | ||
] | ||
|
||
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: | ||
""" | ||
Based on TA indicators. Should be a copy of from strategy | ||
must align to populate_indicators in this file | ||
Only used when --spaces does not include buy | ||
""" | ||
dataframe.loc[ | ||
( | ||
qtpylib.crossed_above( | ||
dataframe[f'maShort({shortRangeBegin})'], | ||
dataframe[f'maMedium({mediumRangeBegin})']) | ||
), | ||
'buy'] = 1 | ||
|
||
return dataframe | ||
|
||
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: | ||
""" | ||
Based on TA indicators. Should be a copy of from strategy | ||
must align to populate_indicators in this file | ||
Only used when --spaces does not include sell | ||
""" | ||
dataframe.loc[ | ||
( | ||
qtpylib.crossed_above( | ||
dataframe[f'maMedium({mediumRangeBegin})'], | ||
dataframe[f'maShort({shortRangeBegin})']) | ||
), | ||
'sell'] = 1 | ||
|
||
return dataframe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ab9cba8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, the code freezes with less than 30 evaluations. I have tried on two different servers, one with heaps of ram and processors, both had the same issue.
ab9cba8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It may appear to freeze - best use
--print-all
so all epochs are shown (this avoids the feeling of the process being stuck").Also - it is possible that certain epochs require more time than others - so a linear execution cannot be guaranteed. Just give it a bit more time.
ab9cba8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, here are two videos from the run, both stopped at 28 evaluations.
https://www.screencast.com/t/21negUipXOD -12 min long (stops flashing at 1:35 and no change for the rest of the video, you can see the cores reduce to 1/4)
https://www.screencast.com/t/vffx86lApCAa - 6 min long (same as above)
ab9cba8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please open an issue with this informations.
Comments on commits are very hard to track, and once i close this window it's gone and i'll probably never find it again.
Thanks