回测roc(low-20, 30)回测ts_argmax(ts_argmax(shift(ts_min(close*5, 25), 5), 5), 20)回测ts_argmax(high+ts_argmin(high, 40)*40, 25)回测roc(close*high-25, 30)回测ts_argmax(ts_max(low, 5), 30)回测ts_argmax(open*40, 30)回测roc(ts_argmax(shift(ts_min(ts_argmin(high+open, 5), 25), 5), 5), 20)回测low/40回测ts_argmax(open*40, 25)回测ts_argmax(low*40, 25)回测ts_argmax(open*30, 25)回测ts_argmax(ts_argmin(open+20, 5)*40, 25)回测ts_argmax(high+open*20, 25)100%|██████████| 42/42 [00:57<00:00, 1.36s/it][(np.float64(0.10995394170666328),), (np.float64(0.16062468319023337),), (np.float64(0.1294917159107123),), (np.float64(0.15599733116392978),), (np.float64(0.097866507140137),), (np.float64(0.08739382549965424),), (np.float64(0.1149561323071675),), (np.float64(0.10976038324229487),), (np.float64(0.11726512324352023),), (np.float64(0.12002225121233767),), (np.float64(0.14983263913601497),), (np.float64(0.07814158484872391),), (np.float64(0.1149561323071675),), (np.float64(0.06282097525146346),), (np.float64(0.05872432771887004),), (np.float64(0.05270703830191481),), (np.float64(0.15105884232813205),), (np.float64(0.14983263913601497),), (np.float64(0.0802239797162283),), (np.float64(0.06002950981921851),), (np.float64(0.06398200094792994),), (np.float64(0.11073416959713422),), (np.float64(0.10834546278044943),), (np.float64(0.16062468319023337),), (np.float64(0.10596689556202388),), (np.float64(0.04176307455015538),), (np.float64(0.08700065115521194),), (np.float64(0.12752585523697268),), (np.float64(0.08700065115521194),), (np.float64(0
.0948777025189329),), (np.float64(0.07011668572046625),), (np.float64(0.12287256988527973),), (np.float64(0.07014610594097404),), (np.float64(0.10678697725540909),), (np.float64(0.09598350460895055),), (np.float64(0.10271075698807741),), (np.float64(0.0802239797162283),), (np.float64(0.08700065115521194),), (np.float64(0.097866507140137),), (np.float64(0.08700065115521194),), (np.float64(0.1337976299550776),), (np.float64(0.10298669067168698),)]{'ts_argmax(low, 30)': np.float64(0.2645635973140159), 'ts_argmax(high+open*30, 25)': np.float64(0.2645635973140159), 'ts_argmax(high+open*40, 25)': np.float64(0.2645635973140159), 'ts_argmax(low/high+open, 25)': np.float64(0.2645635973140159), 'roc(open*high-20, 120)': np.float64(0.26283162629674517), 'ts_argmin(open+20, 5)-30': np.float64(0.25377965361984667), 'ts_argmax(open/close, 20)/ts_argmin(high+open, 40)': np.float64(0.22108661379291172), 'ts_argmax(low, 25)': np.float64(0.19523061072765335), 'low/20': np.float64(0.18693295297448653), 'roc(ts_argmax(shift(ts_min(close*5, 25), 5), 5), 20)': np.float64(0.16987331593202004), 'ts_argmax(close*5*40, 25)': np.float64(0.16707295447190162), 'sum(stddev(ts_min(sum(close, 40), 40), 120), 120)': np.float64(0.16062468319023337), 'ts_argmax(open*25, 25)': np.float64(0.1541661744279661), 'ts_argmax(open*40, 25)': np.float64(0.1541661744279661), 'ts_argmax(ts_min(close*5, 10)*40, 25)': np.float64(0.15105884232813205), 'ts_argmax(open/close, 20)/ts_argmin(high+open, 5)': np.float64(0.1473819159492742), 'ts_argmax(ts_argmin(open+20, 5)*40, 25)': np.float64(0.12752585523697268), 'sum(low, 120)': np.float64(0.10995394170666328), 'ts_argmax(high+open*10, 25)': np.float64(0.10976038324229487), 'ts_argmax(high+open*40, 40)': np.float64(0.0802239797162283)}{'avg': array([0.22980368]), 'std': array([0.04809951]), 'min': array([0.08022398]), 'max': array([0.2645636])}
from deap import base, creator, gp
creator.create("FitnessMax", base.Fitness, weights=(1.0,))def init_tool_box():
from deap import base, creator, tools
creator.create("Individual", gp.PrimitiveTree, fitness=creator.FitnessMax)
toolbox = base.Toolbox() from deap_factor.init_pset import get_pset
pset = get_pset() toolbox.register("expr", gp.genHalfAndHalf, pset=pset, min_=1, max_=5) toolbox.register("individual", tools.initIterate, creator.Individual, toolbox.expr) toolbox.register("population", tools.initRepeat, list, toolbox.individual)
toolbox.register("evaluate", print)
toolbox.register("select", tools.selTournament, tournsize=3) toolbox.register("mate", gp.cxOnePoint) toolbox.register("expr_mut", gp.genFull, min_=0, max_=2) toolbox.register("mutate", gp.mutUniform, expr=toolbox.expr_mut, pset=pset)
from deap_factor.backtest import backtester toolbox.register('map', backtester)
import operator
toolbox.decorate("mate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17)) toolbox.decorate("mutate", gp.staticLimit(key=operator.attrgetter("height"), max_value=17)) return toolbox
if __name__ == '__main__': from deap_factor import utils from deap import tools import numpy as np my_tool_box = init_tool_box() print('开始生成因子...') pop = my_tool_box.population(10) for p in pop: print(utils.stringify_for_sympy(p))
hof = tools.HallOfFame(10) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("avg", np.nanmean, axis=0) stats.register("std", np.nanstd, axis=0) stats.register("min", np.nanmin, axis=0) stats.register("max", np.nanmax, axis=0) from deap_factor.deap_patch import * population, logbook = eaMuPlusLambda(pop, my_tool_box, mu=150, lambda_=100, cxpb=0.5, mutpb=0.1, ngen=3, stats=stats, halloffame=hof, verbose=True, early_stopping_rounds=5)