forked from iterativv/NostalgiaForInfinity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathho_to_raw_codemod.py
More file actions
86 lines (69 loc) · 2.61 KB
/
ho_to_raw_codemod.py
File metadata and controls
86 lines (69 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import ast
import re
from freqtrade.configuration import Configuration
from freqtrade.resolvers import StrategyResolver
from freqtrade.strategy.interface import IStrategy
pattern = re.compile(
r"(CategoricalParameter|DecimalParameter|IntParameter|RealParameter).*((default=)(?P<value>.+?),.*\)|(default=)(?P<edge_value>.+?)\))"
)
def validate_syntax(src: str):
ast.parse(src) # Throw if syntax is not valid
def transform_code(src: str):
def repl(matchobj):
groupdict = matchobj.groupdict()
return groupdict["value"] or groupdict["edge_value"]
return pattern.sub(repl, src)
def replace_references(source: str, references_to_replace: list[str]):
"""
Replace references from transformed code
:param transformed_code: Transformed code
:param references_to_replace: A list of variables to remove its references
"""
modified_source = source
for reference_to_replace in references_to_replace:
modified_source = modified_source.replace(f"{reference_to_replace}.value", reference_to_replace)
return modified_source
def replace_all_references(strategy: IStrategy, source: str):
params = strategy.detect_all_parameters()
buy_params = [param[0] for param in params["buy"]]
sell_params = [param[0] for param in params["sell"]]
print("Replacing references of 'buy' hyperopting params...")
new_source = replace_references(source, buy_params)
print("Replacing references of 'sell' hyperopting params...")
new_source = replace_references(new_source, sell_params)
print("Replacing 'references' of 'buy_protection_params' hyperopting params...")
new_source = re.sub(r"(global_buy_protection_params\[([\"\'])[\w\_]+\2\]).value", r"\1", new_source)
return new_source
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--strategy",
"-s",
metavar="STRATEGY",
help="Name of the strategy",
type=str,
default="NostalgiaForInfinityNext",
)
parser.add_argument(
"--output",
"-o",
metavar="OUTPUT_PATH",
help="Output of transformed file",
type=str,
default="NostalgiaForInfinityNext_Raw.py",
)
args = parser.parse_args()
config = Configuration.from_files([])
config["strategy"] = args.strategy
strategy = StrategyResolver.load_strategy(config)
source = ""
with open(strategy.__file__) as f:
source = f.read()
validate_syntax(source)
new_source = replace_all_references(strategy, source)
print("Transforming all hyperopt values to raw...")
transformed = transform_code(new_source)
with open(args.output, "w") as f:
f.write(transformed)
print(f"Path of the transformed file: {args.output}")