def __init__(self, instrument, granularity, models_loc): while True: self.market_encoder = LSTMEncoder() self.actor_critic = ActorCritic() self.encoder_to_others = EncoderToOthers() try: self.market_encoder.load_state_dict(torch.load(models_loc + 'market_encoder.pt', map_location='cpu')) self.encoder_to_others.load_state_dict(torch.load(models_loc + 'encoder_to_others.pt', map_location='cpu')) self.actor_critic.load_state_dict(torch.load(models_loc + 'actor_critic.pt', map_location='cpu')) self.market_encoder = self.market_encoder.cpu() self.encoder_to_others = self.encoder_to_others.cpu() self.actor_critic = self.actor_critic.cpu() break except Exception: print("Failed to load models") time.sleep(0.1) self.models_loc = models_loc self.instrument = instrument self.granularity = granularity self.zeus = Zeus(instrument, granularity) self.live = False self.time_states = [] self.window = networks.WINDOW self.tradeable_percentage = 1 self.trade_percent = self.tradeable_percentage / 1000
class Worker(object): def __init__(self, instrument, granularity, server_host, start, max_time): self.server = redis.Redis(server_host) self.zeus = Zeus(instrument, granularity) self.window = networks.WINDOW + 10 self.start = start self.time_states = [] self.last_time = None self.max_time = max_time self.max_sent = 100 self.n_sent = 0 self.t0 = 0 def add_bar(self, bar): if self.n_sent == self.max_sent or bar.date > self.max_time: print("experiences sent: {n_sent}, time: {time}".format(n_sent=self.n_sent, time=time.time()-self.t0)) quit() time_state = [bar.open, bar.high, bar.low, bar.close] if bar.date != self.last_time: self.time_states.append(time_state) self.last_time = bar.date if len(self.time_states) > self.window: del self.time_states[0] if len(self.time_states) == self.window: experience = Experience(time_states=self.time_states) experience = msgpack.packb(experience, use_bin_type=True) n_experiences = self.server.llen("experience") try: loc = np.random.randint(0, n_experiences) ref = self.server.lindex("experience", loc) self.server.linsert("experience", "before", ref, experience) except Exception as e: self.server.lpush("experience", experience) self.n_sent += 1 # self.time_states = [] del self.time_states[:10] def run(self): self.t0 = time.time() start = self.start while self.n_sent < self.max_sent: n_seconds = (self.window - len(self.time_states) + (self.max_sent - self.n_sent)) * 60 * 10 self.zeus.stream_range(start, start + n_seconds, self.add_bar) start += n_seconds print("experiences sent: {n_sent}, time: {time}".format(n_sent=self.n_sent, time=time.time()-self.t0))
def run(self): self.market_encoder.eval() self.encoder_to_others.eval() self.actor_critic.eval() print("fetching last", self.window, "bars...") self.zeus.stream_bars(self.window, self.add_bar) print("going live...") self.live = True self.zeus = Zeus(self.instrument, self.granularity, live=True) self.zeus.stream_live(self.add_bar)
def __init__(self, instrument, granularity, server_host, start): self.server = redis.Redis(server_host) self.zeus = Zeus(instrument, granularity) self.n_steps_future = int( self.server.get("n_steps_future").decode("utf-8")) self.window = networks.WINDOW self.start = start self.time_states = [] self.last_time = None self.n_sent = 0
def __init__(self, instrument, granularity, n_steps, models_loc): self.server = redis.Redis("localhost") self.instrument = instrument self.granularity = granularity self.zeus = Zeus(instrument, granularity) while True: # putting this while loop here because sometime the optimizer # is writing to the files and causes an exception # self.market_encoder = AttentionMarketEncoder(device='cpu') self.market_encoder = CNNEncoder().cpu() self.decoder = ClassifierDecoder().cpu() try: self.market_encoder.load_state_dict( torch.load(models_loc + 'market_encoder.pt')) self.decoder.load_state_dict( torch.load(models_loc + 'decoder.pt')) self.market_encoder = self.market_encoder.cpu() self.decoder = self.decoder.cpu() break except Exception as e: print(e) self.trajectory_steps = float( self.server.get("trajectory_steps").decode("utf-8")) self.window = networks.WINDOW self.n_steps = n_steps self.time_states = [] self.trade = None self.pos = None self.n_steps_future = 1 self.steps_since_trade = 1 self.cur_value = self.zeus.unrealized_balance() self.prev_value = self.cur_value self.n = 0 self.n_profit = 0 self.n_loss = 0 self.n_buy = 0 self.n_sell = 0 self.n_stay = 0 self.profit = 0 self.stop_loss = None self.take_profit = None
def __init__(self, instrument, granularity, server_host, start, max_time): self.server = redis.Redis(server_host) self.zeus = Zeus(instrument, granularity) self.window = networks.WINDOW + 10 self.start = start self.time_states = [] self.last_time = None self.max_time = max_time self.max_sent = 100 self.n_sent = 0 self.t0 = 0
class Worker(object): def __init__(self, instrument, granularity, server_host, start): self.server = redis.Redis(server_host) self.zeus = Zeus(instrument, granularity) self.n_steps_future = int( self.server.get("n_steps_future").decode("utf-8")) self.window = networks.WINDOW self.start = start self.time_states = [] self.last_time = None self.n_sent = 0 def add_bar(self, bar): time_state = [[[bar.open, bar.high, bar.low, bar.close]]] if bar.date != self.last_time: self.time_states.append(time_state) if len(self.time_states) > self.window + self.n_steps_future: del self.time_states[0] if bar.date != self.last_time and len( self.time_states) == self.window + self.n_steps_future: experience = Experience(self.time_states) experience = msgpack.packb(experience, use_bin_type=True) self.server.lpush("experience", experience) # self.server.lpush("experience_dev", experience) self.n_sent += 1 del self.time_states[:self.n_steps_future] # self.time_states = [] self.last_time = bar.date def run(self): t0 = time.time() start = self.start while self.n_sent < 100: n_seconds = (self.window + self.n_steps_future - len(self.time_states) + (self.n_steps_future * (100 - self.n_sent))) * 60 # n_seconds = 10000 * 60 self.zeus.stream_range(start, start + n_seconds, self.add_bar) start += n_seconds print("time: {time}".format(time=time.time() - t0))
def __init__(self, instrument, granularity, server_host, start, max_bar_time, models_loc): self.server = redis.Redis(server_host) self.zeus = Zeus(instrument, granularity, margin=1) self.models_loc = models_loc self.window = networks.WINDOW self.start = start self.max_bar_time = max_bar_time self.time_states = [] self.last_time = None self.generator = Generator().cuda() try: self.generator.load_state_dict( torch.load(self.models_loc + 'generator.pt', map_location='cuda')) except FileNotFoundError as e: print("model load failed", e) self.generator = Generator().cuda() self.generator.eval() n = 0 for name, param in self.generator.named_parameters(): # print(np.prod(param.size()), name) n += np.prod(param.size()) print("generator parameters:", n) # for name, param in self.generator.named_parameters(): # print(param.std(), name) self.n_future_samples = 100 self.n_steps_future = 10 self.plot = 0 self.step = 0 self.last_price = None self.last_action = None self.steps_since_trade = 0 self.n_correct = 0 self.n_trades = 0 self.n = 0 self.t0 = 0
class Worker(object): def __init__(self, instrument, granularity, n_steps, models_loc): self.server = redis.Redis("localhost") self.instrument = instrument self.granularity = granularity self.zeus = Zeus(instrument, granularity) while True: # putting this while loop here because sometime the optimizer # is writing to the files and causes an exception # self.market_encoder = AttentionMarketEncoder(device='cpu') self.market_encoder = CNNEncoder().cpu() self.decoder = ClassifierDecoder().cpu() try: self.market_encoder.load_state_dict( torch.load(models_loc + 'market_encoder.pt')) self.decoder.load_state_dict( torch.load(models_loc + 'decoder.pt')) self.market_encoder = self.market_encoder.cpu() self.decoder = self.decoder.cpu() break except Exception as e: print(e) self.trajectory_steps = float( self.server.get("trajectory_steps").decode("utf-8")) self.window = networks.WINDOW self.n_steps = n_steps self.time_states = [] self.trade = None self.pos = None self.n_steps_future = 1 self.steps_since_trade = 1 self.cur_value = self.zeus.unrealized_balance() self.prev_value = self.cur_value self.n = 0 self.n_profit = 0 self.n_loss = 0 self.n_buy = 0 self.n_sell = 0 self.n_stay = 0 self.profit = 0 self.stop_loss = None self.take_profit = None def add_bar(self, bar): if self.pos == "Long" and self.trade is not None: if bar.close > self.take_profit or bar.close < self.stop_loss: self.zeus.close_trade(self.trade) self.trade = None elif self.pos == "Short" and self.trade is not None: if bar.close < self.take_profit or bar.close > self.stop_loss: self.zeus.close_trade(self.trade) self.trade = None time_state = torch.Tensor([ bar.open, bar.high, bar.low, bar.close, np.log(bar.volume + 1e-1) ]).view(1, 1, -1) self.time_states.append(time_state) if len(self.time_states) > self.window: del self.time_states[0] spread = bar.spread if len(self.time_states) == self.window and ( self.steps_since_trade >= self.n_steps_future or (self.pos != "Stay" and self.trade is None)): print(bar) print(self.trade) time_states_ = torch.cat(self.time_states, dim=1).clone() mean = time_states_[:, :, :4].contiguous().view(1, -1).mean(1).view( 1, 1, 1) std = time_states_[:, :, :4].contiguous().view(1, -1).std(1).view( 1, 1, 1) time_states_[:, :, :4] = (time_states_[:, :, :4] - mean) / std spread_ = torch.Tensor([spread]).view(1, 1, 1) / std time_states_ = time_states_.transpose(0, 1) self.n_steps_future = self.trajectory_steps # confidence_interval = np.random.exponential(0.0015) confidence_interval = 0.001 print("confidence:", confidence_interval) market_encoding = self.market_encoder.forward(time_states_) probabilities = self.decoder.forward( market_encoding, spread_ * std, std, torch.Tensor([confidence_interval])) print(probabilities) # action = int(torch.argmax(probabilities.squeeze())) # print("argmax") # action = int(torch.argmax(probabilities.squeeze()[:2])) # print("argmax without hold") action = int(torch.multinomial(probabilities.squeeze(), 1)) print("sampled") # action = int(torch.multinomial(probabilities.squeeze()[:2], 1)) # print("sampled without hold") # action = np.random.randint(0, 2) # print("random") if action == 0: self.stop_loss = self.time_states[-1][0, 0, 3] * ( 1 - confidence_interval) - spread self.take_profit = self.time_states[-1][0, 0, 3] * ( 1 + confidence_interval) - spread self.n_buy += 1 if self.pos != "Long": if self.trade is not None: self.zeus.close_trade(self.trade) self.trade = self.zeus.place_trade(100, "Long") self.pos = "Long" print( "+++++++++++++++++++++++++++++++++++++++++++++++++++++++") elif action == 1: self.stop_loss = self.time_states[-1][0, 0, 3] * ( 1 + confidence_interval) - spread self.take_profit = self.time_states[-1][0, 0, 3] * ( 1 - confidence_interval) - spread self.n_sell += 1 if self.pos != "Short": if self.trade is not None: self.zeus.close_trade(self.trade) self.trade = self.zeus.place_trade(100, "Short") self.pos = "Short" print( "-------------------------------------------------------") else: self.stop_loss = None self.take_profit = None self.n_stay += 1 if self.trade is not None: self.zeus.close_trade(self.trade) self.trade = None self.pos = "Stay" print( "///////////////////////////////////////////////////////") self.cur_value = self.zeus.unrealized_balance() print(self.cur_value) self.steps_since_trade = 0 if self.prev_value < self.cur_value: self.n_profit += 1 elif self.prev_value > self.cur_value: self.n_loss += 1 self.profit += self.cur_value - self.prev_value self.n += 1 print("profit this trade:", self.cur_value - self.prev_value) print("profit:", self.profit) print("profit per trade:", self.profit / self.n) print("gain per trade:", self.profit / (self.n * 1000)) print("p loss:", self.n_loss / self.n) print("p profit:", self.n_profit / self.n) print("total trades:", self.n) print("p buy:", self.n_buy / self.n) print("p sell:", self.n_sell / self.n) print("p stay:", self.n_stay / self.n) print() self.prev_value = self.cur_value else: self.steps_since_trade += 1 def run(self): self.zeus.stream_bars(self.n_steps, self.add_bar)
for size_ in param.size(): prod *= size_ if size_ != 0 else 1 n_param += prod print("n params all networks", n_param) MEN.eval() ETO.eval() PN.eval() ACN.eval() instrument = np.random.choice(["EUR_USD", "GBP_USD", "AUD_USD", "NZD_USD"]) start = np.random.randint(1136073600, 1546300800) # instrument = "EUR_USD" # start = np.random.randint(1546214400, 1546819200) zeus = Zeus(instrument, "M1") time_states = [] steps_since_last = 0 def add_bar(bar): global steps_since_last steps_since_last += 1 time_state = [[[bar.open, bar.high, bar.low, bar.close, np.log(bar.volume + 1e-1)]]] if len(time_states) == 0 or time_state != time_states[-1]: time_states.append(time_state) else: return if len(time_states) >= networks.WINDOW and steps_since_last >= networks.WINDOW: percent_in = zeus.position_size() / (abs(zeus.position_size()) + zeus.units_available() + 1e-9)
class Worker(object): def __init__(self, instrument, granularity, models_loc): while True: self.market_encoder = LSTMEncoder() self.actor_critic = ActorCritic() self.encoder_to_others = EncoderToOthers() try: self.market_encoder.load_state_dict(torch.load(models_loc + 'market_encoder.pt', map_location='cpu')) self.encoder_to_others.load_state_dict(torch.load(models_loc + 'encoder_to_others.pt', map_location='cpu')) self.actor_critic.load_state_dict(torch.load(models_loc + 'actor_critic.pt', map_location='cpu')) self.market_encoder = self.market_encoder.cpu() self.encoder_to_others = self.encoder_to_others.cpu() self.actor_critic = self.actor_critic.cpu() break except Exception: print("Failed to load models") time.sleep(0.1) self.models_loc = models_loc self.instrument = instrument self.granularity = granularity self.zeus = Zeus(instrument, granularity) self.live = False self.time_states = [] self.window = networks.WINDOW self.tradeable_percentage = 1 self.trade_percent = self.tradeable_percentage / 1000 def add_bar(self, bar): time_state = [[[bar.open, bar.high, bar.low, bar.close, np.log(bar.volume + 1e-1)]]] if len(self.time_states) == 0 or time_state != self.time_states[-1]: self.time_states.append(time_state) else: return if len(self.time_states) == self.window + 1: del self.time_states[0] if len(self.time_states) == self.window and self.live: in_ = self.zeus.position_size() available_ = self.zeus.units_available() percent_in = (in_ / (abs(in_) + available_ + 1e-9)) / self.tradeable_percentage input_time_states = torch.Tensor(self.time_states).view(self.window, 1, networks.D_BAR).cpu() mean = input_time_states[:, 0, :4].mean() std = input_time_states[:, 0, :4].std() input_time_states[:, 0, :4] = (input_time_states[:, 0, :4] - mean) / std spread_normalized = bar.spread / std market_encoding = self.market_encoder.forward(input_time_states) market_encoding = self.encoder_to_others.forward(market_encoding, torch.Tensor([spread_normalized]), torch.Tensor([percent_in])) policy, value = self.actor_critic.forward(market_encoding) # action = torch.argmax(policy).item() action = torch.multinomial(policy, 1).item() def place_action(desired_percent): current_percent_in = percent_in * self.tradeable_percentage if desired_percent == 0 and current_percent_in != 0: self.zeus.close_units(self.zeus.position_size()) elif desired_percent > 0 and current_percent_in > 0: if desired_percent > current_percent_in: total_tradeable = abs(self.zeus.position_size()) + self.zeus.units_available() self.zeus.place_trade(int(abs(desired_percent - current_percent_in) * total_tradeable), "Long") else: total_tradeable = abs(self.zeus.position_size()) + self.zeus.units_available() self.zeus.close_units(int(abs((desired_percent - current_percent_in)) * total_tradeable)) elif desired_percent > 0 and current_percent_in <= 0: self.zeus.close_units(self.zeus.position_size()) total_tradeable = abs(self.zeus.position_size()) + self.zeus.units_available() self.zeus.place_trade(int(abs(desired_percent) * total_tradeable), "Long") elif desired_percent < 0 and current_percent_in > 0: self.zeus.close_units(self.zeus.position_size()) total_tradeable = abs(self.zeus.position_size()) + self.zeus.units_available() self.zeus.place_trade(int(abs(desired_percent) * total_tradeable), "Short") elif desired_percent < 0 and current_percent_in <= 0: if desired_percent <= current_percent_in: total_tradeable = abs(self.zeus.position_size()) + self.zeus.units_available() self.zeus.place_trade(int(abs(desired_percent - current_percent_in) * total_tradeable), "Short") else: total_tradeable = abs(self.zeus.position_size()) + self.zeus.units_available() self.zeus.close_units(int(abs((desired_percent - current_percent_in)) * total_tradeable)) change_amounts = {0:-100, 1:-50, 2:-10, 3:-5, 4:-1, 5:0, 6:1, 7:5, 8:10, 9:50, 10:100} if action in change_amounts: desired_percent_in = (percent_in * self.tradeable_percentage) + (self.trade_percent * change_amounts[action]) desired_percent_in = np.clip(desired_percent_in, -self.tradeable_percentage, self.tradeable_percentage) place_action(desired_percent_in) print("instrument", self.instrument) print("purchased:", change_amounts[action]) print("percent in:", round(percent_in, 5)) for i, policy_ in enumerate(policy.tolist()[0]): print("probability {i}: {p}".format(i=i, p=round(policy_, 5))) print("-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-") torch.cuda.empty_cache() def run(self): self.market_encoder.eval() self.encoder_to_others.eval() self.actor_critic.eval() print("fetching last", self.window, "bars...") self.zeus.stream_bars(self.window, self.add_bar) print("going live...") self.live = True self.zeus = Zeus(self.instrument, self.granularity, live=True) self.zeus.stream_live(self.add_bar)
class Worker(object): def __init__(self, instrument, granularity, n_steps, models_loc): self.server = redis.Redis("localhost") self.instrument = instrument self.granularity = granularity self.zeus = Zeus(instrument, granularity) while True: # putting this while loop here because sometime the optimizer # is writing to the files and causes an exception # self.market_encoder = AttentionMarketEncoder(device='cpu') self.market_encoder = CNNEncoder().cpu() self.decoder = Decoder().cpu() try: self.market_encoder.load_state_dict( torch.load(models_loc + 'market_encoder.pt')) self.decoder.load_state_dict( torch.load(models_loc + 'decoder.pt')) self.market_encoder = self.market_encoder.cpu() self.decoder = self.decoder.cpu() break except Exception as e: print(e) self.window = networks.WINDOW self.n_steps = n_steps self.time_states = [] self.trade = None self.pos = None self.n_steps_future = 1 self.steps_since_trade = 1 self.cur_value = self.zeus.unrealized_balance() self.prev_value = self.cur_value self.n = 0 self.n_profit = 0 self.n_loss = 0 self.n_buy = 0 self.n_sell = 0 self.n_stay = 0 self.profit = 0 def add_bar(self, bar): time_state = torch.Tensor([ bar.open, bar.high, bar.low, bar.close, np.log(bar.volume + 1e-1) ]).view(1, 1, -1) self.time_states.append(time_state) if len(self.time_states) > self.window: del self.time_states[0] spread = bar.spread if len( self.time_states ) == self.window and self.steps_since_trade >= self.n_steps_future: if self.trade is not None: self.zeus.close_trade(self.trade) time_states_ = torch.cat(self.time_states, dim=1).clone() mean = time_states_[:, :, :4].contiguous().view(1, -1).mean(1).view( 1, 1, 1) std = time_states_[:, :, :4].contiguous().view(1, -1).std(1).view( 1, 1, 1) time_states_[:, :, :4] = (time_states_[:, :, :4] - mean) / std spread_ = torch.Tensor([spread]).view(1, 1, 1) / std time_states_ = time_states_.transpose(0, 1) self.n_steps_future = np.random.randint(1, 60) self.n_steps_future = 30 print(self.n_steps_future) market_encoding = self.market_encoder.forward(time_states_) value_ = self.decoder.forward( market_encoding, spread_, torch.Tensor([self.n_steps_future]).log()) print(value_) # action = int(torch.argmax(torch.cat([value_.squeeze(), torch.Tensor([0])], dim=0))) # print("calculated with 0") action = int(torch.argmax(value_.squeeze())) print("calculated without 0") # action = np.random.randint(0, 2) # print("random") if action == 0: self.n_buy += 1 if self.pos != "Long": self.trade = self.zeus.place_trade(1000, "Long") self.pos = "Long" print( "+++++++++++++++++++++++++++++++++++++++++++++++++++++++") elif action == 1: self.n_sell += 1 if self.pos != "Short": self.trade = self.zeus.place_trade(1000, "Short") self.pos = "Short" print( "-------------------------------------------------------") else: self.n_stay += 1 self.trade = None self.pos = "Stay" print( "///////////////////////////////////////////////////////") self.cur_value = self.zeus.unrealized_balance() print(self.cur_value) self.steps_since_trade = 0 if self.prev_value < self.cur_value: self.n_profit += 1 elif self.prev_value > self.cur_value: self.n_loss += 1 self.profit += self.cur_value - self.prev_value self.n += 1 print("profit this trade:", self.cur_value - self.prev_value) print("profit:", self.profit) print("profit per trade:", self.profit / self.n) print("gain per trade:", self.profit / (self.n * 1000)) print("p loss:", self.n_loss / self.n) print("p profit:", self.n_profit / self.n) print("total trades:", self.n) print("p buy:", self.n_buy / self.n) print("p sell:", self.n_sell / self.n) print("p stay:", self.n_stay / self.n) print() self.prev_value = self.cur_value else: self.steps_since_trade += 1 def run(self): self.zeus.stream_bars(self.n_steps, self.add_bar)
n_bars_ = 0 def thing(bar): global n_bars_ global zeus id = zeus.place_trade(100, "Long") print(zeus.close_trade(id)) print(bar) print(zeus.current_balance()) print(zeus.unrealized_balance()) print() n_bars_ += 1 zeus = Zeus("EUR_USD", "M1") # zeus.stream_bars(100, thing) # print(zeus.unrealized_balance()) start = 1136073600 # 2006 n_bars = 1440 while n_bars_ != n_bars: n_seconds = (n_bars - n_bars_) * 60 zeus.stream_range(start, start + n_seconds, thing) start += n_seconds ''' zeus = cdll.LoadLibrary('target/debug/libzeus.so') zeus.create_session.restype = c_void_p sess = zeus.create_session("EUR_USD", "M", 1) zeus.test.restype = c_void_p
class Worker(object): def __init__(self, name, instrument, granularity, server_host, start, test=False): if not test: torch.set_default_tensor_type(torch.FloatTensor) else: torch.set_default_tensor_type(torch.cuda.FloatTensor) self.name = name self.instrument = instrument self.granularity = granularity self.server = redis.Redis(server_host) while True: if not test: self.market_encoder = Encoder().cpu() self.actor_critic = ActorCritic().cpu() try: MEN_compressed_state_dict = self.server.get("market_encoder") ACN_compressed_state_dict = self.server.get("actor_critic") MEN_state_dict_buffer = pickle.loads(MEN_compressed_state_dict) ACN_state_dict_buffer = pickle.loads(ACN_compressed_state_dict) MEN_state_dict_buffer.seek(0) ACN_state_dict_buffer.seek(0) self.market_encoder.load_state_dict(torch.load(MEN_state_dict_buffer, map_location='cpu')) self.actor_critic.load_state_dict(torch.load(ACN_state_dict_buffer, map_location='cpu')) break except Exception as e: print("Failed to load models") time.sleep(0.1) else: self.market_encoder = Encoder().cuda() self.actor_critic = ActorCritic().cuda() try: MEN_compressed_state_dict = self.server.get("market_encoder") ACN_compressed_state_dict = self.server.get("actor_critic") MEN_state_dict_buffer = pickle.loads(MEN_compressed_state_dict) ACN_state_dict_buffer = pickle.loads(ACN_compressed_state_dict) MEN_state_dict_buffer.seek(0) ACN_state_dict_buffer.seek(0) self.market_encoder.load_state_dict(torch.load(MEN_state_dict_buffer, map_location='cuda')) self.actor_critic.load_state_dict(torch.load(ACN_state_dict_buffer, map_location='cuda')) break except Exception: print("Failed to load models") time.sleep(0.1) self.market_encoder.eval() self.actor_critic.eval() if test: n = 0 for network in [self.market_encoder, self.actor_critic]: for param in network.parameters(): n += np.prod(param.size()) print("number of parameters:", n) self.time_states = [] self.exp_time_states = [] self.exp_percents_in = [] self.exp_trades_open = [] self.exp_spreads = [] self.exp_mus = [] self.exp_actions = [] self.exp_rewards = [] # self.action_last_step = False self.last_bar_date = -1 self.total_rewards = 0 self.reward = 0 self.window = 60 * 6 self.start = start self.trajectory_steps = int(self.server.get("trajectory_steps").decode("utf-8")) self.i_step = 0 self.steps_since_push = 0 self.n_experiences = 0 self.steps_between_experiences = self.trajectory_steps self.test = test if self.test: self.zeus = Zeus(instrument, granularity, margin=1) self.tradeable_percentage = 1 self.n_steps_left = self.window + (1440 * 5) self.n_total_experiences = 0 self.actor_temp = 1 self.trade_steps = [] self.rewards = [] else: self.zeus = Zeus(instrument, granularity, margin=1) self.tradeable_percentage = 1 self.n_total_experiences = 100 self.n_steps_left = self.window + (self.steps_between_experiences + self.trajectory_steps) * self.n_total_experiences actor_base_temp = float(self.server.get("actor_temp").decode("utf-8")) self.actor_temp = np.random.exponential(actor_base_temp / np.log(2)) self.trade_percent = self.tradeable_percentage / 100 self.trade_units = 0 self.long_trades = [] self.short_trades = [] self.pos_trades = [] self.n_closed_early = 0 self.n_tp = 0 self.neg_trades = [] self.plot = self.test and False if self.plot: self.all_times = [] self.long_times = [] self.short_times = [] self.long_bars = [] self.short_bars = [] self.all_bars = [] def add_bar(self, bar): time_state = [[[bar.open, bar.high, bar.low, bar.close]]] if (self.test and self.n_steps_left == 0) or (not self.test and bar.date > 1546300800) or (not self.test and self.n_experiences == self.n_total_experiences): self.quit() if self.i_step == 0: self.trade_units = self.zeus.units_available() if len(self.time_states) == 0 or bar.date != self.last_bar_date: self.time_states.append(time_state) self.last_bar_date = bar.date else: return if len(self.time_states) == self.window + 1: del self.time_states[0] if len(self.time_states) == self.window: if self.plot: self.all_times.append(self.i_step) self.all_bars.append(bar.close) test_spread_amount = float(self.server.get("spread_amount").decode("utf-8")) worker_spread_amount = float(self.server.get("spread_amount").decode("utf-8")) if self.test: spread_amount = test_spread_amount else: spread_amount = worker_spread_amount # process short trades completed_trades = set() for i, trade in enumerate(self.short_trades): trade_open = trade['open'] - (trade['spread'] * spread_amount) if trade_open - bar.low > trade['tp']: self.reward += trade['tp'] self.pos_trades.append(trade['tp']) self.n_tp += 1 completed_trades.add(i) if bar.high - trade_open > trade['sl']: self.reward -= trade['sl'] self.neg_trades.append(-trade['sl']) completed_trades.add(i) completed_trades = sorted(list(completed_trades)) completed_trades.reverse() for i in completed_trades: if self.test: self.trade_steps.append(self.i_step - self.short_trades[i]['step']) del self.short_trades[i] # process long trades completed_trades = set() for i, trade in enumerate(self.long_trades): trade_open = trade['open'] + (trade['spread'] * spread_amount) if bar.high - trade_open > trade['tp']: self.reward += trade['tp'] self.pos_trades.append(trade['tp']) self.n_tp += 1 completed_trades.add(i) if trade_open - bar.low > trade['sl']: self.reward -= trade['sl'] self.neg_trades.append(-trade['sl']) completed_trades.add(i) completed_trades = sorted(list(completed_trades)) completed_trades.reverse() for i in completed_trades: if self.test: self.trade_steps.append(self.i_step - self.long_trades[i]['step']) del self.long_trades[i] self.reward *= 1000 self.total_rewards += self.reward if self.test: self.rewards.append(self.reward) percent_in = -1 * len(self.short_trades) + 1 * len(self.long_trades) self.exp_rewards.append(self.reward) if len(self.exp_rewards) == self.trajectory_steps: del self.exp_rewards[0] self.reward = 0 trade_open = bar.close if len(self.short_trades) > 0: trade_open = self.short_trades[0]["open"] elif len(self.long_trades) > 0: trade_open = self.long_trades[0]["open"] five_min_bars = [] i = 0 five_min_bar_high = float('-inf') five_min_bar_low = float('inf') five_min_bar = [] for time_state in self.time_states[:-60]: time_state = time_state[0][0] if i == 0: five_min_bar.append(time_state[0]) if time_state[1] > five_min_bar_high: five_min_bar_high = time_state[1] if time_state[2] < five_min_bar_low: five_min_bar_low = time_state[2] i += 1 if i % 5 == 0: five_min_bar.append(five_min_bar_high) five_min_bar.append(five_min_bar_low) five_min_bar.append(time_state[3]) five_min_bars.append(five_min_bar) five_min_bar = [] i = 0 five_min_bar_high = float('-inf') five_min_bar_low = float('inf') five_min_time_states = torch.Tensor(five_min_bars).view(1, 60, 4) one_min_time_states = torch.Tensor(self.time_states[-60:]).view(1, 60, 4) input_time_states = torch.cat([five_min_time_states, one_min_time_states], dim=1) # input_time_states = torch.Tensor(self.time_states[-self.window:]).view(1, self.window, networks.D_BAR) assert torch.isnan(input_time_states).sum() == 0 market_encoding = self.market_encoder(input_time_states, torch.Tensor([bar.spread]), torch.Tensor([percent_in]), torch.Tensor([trade_open])) policy, value = self.actor_critic(market_encoding, temp=self.actor_temp) if self.test and False: action = np.random.randint(0, 3) # action = torch.multinomial(policy, 1).item() # action = torch.argmax(policy, 1).item() else: action = torch.multinomial(policy, 1).item() if self.plot: if action == 0: self.short_times.append(self.i_step) self.short_bars.append(bar.close) elif action == 1: self.long_times.append(self.i_step) self.long_bars.append(bar.close) mu = policy[0, action].item() # self.action_last_step = True self.exp_time_states.append(input_time_states.tolist()) self.exp_percents_in.append(percent_in) self.exp_trades_open.append(trade_open) self.exp_spreads.append(bar.spread) if not self.test and len(self.exp_time_states) == self.trajectory_steps + 1: del self.exp_time_states[0] del self.exp_percents_in[0] del self.exp_trades_open[0] del self.exp_spreads[0] # process action open = bar.close spread = bar.spread step = self.i_step if self.test and False: tp = 1000 / 10000 - (spread * spread_amount) sl = 1000 / 10000 + (spread * spread_amount) else: tp = 1000 / 10000 - (spread * spread_amount) sl = 1000 / 10000 + (spread * spread_amount) new_trade = {'open':open, 'spread':spread, 'step':step, 'tp':tp, 'sl':sl} max_trades = 1 if action == 0: if len(self.long_trades) > 0: trade = self.long_trades[0] trade_open = trade['open'] + (trade['spread'] * spread_amount) self.reward += bar.close - trade_open if (bar.close - trade_open) > 0: self.pos_trades.append(bar.close - trade_open) self.n_closed_early += 1 else: self.neg_trades.append(bar.close - trade_open) del self.long_trades[0] if len(self.short_trades) < max_trades and len(self.long_trades) == 0: self.short_trades.append(new_trade) elif action == 1: if len(self.short_trades) > 0: trade = self.short_trades[0] trade_open = trade['open'] - (trade['spread'] * spread_amount) self.reward += trade_open - bar.close if (trade_open - bar.close) > 0: self.pos_trades.append(trade_open - bar.close) self.n_closed_early += 1 else: self.neg_trades.append(trade_open - bar.close) del self.short_trades[0] if len(self.long_trades) < max_trades and len(self.short_trades) == 0: self.long_trades.append(new_trade) elif action == 2: while len(self.short_trades) > 0: trade = self.short_trades[0] trade_open = trade['open'] - (trade['spread'] * spread_amount) self.reward += trade_open - bar.close if (trade_open - bar.close) > 0: self.pos_trades.append(trade_open - bar.close) self.n_closed_early += 1 else: self.neg_trades.append(trade_open - bar.close) del self.short_trades[0] while len(self.long_trades) > 0: trade = self.long_trades[0] trade_open = trade['open'] + (trade['spread'] * spread_amount) self.reward += bar.close - trade_open if (bar.close - trade_open) > 0: self.pos_trades.append(bar.close - trade_open) self.n_closed_early += 1 else: self.neg_trades.append(bar.close - trade_open) del self.long_trades[0] if self.test: reward_tau = float(self.server.get("test_reward_tau").decode("utf-8")) reward_ema = self.server.get("test_reward_ema") reward_emsd = self.server.get("test_reward_emsd") if reward_ema != None: reward_ema = float(reward_ema.decode("utf-8")) reward_emsd = float(reward_emsd.decode("utf-8")) else: reward_ema = 0 reward_emsd = 0 p_pos = (np.array(self.rewards) > 0).sum() / (np.array(self.rewards) != 0).sum() print("n trades:", len(self.pos_trades + self.neg_trades)) print("n tp, n closed early:", self.n_tp, self.n_closed_early) print("p_pos:", p_pos) print("spread mean:", np.mean(self.exp_spreads) * 10000) print("pos mean:", np.mean(self.pos_trades) * 10000) print("neg mean:", np.mean(self.neg_trades) * 10000) print("sum all:", np.sum(self.pos_trades + self.neg_trades) * 10000) print("with spread at 100%:", np.sum(np.array(self.pos_trades + self.neg_trades) - np.mean(self.exp_spreads)) * 10000) print('-----------------------------------------------------------------------------') print("step: {s} \ \naction: {a} \ \ndirection: {dir} \ \npolicy: {p} \ \nvalue: {v} \ \ntotal rewards: {t_r} \ \nreward_ema: {ema} \ \nreward_emsd: {emsd} \ \n1/tau: {tau} \ \nspread amount: {sa} \ \nbar close: {close} \ \nwindow std: {std} \ \ninstrument: {ins} \ \nstart: {start} \n".format(s=self.i_step, a=action, dir=percent_in, p=[round(policy_, 5) for policy_ in policy[0].tolist()], v=round(value.item(), 5), t_r=self.total_rewards, ema=round(reward_ema, 5), emsd=round(reward_emsd, 5), tau=round(1/reward_tau, 5), sa=round(spread_amount, 5), std=round(np.std([ts[0][0][3] for ts in self.time_states[-self.window:]]), 5), close=bar.close, ins=self.instrument, start=self.start)) print('-----------------------------------------------------------------------------') print('*****************************************************************************') # if self.i_step % 1000 == 0: # try: # MEN_compressed_state_dict = self.server.get("market_encoder") # ACN_compressed_state_dict = self.server.get("actor_critic") # # MEN_state_dict_buffer = pickle.loads(MEN_compressed_state_dict) # ACN_state_dict_buffer = pickle.loads(ACN_compressed_state_dict) # # MEN_state_dict_buffer.seek(0) # ACN_state_dict_buffer.seek(0) # # self.market_encoder.load_state_dict(torch.load(MEN_state_dict_buffer, map_location='cuda')) # self.actor_critic.load_state_dict(torch.load(ACN_state_dict_buffer, map_location='cuda')) # except Exception as e: # print("Failed to load models") if (self.steps_since_push == self.steps_between_experiences) and (not self.test) and len(self.exp_time_states) == self.trajectory_steps: experience = Experience( time_states=self.exp_time_states, percents_in=self.exp_percents_in, trades_open=self.exp_trades_open, spreads=self.exp_spreads, mus=self.exp_mus, place_actions=self.exp_actions, rewards=self.exp_rewards ) experience = msgpack.packb(experience, use_bin_type=True) # self.add_to_replay_buffer(experience) n_experiences = self.server.llen("experience") if n_experiences > 0: try: loc = np.random.randint(0, n_experiences) ref = self.server.lindex("experience", loc) self.server.linsert("experience", "before", ref, experience) except redis.exceptions.DataError: self.server.lpush("experience", experience) else: self.server.lpush("experience", experience) self.n_experiences += 1 self.steps_since_push = 1 self.exp_time_states = [] self.exp_percents_in = [] self.exp_trades_open = [] self.exp_spreads = [] self.exp_mus = [] self.exp_actions = [] self.exp_rewards = [] # self.action_last_step = False else: self.steps_since_push += 1 # self.action_last_step = True self.exp_actions.append(action) self.exp_mus.append(mu) if len(self.exp_actions) == self.trajectory_steps: del self.exp_actions[0] del self.exp_mus[0] self.i_step += 1 self.n_steps_left -= 1 # torch.cuda.empty_cache() self.t_final_prev = time.time() def run(self): self.t0 = time.time() n = 0 start = self.start while (self.test and self.n_steps_left > 0) or (not self.test and self.n_experiences < self.n_total_experiences): if self.test: n_seconds = min(self.n_steps_left, 7200) * 60 else: n_seconds = (self.n_total_experiences - self.n_experiences) * (self.steps_between_experiences + self.trajectory_steps) * 10 * 60 if self.granularity == "M5": n_seconds *= 5 if self.test: print("starting new stream. n minutes left:", n_seconds // 60) self.zeus.stream_range(start, min(start + n_seconds, int(time.time())), self.add_bar) start += n_seconds n += 1 self.quit() def quit(self): print(("time: {time}, " "rewards: {reward}, " "temp: {actor_temp}, " "n exp: {n_experiences}, " "instr: {instrument}, " "start: {start}").format( time=round(time.time()-self.t0, 2), reward=self.total_rewards, actor_temp=round(self.actor_temp, 3), n_experiences=self.n_experiences, instrument=self.instrument, start=self.start ) ) if self.test: if self.plot: import matplotlib.pyplot as plt plt.plot(self.all_times, self.all_bars) plt.scatter(np.array(self.long_times), self.long_bars, c='g', alpha=1) plt.scatter(np.array(self.short_times), self.short_bars, c='r', alpha=1) plt.show() reward_tau = float(self.server.get("test_reward_tau").decode("utf-8")) reward_ema = float(self.server.get("test_reward_ema").decode("utf-8")) reward_emsd = float(self.server.get("test_reward_emsd").decode("utf-8")) delta = self.total_rewards - reward_ema new_reward_ema = reward_ema + reward_tau * delta new_reward_emsd = math.sqrt((1 - reward_tau) * (reward_emsd**2 + reward_tau * (delta**2))) new_reward_tau = max(1 / ((1 / reward_tau) + 1), 0.01) # spread_amount = float(self.server.get("spread_amount").decode("utf-8")) # new_spread_amount = min(1, spread_amount + 0.25) # # if new_reward_tau == 0.1 and new_reward_ema > 5 and spread_amount < 1: # self.server.set("spread_amount", new_spread_amount) # new_reward_ema = 0 # new_reward_emsd = 0 # new_reward_tau = 1 self.server.set("test_reward_ema", new_reward_ema) self.server.set("test_reward_emsd", new_reward_emsd) self.server.set("test_reward_tau", new_reward_tau) quit() def add_to_replay_buffer(self, compressed_experience): # try: # loc = np.random.randint(0, replay_size) if replay_size > 0 else 0 # ref = self.server.lindex("replay_buffer", loc) # self.server.linsert("replay_buffer", "before", ref, compressed_experience) # except redis.exceptions.DataError: self.server.lpush("replay_buffer", compressed_experience) max_replay_size = int(self.server.get("replay_buffer_size").decode("utf-8")) replay_size = self.server.llen("replay_buffer") while replay_size - 1 > max_replay_size: replay_size = self.server.llen("replay_buffer") try: loc = replay_size - 1 ref = self.server.lindex("replay_buffer", loc) self.server.lrem("replay_buffer", -1, ref) except Exception as e: pass
class Worker(object): def __init__(self, instrument, granularity, server_host, start, max_bar_time, models_loc): self.server = redis.Redis(server_host) self.zeus = Zeus(instrument, granularity, margin=1) self.models_loc = models_loc self.window = networks.WINDOW self.start = start self.max_bar_time = max_bar_time self.time_states = [] self.last_time = None self.generator = Generator().cuda() try: self.generator.load_state_dict( torch.load(self.models_loc + 'generator.pt', map_location='cuda')) except FileNotFoundError as e: print("model load failed", e) self.generator = Generator().cuda() self.generator.eval() n = 0 for name, param in self.generator.named_parameters(): # print(np.prod(param.size()), name) n += np.prod(param.size()) print("generator parameters:", n) # for name, param in self.generator.named_parameters(): # print(param.std(), name) self.n_future_samples = 100 self.n_steps_future = 10 self.plot = 0 self.step = 0 self.last_price = None self.last_action = None self.steps_since_trade = 0 self.n_correct = 0 self.n_trades = 0 self.n = 0 self.t0 = 0 def add_bar(self, bar): t_ = time.time() if bar.date > self.max_bar_time: print("out of bars") print("elapsed time: {time}".format(time=time.time() - self.t0)) quit() time_state = [bar.open, bar.high, bar.low, bar.close] if bar.date != self.last_time: self.time_states.append(time_state) self.n += 1 if len(self.time_states) > self.window + self.n_steps_future: del self.time_states[0] if bar.date != self.last_time: input_time_states = [] for time_state in self.time_states: input_time_states.append( torch.Tensor(time_state).view(1, 1, networks.D_BAR)) if self.plot: time_state_values = torch.cat( input_time_states, dim=1)[0, :, 3].cpu().detach().numpy() plt.plot(np.arange(0, self.window), time_state_values[:self.window], 'b-') plt.plot( np.arange(self.window - 1, self.window + self.n_steps_future), time_state_values[self.window - 1:], 'g-') input_time_states_original = [] for i_time_state in range(self.window): input_time_states_original.append( torch.Tensor(self.time_states[i_time_state]).view( 1, 1, D_BAR)) input_time_states_original = torch.cat( input_time_states_original, dim=1) generation = self.generator( input_time_states_original.repeat(self.n_future_samples, 1, 1)) if self.plot: time_state_values = torch.cat( [ input_time_states_original[0, -1, 3].view( 1, 1, 1).repeat(self.n_future_samples, 1, 1), generation ], dim=1).cpu().detach().numpy() x = np.arange(self.window - 1, self.window + self.n_steps_future).reshape( 1, -1).repeat(self.n_future_samples, axis=0) y = time_state_values x = x.reshape(self.n_future_samples, self.n_steps_future + 1).T y = y.reshape(self.n_future_samples, self.n_steps_future + 1).T if self.n_future_samples == 1: plt.plot(x, y, 'y-', alpha=1) else: plt.plot(x, y, 'y-', alpha=0.1) predicted_future = generation[:, -1].mean().item() actual_future = self.time_states[-1][3] original = input_time_states[self.window - 1][0, 0, 3].item() input_mean = input_time_states_original.mean().item() input_std = input_time_states_original.std().item() plt.plot(np.arange(0, self.window + self.n_steps_future), np.zeros(self.window + self.n_steps_future) + input_mean, 'r', alpha=0.1) plt.plot(np.arange(0, self.window + self.n_steps_future), np.zeros(self.window + self.n_steps_future) + original + (0.25 * input_std), 'y', alpha=0.1) plt.plot(np.arange(0, self.window + self.n_steps_future), np.zeros(self.window + self.n_steps_future) + original - (0.25 * input_std), 'y', alpha=0.1) # my_pred = input("b/s/h: ") # if (my_pred == 'b' and actual_future > original) or (my_pred == 's' and actual_future < original): # self.n_correct += 1 # if my_pred == 'b' or my_pred == 's': # self.n_trades += 1 # n_greater = (generation[:, -1] > original).sum().item() # print("number greater than original:", n_greater) # threshold = 90 # if n_greater > threshold or n_greater < (100 - threshold): # self.n_trades += 1 # if (n_greater > threshold and actual_future > original) or (n_greater < (100 - threshold) and actual_future < original): # self.n_correct += 1 if (predicted_future > original and actual_future > original ) or (predicted_future < original and actual_future < original): self.n_correct += 1 self.n_trades += 1 print( "trade: {trade}, time: {time}, actual change: {actual}, predicted change: {pred}, pred off mean: {pom}, accuracy: {acc}" .format(trade=self.n_trades, time=round(time.time() - t_, 5), actual=round( (actual_future - original) / input_time_states_original.std().item(), 3), pred=round((predicted_future - original) / input_time_states_original.std().item(), 3), pom=round((predicted_future - input_mean) / input_time_states_original.std().item(), 5), acc=round(self.n_correct / (self.n_trades + 1e-9), 7))) print() if self.plot: plt.show() # del self.time_states[:self.n_steps_future] self.time_states = [] self.last_time = bar.date self.step += 1 self.steps_since_trade += 1 def run(self): self.t0 = time.time() with torch.no_grad(): start = self.start while True: n_seconds = 60 * 1440 * 50 self.zeus.stream_range(start, start + n_seconds, self.add_bar) start += n_seconds print("time: {time}".format(time=time.time() - t0))
def __init__(self, name, instrument, granularity, server_host, start, test=False): if not test: torch.set_default_tensor_type(torch.FloatTensor) else: torch.set_default_tensor_type(torch.cuda.FloatTensor) self.name = name self.instrument = instrument self.granularity = granularity self.server = redis.Redis(server_host) while True: if not test: self.market_encoder = Encoder().cpu() self.actor_critic = ActorCritic().cpu() try: MEN_compressed_state_dict = self.server.get("market_encoder") ACN_compressed_state_dict = self.server.get("actor_critic") MEN_state_dict_buffer = pickle.loads(MEN_compressed_state_dict) ACN_state_dict_buffer = pickle.loads(ACN_compressed_state_dict) MEN_state_dict_buffer.seek(0) ACN_state_dict_buffer.seek(0) self.market_encoder.load_state_dict(torch.load(MEN_state_dict_buffer, map_location='cpu')) self.actor_critic.load_state_dict(torch.load(ACN_state_dict_buffer, map_location='cpu')) break except Exception as e: print("Failed to load models") time.sleep(0.1) else: self.market_encoder = Encoder().cuda() self.actor_critic = ActorCritic().cuda() try: MEN_compressed_state_dict = self.server.get("market_encoder") ACN_compressed_state_dict = self.server.get("actor_critic") MEN_state_dict_buffer = pickle.loads(MEN_compressed_state_dict) ACN_state_dict_buffer = pickle.loads(ACN_compressed_state_dict) MEN_state_dict_buffer.seek(0) ACN_state_dict_buffer.seek(0) self.market_encoder.load_state_dict(torch.load(MEN_state_dict_buffer, map_location='cuda')) self.actor_critic.load_state_dict(torch.load(ACN_state_dict_buffer, map_location='cuda')) break except Exception: print("Failed to load models") time.sleep(0.1) self.market_encoder.eval() self.actor_critic.eval() if test: n = 0 for network in [self.market_encoder, self.actor_critic]: for param in network.parameters(): n += np.prod(param.size()) print("number of parameters:", n) self.time_states = [] self.exp_time_states = [] self.exp_percents_in = [] self.exp_trades_open = [] self.exp_spreads = [] self.exp_mus = [] self.exp_actions = [] self.exp_rewards = [] # self.action_last_step = False self.last_bar_date = -1 self.total_rewards = 0 self.reward = 0 self.window = 60 * 6 self.start = start self.trajectory_steps = int(self.server.get("trajectory_steps").decode("utf-8")) self.i_step = 0 self.steps_since_push = 0 self.n_experiences = 0 self.steps_between_experiences = self.trajectory_steps self.test = test if self.test: self.zeus = Zeus(instrument, granularity, margin=1) self.tradeable_percentage = 1 self.n_steps_left = self.window + (1440 * 5) self.n_total_experiences = 0 self.actor_temp = 1 self.trade_steps = [] self.rewards = [] else: self.zeus = Zeus(instrument, granularity, margin=1) self.tradeable_percentage = 1 self.n_total_experiences = 100 self.n_steps_left = self.window + (self.steps_between_experiences + self.trajectory_steps) * self.n_total_experiences actor_base_temp = float(self.server.get("actor_temp").decode("utf-8")) self.actor_temp = np.random.exponential(actor_base_temp / np.log(2)) self.trade_percent = self.tradeable_percentage / 100 self.trade_units = 0 self.long_trades = [] self.short_trades = [] self.pos_trades = [] self.n_closed_early = 0 self.n_tp = 0 self.neg_trades = [] self.plot = self.test and False if self.plot: self.all_times = [] self.long_times = [] self.short_times = [] self.long_bars = [] self.short_bars = [] self.all_bars = []
class Worker(object): def __init__(self, instrument, granularity, server_host, start, max_bar_time, models_loc): self.server = redis.Redis(server_host) self.zeus = Zeus(instrument, granularity, margin=1) self.models_loc = models_loc self.window = networks.WINDOW self.start = start self.max_bar_time = max_bar_time self.time_states = [] self.last_time = None self.network = Network().cuda() try: self.network.load_state_dict( torch.load(self.models_loc + 'network.pt', map_location='cuda')) except FileNotFoundError as e: print("model load failed", e) self.network = Network().cuda() self.network.eval() n = 0 for name, param in self.network.named_parameters(): n += np.prod(param.size()) print("network parameters:", n) self.n_future_samples = 1 self.n_steps_future = 10 self.plot = 0 self.step = 0 self.last_price = None self.last_action = None self.steps_since_trade = 0 self.n_correct = 0 self.n_trades = 0 self.n = 0 self.t0 = 0 def add_bar(self, bar): t_ = time.time() if bar.date > self.max_bar_time: print("out of bars") print("elapsed time: {time}".format(time=time.time() - self.t0)) quit() time_state = [bar.open, bar.high, bar.low, bar.close] if bar.date != self.last_time: self.time_states.append(time_state) self.n += 1 if len(self.time_states) > self.window + self.n_steps_future: del self.time_states[0] if bar.date != self.last_time: input_time_states = [] for time_state in self.time_states: input_time_states.append( torch.Tensor(time_state).view(1, 1, networks.D_BAR)) if self.plot: time_state_values = torch.cat( input_time_states, dim=1)[0, :, 3].cpu().detach().numpy() plt.plot(np.arange(0, self.window), time_state_values[:self.window], 'b-') plt.plot( np.arange(self.window - 1, self.window + self.n_steps_future), time_state_values[self.window - 1:], 'g-') input_time_states_original = [] for i_time_state in range(self.window): input_time_states_original.append( torch.Tensor(self.time_states[i_time_state]).view( 1, 1, D_BAR)) input_time_states_original = torch.cat( input_time_states_original, dim=1) prediction, _, mean, std = self.network( input_time_states_original) original = input_time_states[self.window - 1][0, 0, 3].item() print(prediction) action = torch.argmax(prediction) confidence = 0.75 if action != 1 and prediction.max() > confidence: self.n_trades += 1 future = self.time_states[-1][3] if (action == 0 and (future > original)) or (action == 2 and (future < original)): self.n_correct += 1 if self.plot: plt.plot(np.arange(0, self.window + self.n_steps_future), np.zeros(self.window + self.n_steps_future) + mean.item(), 'r', alpha=0.1) plt.plot(np.arange(0, self.window + self.n_steps_future), np.zeros(self.window + self.n_steps_future) + original + (0.25 * std.item()), 'y', alpha=0.1) plt.plot(np.arange(0, self.window + self.n_steps_future), np.zeros(self.window + self.n_steps_future) + original - (0.25 * std.item()), 'y', alpha=0.1) print("trade: {trade}, time: {time}, accuracy: {acc}".format( trade=self.n_trades, time=round(time.time() - t_, 5), acc=round(self.n_correct / (self.n_trades + 1e-9), 7))) print() if self.plot: plt.show() del self.time_states[:self.n_steps_future] # self.time_states = [] self.last_time = bar.date self.step += 1 self.steps_since_trade += 1 def run(self): self.t0 = time.time() with torch.no_grad(): start = self.start while True: n_seconds = 60 * 1440 * 10 self.zeus.stream_range(start, start + n_seconds, self.add_bar) start += n_seconds print("time: {time}".format(time=time.time() - t0))