def cmd_forex(self, user, *msg): # todo consider accepting queries like !fx xau where the usd part of xauusd is just implicit if len(msg) == 1 and len(msg[0]) == 6: # has to be an invocation like !forex cnyusd amount = 1 from_currency = msg[0][:3] to_currency = msg[0][3:] elif len(msg) == 2 and len(msg[1]) == 6: # has to be an invocation like !forex 2345 eurusd amount = msg[0] from_currency = msg[1][:3] to_currency = msg[1][3:] elif len(msg) == 4 and msg[2].lower() == 'to': # has to be an invocation like !forex 123 cny to usd amount = msg[0] from_currency = msg[1] to_currency = msg[3] elif len(msg) == 0: # spit out help message # todo update help msg return ( "ECB forex rates, updated daily. Usage: " "{0}forex cnyusd, {0}forex 5000 mxneur, {0}forex 9001 eur to usd." ).format(self.config['command_prefix']) else: # unsupported usage return if from_currency == to_currency: return try: converted = self.forex.convert(amount, from_currency, to_currency) except ValueError as e: log.info('Forex conversion issue: {}'.format(e.message)) else: if converted <= 1e-4 or converted >= 1e20: # These are arbitrary but at least the upper cap is 100% required to avoid situations like # !forex 10e23892348 RUBUSD which DDoS the bot and then the channel once it finally prints it. return None amount_str = utils.truncatefloat(Decimal(amount), decimals=5, commas=True) converted_str = utils.truncatefloat(converted, decimals=5, commas=True) # todo display info on data source return "{} {} is {} {}".format(amount_str, from_currency.upper(), converted_str, to_currency.upper())
def cmd_forex(self, user, *msg): # todo consider accepting queries like !fx xau where the usd part of xauusd is just implicit if len(msg) == 1 and len(msg[0]) == 6: # has to be an invocation like !forex cnyusd amount = 1 from_currency = msg[0][:3] to_currency = msg[0][3:] elif len(msg) == 2 and len(msg[1]) == 6: # has to be an invocation like !forex 2345 eurusd amount = msg[0] from_currency = msg[1][:3] to_currency = msg[1][3:] elif len(msg) == 4 and msg[2].lower() == 'to': # has to be an invocation like !forex 123 cny to usd amount = msg[0] from_currency = msg[1] to_currency = msg[3] elif len(msg) == 0: # spit out help message # todo update help msg return ("ECB forex rates, updated daily. Usage: " "{0}forex cnyusd, {0}forex 5000 mxneur, {0}forex 9001 eur to usd.").format( self.config['command_prefix']) else: # unsupported usage return if from_currency == to_currency: return try: converted = self.forex.convert(amount, from_currency, to_currency) except ValueError as e: log.info('Forex conversion issue: {}'.format(e.message)) else: if converted <= 1e-4 or converted >= 1e20: # These are arbitrary but at least the upper cap is 100% required to avoid situations like # !forex 10e23892348 RUBUSD which DDoS the bot and then the channel once it finally prints it. return None amount_str = utils.truncatefloat(Decimal(amount), decimals=5, commas=True) converted_str = utils.truncatefloat(converted, decimals=5, commas=True) # todo display info on data source return "{} {} is {} {}".format(amount_str, from_currency.upper(), converted_str, to_currency.upper())
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 _send_alert(self, data): """Call with Trade object that has amount and price data.""" ann_str = u"Bitstamp alert | " try: if data.is_buy: ann_str += u'\u25B2 BUY ' else: ann_str += u'\u25BC SELL ' except AttributeError: pass amt_str = utils.truncatefloat(data.amount) ann = u"%s %s BTC at $%0.2f" % (ann_str, amt_str, data.price) log.info(ann.encode('utf8')) for cb in self.alert_callbacks: cb(ann)
def announce_whale_order(self, data): """Call with dict in form of {'amount': ordersize, 'price': orderprice}. Additional data in dict is ignored""" ann_str = u"Bitstamp alert | " if 'is_buy' in data: if data['is_buy'] is True: ann_str += u'\u25B2 BUY ' elif data['is_buy'] is False: ann_str += u'\u25BC SELL ' else: #ann_str = "Whale alert!" ann_str += "" amt_str = utils.truncatefloat(data['amount']) ann = u"%s %s BTC at $%0.2f" % (ann_str, amt_str, data['price']) log.info(ann.encode('utf8')) # sendline won't accept unicode, but moved the encoding into the actual callbacks self._send_alert(ann)
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))))