A module responsible for validating and normalizing strategy tester configurations.
Source code in strategytester5\config_validators.py
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 | class TesterConfigValidators:
"""
A module responsible for validating and normalizing strategy tester configurations.
"""
def __init__(self):
pass
@staticmethod
def _validate_keys(raw_config: Dict) -> None:
required_keys = config.REQUIRED_TESTER_CONFIG_KEYS
provided_keys = set(raw_config.keys())
missing = required_keys - provided_keys
if missing:
raise RuntimeError(f"Missing tester config keys: {missing}")
extra = provided_keys - required_keys
if extra:
raise RuntimeError(f"Unknown tester config keys: {extra}")
@staticmethod
def _parse_leverage(leverage: str) -> int:
"""
Converts '1:100' -> 100
"""
try:
left, right = leverage.split(":")
if left != "1":
raise ValueError
value = int(right)
if value <= 0:
raise ValueError
return value
except Exception:
raise RuntimeError(f"Invalid leverage format: {leverage}")
@staticmethod
def _parse_modelling(value):
# already integer
if isinstance(value, int):
if value not in config.SUPPORTED_TESTER_MODELLING:
raise RuntimeError(f"Invalid modelling integer: {value}")
return value
# string input
if isinstance(value, str):
key = value.lower()
if key not in config.SUPPORTED_TESTER_MODELLING_REVERSE:
raise RuntimeError(
f"Invalid modelling: {value}, supported: {list(config.SUPPORTED_TESTER_MODELLING.values())}"
)
return config.SUPPORTED_TESTER_MODELLING_REVERSE[key]
raise RuntimeError(f"Invalid modelling type: {type(value)}")
@staticmethod
def parse_date(date_str: str) -> datetime:
try:
return parser.parse(date_str, dayfirst=True)
except Exception:
raise RuntimeError(f"Invalid date format: '{date_str}'")
@staticmethod
def parse_tester_configs(raw_config: Dict) -> Dict:
""" Validates and normalizes raw tester configuration dictionary. """
TesterConfigValidators._validate_keys(raw_config)
cfg: Dict = {}
# --- BOT NAME ---
cfg["bot_name"] = str(raw_config["bot_name"])
# --- SYMBOLS ---
symbols = raw_config["symbols"]
if not isinstance(symbols, list) or not symbols:
raise RuntimeError("symbols must be a non-empty list")
cfg["symbols"] = symbols
# --- TIMEFRAME ---
timeframe = raw_config["timeframe"]
if timeframe not in OverLoadedMetaTrader5API.STRING2TIMEFRAME_MAP:
raise RuntimeError(
f"Invalid timeframe: {timeframe} supported: {OverLoadedMetaTrader5API.STRING2TIMEFRAME_MAP.keys()}")
cfg["timeframe"] = timeframe
# --- MODELLING ---
cfg["modelling"] = TesterConfigValidators._parse_modelling(raw_config["modelling"])
# --- DATE PARSING ---
start_date = TesterConfigValidators.parse_date(raw_config["start_date"])
end_date = TesterConfigValidators.parse_date(raw_config["end_date"])
if start_date >= end_date:
raise RuntimeError("start_date must be earlier than end_date")
cfg["start_date"] = start_date
cfg["end_date"] = end_date
# --- DEPOSIT ---
deposit = float(raw_config["deposit"])
if deposit <= 0:
raise RuntimeError("deposit must be > 0")
cfg["deposit"] = deposit
# --- LEVERAGE ---
cfg["leverage"] = TesterConfigValidators._parse_leverage(raw_config["leverage"])
return cfg
|
parse_tester_configs(raw_config)
staticmethod
Validates and normalizes raw tester configuration dictionary.
Source code in strategytester5\config_validators.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123 | @staticmethod
def parse_tester_configs(raw_config: Dict) -> Dict:
""" Validates and normalizes raw tester configuration dictionary. """
TesterConfigValidators._validate_keys(raw_config)
cfg: Dict = {}
# --- BOT NAME ---
cfg["bot_name"] = str(raw_config["bot_name"])
# --- SYMBOLS ---
symbols = raw_config["symbols"]
if not isinstance(symbols, list) or not symbols:
raise RuntimeError("symbols must be a non-empty list")
cfg["symbols"] = symbols
# --- TIMEFRAME ---
timeframe = raw_config["timeframe"]
if timeframe not in OverLoadedMetaTrader5API.STRING2TIMEFRAME_MAP:
raise RuntimeError(
f"Invalid timeframe: {timeframe} supported: {OverLoadedMetaTrader5API.STRING2TIMEFRAME_MAP.keys()}")
cfg["timeframe"] = timeframe
# --- MODELLING ---
cfg["modelling"] = TesterConfigValidators._parse_modelling(raw_config["modelling"])
# --- DATE PARSING ---
start_date = TesterConfigValidators.parse_date(raw_config["start_date"])
end_date = TesterConfigValidators.parse_date(raw_config["end_date"])
if start_date >= end_date:
raise RuntimeError("start_date must be earlier than end_date")
cfg["start_date"] = start_date
cfg["end_date"] = end_date
# --- DEPOSIT ---
deposit = float(raw_config["deposit"])
if deposit <= 0:
raise RuntimeError("deposit must be > 0")
cfg["deposit"] = deposit
# --- LEVERAGE ---
cfg["leverage"] = TesterConfigValidators._parse_leverage(raw_config["leverage"])
return cfg
|