Esempio n. 1
0
def run():
    while True:
        try:
            collect_order_book()
        except Exception as e:
            sentry_client.captureException()
            tb = traceback.format_exc()
            logger.error(tb)

        time.sleep(delay)
Esempio n. 2
0
def try_delta_buy_gdax_sell_cex(amount, timestamp, search_window, default = None):
    ans = default
    try:
        gdax_ob = get_by_timestamp(timestamp, 'gdax', search_window)
        cex_ob = get_by_timestamp(timestamp, 'cex', search_window)
        ans = delta(amount, gdax_ob, cex_ob)
    except TypeError:
        stacktrace = traceback.format_exc()
        logger.error(stacktrace)

    return ans
Esempio n. 3
0
    def snap(self, timestamp=-1, fail_fast=True):
        snap_result = None
        if fail_fast:
            snap_result = self.strategy.snap(timestamp)
        else:
            try:
                snap_result = self.strategy.snap(timestamp)
            except:  # any Exception
                sentry_client.captureException()
                logger.error(traceback.format_exc())  # log traceback

        return snap_result
Esempio n. 4
0
    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)
Esempio n. 5
0
    def get_balance(self, ticker):
        gdax_ticker = self.gdax_ticker_from_ticker[ticker]
        accounts = self.auth_client.get_accounts()

        balance = None
        for account in accounts:
            if gdax_ticker == account['currency']:
                balance = account['available']
                break

        # Validate an assumption: only one account per currency
        currencies = set([x['currency'] for x in accounts])
        if len(currencies) != len(accounts):
            logger.error(pretty_json(accounts))
            raise RuntimeError(
                'There are too many accounts. Assumption is violated.')

        # Fail fast if some unknow thing happen
        if balance is None:
            logger.error(pretty_json(accounts))
            raise RuntimeError('Cannot get balance')

        return balance
Esempio n. 6
0
    def place_limit_order(self, side, ticker, price, shares, timestamp=-1):
        if timestamp != -1:
            raise RuntimeError('should always be -1')

        # fix size precision
        shares = float("{0:.1f}".format(shares))

        exh_client = self._get_exh_client()
        if side == 'buy':
            exh_client.place_limit_order('buy', ticker, price, shares)
            account = self.sync_account_with_exh()
        elif side == 'sell':
            exh_client.place_limit_order('sell', ticker, price, shares)
            account = self.sync_account_with_exh()
        else:
            msg = 'side:{}|ticker:{}|price:{}|shares:{}'.format(
                side, ticker, price, shares)
            logger.error(msg)
            raise RuntimeError('unknown market execution')

        # TODO: create an audit trails for transactions

        return account
Esempio n. 7
0
    def get_balances(self):
        gdax_tickers = self.gdax_ticker_from_ticker.values()
        accounts = self.auth_client.get_accounts()

        balances = OrderedDict()
        for account in accounts:
            gdax_ticker = account['currency']
            if gdax_ticker in gdax_tickers:
                ticker = self.ticker_from_gdax_ticker[gdax_ticker]
                balances[ticker] = account['available']

        # Validate an assumption: only one account per currency
        currencies = set([x['currency'] for x in accounts])
        if len(currencies) != len(accounts):
            logger.error(pretty_json(accounts))
            raise RuntimeError(
                'There are too many accounts. Assumption is violated.')

        # More validations
        if not balances:
            logger.error(pretty_json(accounts))
            raise RuntimeError('Cannot get balance')

        return balances