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 test__009(self): """ compute_sell """ ob_js = { "uid": "71e689f5d4884dddb17f3f3b660b40f1", "created__long": 1503543603464, "modified__long": 1503543603468, "exchange": "gdax", "product": "eth-usd", "bids": [ { "price__num": 316.17, "size__num": 45.39426145, "num_orders__int": 1.0 }, { "price__num": 316.01, "size__num": 0.03269, "num_orders__int": 1.0 }, { "price__num": 316.0, "size__num": 22.6947, "num_orders__int": 6.0 } ] } ob = OrderBookModel.parse(ob_js) shares = 50.0 usd = helpers.compute_sell(shares, ob) direct_calculation = 316.17 * 45.39426145 + 316.01 * 0.03269 + 316.0 * (shares - 45.39426145 - 0.03269) assert usd == direct_calculation
def test__0011(self): """ compute_buy """ ob_js = { "uid": "71e689f5d4884dddb17f3f3b660b40f1", "created__long": 1503543603464, "modified__long": 1503543603468, "exchange": "gdax", "product": "eth-usd", "asks": [ { "price__num": 316.19, "size__num": 3.0, "num_orders__int": 1.0 }, { "price__num": 316.2, "size__num": 8.8439152, "num_orders__int": 1.0 }, { "price__num": 316.25, "size__num": 7.45, "num_orders__int": 2.0 } ] } ob = OrderBookModel.parse(ob_js) amount = 3000 shares = helpers.compute_buy(amount, ob) direct_calculation = 3.0 + (amount - 316.19*3)/316.2 assert shares == direct_calculation
def test__01(self): """ buy stopping price """ ob_js = { "uid": "71e689f5d4884dddb17f3f3b660b40f1", "created__long": 1503543603464, "modified__long": 1503543603468, "exchange": "gdax", "product": "eth-usd", "asks": [{ "price__num": 316.19, "size__num": 3.0, "num_orders__int": 1.0 }, { "price__num": 316.2, "size__num": 8.8439152, "num_orders__int": 1.0 }, { "price__num": 316.25, "size__num": 7.45, "num_orders__int": 2.0 }] } ob = OrderBookModel.parse(ob_js) shares = 11 stopping_price = helpers.compute_buy_stopping_price(shares, ob) direct_calculation = 316.2 assert stopping_price == direct_calculation
def test__02(self): """ sell stopping price """ ob_js = { "uid": "71e689f5d4884dddb17f3f3b660b40f1", "created__long": 1503543603464, "modified__long": 1503543603468, "exchange": "gdax", "product": "eth-usd", "bids": [{ "price__num": 316.17, "size__num": 2.39426145, "num_orders__int": 1.0 }, { "price__num": 316.01, "size__num": 0.03269, "num_orders__int": 1.0 }, { "price__num": 316.0, "size__num": 22.6947, "num_orders__int": 6.0 }] } ob = OrderBookModel.parse(ob_js) shares = 10 stopping_price = helpers.compute_sell_stopping_price(shares, ob) direct_calculation = 316.0 assert stopping_price == direct_calculation
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 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__007(self): ob_js = { "uid": "71e689f5d4884dddb17f3f3b660b40f1", "created__long": 1503543603464, "modified__long": 1503543603468, "exchange": "gdax", "product": "eth-usd", "asks": [ { "price__num": 316.19, "size__num": 25.86, "num_orders__int": 1.0 }, { "price__num": 316.2, "size__num": 8.8439152, "num_orders__int": 1.0 }, { "price__num": 316.25, "size__num": 7.45, "num_orders__int": 2.0 } ] } ob = OrderBookModel.parse(ob_js) shares = 40.0 usd = helpers.compute_usd_spent(shares, ob) direct_calculation = 316.19 * 25.86 + 316.2 * 8.8439152 + 316.25 * (shares - 25.86 - 8.8439152) assert usd == direct_calculation
def test__compute_money_made__01(self): ob_js = { "uid": "71e689f5d4884dddb17f3f3b660b40f1", "created__long": 1503543603464, "modified__long": 1503543603468, "exchange": "gdax", "product": "eth-usd", "timestamp__long": 1503543603463, "sequence__long": 1036028392, "bids": [ { "price__num": 316.17, "size__num": 2.39426145, "num_orders__int": 1.0 }, { "price__num": 316.01, "size__num": 0.03269, "num_orders__int": 1.0 }, { "price__num": 316.0, "size__num": 22.6947, "num_orders__int": 6.0 } ], } ob = OrderBookModel.parse(ob_js) shares = 20 usd = helpers.compute_usd_made(shares, ob) direct_calculation = 316.17*2.39426145 + 316.01*0.03269 + 316.0 * (shares - 2.39426145- 0.03269) assert usd == direct_calculation
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__parse_order_book_model_should_pass__001(self): js = { "uid": "5cdf981ca55344fc84f390faa10dbafb", "key1__bool": False, "key2__long": 343, "product": "usdeth", "exchange": "gdax", } ob = OrderBookModel.parse(js)
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__computing_sell_eth_value__014(self): """ computing sell value of some number of shares """ ob_js = { "uid": "71e689f5d4884dddb17f3f3b660b40f1", "created__long": 1503543603464, "modified__long": 1503543603468, "exchange": "gdax", "product": "eth-usd", "bids": [ { "price__num": 316.17, "size__num": 2.39426145, "num_orders__int": 1.0 }, { "price__num": 316.01, "size__num": 0.03269, "num_orders__int": 1.0 }, { "price__num": 316.0, "size__num": 22.6947, "num_orders__int": 6.0 } ], "asks": [ { "price__num": 316.19, "size__num": 3.0, "num_orders__int": 1.0 }, { "price__num": 316.2, "size__num": 8.8439152, "num_orders__int": 1.0 }, { "price__num": 316.25, "size__num": 7.45, "num_orders__int": 2.0 } ] } ob = OrderBookModel.parse(ob_js) shares = 3.0 shares_value = helpers.compute_sell_value(shares, ob) direct_cal = 2.39426145 * 316.17 + 0.03269 * 316.01 + (3 - 2.39426145 - 0.03269) * 316.0 assert shares_value == direct_cal
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
def test__parse_order_book_model_should_fail__001(self): """ should fail due to missing key """ js = { # "uid": "5cdf981ca55344fc84f390faa10dbafb", "key1__bool": False, "key2__long": 343, "product": "usdeth", "exchange": "gdax", } got_error = False try: ob = OrderBookModel.parse(js) except ModelKeyValueException as e: got_error = True assert got_error
def test__03(self): """ sell stopping price """ ob_js = { "uid": "71e689f5d4884dddb17f3f3b660b40f1", "created__long": 1503543603464, "modified__long": 1503543603468, "exchange": "gdax", "product": "eth-usd", "bids": [{ "price__num": 316.17, "size__num": 2.39426145, "num_orders__int": 1.0 }, { "price__num": 316.01, "size__num": 0.03269, "num_orders__int": 1.0 }, { "price__num": 316.0, "size__num": 22.6947, "num_orders__int": 6.0 }] } ob = OrderBookModel.parse(ob_js) shares = 10 stopping_price = helpers.compute_sell_stopping_price(shares, ob) logger.info(stopping_price * 0.999) lowerbound = helpers.compute_sell_lowerbound(shares, ob) logger.info(ob) logger.info(stopping_price) logger.info(lowerbound)