def evaluate(self, network, es, rand_start, g, verbose=False):
        portfolio_start = .05
        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):
                for x in np.random.permutation(self.outputs):
                    sym = self.hs.coin_dict[x]
                    active = self.get_single_symbol_epoch(z, x)
                    network.reset()
                    for n in range(1, self.hd+1):
                        out = network.activate(active[self.hd-n])
                    #print(len(out))

                    #print(out[x])
                    #try:
                    if(out[0] < -.5):
                        #print("selling")
                        portfolio.sell_coin(sym, self.hs.currentHists[sym]['close'][z])
                        #print("bought ", sym)
                    elif(out[0] > .5):
                        #print("buying")
                        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
                    end_prices[sym] = self.hs.currentHists[sym]['close'][self.epoch_len+rand_start]
            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 evaluate(self, network, verbose=False):
        portfolio = CryptoFolio(1, self.hs.coin_dict)
        active = []
        results = {}
        end_prices = {}
        if not isinstance(network, NeuralNetwork):
            network = NeuralNetwork(network)
        rand_start = randint(0, self.hs.hist_full_size - 89) #get random start point with a week of padding from end
        for z in range(rand_start, rand_start+89):
            '''
            if(z == 0):
                old_idx = 1
            else:
                old_idx = z * 5
            new_idx = (z + 1) * 5
            '''
            active = self.get_one_bar_input_2d(z)
            results[z] = network.feed(active)

        for i in range(rand_start, rand_start+89):
            out = results[i]
            for x in range(0, self.outputs):
                sym = self.hs.coin_dict[x]
                if(out[x] == 1.0):
                    portfolio.buy_coin(sym, self.hs.currentHists[sym][x]['close'])
                elif(out[x] == 0.0):
                    portfolio.sell_coin(sym, self.hs.currentHists[sym][x]['close'])
        for y in range(len(out)):
            end_prices[self.hs.coin_dict[y]] = self.hs.hist_shaped[y][89][2]
        result_val = portfolio.get_total_btc_value(end_prices)
        print(results)
        if(results > self.highest_returns):
            self.highest_returns = results
        return results
Exemplo n.º 3
0
 def evaluate_champ(self, network, es, rand_start, g, verbose=False):
     portfolio_start = 10000.0
     portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict, "USDT")
     end_prices = {}
     buys = 0
     sells = 0
     with open("./trade_hists/" + str(g.key) + "_hist.txt", "w") as ft:
         ft.write('date,current_balance \n')
         for z_minus in range(0, self.epoch_len - 1):
             #TODO add comments to clarify all the 
             #shit im doing here
             z = rand_start - z_minus
             active = self.get_one_epoch_input(z)
             buy_signals = []
             buy_syms = []
             sell_syms = []
             sell_signals = []
             network.reset()
             #activate net fully
             for n in range(1, self.hd+1):
                 out = network.activate(active[self.hd-n])
             for x in range(len(out)):
                 sym = self.hs.coin_dict[x]
                 end_prices[sym] = self.hs.currentHists[sym]['close_price'][z]
                 if(out[x] > .5):
                     buy_signals.append(out[x])
                     buy_syms.append(sym)
                 if(out[x] < -.5):
                     sell_signals.append(out[x])
                     sell_syms.append(sym)
             sorted_buys = np.argsort(buy_signals)[::-1]
             sorted_sells = np.argsort(sell_signals)
             #print(len(sorted_shit), len(key_list))
             for x in sorted_sells:
                 sym = sell_syms[x]
                 portfolio.sell_coin(sym, self.hs.currentHists[sym]['close_price'][z])
             for x in sorted_buys:
                 sym = buy_syms[x]
                 portfolio.buy_coin(sym, self.hs.currentHists[sym]['close_price'][z])
             for x in range(len(out)):
                 sym = self.hs.coin_dict[x]
                 if (out[x] > .5):
                     portfolio.buy_coin(sym, self.hs.currentHists[sym]['open_price'][z])
                 if (out[x] < -.5):
                     portfolio.sell_coin(sym, self.hs.currentHists[sym]['open_price'][z])
                 end_prices[sym] = self.hs.currentHists[sym]['open_price'][z]
             ft.write(self.hs.currentHists[sym]['begins_at'][z] + ",")
             ft.write(str(portfolio.get_total_btc_value_no_sell(end_prices)[0])+ " \n")
         result_val = portfolio.get_total_btc_value(end_prices)
         print("genome id ", g.key, " : ")
         print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2])
         if result_val[1] == 0:
             ft = .7
         else:
             ft = result_val[0]
         return ft
Exemplo n.º 4
0
 def evaluate(self, builder, rand_start, g, verbose=False):
     portfolio_start = 1.0
     portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict, "USDT")
     end_prices = {}
     phenotypes = {}
     buys = 0
     sells = 0
     last_val = portfolio_start
     ft = 0.0
     for z_minus in range(0, self.epoch_len):
         for x in range(self.num_syms):
             #TODO add comments to clarify all the 
             #shit im doing here
             sym = self.hs.coin_dict[x]
             z = rand_start - z_minus
             pos_size = portfolio.ledger[sym]
             active = self.get_single_symbol_epoch_recurrent_with_position_size(z, x, pos_size)
             #print(active)
             if(z_minus == 0 or (z_minus + 1) % 8 == 0):
                 self.reset_substrate(active[0])
                 builder.substrate = self.substrate
                 phenotypes[sym] = builder.create_phenotype_network_nd()
                 network = phenotypes[sym]
             network.reset()
             for n in range(1, self.hd+1):
                 network.activate([active[self.hd-n]])
             out = network.activate([active[0]])
             #print(out[0])
             if(out[0] < 0.5):
                 #print("selling")
                 portfolio.sell_coin(sym, self.hs.currentHists[sym]['close'][z])
                 #print("bought ", sym)
             elif(out[0] > 0.5):
                 #print("buying")
                 portfolio.buy_coin(sym, self.hs.currentHists[sym]['close'][z])
             #rng = iter(shuffle(rng))
             end_prices[sym] = self.hs.currentHists[sym]['close'][z]
             
             bal_now = portfolio.get_total_btc_value_no_sell(end_prices)[0] 
             ft += bal_now - last_val
             last_val = bal_now
             
     result_val = portfolio.get_total_btc_value(end_prices)
     print(g.key, " : ")
     print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2])
     '''
     if result_val[0] == portfolio_start:
         ft = -.2
     '''
     return ft
Exemplo n.º 5
0
 def evaluate(self, network, es, rand_start, g, verbose=False):
     portfolio_start = 1.0
     portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict, "USDT")
     end_prices = {}
     buys = 0
     sells = 0
     last_val = portfolio_start
     ft = 0.0
     for z_minus in range(0, self.epoch_len):
         #TODO add comments to clarify all the
         #shit im doing here
         z = rand_start - z_minus
         active = self.get_one_epoch_input(z)
         buy_signals = []
         buy_syms = []
         sell_syms = []
         sell_signals = []
         network.reset()
         for n in range(1, self.hd + 1):
             network.activate(active[self.hd - n])
         out = network.activate(active[0])
         for x in range(len(out)):
             sym = self.hs.coin_dict[x]
             end_prices[sym] = self.hs.currentHists[sym]['close'][z]
             if (out[x] > .5):
                 buy_signals.append(out[x])
                 buy_syms.append(sym)
             if (out[x] < -.5):
                 sell_signals.append(out[x])
                 sell_syms.append(sym)
         #rng = iter(shuffle(rng))
         sorted_buys = np.argsort(buy_signals)[::-1]
         sorted_sells = np.argsort(sell_signals)
         #print(len(sorted_shit), len(key_list))
         for x in sorted_sells:
             sym = sell_syms[x]
             portfolio.sell_coin(sym, self.hs.currentHists[sym]['close'][z])
         for x in sorted_buys:
             sym = buy_syms[x]
             portfolio.buy_coin(sym, self.hs.currentHists[sym]['close'][z])
         bal_now = portfolio.get_total_btc_value_no_sell(end_prices)[0]
         ft += bal_now - last_val
         last_val = bal_now
     result_val = portfolio.get_total_btc_value(end_prices)
     print(g.key, " : ")
     print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2])
     if result_val[0] == portfolio_start:
         ft = ft * .9
     return ft
Exemplo n.º 6
0
 def evaluate(self, network, es, rand_start, g, verbose=False):
     portfolio_start = 1.0
     portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict, "USDT")
     end_prices = {}
     buys = 0
     sells = 0
     for z in range(rand_start, rand_start + self.epoch_len):
         #TODO add comments to clarify all the
         #shit im doing here
         active = self.get_one_epoch_input(z)
         buy_signals = []
         buy_syms = []
         sell_syms = []
         sell_signals = []
         network.reset()
         for n in range(1, self.hd + 1):
             network.activate(active[self.hd - n])
         out = network.activate(active[0])
         for x in range(len(out)):
             if (z > (self.epoch_len + rand_start) - 2):
                 sym = self.hs.coin_dict[x]
                 end_prices[sym] = self.hs.currentHists[sym]['close'][
                     self.epoch_len + rand_start]
             if (out[x] > .5):
                 buy_signals.append(out[x])
                 buy_syms.append(self.hs.coin_dict[x])
             if (out[x] < -.5):
                 sell_signals.append(out[x])
                 sell_syms.append(self.hs.coin_dict[x])
         #rng = iter(shuffle(rng))
         sorted_buys = np.argsort(buy_signals)[::-1]
         sorted_sells = np.argsort(sell_signals)
         #print(len(sorted_shit), len(key_list))
         for x in sorted_sells:
             sym = sell_syms[x]
             portfolio.sell_coin(sym, self.hs.currentHists[sym]['close'][z])
         for x in sorted_buys:
             sym = buy_syms[x]
             portfolio.target_amount = .1 + (out[x] * .1)
             portfolio.buy_coin(sym, self.hs.currentHists[sym]['close'][z])
     result_val = portfolio.get_total_btc_value(end_prices)
     print(g.key, " : ")
     print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2])
     if result_val[1] == 0:
         ft = .7
     else:
         ft = result_val[0]
     return ft
 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
Exemplo n.º 8
0
 def evaluate(self, builder, rand_start, g, verbose=False):
     portfolio_start = 1000.0
     end_prices = {}
     phenotypes = {}
     balances = []
     buys = 0
     sells = 0
     last_val = portfolio_start
     ft = 0.0
     for x in range(self.num_syms):
         portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict, "USD")
         portfolio.target_amount = .25
         sym = self.hs.coin_dict[x]
         for z_minus in range(0, self.epoch_len):
             z = rand_start - z_minus
             pos_size = portfolio.ledger[sym]
             active = self.get_single_symbol_epoch_recurrent_with_position_size(z, x, pos_size)
             #print(active)
             if(z_minus == 0 or (z_minus + 1) % 8 == 0):
                 self.reset_substrate(active[0])
                 builder.substrate = self.substrate
                 phenotypes[sym] = builder.create_phenotype_network_nd()
                 network = phenotypes[sym]
             network.reset()
             
             for n in range(1, self.hd+1):
                 network.activate([active[self.hd-n]])
             
             out = network.activate([active[0]])
             if(out[0] < 0.0):
                 portfolio.sell_coin(sym, self.hs.currentHists[sym]['close'][z])
                 #print("bought ", sym)
             elif(out[0] > 0.0):
                 did_buy = portfolio.buy_coin(sym, self.hs.currentHists[sym]['close'][z])
             #rng = iter(shuffle(rng))
             end_prices[sym] = self.hs.currentHists[sym]['close'][z]
             bal_now = portfolio.get_total_btc_value_no_sell(end_prices)[0]
             if bal_now == last_val:
                 ft += -.01
             else: 
                 ft += bal_now - last_val
             last_val = bal_now
     print(g.key, " : ")
     print(ft)
     #print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2])
     return ft
Exemplo n.º 9
0
 def evaluate(self, network, es, rand_start, g, verbose=False):
     portfolio_start = 10000.0
     portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict, "USDT")
     end_prices = {}
     buys = 0
     sells = 0
     loss_factor = 0
     ft = 0
     last_val = portfolio_start
     for z_minus in range(0, self.epoch_len - 1):
         #TODO add comments to clarify all the 
         #shit im doing here
         z = rand_start - z_minus
         active = self.get_one_epoch_input(z)
         buy_signals = []
         buy_syms = []
         sell_syms = []
         sell_signals = []
         network.reset()
         for n in range(1, self.hd+1):
             out = network.activate(active[self.hd-n])
         if (out[0] > .5):
             #print("buying " + sym)
             portfolio.buy_coin("SPXL", self.hs.currentHists["SPXL"]['close_price'][z])
         if (out[0] < -.5):
             #print("selling " + sym)
             portfolio.buy_coin("SPXS", self.hs.currentHists["SPXS"]['close_price'][z])
         if (out[0] > -.5 and out[0] < .5):
             portfolio.sell_coin("SPXL", self.hs.currentHists["SPXL"]['close_price'][z])
             portfolio.sell_coin("SPXS", self.hs.currentHists["SPXS"]['close_price'][z])
         end_prices["SPXS"] = self.hs.currentHists["SPXS"]['close_price'][z]
         end_prices["SPXL"] = self.hs.currentHists["SPXL"]['close_price'][z]
         bal_now = portfolio.get_total_btc_value_no_sell(end_prices)[0]
         ft += bal_now - last_val 
         last_val = bal_now
     result_val = portfolio.get_total_btc_value(end_prices)
     print("genome id ", g.key, " : ")
     print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2])
     '''
     if result_val[1] == 0:
         ft = .5
     else:
         ft = result_val[0] - loss_factor
     '''
     return ft
Exemplo n.º 10
0
 def evaluate_champ(self, builder, rand_start, g, verbose=False):
     portfolio_start = 1000.0
     portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict, "USDT")
     end_prices = {}
     phenotypes = {}
     buys = 0
     sells = 0
     with open("./trade_hists/kraken/" + str(g.key) + "_hist.txt",
               "w") as ft:
         ft.write('date,current_balance \n')
         for z_minus in range(0, self.epoch_len - 1):
             for x in range(self.num_syms):
                 z = rand_start - z_minus
                 sym = self.hs.coin_dict[x]
                 z = rand_start - z_minus
                 pos_size = portfolio.ledger[sym]
                 active = self.get_single_symbol_epoch_recurrent_with_position_size(
                     z, x, pos_size)
                 if (z_minus == 0 or (z_minus + 1) % 8 == 0):
                     self.reset_substrate(active[0])
                     builder.substrate = self.substrate
                     phenotypes[sym] = builder.create_phenotype_network_nd()
                     network = phenotypes[sym]
                 network.reset()
                 for n in range(1, self.hd + 1):
                     network.activate([active[self.hd - n]])
                 out = network.activate([active[0]])
                 end_prices[sym] = self.hs.currentHists[sym]['close'][z]
                 if (out[0] < .5 or (z_minus + 1) % 16 == 0):
                     portfolio.sell_coin(
                         sym, self.hs.currentHists[sym]['close'][z])
                 else:
                     portfolio.buy_coin(
                         sym, self.hs.currentHists[sym]['close'][z])
                 ft.write(str(self.hs.currentHists[sym]['time'][z]) + ",")
                 ft.write(
                     str(
                         portfolio.get_total_btc_value_no_sell(end_prices)
                         [0]) + " \n")
         result_val = portfolio.get_total_btc_value(end_prices)
         print("genome id ", g.key, " : ")
         print(result_val[0], "buys: ", result_val[1], "sells: ",
               result_val[2])
         ft = result_val[0]
         return ft
 def evaluate(self, network, es, rand_start, g, verbose=False):
     portfolio_start = .05
     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)
             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]
             #print(sorted_shit, len(sorted_shit))
             #print(len(sorted_shit), len(key_list))
             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):
                     end_prices[sym] = self.hs.currentHists[sym]['close'][
                         self.epoch_len + rand_start]
         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
Exemplo n.º 12
0
 def evaluate_champ(self, network, es, rand_start, g, verbose=False):
     portfolio_start = 10000.0
     portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict, "USDT")
     end_prices = {}
     buys = 0
     sells = 0
     with open("./trade_hists/" + str(g.key) + "_hist.txt", "w") as ft:
         ft.write('date,current_balance \n')
         for z in range(rand_start, rand_start + self.epoch_len):
             #TODO add comments to clarify all the
             #shit im doing here
             active = self.get_net_input(z)
             buy_signals = []
             buy_syms = []
             sell_syms = []
             sell_signals = []
             network.reset()
             for n in range(1, self.hd + 1):
                 network.activate(active[self.hd - n])
             out = network.activate(active[0])
             for x in range(len(out)):
                 sym = self.hs.coin_dict[x]
                 if (out[x] > .5):
                     portfolio.buy_coin(
                         sym, self.hs.currentHists[sym]['open_price'][z])
                 if (out[x] < -.5):
                     portfolio.sell_coin(
                         sym, self.hs.currentHists[sym]['open_price'][z])
                 end_prices[sym] = self.hs.currentHists[sym]['open_price'][
                     self.epoch_len + rand_start]
             ft.write(self.hs.currentHists[sym]['begins_at'][z] + ",")
             ft.write(
                 str(portfolio.get_total_btc_value_no_sell(end_prices)[0]) +
                 " \n")
         result_val = portfolio.get_total_btc_value(end_prices)
         print("genome id ", g.key, " : ")
         print(result_val[0], "buys: ", result_val[1], "sells: ",
               result_val[2])
         if result_val[1] == 0:
             ft = .7
         else:
             ft = result_val[0]
         return ft
Exemplo n.º 13
0
 def evaluate_champ(self, builder, rand_start, g, champ_num, verbose=False):
     end_prices = {}
     phenotypes = {}
     buys = 0
     sells = 0
     pathlib.Path(str(pathlib.Path(__file__).parent.absolute()) + '/trade_hists/binance_per_symbol/champ_' + str(champ_num)).mkdir(exist_ok=True)
     balances = [] 
     for x in range(self.num_syms):
         sym = self.hs.coin_dict[x]
         portfolio = CryptoFolio(1000.0, self.hs.coin_dict, "USD")
         with open("./trade_hists/binance_per_symbol/champ_" + str(champ_num) + "/" + sym + "_hist.txt", "w") as ft:
             ft.write('0,1\n')
             for z_minus in range(0, self.epoch_len - 1):
                 z = rand_start - z_minus
                 pos_size = portfolio.ledger[sym]
                 active = self.get_single_symbol_epoch_recurrent_with_position_size(z, x, pos_size)
                 if(z_minus == 0 or (z_minus + 1) % 8 == 0):
                     self.reset_substrate(active[0])
                     builder.substrate = self.substrate
                     phenotypes[sym] = builder.create_phenotype_network_nd()
                     network = phenotypes[sym]
                 network.reset()
                 
                 for n in range(1, self.hd+1):
                     network.activate([active[self.hd-n]])
                 out = network.activate([active[0]])
                 end_prices[sym] = self.hs.currentHists[sym]['close'][z]
                 if(out[0] < 0.0):
                     portfolio.sell_coin(sym, self.hs.currentHists[sym]['close'][z])
                 if(out[0] > 0.0):
                     portfolio.buy_coin(sym, self.hs.currentHists[sym]['close'][z])
                 balance = portfolio.get_total_btc_value_no_sell(end_prices)[0]
                 ft.write(str(self.hs.currentHists[sym]['date'][z]) + ",")
                 ft.write(str(balance)+ " \n")
         result_val = portfolio.get_total_btc_value(end_prices)
         balances.append(result_val[0])
         print("genome id ", g.key, " : ")
         print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2])
     return np.asarray(balances, dtype=np.float32).mean()
 def evaluate(self, network, es, rand_start, verbose=False):
     portfolio = CryptoFolio(.05, self.hs.coin_dict)
     end_prices = {}
     buys = 0
     sells = 0 
     for z in range(rand_start, rand_start+self.epoch_len):
         '''
         if(z == 0):
             old_idx = 1
         else:
             old_idx = z * 5
         new_idx = (z + 1) * 5
         '''
         active = self.get_one_bar_input_2d(z)
         network.reset()
         for n in range(es.activations):
             out = network.activate(active)
         #print(len(out))
         rng = len(out)
         #rng = iter(shuffle(rng))
         for x in np.random.permutation(rng):
             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])
                 elif(out[x] > .5):
                     #print("buying")
                     portfolio.buy_coin(sym, self.hs.currentHists[sym]['close'][z])
             except:
                 print('error', sym)
             #skip the hold case because we just dont buy or sell hehe
             end_prices[sym] = self.hs.hist_shaped[x][self.epoch_len][2]
     result_val = portfolio.get_total_btc_value(end_prices)
     print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2])
     return result_val[0]
 def evaluate(self, network, es, rand_start, g, p_name):
     portfolio_start = 1.0
     portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict)
     end_prices = {}
     buys = 0
     sells = 0
     th = []
     with open('./champs_hist2/trade_hist'+p_name + '.txt', 'w') as ft:
         ft.write('date,symbol,type,amnt,price,current_balance \n')
         for z in range(self.hd, self.hs.hist_full_size -1):
             for x in np.random.permutation(self.outputs):
                 sym = self.hs.coin_dict[x]
                 active = self.get_single_symbol_epoch(z, x)
                 network.reset()
                 for n in range(1, self.hd+1):
                     out = network.activate(active[self.hd-n])
                 end_prices[sym] = self.hs.currentHists[sym]['close'][self.hs.hist_full_size-1]
                 #rng = iter(shuffle(rng))
                     #print(out[x])
                     #try:
                 if(out[0] < -.5):
                     #print("selling")
                     did_sell = portfolio.sell_coin(sym, self.hs.currentHists[sym]['close'][z])
                     if did_sell:
                         ft.write(str(self.hs.currentHists[sym]['date'][z]) + ",")
                         ft.write(sym +",")
                         ft.write('sell,')
                         ft.write(str(portfolio.ledger[sym])+",")
                         ft.write(str(self.hs.currentHists[sym]['close'][z])+",")
                         ft.write(str(portfolio.get_total_btc_value_no_sell(end_prices)[0])+ " \n")
                     #print("bought ", sym)
                 elif(out[0] > .5):
                     did_buy = portfolio.buy_coin(sym, self.hs.currentHists[sym]['close'][z])
                     if did_buy:
                         ft.write(str(self.hs.currentHists[sym]['date'][z]) + ",")
                         ft.write(sym +",")
                         ft.write('buy,')
                         ft.write(str(portfolio.target_amount)+",")
                         ft.write(str(self.hs.currentHists[sym]['close'][z])+",")
                         ft.write(str(portfolio.get_total_btc_value_no_sell(end_prices)[0])+ " \n")
                     #print("sold ", sym)
                 #skip the hold case because we just dont buy or sell heh
     result_val = portfolio.get_total_btc_value(end_prices)
     print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2], p_name)
     ft = result_val[0]
     return ft
Exemplo n.º 16
0
class PaperTrader:
    params = {
        "initial_depth": 3,
        "max_depth": 4,
        "variance_threshold": 0.00013,
        "band_threshold": 0.00013,
        "iteration_level": 3,
        "division_threshold": 0.00013,
        "max_weight": 8.0,
        "activation": "tanh"
    }

    # Config for CPPN.
    config = neat.config.Config(neat.genome.DefaultGenome,
                                neat.reproduction.DefaultReproduction,
                                neat.species.DefaultSpeciesSet,
                                neat.stagnation.DefaultStagnation,
                                'config_trader')
    in_shapes = []
    out_shapes = []

    def __init__(self, ticker_len, start_amount, histdepth, base_sym):
        self.trade_hist = {}
        self.polo = Poloniex()
        self.hd = histdepth
        self.ticker_len = ticker_len
        self.end_ts = datetime.now() + timedelta(seconds=(ticker_len * 24))
        self.start_amount = start_amount
        self.hs = HistWorker()
        self.hs.combine_live_usd_frames()
        print(self.hs.currentHists.keys())
        self.end_idx = len(self.hs.hist_shaped[0]) - 1
        self.but_target = .1
        self.inputs = self.hs.hist_shaped.shape[0] * (
            self.hs.hist_shaped[0].shape[1])
        self.outputs = self.hs.hist_shaped.shape[0]
        self.folio = CryptoFolio(start_amount,
                                 list(self.hs.currentHists.keys()), base_sym)
        self.base_sym = base_sym
        sign = 1
        for ix in range(1, self.outputs + 1):
            sign = sign * -1
            self.out_shapes.append((0.0 - (sign * .005 * ix), 0.0, -1.0))
            for ix2 in range(1, (self.inputs // self.outputs) + 1):
                self.in_shapes.append(
                    (0.0 + (sign * .01 * ix2), 0.0 - (sign * .01 * ix2), 0.0))
        self.subStrate = Substrate(self.in_shapes, self.out_shapes)
        self.load_net()
        print(self.hs.coin_dict)
        self.poloTrader()

    def refresh_data(self):
        try:
            self.hs.pull_polo_usd_live(21)
            self.hs.combine_live_usd_frames()
            self.end_idx = len(self.hs.hist_shaped[0]) - 1
        except Exception as e:
            print(e)
            time.sleep(360)
            self.refresh_data()
        return

    def load_net(self):
        champ_file = open("./champ_data/latest_greatest.pkl", 'rb')
        g = pickle.load(champ_file)
        #file.close()
        the_cppn = neat.nn.FeedForwardNetwork.create(g, self.config)
        self.cppn = the_cppn

    def make_shapes(self):
        self.in_shapes = []
        self.out_shapes = []
        sign = 1
        for ix in range(1, self.outputs + 1):
            sign = sign * -1
            self.out_shapes.append((0.0 - (sign * .005 * ix), -1.0, -1.0))
            for ix2 in range(1, (self.inputs // self.outputs) + 1):
                self.in_shapes.append(
                    (0.0 + (sign * .01 * ix2), 0.0 - (sign * .01 * ix2), 1.0))

    def reset_tickers(self):
        try:
            self.tickers = self.polo.returnTicker()
        except Exception as e:
            time.sleep(360)
            self.reset_tickers()
        return

    def get_price(self, coin):
        return self.tickers[coin]['last']

    def get_current_balance(self):
        #self.refresh_data()
        self.reset_tickers()
        c_prices = {}
        for s in self.hs.currentHists.keys():
            if s != self.base_sym:
                c_prices[s] = self.get_price(self.base_sym + "_" + s)
        return self.folio.get_total_btc_value_no_sell(c_prices)

    def get_one_bar_input_2d(self):
        master_active = []
        try:
            for x in range(0, self.hd):
                active = []
                #print(self.outputs)
                for y in range(0, self.outputs):
                    sym_data = self.hs.hist_shaped[y][self.end_idx - x]
                    #print(len(sym_data))
                    active += sym_data.tolist()
                master_active.append(active)
        except:
            self.refresh_data()
            self.get_one_bar_input_2d()
        #print(active)
        return master_active

    def poloTrader(self):
        try:
            trade_df = pd.read_json("./live_hist/json_hist.json")
        except Exception as e:
            trade_df = pd.DataFrame()
        end_prices = {}
        active = self.get_one_bar_input_2d()
        self.load_net()
        sub = Substrate(self.in_shapes, self.out_shapes)
        net = ESNetwork(sub, self.cppn, self.params, self.hd)
        network = net.create_phenotype_network_nd('paper_net.png')
        sell_syms = []
        buy_syms = []
        buy_signals = []
        sell_signals = []
        for n in range(1, self.hd):
            network.activate(active[self.hd - n])
        out = network.activate(active[0])
        self.reset_tickers()
        for x in range(len(out)):
            sym = self.hs.coin_dict[x]
            end_prices[sym] = self.get_price(self.base_sym + "_" + sym)
            if (out[x] > .5):
                buy_signals.append(out[x])
                buy_syms.append(sym)
            if (out[x] < -.5):
                sell_signals.append(out[x])
                sell_syms.append(sym)
        #rng = iter(shuffle(rng))
        sorted_buys = np.argsort(buy_signals)[::-1]
        sorted_sells = np.argsort(sell_signals)
        for x in sorted_sells:
            try:
                sym = sell_syms[x]
                p = end_prices[sym]
                print("selling: ", sym)
                self.folio.sell_coin(sym, p)
            except Exception as e:
                print("error placing order")
        for x in sorted_buys:
            try:
                sym = buy_syms[x]
                p = end_prices[sym]
                print("buying: ", sym)
                self.folio.buy_coin(sym, p)
            except Exception as e:
                print("error placing order")
        '''
        self.trade_hist["date"] = datetime.now()
        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
        trade_df.append(self.trade_hist)
        trade_df.to_json("./live_hist/json_hist.json")
        
        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):
                p_vals = self.get_current_balance()
                print("current value: ", p_vals[0], "current holdings: ",
                      p_vals[1])
                time.sleep(self.ticker_len / 2)
        self.refresh_data()
        self.poloTrader()
class PaperTrader:
    params = {"initial_depth": 3,
            "max_depth": 4,
            "variance_threshold": 0.00013,
            "band_threshold": 0.00013,
            "iteration_level": 3,
            "division_threshold": 0.00013,
            "max_weight": 3.0,
            "activation": "tanh"}
    in_shapes = []
    out_shapes = []
    db = TinyDB('./live_hist/memories.json')
    # Config for CPPN.
    config = neat.config.Config(neat.genome.DefaultGenome, neat.reproduction.DefaultReproduction,
                                neat.species.DefaultSpeciesSet, neat.stagnation.DefaultStagnation,
                                'config_trader')
    def __init__(self, ticker_len, start_amount, histdepth):
        self.trade_hist = {}
        self.polo = Poloniex()
        self.hist_depth = histdepth
        self.ticker_len = ticker_len
        self.end_ts = datetime.now()+timedelta(seconds=(ticker_len*89))
        self.start_amount = start_amount
        self.hs = HistWorker()
        self.refresh_data()
        self.folio = CryptoFolio(start_amount, list(self.hs.currentHists.keys()))
        self.inputs = self.hs.hist_shaped.shape[0]*(self.hs.hist_shaped[0].shape[1])
        self.outputs = self.hs.hist_shaped.shape[0]
        self.leaf_names = []
        #num_leafs = 2**(len(self.node_names)-1)//2
        self.tree = nDimensionGoldenTree((0.0, 0.0, 0.0), 1.0, 1)
        self.tree.divide_childrens()
        self.set_substrate()
        self.set_leaf_names()
        self.epoch_len = histdepth
        self.load_net()
        print(self.hs.coin_dict)
        self.db.purge()
        self.poloTrader()
        

    def set_leaf_names(self):
        for l in range(len(self.in_shapes[0])):
            self.leaf_names.append('leaf_one_'+str(l))
            self.leaf_names.append('leaf_two_'+str(l))
        #self.leaf_names.append('bias')
    def set_substrate(self):
        sign = 1
        x_increment = 1.0 / self.outputs
        y_increment = 1.0 / len(self.hs.hist_shaped[0][0])
        for ix in range(self.outputs):
            self.out_shapes.append((1.0-(ix*x_increment), 0.0, -1.0))
            for ix2 in range(self.inputs//self.outputs):
                if(ix2 >= len(self.tree.cs)-1):
                    treex = ix2 - len(self.tree.cs)-1
                else:
                    treex = ix2
                center = self.tree.cs[treex]
                self.in_shapes.append((center.coord[0]+(ix*x_increment), center.coord[1] - (ix2*y_increment), center.coord[2]+.5))
        self.subStrate = Substrate(self.in_shapes, self.out_shapes)

    def set_portfolio_keys(self, folio):
        for k in self.hs.currentHists.keys():
            folio.ledger[k] = 0

    def refresh_data(self):
        self.hs.pull_polo_live(21)
        self.hs.combine_live_frames(89)

    def load_net(self):
        #file = open("./champ_gens/thot-checkpoint-13",'rb')
        g = neat.Checkpointer.restore_checkpoint("./binance_champs_2/tradegod-checkpoint-48")
        '''
        best_fit = 0.0
        for gx in g.population:
            if g.population[gx].fitness != None:
                if g.population[gx].fitness > best_fit:
                    bestg = g.population[gx]
        g = bestg
        '''
        #file.close()
        g = g.population[5910]
        [the_cppn] = create_cppn(g, self.config, self.leaf_names, ['cppn_out'])
        self.cppn = the_cppn

    def make_shapes(self):
        self.in_shapes = []
        self.out_shapes = []
        sign = 1
        for ix in range(1,self.outputs+1):
            sign = sign *-1
            self.out_shapes.append((0.0-(sign*.005*ix), -1.0, -1.0))
            for ix2 in range(1,(self.inputs//self.outputs)+1):
                self.in_shapes.append((0.0+(sign*.01*ix2), 0.0-(sign*.01*ix2), 1.0))

    def reset_tickers(self):
        try:
            self.tickers = self.polo.returnTicker()
        except:
            time.sleep(360)
            self.reset_tickers()
        return
    def get_price(self, coin):
        return self.tickers[coin]['last']

    def get_current_balance(self):
        #self.refresh_data()
        c_prices = {}
        for s in self.hs.currentHists.keys():
            if s != 'BTC':
                c_prices[s] = self.hs.currentHists[s]['close'].iloc[-1]
        return self.folio.get_total_btc_value_no_sell(c_prices)

    def get_one_bar_input_2d(self,end_idx=10):
        master_active = []
        for x in range(0, self.hist_depth):
            active = []
            #print(self.outputs)
            for y in range(0, self.outputs):
                sym_data = self.hs.hist_shaped[y][self.hist_depth-x]
                #print(len(sym_data))
                active += sym_data.tolist()
            master_active.append(active)
        #print(active)
        return master_active

    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()
Exemplo n.º 18
0
class PaperTrader:
    params = {
        "initial_depth": 0,
        "max_depth": 4,
        "variance_threshold": 0.03,
        "band_threshold": 0.3,
        "iteration_level": 1,
        "division_threshold": 0.3,
        "max_weight": 5.0,
        "activation": "tanh"
    }

    # Config for CPPN.
    config = neat.config.Config(neat.genome.DefaultGenome,
                                neat.reproduction.DefaultReproduction,
                                neat.species.DefaultSpeciesSet,
                                neat.stagnation.DefaultStagnation,
                                'config_trader')

    def __init__(self, ticker_len, start_amount):
        self.polo = Poloniex()
        self.currentHists = {}
        self.hist_shaped = {}
        self.coin_dict = {}
        self.ticker_len = ticker_len
        self.end_ts = datetime.now() + timedelta(seconds=(ticker_len * 24))
        self.start_amount = start_amount
        file = open("es_trade_god_cppn.pkl", 'rb')
        self.cppn = pickle.load(file)
        file.close()
        self.pull_polo()
        self.inputs = self.hist_shaped.shape[0] * (
            self.hist_shaped[0].shape[1] - 1)
        self.outputs = self.hist_shaped.shape[0]
        self.multiplier = self.inputs / self.outputs
        self.folio = CryptoFolio(start_amount, self.coin_dict)

    def make_shapes(self):
        self.in_shapes = []
        self.out_shapes = []
        sign = 1
        for ix in range(self.outputs):
            sign = sign * -1
            self.out_shapes.append((sign * ix, 1))
            for ix2 in range(len(self.hist_shaped[0][0]) - 1):
                self.in_shapes.append((sign * ix, (1 + ix2) * .1))

    def pull_polo(self):
        try:
            self.coins = self.polo.returnTicker()
        except:
            time.sleep(10)
            self.pull_polo()
        tickLen = '7200'
        start = datetime.today() - timedelta(1)
        start = str(int(start.timestamp()))
        ix = 0
        for coin in self.coins:
            if coin[:3] == 'BTC':
                hist = requests.get(
                    'https://poloniex.com/public?command=returnChartData&currencyPair='
                    + coin + '&start=' + start + '&end=9999999999&period=' +
                    tickLen)
                try:
                    df = pd.DataFrame(hist.json())
                    #df.rename(columns = lambda x: col_prefix+'_'+x, inplace=True)
                    as_array = np.array(df)
                    #print(len(as_array))
                    self.currentHists[coin] = df
                    self.hist_shaped[ix] = as_array
                    self.coin_dict[ix] = coin
                    ix += 1
                except:
                    print("error reading json")
        self.hist_shaped = pd.Series(self.hist_shaped)
        self.end_idx = len(self.hist_shaped[0]) - 1

    def get_current_balance(self):
        self.pull_polo()
        c_prices = {}
        for s in self.folio.ledger.keys():
            if s != 'BTC':
                c_prices[s] = self.currentHists[s]['close'][
                    len(self.currentHists[s]['close']) - 1]
        return self.folio.get_total_btc_value_no_sell(c_prices)

    def get_one_bar_input_2d(self):
        active = []
        misses = 0
        for x in range(0, self.outputs):
            try:
                sym_data = self.hist_shaped[x][self.end_idx]
                for i in range(len(sym_data)):
                    if (i != 1):
                        active.append(sym_data[i].tolist())
            except:
                self.outputs -= 1
                self.inputs -= self.multiplier
                print('error')
        #print(active)
        self.make_shapes()
        return active

    def poloTrader(self):
        end_prices = {}
        active = self.get_one_bar_input_2d()
        sub = Substrate(self.in_shapes, self.out_shapes)
        network = ESNetwork(sub, self.cppn, self.params)
        net = network.create_phenotype_network()
        net.reset()
        for n in range(network.activations):
            out = net.activate(active)
        #print(len(out))
        rng = len(out)
        #rng = iter(shuffle(rng))
        for x in np.random.permutation(rng):
            sym = self.coin_dict[x]
            #print(out[x])
            try:
                if (out[x] < -.5):
                    print("selling: ", sym)
                    self.folio.sell_coin(
                        sym, self.currentHists[sym]['close'][self.end_idx])
                elif (out[x] > .5):
                    print("buying: ", sym)
                    self.folio.buy_coin(
                        sym, self.currentHists[sym]['close'][self.end_idx])
            except:
                print('error', sym)
            #skip the hold case because we just dont buy or sell hehe
            end_prices[sym] = self.hist_shaped[x][len(self.hist_shaped[x]) -
                                                  1][2]

        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(3):
                time.sleep(self.ticker_len / 4)
                p_vals = self.get_current_balance()
                print("current value: ", p_vals[0], "current btc holdings: ",
                      p_vals[1])
                #print(self.folio.ledger)
        time.sleep(self.ticker_len / 4)
        self.pull_polo()
        self.poloTrader()
Exemplo n.º 19
0
    def evaluate(self, network, es, rand_start, g, p_name):
        portfolio_start = 1.0
        portfolio = CryptoFolio(portfolio_start,
                                list(self.hs.currentHists.keys()))
        end_prices = {}
        port_ref = portfolio_start
        with open('./champs_histd3/port_hist' + str(p_name) + '.txt',
                  'w') as pt:
            pt.write('date,current_balance \n')
            with open('./champs_histd3/trade_hist' + str(p_name) + '.txt',
                      'w') as ft:
                ft.write('date,symbol,type,amnt,price,current_balance \n')
                for z in range(34, self.hs.hist_full_size - 1):
                    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])
                        sym2 = list(self.hs.currentHists.keys())[x]
                        end_prices[sym2] = self.hs.currentHists[sym2]['close'][
                            z]
                    sorted_shit = np.argsort(signals)[::-1]
                    rebalance = portfolio_start
                    #rng = iter(shuffle(rng))
                    sym = ""
                    for x in sorted_shit:
                        sym = list(self.hs.currentHists.keys())[x]
                        #print(out[x])
                        #try:
                        if (out[x] < -.5):
                            #print("selling")
                            did_sell = portfolio.sell_coin(
                                sym, self.hs.currentHists[sym]['close'][z])

                            if did_sell:
                                ft.write(
                                    str(self.hs.currentHists[sym]['date'][z]) +
                                    ",")
                                ft.write(sym + ",")
                                ft.write('sell,')
                                ft.write(str(portfolio.ledger[sym]) + ",")
                                ft.write(
                                    str(self.hs.currentHists[sym]['close'][z])
                                    + ",")
                                ft.write(
                                    str(
                                        portfolio.get_total_btc_value_no_sell(
                                            end_prices)[0]) + " \n")

                            #print("bought ", sym)
                        elif (out[x] > .5):
                            did_buy = portfolio.buy_coin(
                                sym, self.hs.currentHists[sym]['close'][z])

                            if did_buy:
                                portfolio.target_amount = .1 + (out[x] * .1)
                                ft.write(
                                    str(self.hs.currentHists[sym]['date'][z]) +
                                    ",")
                                ft.write(sym + ",")
                                ft.write('buy,')
                                ft.write(str(portfolio.target_amount) + ",")
                                ft.write(
                                    str(self.hs.currentHists[sym]['close'][z])
                                    + ",")
                                ft.write(
                                    str(
                                        portfolio.get_total_btc_value_no_sell(
                                            end_prices)[0]) + " \n")

                        #else:
                    pt.write(str(self.hs.currentHists[sym]['date'][z]) + ",")
                    #pt.write(str(self.hs.currentHists[sym]['close'][z])+",")
                    pt.write(
                        str(
                            portfolio.get_total_btc_value_no_sell(end_prices)
                            [0]) + " \n")
                    #print("sold ", sym)
                    new_ref = portfolio.get_total_btc_value_no_sell(
                        end_prices)[0]
                    '''
                    if(new_ref > 1.10 * port_ref or new_ref < .9*port_ref):
                        port_ref = portfolio.get_total_btc_value_no_sell(end_prices)[0]
                        portfolio.start = port_ref
                        #skip the hold case because we just dont buy or sell heh
                    '''

        result_val = portfolio.get_total_btc_value(end_prices)
        print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2],
              p_name)
        fit = result_val[0]
        return fit
Exemplo n.º 20
0
class PaperTrader:
    params = {"initial_depth": 3,
            "max_depth": 4,
            "variance_threshold": 0.00013,
            "band_threshold": 0.0000013,
            "iteration_level": 3,
            "division_threshold": 0.00013,
            "max_weight": 8.0,
            "activation": "tanh"}


    # Config for CPPN.
    config = neat.config.Config(neat.genome.DefaultGenome, neat.reproduction.DefaultReproduction,
                                neat.species.DefaultSpeciesSet, neat.stagnation.DefaultStagnation,
                                'config_trader')
    def __init__(self, ticker_len, start_amount, histdepth):
        self.in_shapes = []
        self.out_shapes = []
        self.trade_hist = {}
        self.polo = Poloniex()
        self.hist_depth = histdepth
        self.ticker_len = ticker_len
        self.end_ts = datetime.now()+timedelta(seconds=(ticker_len*24))
        self.start_amount = start_amount
        self.hs = HistWorker()
        self.refresh_data()
        self.inputs = self.hs.hist_shaped.shape[0]*(self.hs.hist_shaped[0].shape[1])
        self.outputs = self.hs.hist_shaped.shape[0]
        #self.make_shapes()
        self.folio = CryptoFolio(start_amount, list(self.hs.currentHists.keys()))
        self.leaf_names = []
        for ix in range(1,self.inputs+1):
            sign = sign *-1
            self.in_shapes.append((0.0-(sign*.005*ix), -1.0, 0.0+(sign*.005*ix)))
        self.out_shapes.append((0.0, 1.0, 0.0))
        self.subStrate = Substrate(self.in_shapes, self.out_shapes)
        for l in range(len(self.in_shapes[0])):
            self.leaf_names.append('leaf_one_'+str(l))
            self.leaf_names.append('leaf_two_'+str(l))
        self.load_net()
        print(self.hs.coin_dict)
        self.poloTrader()

    def refresh_data(self):
        self.hs.pull_polo_live(12*30)
        self.hs.combine_live_frames(12*30)

    def load_net(self):
        #file = open("./champ_gens/thot-checkpoint-13",'rb')
        g = neat.Checkpointer.restore_checkpoint("./champ_gens/thot-checkpoint-25")
        best_fit = 0.0
        for gx in g.population:
            if g.population[gx].fitness != None:
                if g.population[gx].fitness > best_fit:
                    bestg = g.population[gx]
        g = bestg
        #file.close()
        [the_cppn] = create_cppn(g, self.config, self.leaf_names, ['cppn_out'])
        self.cppn = the_cppn

    def make_shapes(self):
        self.in_shapes = []
        self.out_shapes = []
        sign = 1
        for ix in range(1,self.outputs+1):
            sign = sign *-1
            self.out_shapes.append((0.0-(sign*.005*ix), -1.0, -1.0))
            for ix2 in range(1,(self.inputs//self.outputs)+1):
                self.in_shapes.append((0.0+(sign*.01*ix2), 0.0-(sign*.01*ix2), 1.0))

    def reset_tickers(self):
        try:
            self.tickers = self.polo.returnTicker()
        except:
            time.sleep(360)
            self.reset_tickers()
        return
    def get_price(self, coin):
        return self.tickers[coin]['last']

    def get_current_balance(self):
        #self.refresh_data()
        c_prices = {}
        for s in self.hs.currentHists.keys():
            if s != 'BTC':
                c_prices[s] = self.hs.currentHists[s]['close'].iloc[-1]
        return self.folio.get_total_btc_value_no_sell(c_prices)

    def get_one_bar_input_2d(self,end_idx=10):
        master_active = []
        for x in range(0, self.hist_depth):
            active = []
            #print(self.outputs)
            for y in range(0, self.outputs):
                sym_data = self.hs.hist_shaped[y][self.hist_depth-x]
                #print(len(sym_data))
                active += sym_data.tolist()
            master_active.append(active)
        #print(active)
        return master_active

    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('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):
                    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)
                else:

            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"] = datetime.now()
        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)/sefl.folio.start)*100
        trade_df.append(self.trade_hist)
        trade_df.to_json("./live_hist/json_hist.json")
        '''
        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):
                self.refresh_data
                p_vals = self.get_current_balance()
                print("current value: ", p_vals[0], "current btc holdings: ", p_vals[1])
                time.sleep(self.ticker_len/2)
                #print(self.folio.ledger)
        self.poloTrader()
Exemplo n.º 21
0
    def evaluate(self, network, es, rand_start, g, verbose=False):
        portfolio_start = 10000.0
        portfolio = CryptoFolio(portfolio_start, self.hs.coin_dict, "USDT")
        end_prices = {}
        buys = 0
        sells = 0
        loss_factor = 0
        for z_minus in range(0, self.epoch_len - 1):
            #TODO add comments to clarify all the
            #shit im doing here
            z = rand_start - z_minus
            active = self.get_net_input(z)
            buy_signals = []
            buy_syms = []
            sell_syms = []
            sell_signals = []
            network.reset()
            for n in range(0, es.activations):
                out = network.activate(active)
            for x in range(len(out)):
                if x > 0:
                    if (portfolio.get_total_btc_value_no_sell(end_prices)[0] <
                            portfolio_start):
                        loss_factor += .005
                sym = self.hs.coin_dict[x]
                if (out[x] > .5):
                    #print("buying " + sym)
                    portfolio.buy_coin(
                        sym, self.hs.currentHists[sym]['open_price'][z])
                if (out[x] < -.5):
                    #print("selling " + sym)
                    portfolio.sell_coin(
                        sym, self.hs.currentHists[sym]['open_price'][z])

                end_prices[sym] = self.hs.currentHists[sym]['open_price'][z]
                '''
                # if this is the last loop of bars
                if(z > (self.epoch_len+rand_start)-2):
                    sym = self.hs.coin_dict[x]
                    end_prices[sym] = self.hs.currentHists[sym]['open_price'][self.epoch_len+rand_start]
                if(out[x] > .5):
                    buy_signals.append(out[x])
                    buy_syms.append(self.hs.coin_dict[x])
                if(out[x] < -.5):
                    sell_signals.append(out[x])
                    sell_syms.append(self.hs.coin_dict[x])
            #rng = iter(shuffle(rng))
            sorted_buys = np.argsort(buy_signals)[::-1]
            sorted_sells = np.argsort(sell_signals)
            #print(len(sorted_shit), len(key_list))
            for x in sorted_sells:
                sym = sell_syms[x]
                portfolio.sell_coin(sym, self.hs.currentHists[sym]['open_price'][z])
            for x in sorted_buys:
                sym = buy_syms[x]
                #portfolio.target_amount = .1 + (out[x] * .1)
                portfolio.buy_coin(sym, self.hs.currentHists[sym]['open_price'][z])
            '''
        result_val = portfolio.get_total_btc_value(end_prices)
        print("genome id ", g.key, " : ")
        print(result_val[0], "buys: ", result_val[1], "sells: ", result_val[2])
        if result_val[1] == 0:
            ft = .5
        if result_val[2] > 2:
            ft = (result_val[0] * 1.02) - loss_factor
        else:
            ft = result_val[0] - loss_factor
        return ft