vn.py量化社区
By Traders, For Traders.
Member
avatar
加入于:
帖子: 36
声望: 0

报错信息如下:

ValueError                                Traceback (most recent call last)
<ipython-input-4-61cb6e660c1c> in <module>
      3 setting.add_parameter("break_rate", 0.1, 1, 0.1)
      4 
----> 5 engine.run_ga_optimization(setting)

c:\vnstudio\lib\site-packages\vnpy\app\cta_strategy\backtesting.py in run_ga_optimization(self, optimization_setting, population_size, ngen_size, output)
    685             ngen,
    686             stats,
--> 687             halloffame=hof
    688         )
    689 

c:\vnstudio\lib\site-packages\deap\algorithms.py in eaMuPlusLambda(population, toolbox, mu, lambda_, cxpb, mutpb, ngen, stats, halloffame, verbose)
    314     for gen in range(1, ngen + 1):
    315         # Vary the population
--> 316         offspring = varOr(population, toolbox, lambda_, cxpb, mutpb)
    317 
    318         # Evaluate the individuals with an invalid fitness

c:\vnstudio\lib\site-packages\deap\algorithms.py in varOr(population, toolbox, lambda_, cxpb, mutpb)
    232         if op_choice < cxpb:            # Apply crossover
    233             ind1, ind2 = list(map(toolbox.clone, random.sample(population, 2)))
--> 234             ind1, ind2 = toolbox.mate(ind1, ind2)
    235             del ind1.fitness.values
    236             offspring.append(ind1)

c:\vnstudio\lib\site-packages\deap\tools\crossover.py in cxTwoPoint(ind1, ind2)
     45     size = min(len(ind1), len(ind2))
     46     cxpoint1 = random.randint(1, size)
---> 47     cxpoint2 = random.randint(1, size - 1)
     48     if cxpoint2 >= cxpoint1:
     49         cxpoint2 += 1

c:\vnstudio\lib\random.py in randint(self, a, b)
    220         """
    221 
--> 222         return self.randrange(a, b+1)
    223 
    224     def _randbelow(self, n, int=int, maxsize=1<<BPF, type=type,

c:\vnstudio\lib\random.py in randrange(self, start, stop, step, _int)
    198             return istart + self._randbelow(width)
    199         if step == 1:
--> 200             raise ValueError("empty range for randrange() (%d,%d, %d)" % (istart, istop, width))
    201 
    202         # Non-unit step argument supplied.

ValueError: empty range for randrange() (1,1, 0)

回测的参数为:

setting = OptimizationSetting()
setting.set_target("sharpe_ratio")
setting.add_parameter("break_rate", 0.1, 1, 0.1)

engine.run_ga_optimization(setting)

单单回测的时候是正常的,就是优化参数的时候报错。

Administrator
avatar
加入于:
帖子: 4040
声望: 223

setting.add_parameter("break_rate", 0.1, 1, 0.1)

你这里起始和结束值都是0.1,没有任何参数变动肯定报错了。

我猜你想写的是:

setting.add_parameter("break_rate", 0.1, 0.1, 1)

Member
avatar
加入于:
帖子: 36
声望: 0

用Python的交易员 wrote:

setting.add_parameter("break_rate", 0.1, 1, 0.1)

你这里起始和结束值都是0.1,没有任何参数变动肯定报错了。

我猜你想写的是:

setting.add_parameter("break_rate", 0.1, 0.1, 1)

原本是一个参数优化的,会报错;
再加一个优化的参数就没有报错了。

Administrator
avatar
加入于:
帖子: 4040
声望: 223

哦哦,遗传算法要最少两个,才能生成样本空间的

© 2015-2019 上海韦纳软件科技有限公司
备案服务号:沪ICP备18006526号-3