Simplify printing logic for non-optimized parameters

This commit is contained in:
Matthias 2021-06-15 20:15:37 +02:00
parent 8b7010fc9a
commit a7e9e362b7
2 changed files with 79 additions and 18 deletions

View File

@ -12,11 +12,13 @@ from pandas import isna, json_normalize
from freqtrade.constants import USERPATH_STRATEGIES from freqtrade.constants import USERPATH_STRATEGIES
from freqtrade.exceptions import OperationalException from freqtrade.exceptions import OperationalException
from freqtrade.misc import deep_merge_dicts, round_coin_value, round_dict from freqtrade.misc import deep_merge_dicts, round_coin_value, round_dict, safe_value_fallback2
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
NON_OPT_PARAM_APPENDIX = " # value loaded from strategy"
class HyperoptTools(): class HyperoptTools():
@ -158,33 +160,31 @@ class HyperoptTools():
def _params_pretty_print(params, space: str, header: str, non_optimized={}) -> None: def _params_pretty_print(params, space: str, header: str, non_optimized={}) -> None:
if space in params or space in non_optimized: if space in params or space in non_optimized:
space_params = HyperoptTools._space_params(params, space, 5) space_params = HyperoptTools._space_params(params, space, 5)
no_params = HyperoptTools._space_params(non_optimized, space, 5)
if not space_params and not no_params:
# No parameters - don't print
return
if not space_params:
# Not optimized parameters - append string
non_optimized = NON_OPT_PARAM_APPENDIX
result = f"\n# {header}\n" result = f"\n# {header}\n"
if space == "stoploss": if space == "stoploss":
opt = True stoploss = safe_value_fallback2(space_params, no_params, space, space)
if not space_params: result += (f"stoploss = {stoploss}{non_optimized}")
space_params = HyperoptTools._space_params(params, space, 5)
opt = False
result += (f"stoploss = {space_params.get('stoploss')}"
f"{' # value loaded from strategy' if not opt else ''}")
elif space == "roi": elif space == "roi":
result = result[:-1] + f'{non_optimized}\n'
minimal_roi_result = rapidjson.dumps({ minimal_roi_result = rapidjson.dumps({
str(k): v for k, v in space_params.items() str(k): v for k, v in (space_params or no_params).items()
}, default=str, indent=4, number_mode=rapidjson.NM_NATIVE) }, default=str, indent=4, number_mode=rapidjson.NM_NATIVE)
result += f"minimal_roi = {minimal_roi_result}" result += f"minimal_roi = {minimal_roi_result}"
elif space == "trailing": elif space == "trailing":
opt = True for k, v in (space_params or no_params).items():
if not space_params: result += f"{k} = {v}{non_optimized}\n"
# Not optimized ...
space_params = HyperoptTools._space_params(non_optimized, space, 5)
opt = False
for k, v in space_params.items():
result += f"{k} = {v}{' # value loaded from strategy' if not opt else ''}\n"
else: else:
# Buy / sell parameters # Buy / sell parameters
no_params = HyperoptTools._space_params(non_optimized, space, 5)
result += f"{space}_params = {HyperoptTools.__pprint_dict(space_params, no_params)}" result += f"{space}_params = {HyperoptTools.__pprint_dict(space_params, no_params)}"
@ -212,7 +212,7 @@ class HyperoptTools():
result += " " * indent + f'"{k}": ' result += " " * indent + f'"{k}": '
result += f'"{param}",' if isinstance(param, str) else f'{param},' result += f'"{param}",' if isinstance(param, str) else f'{param},'
if k in non_optimized: if k in non_optimized:
result += " # value loaded from strategy" result += NON_OPT_PARAM_APPENDIX
result += "\n" result += "\n"
result += '}' result += '}'
return result return result

View File

@ -199,3 +199,64 @@ def test_export_params(tmpdir):
assert "roi" in content["params"] assert "roi" in content["params"]
assert "stoploss" in content["params"] assert "stoploss" in content["params"]
assert "trailing" in content["params"] assert "trailing" in content["params"]
def test_params_print(capsys):
params = {
"buy": {
"buy_rsi": 30
},
"sell": {
"sell_rsi": 70
},
}
non_optimized = {
"buy": {
"buy_adx": 44
},
"sell": {
"sell_adx": 65
},
"stoploss": {
"stoploss": -0.05,
},
"roi": {
"0": 0.05,
"20": 0.01,
},
"trailing": {
"trailing_stop": False,
"trailing_stop_positive": 0.05,
"trailing_stop_positive_offset": 0.1,
"trailing_only_offset_is_reached": True
},
}
HyperoptTools._params_pretty_print(params, 'buy', 'No header', non_optimized)
captured = capsys.readouterr()
assert re.search("# No header", captured.out)
assert re.search('"buy_rsi": 30,\n', captured.out)
assert re.search('"buy_adx": 44, # value loaded.*\n', captured.out)
assert not re.search("sell", captured.out)
HyperoptTools._params_pretty_print(params, 'sell', 'Sell Header', non_optimized)
captured = capsys.readouterr()
assert re.search("# Sell Header", captured.out)
assert re.search('"sell_rsi": 70,\n', captured.out)
assert re.search('"sell_adx": 65, # value loaded.*\n', captured.out)
HyperoptTools._params_pretty_print(params, 'roi', 'ROI Table:', non_optimized)
captured = capsys.readouterr()
assert re.search("# ROI Table: # value loaded.*\n", captured.out)
assert re.search('minimal_roi = {\n', captured.out)
assert re.search('"20": 0.01\n', captured.out)
HyperoptTools._params_pretty_print(params, 'trailing', 'Trailing stop:', non_optimized)
captured = capsys.readouterr()
assert re.search("# Trailing stop:", captured.out)
assert re.search('trailing_stop = False # value loaded.*\n', captured.out)
assert re.search('trailing_stop_positive = 0.05 # value loaded.*\n', captured.out)
assert re.search('trailing_stop_positive_offset = 0.1 # value loaded.*\n', captured.out)
assert re.search('trailing_only_offset_is_reached = True # value loaded.*\n', captured.out)