def next(self, tick): if crossover(self.sma1[:tick], self.sma2[:tick]): self.buy() elif crossover(self.sma2[:tick], self.sma1[:tick]): self.sell() else: pass
def next(self, tick): # 如果快线刚好越过慢线,买入全部 if crossover(self.sma1[:tick], self.sma1[:tick]): self.buy() # 如果慢线刚好越过快线,卖出全部 elif crossover(self.sma2[:tick], self.sma2[:tick]): self.sell() # 其他情况不进行操作 else: pass
def next(self, tick): # 如果此时快线刚好越过慢线,买入全部 if crossover(self.sma1[:tick], self.sma2[:tick]): self.buy() # 如果是慢线刚好越过快线,卖出全部 elif crossover(self.sma2[:tick], self.sma1[:tick]): self.sell() # 否则,这个时刻不执行任何操作 else: pass
def next(self, tick): # if fast line overtake slow line, buy all if crossover(self.sma1[:tick], self.sma2[:tick]): self.buy() # if slow line overtake fast line, sell all elif crossover(self.sma2[:tick], self.sma1[:tick]): self.sell() # otherwise,do nothing at this tick. else: pass
def next(self, tick): # buy all, if fast passes slow if crossover(self.sma1[:tick], self.sma2[:tick]): self.buy() # sell all, if flow passes fast elif crossover(self.sma2[:tick], self.sma1[:tick]): self.sell() # do nothing else: pass
def get_single_offspring(args): population, elitism_rate, datas, crossover, mutate = args parent1 = elitism_selection(population, elitism_rate) parent2 = elitism_selection(population, elitism_rate) offspring = crossover(parent1[0], parent2[0]) offspring = mutate(offspring, datas) return offspring
def LL_crossover_phase(x, x_prime, instance, implementation_choice, chromossome_length, n_offsprings, crossover_bias): y = copy(x_prime) T = 0 if implementation_choice == 1: # original implementation for i in range(n_offsprings): offspring = utils.crossover(x, x_prime, crossover_bias) if (offspring.chromossome == x.chromossome).all(): offspring.value = x.value elif (offspring.chromossome == y.chromossome).all(): offspring.value = y.value else: offspring.value = getattr(evaluate, instance.name)(offspring, instance) T += 1 if offspring.value > y.value: y = offspring elif implementation_choice == 2: # new implementation: only consider bits where x and x_prime are different D = np.arange(chromossome_length)[x != x_prime] nD = len(D) if nD >= 2: # no crossover if x and x_prime differ in less than two bits for i in range(n_offsprings): offspring = utils.crossover_different_bits( x, x_prime, crossover_bias, D, nD) offspring.value = getattr(evaluate, instance.name)(offspring, instance) T += 1 if offspring.value > y.value: y = offspring elif implementation_choice == 3: # same as crossover_implementation=1, but count evaluations even if offspring==parent for i in range(n_offsprings): offspring = utils.crossover(x, x_prime, crossover_bias) offspring.value = getattr(evaluate, instance.name)(offspring, instance) T += 1 if offspring.value > y.value: y = offspring return y, T
def next(self, tick, t_stamp,*args): side = '' # 如果此时快线刚好越过慢线,买入全部 if crossover(self.sma1[:tick], self.sma2[:tick]): self.buy(*args) side = "buy" # 如果是慢线刚好越过快线,卖出全部 elif crossover(self.sma2[:tick], self.sma1[:tick]): self.sell(*args) side = "sell" # 否则,这个时刻不执行任何操作。 else: pass if not self._broker._test: CrawlerToSql(symbol='BTCUSD', limit=5).save_to_sql(t_stamp) save_strategy_value(self.data.Close[tick], t_stamp, side)
def main(): population = [] generation = 1 for i in range(INITIAL_POPULATION): population.append(generate_individual(len(MASTER_SOLUTION))) while generation < MAX_ITERATIONS: print("Generation: {}, Population: {}".format(generation, len(population))) new_population = tournament_selection(population) population = [] for x, y in new_population: population.append(crossover(x, y)) population.append(crossover(y, x)) population = mutate_population(population) generation += 1
while (generation < MAX_ITERATION): selected_population = [] new_population = [] elitism_selecteds_size = POPULATION_SIZE - int( (POPULATION_SIZE * SELECTION_PERCENT) / 100) new_population.extend(get_bests(population, elitism_selecteds_size)) for i in range(int((POPULATION_SIZE * SELECTION_PERCENT) / 100)): selected_population.append(roulette_selection(population)) selected_population = zip( selected_population, selected_population[int(len(selected_population) / 2):]) for x in selected_population: sons = crossover(x[0].gene, x[1].gene) new_population.extend(sons) population_to_mutate = int((POPULATION_SIZE * MUTATION_PERCENT) / 100) for i in range(population_to_mutate): new_population[random.randint(0, POPULATION_SIZE - 1)] = mutate( new_population[random.randint(0, POPULATION_SIZE - 1)].gene) population = new_population best_solution = best(population).fitness generation += 1 print(best(population).gene)
population = gen_population(1000, accept, reject) bestest = gen_individual(accept, reject) for i in range(100): best = ['fjkghdfkghdfkghsdkfhgksdfjhgksjdfhgkjsdhfgkjhsdfkghsdfkjghksdfhgkj', -9999] print("\nGeneration:", i) new_pop = [] for _ in range(800): while True: try: m1 = utils.tournament_selection(population, 50) m2 = utils.tournament_selection(population, 50) child = utils.crossover(m1[0], m2[0]) child = [child, utils.calc_score(child, accept, reject)] new_pop.append(child) break except: pass for _ in range(100): new_pop.append(gen_individual(accept, reject)) for _ in range(100): while True: try: a = random.choice(population) b = utils.mutate(a[0]) b = [b, utils.calc_score(b, accept, reject)]