def collect_order_book(): gdax_order_book = gdax_client.get_order_book(ticker='eth', level=2) gob_model = OrderBookModel.build(gdax_order_book) gob_model.db_save(es) cex_order_book = cex_client.get_order_book(ticker='eth', level=2) cob_model = OrderBookModel.build(cex_order_book) cob_model.db_save(es) timestamp = gob_model.js['timestamp__long'] logger.info("saved data: {0}, {1}".format(str(timestamp), epoch.to_str(timestamp)))
def get_order_book_data(self): try: gdax_order_book = self.gdax_public_client.get_product_order_book('ETH-USD', 2) gob_model = OrderBookModel.build(gdax_order_book) gob_model.db_save(es) cex_order_book = self.cex_public_client.get_product_order_book('ETH-USD') cob_model = OrderBookModel.build(cex_order_book) cob_model.db_save(es) logger.info("saved data: gdx:{0} | cex:{1}".format(gob_model.uid, cob_model.uid)) except Exception: sentry_client.captureException() tb = traceback.format_exc() logger.error(tb)
def get_order_book(self, product_id=None, ticker=None, timestamp=-1): """ Search from timstamp, and work backward until one is found. :return: OrderbookModel """ if timestamp == -1: timestamp = epoch.current_milli_time() search_window = 11000 # 11 seconds search_window = 15 * 60000 # 15 minutes search_window = 1000 * 60000 # 1000 minutes (t0, t1) = timestamp - search_window, timestamp + 500 query = { "size": 1, "sort": [ { "timestamp__long": { "order": "desc" } } ], "query": { "bool": { "must": [ { "range": { "timestamp__long": { "gte": t0, "lte": t1 } } }, { "match": { "exchange.raw": self.exh } }, { "match": { "product.raw": 'eth-usd' } } ] } } } params = default_es_get_params() result = es.search("order_book", "data", query, params=params) # validation if result['hits']['total'] == 0: logger.info(pretty_json(query)) logger.info(timestamp) logger.info(epoch.to_str(timestamp)) raise RuntimeError('Cannot find orderbook in backtesting') ob_js = result['hits']['hits'][0]['_source'] ob = OrderBookModel.build(ob_js) return ob
def get_order_book(self, product_id=None, ticker=None, timestamp=-1): if timestamp != -1: raise RuntimeError('should always be -1') exh_client = self._get_exh_client() ob_js = exh_client.get_order_book(product_id=product_id, ticker=ticker, level=2) ob = OrderBookModel.build(ob_js) return ob
def test__build_order_book_model__001(self): js1 = { "uid": "5cdf981ca55344fc84f390faa10dbafb", "key1__bool": False, "key2__long": 343, "key3": "buy", "exchange": "gdax", "product": "sdfds" } uid = js1['uid'] ob = OrderBookModel.build(js1) assert ob.js['uid'] == uid
def test__db_save__and_db_get(self): js = { "product": "usdeth", "exchange": "gdax", } ob = OrderBookModel.build(js) uid = ob.uid # saving data ob.db_save(es) # get data js = ob.db_get(es, uid) ob2 = OrderBookModel.parse(js) assert ob == ob2
def test__build_order_book_model__002(self): js = { "key1__bool": False, "key2__long": 343, # "product": "usdeth", "exchange": "gdax", "key3": "buy" } got_error = False try: ob = OrderBookModel.build(js) except ModelKeyValueException as e: got_error = True assert got_error