indices.append(index) runtimes.append(value['t']) inventories.append(value['i']) rewards.append(value['reward']) actions.append(value['action']) state = orderbook.getState(index) prices.append(state.getBidAskMid()) action_delta = 0.1 * levels[value['action']] if side == OrderSide.BUY: order_prices.append(state.getBidAskMid() + action_delta) else: order_prices.append(state.getBidAskMid() - action_delta) times.append(state.getTimestamp()) # price chart ps = [x.getBidAskMid() for x in orderbook.getStates()] ts = [x.getTimestamp() for x in orderbook.getStates()] plt.plot(ts, ps) # if show_bidask: max_level = 0 buyer = [x.getBuyers()[max_level].getPrice() for x in orderbook.getStates()] seller = [x.getSellers()[max_level].getPrice() for x in orderbook.getStates()] plt.plot(ts, buyer) plt.plot(ts, seller) # order placement #plt.scatter(times, prices, s=60) plt.scatter(times, order_prices, s=60) for i, time in enumerate(times): if i == 0 or i == len(times) - 1:
from orderbook import Orderbook import numpy as np import pandas as pd import os book = 'query_result_test.tsv' tmp = 'feature.tsv' orderbook = Orderbook() orderbook.loadFromFile(book) states = orderbook.getStates() def stateDiff(start, end): """Calculate time difference between two states.""" consumed = (end.getTimestamp() - start.getTimestamp()).total_seconds() return consumed def getPastState(i, t): """Find state at index i - time t.""" endState = states[i] state = endState while (stateDiff(state, endState) < t): i = i - 1 if i < 0: raise Exception("Not enough states available for diff.") state = states[i] return i def traverse(f, g, default=0.0, t=60):