def eval_fitness_single(self, genome, config): r_start = self.epoch_len cppn = neat.nn.FeedForwardNetwork.create(genome, config) network = ESNetwork(self.subStrate, cppn, self.params) net = network.create_phenotype_network_nd() g.fitness = self.evaluate(net, network, r_start) return g.fitness
def eval_fitness(self, genomes, config): r_start = randint(0, self.hs.hist_full_size - self.epoch_len) for idx, g in genomes: cppn = neat.nn.FeedForwardNetwork.create(g, config) network = ESNetwork(self.subStrate, cppn, self.params) net = network.create_phenotype_network_nd() g.fitness = self.evaluate(net, network, r_start)
def trial_run(self): r_start = 0 file = open("es_trade_god_cppn_3d.pkl", 'rb') [cppn] = pickle.load(file) network = ESNetwork(self.subStrate, cppn, self.params) net = network.create_phenotype_network_nd() fitness = self.evaluate(net, network, r_start) return fitness
def eval_fitness(self, genomes, config): self.epoch_len = randint(21, 255) r_start = randint(0 + self.hd, self.hs.hist_full_size - self.epoch_len) for idx, g in genomes: [cppn] = create_cppn(g, config, self.leaf_names, ['cppn_out']) network = ESNetwork(self.subStrate, cppn, self.params) net = network.create_phenotype_network_nd('training_net.png') g.fitness = self.evaluate(net, network, r_start, g) return
def run_champs(self): genomes = os.listdir(os.path.join(os.path.dirname(__file__), 'champs_d2_single')) fitness_data = {} best_fitness = 0.0 for g_ix in range(len(genomes)): genome = self.load_net('./champs_d2_single/'+genomes[g_ix]) start = self.hs.hist_full_size - self.epoch_len network = ESNetwork(self.subStrate, self.cppn, self.params) net = network.create_phenotype_network_nd('./champs_visualized2/genome_'+str(g_ix)) fitness = self.evaluate(net, network, start, g_ix, genomes[g_ix]) if fitness > best_fitness: best_genome = genome
def run_champ(self): genome = neat.Checkpointer.restore_checkpoint( "./binance_champs_2/tradegod-checkpoint-48").population[5808] self.load_net_easy(genome) start = self.hs.hist_full_size - self.epoch_len network = ESNetwork(self.subStrate, self.cppn, self.params) net = network.create_phenotype_network_nd( './champs_visualizedd3/genome_' + str(5808)) fitness = self.evaluate(net, network, start, genome, 5808) with open( './binance_champs_2/perpetual_champion_' + str(5808) + '.pkl', 'wb') as output: pickle.dump(genome, output)
def peer_v_peer(self, g1, g2): cppn = neat.nn.FeedForwardNetwork.create(g1, config) network = ESNetwork(self.subStrate, cppn, self.params) net = network.create_phenotype_network_nd() g1.fitness = self.evaluate(net, network, r_start) cppn2 = neat.nn.FeedForwardNetwork.create(g2, config) network2 = ESNetwork(self.subStrate, cppn, self.params) net2 = network.create_phenotype_network_nd() g2.fitness2 = self.evaluate(net, network, r_start) if g1 > g2: return g1 else: return g2
def main_loopies(): cs = LiqMaster2000() print(cs.local_ip) task = PurpleTrader(13) winner = run_pop(task, 21)[0] print('\nBest genome:\n{!s}'.format(winner)) # Verify network output against training data. print('\nOutput:') cppn = neat.nn.FeedForwardNetwork.create(winner, task.config) network = ESNetwork(task.subStrate, cppn, task.params) with open('local_winner.pkl', 'wb') as output: pickle.dump(cppn, output) #draw_net(cppn, filename="es_trade_god") winner_net = network.create_phenotype_network_nd('dabestest.png') # This will also draw winner_net.
def eval_fitness(self, genomes, config): r_start = randint(0+self.hd, self.hs.hist_full_size - self.epoch_len) fitter = genomes[0] fitter_val = 0.0 for idx, g in genomes: [cppn] = create_cppn(g, config, self.leaf_names, ['cppn_out']) network = ESNetwork(self.subStrate, cppn, self.params) net = network.create_phenotype_network_nd() g.fitness = self.evaluate(net, network, r_start, g) if(g.fitness > fitter_val): fitter = g fitter_val = g.fitness with open('./champs/perpetual_champion_'+str(fitter.key)+'.pkl', 'wb') as output: pickle.dump(fitter, output) print("latest_saved")
def evaluate(self, g, config): rand_start = self.rand_start [cppn] = create_cppn(g, config, self.leaf_names, ['cppn_out']) net = ESNetwork(self.subStrate, cppn, self.params) network = net.create_phenotype_network_nd() portfolio_start = 1.0 key_list = list(self.hs.currentHists.keys()) portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict) end_prices = {} buys = 0 sells = 0 if (len(g.connections) > 0.0): for z in range(rand_start, rand_start + self.epoch_len): active = self.get_one_epoch_input(z) signals = [] network.reset() for n in range(1, self.hd + 1): out = network.activate(active[self.hd - n]) for x in range(len(out)): signals.append(out[x]) #rng = iter(shuffle(rng)) sorted_shit = np.argsort(signals)[::-1] for x in sorted_shit: sym = self.hs.coin_dict[x] #print(out[x]) #try: if (out[x] < -.5): #print("selling") portfolio.sell_coin( sym, self.hs.currentHists[sym]['close'][z]) #print("bought ", sym) if (out[x] > .5): #print("buying") portfolio.target_amount = .1 + (out[x] * .1) portfolio.buy_coin( sym, self.hs.currentHists[sym]['close'][z]) #print("sold ", sym) #skip the hold case because we just dont buy or sell hehe if (z > self.epoch_len + rand_start - 2): end_prices[sym] = self.hs.currentHists[sym]['close'][z] result_val = portfolio.get_total_btc_value(end_prices) print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2]) ft = result_val[0] else: ft = 0.0 return ft
def poloTrader(self): end_prices = {} active = self.get_one_bar_input_2d() self.load_net() sub = Substrate(self.in_shapes, self.out_shapes) network = ESNetwork(sub, self.cppn, self.params) net = network.create_phenotype_network_nd('paper_net.png') net.reset() for n in range(1, self.hist_depth+1): out = net.activate(active[self.hist_depth-n]) #print(len(out)) rng = len(out) #rng = iter(shuffle(rng)) self.reset_tickers() for x in np.random.permutation(rng): sym = self.hs.coin_dict[x] #print(out[x]) try: if(out[x] < -.5): print("selling: ", sym) p = self.get_price('BTC_'+sym) price = p -(p*.01) self.sell_coin('BTC_'+sym, price) elif(out[x] > .5): print("buying: ", sym) self.target_percent = .1 + out[x] - .45 p = self.get_price('BTC_'+sym) price = p*1.01 self.buy_coin('BTC_'+sym, price) except: print('error', sym) #skip the hold case because we just dont buy or sell hehe if datetime.now() >= self.end_ts: return else: time.sleep(self.ticker_len) self.refresh_data() #self.closeOrders() self.poloTrader()
def run_champs(self): genomes = neat.Checkpointer.restore_checkpoint( "./binance_champs_2/tradegod-checkpoint-70").population fitness_data = {} best_fitness = 0.0 best_ix = "" count = 0 for g_ix in genomes: self.load_net_easy(genomes[g_ix]) start = self.hs.hist_full_size - self.epoch_len network = ESNetwork(self.subStrate, self.cppn, self.params) net = network.create_phenotype_network_nd( './champs_visualizedd3/genome_' + str(g_ix)) fitness = self.evaluate(net, network, start, genomes[g_ix], g_ix) if (fitness > best_fitness): best_fitness = fitness best_ix = genomes[g_ix] count += 1 with open( './binance_champs_2/perpetual_champion_' + str(g_ix) + '.pkl', 'wb') as output: pickle.dump(best_ix, output)
winner = pop.run(task.eval_fitness, gens) print("es trade god summoned") return winner, stats # If run as script. if __name__ == '__main__': task = PurpleTrader(8) #print(task.trial_run()) winner = run_pop(task, 89)[0] print('\nBest genome:\n{!s}'.format(winner)) # Verify network output against training data. print('\nOutput:') [cppn] = create_cppn(winner, task.config, task.leaf_names, ['cppn_out']) network = ESNetwork(task.subStrate, cppn, task.params) with open('es_trade_god_cppn_3d.pkl', 'wb') as output: pickle.dump(winner, output) #draw_net(cppn, filename="es_trade_god") winner_net = network.create_phenotype_network_nd('dabestest.png') # This will also draw winner_net. # Save CPPN if wished reused and draw it to file. #draw_net(cppn, filename="es_trade_god") ''' for x in range(len(task.hs.hist_shaped[0])): print(task.hs.hist_shaped[1][x][3],task.hs.hist_shaped[0][x][3]) '''
def poloTrader(self): try: trade_df = pd.read_json("./live_hist/json_hist.json") except: trade_df = pd.DataFrame() end_prices = {} active = self.get_one_bar_input_2d() self.load_net() sub = Substrate(self.in_shapes, self.out_shapes) network = ESNetwork(sub, self.cppn, self.params) net = network.create_phenotype_network_nd() net.reset() signals = [] net.reset() for n in range(1, self.hist_depth+1): out = net.activate(active[self.hist_depth-n]) for x in range(len(out)): signals.append(out[x]) #rng = iter(shuffle(rng)) sorted_shit = np.argsort(signals)[::-1] self.reset_tickers() sym = "" for x in sorted_shit: sym = self.hs.coin_dict[x] #print(out[x]) try: if(out[x] < -.5): p = self.get_price('BTC_'+sym) print("selling: ", sym) self.folio.sell_coin(sym, p) elif(out[x] > .5): p = self.get_price('BTC_'+sym) print("buying: ", sym) self.folio.buy_coin(sym, p) except: print("error buying or selling") #skip the hold case because we just dont buy or sell hehe end_prices[sym] = self.hs.currentHists[sym]["close"].iloc[-1] #self.trade_hist["date"] = self.hs.currentHists[sym]["date"].iloc[-1] self.trade_hist["date"] = time.time() self.trade_hist["portfoliovalue"] = self.folio.get_total_btc_value_no_sell(end_prices)[0] self.trade_hist["portfolio"] = self.folio.ledger self.trade_hist["percentchange"] = ((self.trade_hist["portfoliovalue"] - self.folio.start)/self.folio.start)*100 print(self.trade_hist) print(self.folio.ledger) self.db.insert(self.trade_hist) self.trade_hist = {} ''' if(self.trade_hist["portfoliovalue"] > self.folio.start *1.1): self.folio.start = self.folio.get_total_btc_value(end_prices)[0] ''' if datetime.now() >= self.end_ts: port_info = self.folio.get_total_btc_value(end_prices) print("total val: ", port_info[0], "btc balance: ", port_info[1]) return else: print(self.get_current_balance()) for t in range(2): time.sleep(self.ticker_len/2) #print(self.folio.ledger) self.refresh_data self.poloTrader()