def cmd_swaps(self, user, *msg): if not self.bfx_swap_data_time or utils.now_in_utc_secs() - self.bfx_swap_data_time > 5*30: self.bfx_swap_data = dict() # 2 for loops here so all 3 requests get sent ASAP for currency in ('usd', 'btc', 'ltc'): self.bfx_swap_data[currency] = bitfinex.lends(currency) for c, d in self.bfx_swap_data.iteritems(): self.bfx_swap_data[c] = yield d self.bfx_swap_data_time = utils.now_in_utc_secs() swap_data = {} swap_data_strs = list() for currency in self.bfx_swap_data.iterkeys(): swap_data[currency] = Decimal(self.bfx_swap_data[currency][0]['amount_lent']) swap_data_strs.append('{} {}'.format(currency.upper(), utils.truncatefloat(swap_data[currency], commas=True))) defer.returnValue("Bitfinex open swaps: {}".format(', '.join(reversed(swap_data_strs))))
def _insert_flairupdate(self, rec): for k in ('price', 'usd_amt', 'btc_amt'): rec[k] = int(rec[k]) return self.dbpool.runQuery("""INSERT INTO ircflair(user, flairstatus, flairprice, usd_amount, btc_amount, timestamp) VALUES(?, ?, ?, ?, ?, ?)""", (rec['user'], rec['type'], rec['price'], rec['usd_amt'], rec['btc_amt'], utils.now_in_utc_secs()))
def on_orderbook(self, data): """Callback, called when new bitstamp orderbook data available""" self.orderbook = data if 'bids' in data and 'asks' in data and len(data['bids']) > 0 and len(data['asks']) > 0: self._highestbid = data['bids'][0][0] self._lowestask = data['asks'][0][0] self.last_orderbook = utils.now_in_utc_secs() else: log.warn("Bad orderbook data in on_orderbook: %s" % (data))
def _keep_orderbook_fresh(self): """Check that the orderbook data is fresh. Returns true if it is, or there isn't data, and false if it's stale.""" now = utils.now_in_utc_secs() if self.last_orderbook and now - self.last_orderbook > 60: # 60s+ since last orderbook return False else: # either the orderbook callback hasn't triggered yet or we have fresh data return True
def _update_user_flair(self, user, position, price, usd_amount): log.debug(("Changing {}'s flair to {} ({}) at ${:.2f} with a balance of ${:.2f}." .format(user, Position.to_text(position), position, price, usd_amount))) self.ratelimiter.user_event_now(user.lower()) record = FlairRow(user=user, position=position, price=price, usd_amount=usd_amount, timestamp=utils.now_in_utc_secs()) return self.dbpool.runQuery(""" INSERT INTO ircflair(user, position, price, usd_amount, timestamp) VALUES(?, ?, ?, ?, ?)""", (record.user, record.position, int(record.price*self.usd_pip), int(record.usd_amount*self.usd_pip), record.timestamp))
def on_orderbook(self, data): """Callback, called when new bitstamp orderbook data available""" self.orderbook = data if 'bids' in data and 'asks' in data and len(data['bids']) > 0 and len( data['asks']) > 0: self._highestbid = data['bids'][0]['price'] self._lowestask = data['asks'][0]['price'] self.last_orderbook = utils.now_in_utc_secs() else: log.warn("Bad orderbook data in on_orderbook: %s" % (data))
def cmd_swaps(self, user, *msg): if not self.bfx_swap_data_time or utils.now_in_utc_secs( ) - self.bfx_swap_data_time > 5 * 30: self.bfx_swap_data = dict() # 2 for loops here so all 3 requests get sent ASAP for currency in ('usd', 'btc', 'ltc'): self.bfx_swap_data[currency] = bitfinex.lends(currency) for c, d in self.bfx_swap_data.iteritems(): self.bfx_swap_data[c] = yield d self.bfx_swap_data_time = utils.now_in_utc_secs() swap_data = {} swap_data_strs = list() for currency in self.bfx_swap_data.iterkeys(): swap_data[currency] = Decimal( self.bfx_swap_data[currency][0]['amount_lent']) swap_data_strs.append('{} {}'.format( currency.upper(), utils.truncatefloat(swap_data[currency], commas=True))) defer.returnValue("Bitfinex open swaps: {}".format(', '.join( reversed(swap_data_strs))))
def _get_now(self): return utils.now_in_utc_secs()