def step(self, action): # 0: Sit # 1: But # 2: Sell assert (action in (0, 1, 2)) # State transition next_state = getState(self.data, self.t + 1, self.window_size + 1) # Reward if action == 0: reward = 0 elif action == 1: reward = 0 self.agent.inventory.append(self.data[self.t]) print("Buy: " + formatPrice(self.data[self.t])) else: if len(self.agent.inventory) > 0: bought_price = self.agent.inventory.pop(0) profit = self.data[self.t] - bought_price reward = max(profit, 0) self.total_profit += profit print("Sell: " + formatPrice(self.data[self.t]) + " | Profit: " + formatPrice(profit)) else: reward = 0 # try to sell, but con't do done = True if self.t == len(self.data) - 2 else False self.t += 1 return next_state, reward, done, {}
def reset(self): self.t = 0 self.total_profit = 0 return getState(self.data, self.t, self.window_size + 1)
print( colored( 'To {} ~~> {} {:.7f}\n\n'.format(l, currency.upper(), data[-1]).center(width), 'magenta')) for e in range(episode_count + 1): print( colored(" {}/BTC".format(asset_name).center(width), 'yellow', attrs=['bold'])) print( colored("> Episode {}/{}\n".format(str(e), str(episode_count)).center(width), 'yellow', attrs=['bold'])) state = getState(data, 0, window_size + 1) total_profit = 0 agent.inventory = [] for t in range(l): action = agent.act(state) next_state = getState(data, t + 1, window_size + 1) reward = 0 print("> {} BTC {:.7f}".format(t, data[t]), end='\r') #hold if action == 1: # buy agent.inventory.append(data[t]) print(colored("> {} BTC {:.7f} |".format(t, data[t]), 'cyan'), end='\r') elif action == 2 and len(agent.inventory) > 0: # sell bought_price = agent.inventory.pop(0) reward = max(data[t] - bought_price, 0) total_profit += data[t] - bought_price
import sys # parameters: python testing.py {stock} {model_name} (python 3.6) stock_name, model_name = sys.argv[1], sys.argv[2] model = load_model("models/" + model_name) state_size = model.layers[0].input.shape.as_list()[1] broker = Broker(state_size, True, model_name) data = getStockDataVector(stock_name) data_size = len(data) - 1 batch_size = 100 gain = 0 broker.trades_list = [] reward = 0 state = getState(data, 0, state_size + 1) for t in range(data_size): action = broker.act(state) if action == 1 and broker.portfolio >= data[t]: # buy broker.trades_list.append(data[t]) broker.portfolio = broker.portfolio - data[t] print("Bought: " + formatPrice(data[t]) + "| Portfolio Value: " + formatPrice(broker.portfolio)) reward = 0 elif action == 2 and len(broker.trades_list) > 0: # sell buying_price = broker.trades_list.pop(0)
def getPinState(): """Returns devices' state as JSON""" devices = getState() return json.dumps(devices)
def index(): """Index page""" devices = getState() return render_template('index.html', devices=devices, polling=app.config['GPIO_POLLING_DELAY'], display=app.config['DISPLAY_PIN_ID'])