12. Optimizer strategy optimizer
Optimizer is a strategy optimizer that can optimize strategy parameters using methods such as genetic algorithms and particle swarm optimization.
- class qteasy.optimization.Optimizer(*, op: Operator, method: str, shares: list[str], benchmark: DataFrame, pool_size: int, opti_target: str, opti_direction: str, parallel: bool, opti_start_date: str, opti_end_date: str, test_start_date: str, test_end_date: str, opti_cash_plan: CashPlan, test_cash_plan: CashPlan, cost_params: ndarray, signal_parsing_params: dict, trading_moq_params: dict, trading_delivery_params: dict, search_config: dict, logger: Optional[Logger] = None, evaluate_indicators: str = 'r,m,v,b', test_plot_type: str = 'histo')[source]
Optimal-parameter searcher object
13. Strategy parameter optimization method
Grid search
- class qteasy.optimization.Optimizer._search_grid(self, space: Space)
Grid search: sample the parameter space at a fixed step size and evaluate in batches.
- Parameters:
space (Space) – The parameter space to be searched.
- Returns:
Write the optimal result to
self.result_pool.- Return type:
None
Monte Carlo search
- class qteasy.optimization.Optimizer._search_montecarlo(self, space: Space)
Monte Carlo random search: uniformly sample at random within
spaceand evaluate in batches.- Parameters:
space (Space) – The parameter space to be searched.
- Returns:
Write the optimal result to
self.result_pool.- Return type:
None
Simulated annealing search
- class qteasy.optimization.Optimizer._search_sa(self, space: Space)
Progressive step-size random search: multiple rounds of Monte Carlo + subspace contraction.
In each round, randomly sample within several subspaces; after selecting the best, shrink the neighborhood using configurations such as
reduce_ratioand proceed to the next round, until stopping conditions such asmin_volumeormax_roundsare met.- Parameters:
space (Space) – Initial parameter space.
- Returns:
Write the optimal result to
self.result_pool.- Return type:
None
Genetic algorithm search
- class qteasy.optimization.Optimizer._search_ga(self, space: Space)
Optimal hyperparameter search algorithm 4: Genetic Algorithm. Genetic algorithms are suitable for searching for a global optimum or an approximate global optimum in an extremely large parameter space, while keeping the computational cost within an acceptable range.
Genetic algorithms draw on the iterative genetic process of living organisms. First, a certain number of parameter points are randomly selected in the parameter space; this set of parameter points is called a “population.” Iterative computation is then carried out based on this population. Before each iteration (called a reproduction), the probability that each individual survives or dies is determined according to the value of its evaluation function. The rule is: the closer an individual’s evaluation function value is to the optimal value, the higher its probability of survival and the higher its probability of producing offspring; otherwise, these probabilities are lower. After determining the probabilities of survival/death and reproduction, a certain number of individuals are selected to die according to the survival/death probabilities. From the remaining (surviving) individuals, those with the highest reproduction probabilities are selected for crossover and to reproduce the next generation of individuals. At the same time, random genetic mutations are introduced during reproduction to generate new individuals. Ultimately, the population size is restored to its initial value. In this way, one population iteration is completed. The above process is repeated for thousands or even tens of thousands of generations until the desired optimal or near-optimal solution appears in the population.
- Parameters:
space (qt.Space) – Parameter space object
- Return type:
None,搜索的结果最佳值会被保存在self.result_pool属性中
Gradient descent search
- class qteasy.optimization.Optimizer._search_gradient(self, space: Space)
Optimal parameter search algorithm 5: gradient descent (multi-point simultaneous gradient search)
Find, in the parameter space, the direction in which the optimization result improves the fastest, and always move toward the best direction (using an adaptive step size) until the result no longer changes or the maximum number of steps is reached. Randomly choose N starting points in the parameter space to begin the search; the output is the N results from the final step.
The neighborhood is generated via space.neighbors(point, axis_index, …), supporting int/float/enum/int_array/float_array. Adaptive step sizes (increase/decrease) are applied only to float dimensions; if there is no improvement, the step size is reduced, and the trajectory terminates when the step size becomes too small or when the number of consecutive non-improvements reaches the threshold.
- Parameters:
space (Space) – Parameter space
- Return type:
None
Particle swarm optimization search
- class qteasy.optimization.Optimizer._search_pso(self, space: Space)
Optimal hyperparameter search algorithm 6: Particle Swarm Optimization (PSO)
Randomly choose N starting points in the parameter space as the initial positions of the particle swarm. Each particle adjusts its velocity and position based on its own historical best position and the global historical best position, gradually approaching the optimum. After multiple iterations, the swarm converges to the global optimum or an approximate optimum.
Numeric representation uses space.point_to_vector / space.vector_to_point: update velocity and position in the encoded vector space, then map back to valid parameter points. Inertia weight w=0.7, cognitive coefficient c1=1.5, social coefficient c2=1.5.
- Parameters:
space (qt.Space) – Parameter space object
- Return type:
None,搜索的结果最佳值会被保存在self.result_pool属性中
Bayesian optimization search
- class qteasy.optimization.Optimizer._search_bayesian(self, space: Space)
Optimal hyperparameter search algorithm 6: Bayesian optimization
Bayesian optimization is a global optimization method based on Bayesian statistical theory, suitable for optimizing high-dimensional, non-convex, black-box functions. It guides the parameter search by building a probabilistic model of the objective function (typically a Gaussian process), thereby finding the optimum within a limited number of evaluations. Workflow: initial random sampling -> fit a surrogate with (X, y) (Gaussian process with an RBF kernel) -> iterate: compute the UCB acquisition function on candidate points, select the point with the largest acquisition value for evaluation -> update (X, y) and result_pool, until max_iterations is reached.
- Parameters:
space (qt.Space) – Parameter space object
- Return type:
None,搜索的结果最佳值会被保存在self.result_pool属性中