Exemplo n.º 1
0
def replace_exchange_code_with_name(items):
    replaced = {}
    for item in items:
        item['exchange'] = get_source_name(item['source'])
        item.pop('source', None)
        replaced.update(item)
    return items
Exemplo n.º 2
0
def process_message_from_queue(message_body):
    "Save SQS message to DB: Price and Volume"

    body_dict = json.loads(message_body)
    exchange = json.loads(body_dict['Message'])
    processed = []

    for item in exchange:
        #logger.debug("Save {category} for {symbol} from {source}".format(**item))

        source_code = next((code for code, source_text in SOURCE_CHOICES
                            if source_text == item['source']), None)

        (transaction_currency,
         counter_curency_text) = item['symbol'].split('/')

        counter_currency_code = next(
            (code for code, counter_currency in COUNTER_CURRENCY_CHOICES
             if counter_currency == counter_curency_text), None)
        if None in (source_code, counter_currency_code):
            continue  # skip this source or counter_currency

        if 'price' == item['category']:
            price = int(float(item['value']) * 10**8)  # convert to satoshi
            try:
                Price.objects.create(source=source_code,
                                     transaction_currency=transaction_currency,
                                     counter_currency=counter_currency_code,
                                     price=price,
                                     timestamp=item['timestamp'])
                processed.append("{}/{}".format(transaction_currency,
                                                counter_currency_code))
                #logger.debug(">>> Price saved: source={}, transaction_currency={}, counter_currency={}, price={}, timestamp={}".format(
                #            source_code, transaction_currency, counter_currency_code, price, item['timestamp']))
            except Exception as e:
                logger.debug(">>>> Error saving Price for {}: {}".format(
                    item['symbol'], e))

        elif 'volume' == item['category']:
            volume = float(item['value'])
            try:
                Volume.objects.create(
                    source=source_code,
                    transaction_currency=transaction_currency,
                    counter_currency=counter_currency_code,
                    volume=volume,
                    timestamp=item['timestamp'])
                #logger.debug(">>> Volume saved: source={}, transaction_currency={}, counter_currency={}, volume={}, timestamp={}".format(
                #            source_code, transaction_currency, counter_currency_code, volume, item['timestamp']))
            except Exception as e:
                logger.debug(">>>> Error saving Volume for {}: {}".format(
                    item['symbol'], e))

    #logger.debug("Message for {} saved to db. Coins: {}".format(get_source_name(source_code), ",".join(processed)))
    logger.info("Message for {} ({}) saved to db".format(
        get_source_name(source_code), len(processed)))
Exemplo n.º 3
0
def info_view(update):
    view = f'Hello, hello, {update.message.from_user.first_name}!\n\n'

    exchanges = (get_source_name(index).capitalize() for index in get_exchanges())
    view += f'I support these exchanges: `{", ".join(exchanges)}\n\n`'

    period_in_seconds = 2*60*60 if not LOCAL else 2000*60*60
    trading_pairs = get_currency_pairs(source='all', period_in_seconds=period_in_seconds, counter_currency_format="text")

    coins = set(coin for coin, _ in trading_pairs)
    view += f'And {len(coins)} coins:\n`{", ".join(coins)}`\n\n'

    view += f'And {len(trading_pairs)} trading pairs, like `BTC_USDT, ETH_BTC, XRP_ETH ...`\nI love long text messages, but this message is already too long, even for me 🙂  '

    return view
Exemplo n.º 4
0
def price_view(trading_pair):
    view = ''

    if trading_pair['counter_currency'] == 'USDT':
        currency_symbol = '$'
    else:
        currency_symbol = ''

    counter_currency = COUNTER_CURRENCIES.index(
        trading_pair['counter_currency'])
    currency = trading_pair['transaction_currency']

    exchanges = get_exchanges()

    last_prices_object = Price.objects.filter(
        source__in=exchanges,
        transaction_currency=currency,
        counter_currency=counter_currency).order_by(
            '-timestamp')[:len(exchanges)]

    seen = set()
    seen_add = seen.add
    # remove dublicates
    unique_last_prices = [
        price for price in last_prices_object
        if not (price.source in seen or seen_add(price.source))
    ]
    exchanges_with_price = natural_join([
        get_source_name(price.source).title() for price in unique_last_prices
    ])

    view += f"I found *{currency}*\_{trading_pair['counter_currency']} in {exchanges_with_price}\n"

    for price_obj in sorted(unique_last_prices, key=lambda pr: pr.price):
        view += f"\n*{format_currency(price_obj.price, currency_symbol)}* on {get_source_name(price_obj.source).title()} at {format_timestamp(price_obj.timestamp)}"

    return view
Exemplo n.º 5
0
 def test_get_source_name(self):
     self.assertEqual(get_source_name(0), 'poloniex')
     self.assertEqual(get_source_name(2), 'binance')