Example #1
0
def load_orderbooks():
    summary_file = relative_path('static', 'summary.json')
    with open(summary_file, 'r') as f:
        summaries = json.load(f)
        downloadable = [item_summary for item_summary in summaries.values() if ITEM_FILTER(item_summary)]
        orderbooks = [Orderbook(summaries, item['id']) for item in downloadable]
        return [orderbook for orderbook in orderbooks if ORDERBOOK_FILTER(orderbook)]
 def setUp(self):
     '''
     setUp creates the Orderbook instance and a set of orders
     '''
     self.ex1 = Orderbook(5)
     self.q0_buy = {'order_id': 't1_0', 'timestamp': 1, 'type': 'add', 'quantity': 1, 'side': 'buy',
                    'price': 50}
     self.q1_buy = {'order_id': 't1_1', 'timestamp': 2, 'type': 'add', 'quantity': 1, 'side': 'buy',
                    'price': 50}
     self.q2_buy = {'order_id': 't1_2', 'timestamp': 3, 'type': 'add', 'quantity': 1, 'side': 'buy',
                    'price': 50}
     self.q3_buy = {'order_id': 't10_1', 'timestamp': 4, 'type': 'add', 'quantity': 3, 'side': 'buy',
                    'price': 49}
     self.q4_buy = {'order_id': 't11_1', 'timestamp': 5, 'type': 'add', 'quantity': 3, 'side': 'buy',
                    'price': 47}
     self.q5_buy = {'order_id': 't12_1', 'timestamp': 6, 'type': 'add', 'quantity': 3, 'side': 'buy',
                    'price': 47}
     self.q6_buy = {'order_id': 't13_1', 'timestamp': 7, 'type': 'add', 'quantity': 3, 'side': 'buy',
                    'price': 47}
     self.q0_sell = {'order_id': 't1_5', 'timestamp': 1, 'type': 'add', 'quantity': 1, 'side': 'sell',
                     'price': 52}
     self.q1_sell = {'order_id': 't1_3', 'timestamp': 2, 'type': 'add', 'quantity': 1, 'side': 'sell',
                     'price': 52}
     self.q2_sell = {'order_id': 't1_4', 'timestamp': 3, 'type': 'add', 'quantity': 1, 'side': 'sell',
                     'price': 52}
     self.q3_sell = {'order_id': 't10_2', 'timestamp': 4, 'type': 'add', 'quantity': 3, 'side': 'sell',
                     'price': 53}
     self.q4_sell = {'order_id': 't11_2', 'timestamp': 5, 'type': 'add', 'quantity': 3, 'side': 'sell',
                     'price': 55}
     self.q5_sell = {'order_id': 't12_2', 'timestamp': 6, 'type': 'add', 'quantity': 3, 'side': 'sell',
                     'price': 53}
     self.q6_sell = {'order_id': 't13_2', 'timestamp': 7, 'type': 'add', 'quantity': 3, 'side': 'sell',
                     'price': 55}
Example #3
0
	def docMappings(self, connection, indexName=DEFAULT_INDEX): 
		try: 
			connection.indices.put_mapping(index=indexName, doc_type="orderbook", body=Orderbook().orderbookMapping)
			connection.indices.put_mapping(index=indexName, doc_type="ticker", body=Ticker().tickerMapping)
			connection.indices.put_mapping(index=indexName, doc_type="completed_trades", body=Trade().completedTradeMapping)
			connection.indices.put_mapping(index=indexName, doc_type="future_ticker", body=Ticker().futureTickerMapping)
			connection.indices.put_mapping(index=indexName, doc_type="future_price_index", body=FutureIndex().futurePriceIndexMapping) 
			connection.indices.put_mapping(index=indexName, doc_type="kline_candles", body=KlineCandle().klineMapping) 
		except: 
			raise 
		pass
    def __init__(self, n_days, n_steps_per_day, asset_config_path,
                 agent_config_path):
        self.n_days = n_days
        self.n_steps_per_day = n_steps_per_day

        initial_shares_per_agent = 10
        n_agents = AgentGenerator.n_agents(agent_config_path)
        print(n_agents)

        self.companies = CompanyGenerator.generate_companies(n_companies=10,
                                                             n_sectors=3)
        self.tickers = [each.ticker for each in self.companies]
        # self.tickers = 'EQNR DNO AKER MHG NRS LSG'.split(' ')
        # self.companies = []
        # for ticker in self.tickers:
        #     comp = Company(ticker=ticker,
        #                    n_shares=initial_shares_per_agent * n_agents,
        #                    cash=100 * initial_shares_per_agent * n_agents,
        #                    yield_policy=0.10)
        #     self.companies.append(comp)

        self.assets = [Stock(comp) for comp in self.companies]

        self.agents = AgentGenerator.generate(agent_config_path,
                                              self.assets,
                                              verbose=True)
        n_agents = len(self.agents)
        for agent in self.agents:
            for stock in self.assets:
                agent.portfolio.assets[stock] = initial_shares_per_agent

        self.cov_matrix = test.generate_corr_matrix(len(self.tickers), 2, 0.2)
        self.weights = np.ones(len(self.tickers))
        self.weights = self.weights / self.weights.size

        self.book = Orderbook([], [])

        # self.assets = ...

        self.timestamp = Timestamp(0, 0)

        self.market_portfolio = Portfolio.empty_portfolio(self.assets)
        self.exchange = Exchange(self.agents, self.market_portfolio,
                                 n_steps_per_day)

        self.history = []
Example #5
0
 def subscribe_orderbook(self, symbol1, symbol2, depth=5):
     ccy = "{}:{}".format(symbol1, symbol2)
     self.ccy_order_books[ccy] = Orderbook(ccy, depth, self.logger)
     self.logger.info("[WS] Subscribing to pair {}".format(ccy))
     self.debug_init_time[ccy] = time.time()
     if not self.is_connected:
         self.start()
         self.logger.warning("Not connected, trying to reconnect")
         time.sleep(2)
     oid = "{}_orderbook_{}{}".format(str(self.get_timestamp()), symbol1,
                                      symbol2)
     msg = json.dumps({
         "e": "order-book-subscribe",
         "data": {
             "pair": [symbol1, symbol2],
             "subscribe": True,
             "depth": depth
         },
         "oid": oid
     })
     self.ws.send(msg)
Example #6
0
def main():
    # Load orderbook
    orderbook = Orderbook()
    orderbook.loadFromEvents('ob-1-small.tsv')


    env = gym.make("ctc-executioner-v0")
    env.configure(orderbook)
    model = deepq.models.cnn_to_mlp( convs=[(1, 10, 20)], hiddens=[200])
    act = deepq.learn(
        env,
        q_func=model,
        lr=1e-4,
        max_timesteps=100000,
        buffer_size=5000,
        exploration_fraction=0.1,
        exploration_final_eps=0.1,
        target_network_update_freq=1,
        print_freq=10,
    )
    print("Saving model as ctc-executioner-v0.pkl")
    act.save("ctc-executioner-v0.pkl")
Example #7
0
def compute_spread(market):
    dcx_ob = dcx.get_orderbook(market=market)
    ob = Orderbook()
    ob.init(dcx_orderbook=dcx_ob)
    print(f'spread for market: {market} is {ob.spread}')
Example #8
0
	def consumer(self, marketData): 
		connection = elasticsearch.Elasticsearch(self.esHost) 
		self.ensure(connection) 
		self.docMappings(connection) 
		dataSet = json.loads(marketData) 
		item = {}
		for infoPoint in dataSet: 
			try: 
				channel = str(infoPoint["channel"])
				regex = "ok_sub_(spotusd|futureusd)_(b|l)tc_(.[A-Za-z0-9_]+)"
				search = re.search(regex, channel) 
				if search.group(1) == "futureusd": 
					isFuture = True
				else: 
					isFuture = False 
				currencyPair = str(search.group(2)) + "tc_usd"
				self.count = self.count + 1
				if self.count % 100 == 0: 
					print ("PROCESSED " + str(self.count) + " DATA POINTS SO FAR...") 
				if search.group(3) == "index": 
					myindex = FutureIndex()
					dto = myindex.getFutureIndexDto(infoPoint, currencyPair)
					dto["exchange"] = "OKCOIN"
					self.postDto(dto, connection, "future_price_index")
				elif "depth" in channel: 
					mybook = Orderbook()
					dto = mybook.getDepthDtoList(infoPoint, currencyPair, isFuture)
					for item in dto: 
						item["websocket_name"] = channel
						item["is_future"] = isFuture
						if isFuture == True: 
							check = re.search("depth_(this_week|next_week|quarter)_(20|60)", search.group(3).strip())
							item["contract_type"] = str(check.group(1))
							item["depth"] = str(check.group(2))
						else: 
							item["contract_type"] = "spot"
							depthSearch = re.search("depth_(20|60)", search.group(3).strip()) 
							item["depth"] = depthSearch.group(1) 
						item["exchange"] = "OKCOIN"	
						self.postDto(item, connection, "orderbook")
				elif "ticker" in channel and "data" in infoPoint: 
					myticker = Ticker() 
					if isFuture == False: 
						dto = myticker.getTickerDto(infoPoint, currencyPair) 
						self.postDto(dto, connection, "ticker")
					elif isFuture == True: 
						dto = myticker.getFutureTickerDto(infoPoint, channel, currencyPair)
						dto["exchange"] = "OKCOIN"
						self.postDto(dto, connection, "future_ticker") 
				elif "trade" in channel: 
					mytrade = Trade() 
					if "data" in infoPoint: 
						dtoList = mytrade.getCompletedTradeDtoList(infoPoint, currencyPair)
						for item in dtoList: 
							item["is_future"] = "futureusd" in channel
							item["websocket_name"] = channel 	
							item["exchange"] = "OKCOIN" 	
							self.postDto(item, connection, "completed_trades") 
				elif "kline" in channel: 
					myklein = KlineCandle() 
					if "data" in infoPoint: 
						if len(infoPoint["data"]) > 1: 
							for klineData in infoPoint["data"]: 
								if type(klineData) is list: 
									klineDto = myklein.getKlineDto(klineData, currencyPair, channel) 
									klineDto["exchange"] = "OKCOIN" 
									klineDto["is_future"] = isFuture 
									klineDto["websocket_name"] = channel
								else: 
									klineDto = myklein.getKlineDto(infoPoint["data"], currencyPair, channel) 
							self.postDto(klineDto, connection, "kline_candles")
			except: 
				raise
Example #9
0
 def reset_orderbooks(self):
     for each in self.portfolio.get_stocks():
         bid, ask = each.get_last_price(), each.get_last_price()
         self.orderbooks[each] = Orderbook(bid, ask, [], [])
Example #10
0
                    if len(self.ob.book[symbol]['B']) > 0 and len(
                            self.ob.book[symbol]['S']) > 0:
                        self.strat.store_price(order, self.ob.book)
                        print(
                            'best bids/offers',
                            self.strat.previous_orders_highest[
                                order['Symbol']],
                            self.strat.previous_orders_lowest[order['Symbol']])
                        outcome = self.om.trade_after_news(order, self.strat)
                        self.send(outcome, order)
                        self.ob.clear_crossed_book(order, self.strat)
                        #print(self.ob.book)

                    else:
                        print('no trade, just modify')

                    self.strat.store_signal(order)
                    print(self.strat.signal_table[order['Symbol']])

        finally:
            print('The End!')


if __name__ == '__main__':
    the_strat = Strategy()
    the_om = OrderManager()
    the_ob = Orderbook()
    the_client = ClientConnection(the_ob, the_om, the_strat)
    the_client.start()
Example #11
0
import unittest
from action import Action
from orderbook import Orderbook
from order import Order
from order_type import OrderType
from order_side import OrderSide

orderbook = Orderbook(extraFeatures=False)
orderbook.loadFromFile('test_orderbook_10s.tsv')


class MatchEngineMock():
    def matchOrder(self, order, seconds=None):
        trades = []
        qtyRemain = 0
        index = 1
        return trades, qtyRemain, index


class ActionMock(Action):
    def __init__(self, a, runtime):
        Action.__init__(self, a, runtime)

    def getMatchEngine(self, orderbook):
        return MatchEngineMock()


class ActionTest(unittest.TestCase):
    def testRun(self):
        a = 1
        i = 1.0
Example #12
0
from order_side import OrderSide
from orderbook import Orderbook

orderbook = Orderbook()
orderbook.loadFromEvents('ob-1-small.tsv')
orderbook_test = orderbook
orderbook.summary()

side = OrderSide.SELL
levels = list(range(-20, 21))

episode = {
    'episode': 9,
    'steps': {
        0: {
            'action': 2,
            'index': 167,
            't': 100,
            'i': 0.9999999999999999,
            'reward': -6.224232140000822
        },
        1: {
            'action': 39,
            'index': 173,
            't': 90,
            'i': 0.9999999999999999,
            'reward': 1.9899999999997817
        },
        2: {
            'action': 21,
            'index': 179,
Example #13
0
    "Price": "113.36"
}

test_2 = {
    "Symbol": "ADBE",
    "OrderID": "002",
    "Action": "A",
    "Exchange": "1",
    "Quantity": "329000",
    "News": "50",
    "Side": "S",
    "Description": "Adobe",
    "Price": "118.41"
}

order_book_1 = Orderbook()
order_book_1.add(test_1)
# print(Orderbook.all)
order_book_1.add(test_2)
#print(Orderbook.all)

assert len(order_book_1.book['ADBE']['B']) == 1
assert len(order_book_1.book['ADBE']['S']) == 1

test_3 = {
    "Symbol": "ADBE",
    "OrderID": "002",
    "Action": "M",
    "Exchange": "1",
    "Quantity": "362000",
    "News": "0",
Example #14
0
 def __init__(self, summary, set_description):
     self.item_id = set_description['id']
     self.etf_name = set_description['name']
     self.orderbook = Orderbook(summary, self.item_id)
     self.parts = [Orderbook(summary, part['id']) for part in set_description['parts']]
Example #15
0
 def getOrderbook(self):
     o = Orderbook()
     o.addState