` def add_parameter_list(
self,
name: str,
value_list: List[float]
) -> Tuple[bool, str]:
self.params[name] = value_list
return True, f"列表参数添加成功,数量{len(value_list)}"
def add_constrait(self, constraint: Callable[[dict], bool]):
self.constraint = constraint
def generate_settings(self) -> List[dict]:
""""""
keys: dict_keys = self.params.keys()
values: dict_values = self.params.values()
products: list = list(product(*values))
settings: list = []
for p in products:
setting: dict = dict(zip(keys, p))
if self.constraint is None or self.constraint(setting) is True:
settings.append(setting)
print(f"generate_settings: {len(settings)}/{len(products)}")
return settings
`
使用例子
opt_setting.add_parameter_list("signal_window", [15, 30, 60, 120])
opt_setting.add_parameter("short_entry_window", 10, 40, 2)
opt_setting.add_parameter("short_exit_window", 6, 30, 2)
# opt_setting.add_parameter("keltner_dev", 0, 3, 0.5)
# opt_setting.add_parameter("long_entry_window", 20, 60, 2)
# opt_setting.add_parameter("long_exit_window", 10, 40, 2)
opt_setting.add_parameter("cci_window", 8, 30, 2)
opt_setting.add_parameter("cci_signal", 8, 40, 4)
opt_setting.add_parameter("n_window", 10, 30, 2)
opt_setting.add_parameter("unit_limit", 1, 6, 1)
# opt_setting.add_parameter("trading_size", 1, 4, 1)
# opt_setting.add_parameter("pnl_filter", True,
# opt_setting.add_parameter("price_add", 0.2, 2, 0.2)
opt_setting.add_constrait(lambda params: params["short_exit_window"] < params["short_entry_window"])
# %%
engine.run_ga_optimization(opt_setting, max_workers=max_workers)