Exemple #1
0
    def coin(self):
        from coinmarketcap import Market
        coinmarketcap = Market()
        text = time.strftime("%I:%M:%S %p", \
            time.localtime(int(coinmarketcap.ticker('bitcoin', convert="CNY")\
                               [0]['last_updated']))) + " updated\n\n"
        text += "\033[7;37;40mSym     Rank       %Cper           Prc        \
            Changes       \033[0m \n"

        for c in [
                'bitcoin', 'ethereum', 'ripple', 'bitcoin-cash', 'litecoin',
                'EOS', 'bitcoin-gold', 'qtum', 'bitshares', 'binance-coin',
                'digixdao', 'gxshares', 'decentraland'
        ]:
            msg = coinmarketcap.ticker(c, convert="CNY")[0]
            text += msg["symbol"].lower() + "\t" + msg["rank"] + "\t" + \
            str("%.8f"%float(msg["price_btc"])) + "\t" + \
            " " * (8-len(str("%.2f"%float(msg["price_cny"])))) + \
            str("%.2f"%float(msg["price_cny"])) + "\t" + \
            " " * (7-len(str("%.2f"%float(msg['percent_change_1h'])))) + \
             str("%.2f"%float(msg['percent_change_1h'])) + " /" +\
            " " * (7-len(str("%.2f"%float(msg['percent_change_24h'])))) + \
             str("%.2f"%float(msg['percent_change_24h'])) + " /" +\
            " " * (7-len(str("%.2f"%float(msg['percent_change_7d'])))) + \
             str("%.2f"%float(msg['percent_change_7d'])) + "\n"
        return text
Exemple #2
0
    def messaged_ticker(self, ticker_call):

        currency_dict = {
            "€": ["EUR", "price_eur", "market_cap_eur", "€"],
            "£": ["GBP", "price_gbp", "market_cap_gbp", "£"],
            "$": ["dollar", "price_usd", "market_cap_usd", "$"],
            "฿": ["bitcoin", "price_btc", "market_cap_usd", "$"],
            "Ƀ": ["bitcoin", "price_btc", "market_cap_usd", "$"]
        }

        coinmarketcap = Market()

        currency = ticker_call[0]
        out_curr = (currency_dict[currency][0])
        ticker1 = ticker_call[1:]

        if currency == '€' or '£':
            crypto_market = coinmarketcap.ticker(convert=out_curr)
        else:
            crypto_market = coinmarketcap.ticker(convert="")

        if currency == "฿":
            currency = "Ƀ"

        coin_data = self.sym_to_list(ticker1, crypto_market)
        if coin_data == False:
            return False

        url_name = self.list_to_id(coin_data)

        try:
            if currency != "Ƀ":
                self.ticker_info = (
                    coin_data.get('name') + '\n'
                    'Price: ' + currency + " {:0,.2f}".format(
                        float(coin_data.get(currency_dict[currency][1]))) +
                    '\n'
                    '1h: ' + coin_data.get('percent_change_1h') + ' %' + '\n'
                    '24h: ' + coin_data.get('percent_change_24h') + ' %' + '\n'
                    '7d: ' + coin_data.get('percent_change_7d') + ' %' + '\n'
                    'Market Cap: ' + currency_dict[currency][3] +
                    ' {:0,.2f}'.format(
                        float(coin_data.get(currency_dict[currency][2]))))
            else:
                self.ticker_info = (
                    coin_data.get('name') + '\n'
                    'Price: ' + currency + " {:0,.8f}".format(
                        float(coin_data.get(currency_dict[currency][1]))) +
                    '\n'
                    '1h: ' + coin_data.get('percent_change_1h') + ' %' + '\n'
                    '24h: ' + coin_data.get('percent_change_24h') + ' %' + '\n'
                    '7d: ' + coin_data.get('percent_change_7d') + ' %' + '\n'
                    'Market Cap: ' + currency_dict[currency][3] +
                    ' {:0,.2f}'.format(
                        float(coin_data.get(currency_dict[currency][2]))))

        except:
            self.ticker_info = 'Error, not found'

        return self.ticker_info
Exemple #3
0
def main():
    coinmarketcap = Market()
    markets_coin = coinmarketcap.ticker(limit=20)

    coins = []
    for i in range(0, 20):
        coins.append(
            Coin(name=markets_coin[i]['name'],
                 accronym=markets_coin[i]['symbol'],
                 in_usd=markets_coin[i]['price_usd'],
                 available_supply=markets_coin[i]['available_supply'],
                 total_supply=markets_coin[i]['total_supply']))

    print("\033c")
    for i in coins:
        sys.stdout.write(str(i))
    time.sleep(20)

    while (True):

        count_coin = 0
        markets_coin = coinmarketcap.ticker(limit=20)
        for i in coins:
            i.update(markets_coin[count_coin]['price_usd'])
            count_coin += 1

        print("\033c")
        for i in coins:
            sys.stdout.write(str(i))

        time.sleep(20)
def coinmarketcap():
    try:
        cmc = Market()
        for currency in currency_list:
            try:
                price_data = cmc.ticker(1567, convert=currency.upper())
                data_name = currency.upper()
                price_currency = price_data['data']['quotes'][data_name][
                    'price']
                print(
                    rdata.hset("prices",
                               "coinmarketcap:nano-" + currency.lower(),
                               price_currency),
                    "Coinmarketcap NANO-" + currency.upper(), price_currency)
            except:
                exc_type, exc_obj, exc_tb = sys.exc_info()
                print('exception', exc_type, exc_obj, exc_tb.tb_lineno)
                print("Failed to get price for NANO-" + currency.upper() +
                      " Error")
        price_data = cmc.ticker(1567, convert='BTC')
        price_btc = price_data['data']['quotes']['BTC']['price']
        print(rdata.hset("prices", "coinmarketcap:nano-btc", price_btc),
              price_btc)
        print(
            rdata.hset("prices", "coinmarketcap:lastupdate", int(time.time())),
            int(time.time()))
    except:
        exc_type, exc_obj, exc_tb = sys.exc_info()
        print('exception', exc_type, exc_obj, exc_tb.tb_lineno)
        print("Failed to load from CoinMarketCap")
Exemple #5
0
def scrapeCoin(index,dbloc):

    #Create market API instance
    coinmarketcap = Market()
    #Scrape relevant data
    bitcoin = coinmarketcap.ticker("bitcoin")
    litecoin = coinmarketcap.ticker("litecoin")
    vertcoin = coinmarketcap.ticker("vertcoin")
    #Load into variables
    btcout = bitcoin[0]
    ltcout = litecoin[0]
    vtcout = vertcoin[0]

    #Create coin array and dictionary
    coins = ["'Bitcoin'","'Litecoin'","'Vertcoin'"]
    coinDict = {"Bitcoin":0, "Litecoin":0, "Vertcoin":0}

    #Load values into dictionary
    if index == 0:
        coinDict["'Bitcoin'"] = btcout["price_usd"]
    elif index == 1:
        coinDict["'Litecoin'"] = ltcout["price_usd"]
    elif index == 2:
        coinDict["'Vertcoin'"] = vtcout["price_usd"]

    return coinDict[coins[index]]
def main(arguments):
    parser = argparse.ArgumentParser()
    parser.add_argument('currency', help="curency")
    parser.add_argument('key', help="Выводимый ключ")
    parser.add_argument('convert', help="convert to", default='USD')
    args = parser.parse_args(arguments)
    print(args.currency)
    coinmarketcap = Market()
    print(coinmarketcap.ticker(args.currency, limit=1, convert=args.currency))
    print(
        coinmarketcap.ticker(args.currency, limit=1,
                             convert=args.currency)[0].get(args.key))
Exemple #7
0
 def _convert_usd(self, currency):
     market = Market()
     if currency != 'USD':
         buy_price_USD = float(market.ticker(self.name)[0]['price_usd'])
     else:
         buy_price_USD = self.buy_price
     return buy_price_USD * self.quantity
Exemple #8
0
class Statistics:

    def __init__(self, default):
        self.market = Market()
        self.default = default

    def get_best_asset(self, period, ticker_range):
        best_asset_symbol = self.default
        best_asset_percentage = 0

        response = self.market.ticker(limit=100, convert='USD')
        ticker_list_length = len(response)
        for i in range(0, ticker_list_length):
            item = response[i]

            if item["symbol"] in str(ticker_range):
                if period == PeriodRange.HOUR:
                    if float(item["percent_change_1h"]) > float(best_asset_percentage):
                        best_asset_symbol = item["symbol"]
                        best_asset_percentage = item["percent_change_1h"]
                elif period == PeriodRange.DAY:
                    if float(item["percent_change_24h"]) > float(best_asset_percentage):
                        best_asset_symbol = item["symbol"]
                        best_asset_percentage = item["percent_change_24h"]
                elif period == PeriodRange.WEEK:
                    if float(item["percent_change_7d"]) > float(best_asset_percentage):
                        best_asset_symbol = item["symbol"]
                        best_asset_percentage = item["percent_change_7d"]

        return best_asset_symbol
Exemple #9
0
def get_weights(coins, fiat_currency):
    coinmarketcap = Market()

    market_cap = {}
    for c in coins:
        try:
            listings = coinmarketcap.listings()
            print(listings['data'][0])
            id = [
                item['id'] for item in listings['data'] if item['symbol'] == c
            ][0]
            ticker = coinmarketcap.ticker(id)
            print(ticker)
            market_cap[c] = \
                float(ticker['data']['quotes'][fiat_currency]['market_cap'])
        except HTTPError as e:
            print('caught exception when fetching ticker for {} with name={}'.
                  format(c, coins[c]['name']))
            raise e

    total_market_cap = sum(market_cap.values())

    weights = {}
    for c in coins:
        weights[c] = market_cap[c] / total_market_cap
    print('coin weights:')
    for w in weights:
        print('  {0}: {1:.4f}'.format(w, weights[w]))
    print()
    return weights
def symbol_id_list():
    coinmarketcap = Market()
    api_result = coinmarketcap.ticker(start=0, limit=0, convert='krw')
    for coin in api_result:  # loop over arrays
        coin_name = coin['id']
        coin_symbol = coin['symbol']
        symbol = Symbol.objects.create(coin_name=coin_name, symbol=coin_symbol)
Exemple #11
0
def collect_data(currency_name, price_list):
    coinmarketcap = Market()
    data = coinmarketcap.ticker(currency_name, limit=3, convert='USD')
    data = data.pop(0)
    now_time = datetime.now()
    now_time = str(now_time.strftime('%Y/%m/%d %H:%M'))
    data_list = [now_time]
    for key, value in data.items():
        if key == 'price_usd':
            value = float(value)
            data_list.append(value)
            if value > price_list[-1]:
                # Här kan vi skapa flera fält för att se till olika lagg
                data_list.append(1)
            else:
                data_list.append(0)
            price_list.append(value)
        elif key == 'price_btc':
            data_list.append(value)
        elif key == '24h_volume_usd':
            data_list.append(value)
        elif key == 'percent_change_1h':
            data_list.append(value)
        elif key == 'percent_change_24h':
            data_list.append(value)
        elif key == 'percent_change_7d':
            data_list.append(value)
    return data_list
def post_update_all(request):
    #symbol_id_list()
    user = request.user.id
    coin_dicts = Post.objects.filter(
        published_date__lte=timezone.now(),
        author_id=user).order_by('published_date').values(
            'coin_name', 'quantity', 'id')
    for coin_dict in coin_dicts:
        post = Post.objects.get(published_date__lte=timezone.now(),
                                id=coin_dict['id'])
        coinmarketcap = Market()
        coin = (coinmarketcap.ticker(coin_dict['coin_name'], convert='KRW'))[0]
        post.total_price_krw = float(coin["price_krw"]) * float(
            coin_dict['quantity'])
        post.price_krw = int(float(coin["price_krw"]))

        print(post.price_krw)
        post.price_usd = coin["price_usd"]
        post.price_btc = coin["price_btc"]
        post.author = request.user
        post.publish()
        post.save()
        print(post.coin_name, post.total_price_krw, post.price_krw)

    posts = Post.objects.filter(published_date__lte=timezone.now(),
                                author_id=user).order_by('published_date')
    return render(request, 'blog/post_list.html', {'posts': posts})
Exemple #13
0
def choose(bot, update, user_data):
    coin = Market()
    data = coin.ticker(1, convert="USD")
    price = str(data["data"]["quotes"]["USD"]["price"])
    update.message.reply_text(text="Ціна бітка станом на сьогодні: \n" +
                              price + " USD")
    print('here')
async def handle(client, config, message):
    if not is_channel_valid(config, 'crypto_channels', message):
        return

    if not has_prefix(config, message):
        return

    content = get_content_without_prefix(config, message)
    command_args = content.split()

    if len(command_args) != 2:
        return
    if command_args[0] != 'val':
        return

    client.send_typing(message.channel)
    command_args[1] = command_args[1].upper()
    if command_args[1] in currency_mapping:
        coin_market = Market()
        try:
            data = coin_market.ticker(currency_mapping[command_args[1]],
                                      limit=1,
                                      convert='CAD')[0]
            await client.send_message(message.channel,
                                      embed=__create_message(data))
        except Exception as err:
            await client.send_message(
                message.channel,
                'There was an error fetching the value of %s!' %
                command_args[1])
            print('Error fetching token %s' % command_args[1])
            print('%s\n' % err)
    else:
        await client.send_message(message.channel,
                                  'Token %s not found!' % command_args[1])
Exemple #15
0
def recebe_xrp(lista_xrp):
    market = Market()
    ticker = market.ticker(convert="BRL")
    data = ticker['data']['52']['quotes']['USD']['price']
    xrp = data
    lista_xrp.append(xrp)
    return lista_xrp
Exemple #16
0
 def getMarketPrice(self):
     coinmarketcap = Market()
     listings = coinmarketcap.ticker()
     for symbolOn in listings['data'].items():
         if symbolOn[1]['symbol']==self.symbol:
             return symbolOn[1]['quotes']['USD']['price']
     return 0.0
def main():
    print("""
    --  DASH CORRUPTED GOVERNANCE ATTACK SIMULATOR  --

    --  Please provide customised information for any parameter OR
    --  Press enter to proceed with the real time values  --
    """)

    # Retrieves Dash real time data.
    cmc = Market()
    coins = cmc.ticker(limit=20)
    dash = ''
    for i in range(0, 20):
        if coins[i]['id'] == 'dash':
            dash = coins[i]
            break

    # Retrieves master nodes real time data.
    mn_stats = 'https://stats.masternode.me/network-report/latest/json'
    req_stats = Request(mn_stats, headers={'User-Agent': 'Mozilla/5.0'})
    stats = json.loads(urlopen(req_stats).read().decode("utf-8"))

    i = 1  # Keep looping with same questions unless data in correct form is provided.
    while True:
        price = input(
            'Customised Price($): (press enter for real time price) ')
        exp = input(
            'Price Increase Factor (1-10)(1: Aggressive, 10: Slow): (press enter for default factor) '
        )
        mn = input(
            'Number of Active Masternodes:  (press enter for real time active MN) '
        )
        coins = input(
            'Coins in Circulation (in Millions):  (press enter for real time circulation) '
        )
        num_mn = input(
            'How many masternodes you want to control?:  (press enter for net 10% malicious MN) '
        )

        try:
            price = float(price) if price else float(dash['price_usd'])
            exp = float((int(exp) + 5) *
                        -1) if exp and (int(exp) > 0
                                        and int(exp) < 11) else float(-11.4)
            exp_incr = math.pow(math.e, exp)
            mn = int(mn) if mn else stats["raw"]["mn_count"]
            coins = float(coins * 1000000) if coins else float(
                dash['available_supply'])
            num_mn = int(num_mn) if num_mn else int((mn * 1.1) + 1)
            break
        except ValueError:
            pass

    while i <= price:
        ...
        i += 1

    report(price, exp_incr, mn, coins)
    cost, new_price = buy_X_MN(num_mn, price, exp_incr, coins, mn)
    print()
Exemple #18
0
def recebe_btc(lista_btc):
    market = Market()
    ticker = market.ticker(convert="BRL")
    data = ticker['data']['1']['quotes']['USD']['price']
    btc = data
    lista_btc.append(btc)
    return lista_btc
def get_currency_stats(cur_list):
    req = urllib.request.Request("https://api.coinmarketcap.com/v1/ticker/")
    opener = urllib.request.build_opener()
    f = opener.open(req)
    jso = json.loads(f.read().decode('utf-8'))
    total_bal = []
    for ci in cur_list:
        for cp in jso:
            if cp['symbol'] == ci['currency']:
                price_usd = float(cp['price_usd'].encode('utf-8'))
                cur_bal = float(ci['Balance'])
                usd_balance = price_usd * cur_bal
                total_bal.append(usd_balance)
                print(cp['name']+ ' $'+ str(usd_balance))
                coin_market = CMC.ticker(cp['id'])[0]
                print('Rank '+ coin_market['rank'],end=' | ')
                print('1HR %'+ coin_market['percent_change_1h'],end=' | ')
                print('24HR % '+ coin_market['percent_change_24h'],end=' | ')
                try:
                    print('7D %'+ coin_market['percent_change_7d'])
                except:
                    print('No Data')
                print("="*60)

    total_bal = reduce(lambda x, y: x+y, total_bal)
    total_bal = 'Balance $'+str(total_bal)
    return total_bal
class Coins(object):
    def __init__(self):
        self.coinmarketcap = Market()
        self.current_reference = 'BTC'

    def _parseresponse(self, response):
        result = []
        for coin in response['data'].keys():
            res_coin = {}
            for field, val in response['data'][coin].items():
                if isinstance(val, dict):
                    for currency in response['data'][coin][field].keys():
                        res_coin.update({
                            x + '_' + currency: y
                            for x, y in response['data'][coin][field]
                            [currency].items()
                        })
                else:
                    res_coin[field] = val
            result.append(res_coin)
        return result

    def get_coins_summary(self, start=0, limit=100):
        raw_data = self.coinmarketcap.ticker(start=start,
                                             limit=limit,
                                             convert=self.current_reference)
        data = pd.DataFrame(self._parseresponse(raw_data))
        data['DATETIME'] = pd.to_datetime('today').strftime("%Y-%m-%d")
        return data
def getCoinData(coin):
    cap = Market()

    data = cap.ticker(coin)
    if data == {'error': 'id not found'}:
        return None
    return data[0]
Exemple #22
0
class Strategy:
    def __init__(self, exchange=None, **kwargs):
        print('Strategy initializing...')
        self.exchange = exchange if exchange else DEFAULT_EXCHANGE
        self.type = ROLLBACK
        self.purchase_different_coins = 4  # 3-10 recommended
        self.drop_range_to_buy_pct = range(-40, -10)
        self.deposit_threshold_pct = 50
        self.capitalization_threshold_usd = float(20000000.0)  # > 30 billions recommended
        self.market_volume_threshold_usd_24h = 500000
        self.your_margin_pct = 5
        self.market = Market()
        self.drops = None
        self.suitable_coins_marketcap = {}
        self.suitable_tickers = []
        self.coins_listing = {item['symbol']: {'id': item['id'], 'name': item['name']} for item in self.market.listings()['data']}
        self.crypto_only = False  # include tokens, ico etc.
        self.currencies = None
        self.profit_policy = HALF_REINVEST
        for key in kwargs:
            if hasattr(self, key):
                setattr(self, key, kwargs[key])

    def fetch_currencies(self):
        curs = self.exchange.fetch_currencies()
        if self.crypto_only:
            curs = dict((k + '/' + BASE_TICKER, v) for k, v in curs.items() if v['type'] == 'crypto')
        else:
            curs = dict((k + '/' + BASE_TICKER, v) for k, v in curs.items())
        self.currencies = curs

    def fetch_suitable_coins(self):
        self.fetch_currencies()
        try:
            tickers = self.exchange.fetch_tickers()
        except:
            tickers = {}
        self.drops = {pair: data for pair, data in tickers.items()
                      if pair.endswith(BASE_TICKER)
                      and data['percentage'] is not None
                      and int(data['percentage']) in self.drop_range_to_buy_pct
                      and pair in self.currencies}
        if self.market:
            for pair, market_data in self.drops.items():
                ticker = pair.split('/')[0]
                if ticker in self.coins_listing:
                    market_cap_id = self.coins_listing[ticker]['id']
                    if bool(market_cap_id):
                        market_data = self.market.ticker(market_cap_id, convert=USD)['data']['quotes'][USD]
                        capital = market_data['market_cap']
                        if isinstance(capital, float):
                            if capital >= self.capitalization_threshold_usd:
                                    print(ticker, self.coins_listing[ticker]['name'], capital)
                                    self.suitable_coins_marketcap[ticker] = capital
                else:
                    print('Capitalization is unknown: {}... pass'.format(ticker))
                if len(self.suitable_coins_marketcap) > 0:
                    scm = self.suitable_coins_marketcap
                    self.suitable_tickers = sorted(scm, key=scm.get, reverse=True)[:self.purchase_different_coins]
Exemple #23
0
 def get_coin_data(self):
     coinmarketcap = Market()
     coin_data = coinmarketcap.ticker(limit=300, convert='EUR')
     coin_data = coin_data['data']
     coin2eur = {d['symbol']: d['quotes']['EUR']['price'] for d in coin_data.values()}
     coin2eur = helpers.recursive_replace(coin2eur, coin_thesaurus)
     self.coin2eur = self.sort_dict_desc(coin2eur)
     return coin_data
Exemple #24
0
def get_coin(coin=None):
    """ Get Price of Coin by USD """
    for i in coins:
        if coin.upper() == i['symbol']:
            coinmarketcap = Market()
            coin_info = coinmarketcap.ticker(currency=i['id'], convert='USD')
            return coin_info[0]
    return None
    def coinmarket(self, symbol, message):
        coinmarketcap = Market()
        try:
            result = coinmarketcap.ticker(symbol, limit=3, convert='EUR')
        except:
            return self.getCoinNotFoundError(message, 'Id not found')

        return self.handle_result_coinmarket(result)
Exemple #26
0
def get_current_crypto_price(crypto_name):
    coinmarketcap = Market()
    data = coinmarketcap.ticker()["data"]
    for chunk in data:
        name = data[chunk]["symbol"]
        price = data[chunk]["quotes"]["USD"]["price"]
        if name == crypto_name:
            return price
def getAllMarkets(limit=None):
    cap = Market()

    if not limit:
        stats = cap.stats()
        limit = stats['active_currencies'] + stats['active_assets']

    return cap.ticker(limit=limit)
Exemple #28
0
def price():
    b = BtcConverter()
    bitcoin_price = b.get_latest_price('USD')
    coinmarketcap = Market()
    y = coinmarketcap.ticker(start=0, limit=2, convert='USD')
    return {
        "btc": bitcoin_price,
        "eth": y["data"]["1027"]["quotes"]['USD']['price']
    }
Exemple #29
0
def recebe_btc(lista):
    market = Market()
    ticker = market.ticker(convert='BRL')
    data = ticker['data']

    btc = data['1']['quotes']['BRL']['price']

    lista.append(btc)
    return lista
Exemple #30
0
 def __init__(self):
     click.secho("[*] Getting Coinmarketcap data... ", nl=False)
     coinmarketcap = Market()
     dogecoin = coinmarketcap.ticker("Dogecoin", limit=3, convert="USD")[0]
     self.usdprice = dogecoin["price_usd"]
     self.btcprice = dogecoin["price_btc"]
     self.rank = dogecoin["rank"]
     self.supply = dogecoin["total_supply"]
     click.secho("Done", fg="green")
Exemple #31
0
import json
import os
from datetime import datetime
from coinmarketcap import Market
from apscheduler.schedulers.blocking import BlockingScheduler

coin_market = Market()
json_data = coin_market.ticker().decode('utf-8')
data = json.loads(json_data)

path = 'Data'
poloniex_symbols = ['ETH', 'XRP', 'ETC', 'GNT', 'STR', 'DOGE', 'XEM', 'LTC',
                    'DGB', 'XMR', 'ZEC', 'SC', 'BCN', 'DASH', 'BTS', 'BTM',
                    'FCT', 'STEEM', 'STRAT', 'REP', 'LSK', 'SYS', 'NXT',
                    'MAID', 'GAME', 'ARDR', 'RIC', 'GNO', 'DCR', 'AMP', 'VTC',
                    'CLAM', 'BURST', 'BCY', 'LBC', 'SJCX', 'NAV', 'PINK', 
                    'BTCD', 'EXP', 'PPC', 'XCP', 'NEOS', 'NAUT', 'POT',
                    'PASC', 'BELA', 'EMC2', 'BLK', 'FLO', 'FLDC', 'NMC', 'GRC',
                    'XVC', 'XPM', 'OMNI', 'SBD', 'XBC', 'NXC', 'VRC', 'HUC',
                    'NOTE'
                    ]

def increasing():
    for coin in data:
        if coin['percent_change_1h'] is not None:
            #check for volume over $1 million dollars
            if float(coin['24h_volume_usd']) > 1000000:
                percent_change_1h = float(coin['percent_change_1h'])
                if percent_change_1h > 10:
                    print (coin['name'])
Exemple #32
0
class Cerberus(object):
    def __init__(self, n_streams, master_lock, mode, keychain, loki):
        self.nP = n_streams
        self.mode = mode
        self.totalScore = 0
        self.lock = master_lock
        self.oracle = Oracle(keychain, loki)
        self.json_db_filename = json_db
        self.SQL_db_filename = sql_db
        self.nbatches = 0
        self.market = Market()
        self.initResources()

    def initResources(self):
        self.stacks = [Queue() for _ in range(self.nP)]
        if self.mode == 'mongo':
            self.client = MongoClient()
            self.db = self.client[mongo_db]

        if self.mode == 'SQL':
            self.db = self.SQL_db_filename
            self.conn = _sqlite3.connect(self.SQL_db_filename)
            self.curs = self.conn.cursor()
            self.curs.execute("CREATE TABLE IF NOT EXISTS tweets (tid integer, username text, cat text, content text, coordinates text, sentiment text, source text)")

        if self.mode == 'JSON':
            self.db = self.json_db_filename

    def db_count(self, table):
        r_query = "SELECT Count() FROM %s" % table
        self.curs.execute(r_query)
        n_rows = self.curs.fetchone()[0]
        return n_rows

    def handleNewTweet(self, pID, pDesc, tweet):
        self.lock.acquire()
        try:
            self.stacks[int(pID)].put(tweet)
        except Exception as e:
            print 'caught', e, 'while handling new entry'
        finally:
            self.lock.release()

    def printScore(self, batchc):

        if self.mode == 'mongo':
            name = self.mode + '.' + self.db.name
            dbc = sum([self.db[cname].count() for cname in self.db.collection_names()])
        else:
            name = self.db
            dbc = self.db_count('tweets')

        batch_number = 'Batch ' + str(self.nbatches)
        announcement = '    Wrote ' + str(sum(batchc)) + \
                       ' entities to ' + str(name) + ' for a total of ' + str(dbc) + '   '
        statistics = str(batchc)

        l = len(announcement)
        t1 = int((l - len(batch_number))/2)
        t2 = int((l - len(statistics))/2)

        print ''
        print t1*'-', batch_number, (l-len(batch_number)-t1)*'-'
        print announcement
        print t2*'-', statistics, (l-len(statistics)-t2)*'-'

    def readFromJSON(self):
        json_file = open(self.json_db_filename,'r')
        json_data = json.load(json_file)
        json_file.close()
        return json_data, len(json_data)

    def writeToJSON(self, json_data):
        json_file = open(self.json_db_filename,'w')
        json_file.write(json.dumps(json_data))
        json_file.close()
        return len(json_data)

    def executeBatch(self, dbs):

        self.lock.acquire()
        self.dbs = dbs
        self.nbatches += 1
        buffers = []
        batch_cnt = []
        try:
            for i, stack in enumerate(self.stacks):
                buf = []
                while not stack.empty():
                    tweet = stack.get(timeout = 3)
                    buf.append(tweet)
                buffers.append(buf)
                batch_cnt.append(len(buf))

            if len(buffers):
                if self.mode == 'JSON':
                    json_data = []
                    try:
                        json_data, u = self.readFromJSON()
                    except Exception as e:
                        print e, 'exception caught while writing to JSON'
                    for buf in buffers:
                        for tweet in buf:
                            json_data.append({'user': tweet.userID, 'tweet': tweet.tweetID, 'text': tweet.text,
                                              'created_at': str(tweet.createdAt), 'location': tweet.location, 'source': tweet.device})

                    self.writeToJSON(json_data)
                    score = len(buf)
                    self.totalScore += score
                    self.printScore(score, self.totalScore)

                if self.mode == 'SQL':
                    for buf in buffers:
                        for tweet in buf:
                            self.curs.execute("insert into tweets (tid, username, content, cat, /"
                                              "coordinates, sentiment, source) values(?, ?, ?, ?, ?, ?, ?)",
                                              (tweet.tweetID, tweet.userID, tweet.text, str(tweet.createdAt),
                                               tweet.location, tweet.sentiment, tweet.device))
                            self.conn.commit()

                    self.printScore(batch_cnt)

                if self.mode == 'mongo':
                    score = 0
                    for i, buf in enumerate(buffers):
                        col = self.dbs[i]
                        objs = []
                        
                        for obj in buf:
                            cat = date_convert(obj.json['created_at'])
                            objs.append({'created': cat, 'payload':obj.json})
                        try:
                            if len(objs):
                                self.db[col].insert_many(objs)
                        except Exception as e:
                            print(e)
                            raise
                        
                        try:
                            price = {'created': datetime.now() - timedelta(hours = 3),
                                     'payload':self.market.ticker(col)[0]}
                            self.db[col+'_price'].insert_one(price)
                        except Exception as e:
                            print(e)
                            raise

                    self.printScore(batch_cnt)

        except Exception as e:
            print type(e).__name__,'exception caught while writing to database'

        finally:
            self.lock.release()
            return batch_cnt

    def reboot(self):
        del self.stacks
        self.executeBatch()
        self.initResources()
Exemple #33
0
from coinmarketcap import Market
import coinmarketcap
c = CurrencyRates()
b=BtcConverter()
m=Market()
cc=CurrencyCodes()
allcoins={}
curlist=list(c.get_rates("USD").keys())
#print(c.get_rates("USD"))
curlist.append("USD")
curlist.sort()
for cur in curlist:
    allcoins[cur]=cc.get_currency_name(cur)
altcoinmap={}
#print (coinmarketcap.price("BTC"))
json=m.ticker(convert='EUR')
#print(json)
for currency in json:
    altcoinmap[currency["symbol"]]=currency["id"]
    allcoins[currency["symbol"]]=currency["name"]
#print(altcoinmap)
#print(json)
#print(m.ticker("bitcoin",convert="EUR")[0]["price_usd"]*100)
allcoins=SortedDict(allcoins)

wordhash={}
credithash={}
initialvalue=100000.00

investfile = '/home/bananapi/.sopel/modules/invest.xml'
Exemple #34
0
    config = configparser.ConfigParser()
    config.read(os.path.join(os.path.expanduser('~'), '.config', 'me.ini'))
    
    # prepare database connection
    dburl = config['db']['url']
    engine = create_engine(dburl)
    
    # get coin market cap data
    coinmarketcap = Market()

    # Since data is falling quite a bit, temporarly check if we got
    # it, and if not, increase the limit of records returned.
    contains_data = False
    limit = 125
    while not contains_data:
        markets = coinmarketcap.ticker(limit=limit, convert='EUR')
        mktdf = pd.DataFrame(markets)

        if len(mktdf[mktdf.symbol == 'DATA']) > 0:
            contains_data = True

        limit += 10
    
    mktdf = pd.DataFrame(markets)
    # convert some columns
    nonnumeric = ['cached', 'id', 'name', 'symbol']
    for col in [numcol for numcol in mktdf.columns if numcol not in nonnumeric]:
        mktdf[col] = pd.to_numeric(mktdf[col])
    # add a read date and save current values to the database
    mktdf['read_date'] = current_date
    mktdf = mktdf[['read_date'] + list(mktdf.columns[:-1])]