Ejemplo n.º 1
0
def _okex_tick():
    # 链接 okex行情
    ws = create_connection("wss://real.okex.com:10441/websocket")
    sym_dic = cfg.get_symbols('okex')
    symbols = []
    for k in sym_dic:
        for i in sym_dic[k]:
            tradeStr = '%s_%s' % (i, k)
            symbols.append(tradeStr)
    print(len(symbols))
    for symbol in symbols:
        item = {}
        item['event'] = 'addChannel'
        item['channel'] = 'ok_sub_spot_%s_ticker' % (symbol)
        sub_str = json.dumps(item)
        ws.send(sub_str)
    while (1):
        result = ws.recv()
        res = json.loads(result)[0]
        if 'data' in res and res['channel'] != 'addChannel':
            channel = res['channel'].strip().split('_')
            key = 'tick/%s/%s%s' % ('okex', channel[3], channel[4])
            res = parse_price('okex', res)
            store.set(key, json.dumps(res))
    pass
Ejemplo n.º 2
0
def _bitfinex_tick():
    # 连接bitfinex行情
    ws = create_connection("wss://api.bitfinex.com/ws/2")
    sym_dic = cfg.get_symbols('bitfinex')
    symbols = []
    for k in sym_dic:
        for i in sym_dic[k]:
            tradeStr = '%s%s' % (i, k)
            symbols.append(tradeStr.upper())
    print(len(symbols))
    for symbol in symbols:
        item = {}
        item['event'] = 'subscribe'
        item['channel'] = 'ticker'
        item['symbol'] = symbol
        sub_str = json.dumps(item)
        ws.send(sub_str)
    id_map = {}
    while (1):
        result = ws.recv()
        if 'event' in result:
            res = json.loads(result)
            if res['event'] == 'subscribed':
                id_map[res['chanId']] = res['pair']
        elif 'hb' not in result:
            res = json.loads(result)
            ch_id = res[0]
            if ch_id in id_map:
                key = 'tick/bitfinex/%s' % (id_map[ch_id].lower())
                res = parse_price('bitfinex', res)
                store.set(key, json.dumps(res))
    pass
Ejemplo n.º 3
0
def _binance_tick():
    currency_list = []
    sym_dic = cfg.get_symbols('binance')
    symbols = []
    for k in sym_dic:
        for i in sym_dic[k]:
            tradeStr = 'binance/%s.%s' % (i, k)
            currency_list.append(tradeStr)
    ws = create_connection("wss://1token.trade/api/v1/ws/tick")
    auth_data = {"uri": "auth"}
    ws.send(json.dumps(auth_data))
    for cur in currency_list:
        request_data = {
            "uri": "subscribe-single-tick-verbose",
            "contract": cur
        }
        ws.send(json.dumps(request_data))
    while True:
        data = ws.recv()
        json_data = json.loads(data)
        if 'data' in json_data:
            currency = json_data['data']['contract']
            key = 'tick/%s' % (currency.replace('.', ''))
            res = parse_price('binance', json_data['data'])
            store.set(key, json.dumps(res))
    pass
Ejemplo n.º 4
0
def _otcbtc_tick():
    while True:
        url = 'https://bb.otcbtc.com/api/v2/tickers'
        response = requests.request("GET", url)
        data = json.loads(response.text)
        for d in data:
            key = 'tick/otcbtc/%s' % (d.replace('_', ''))
            res = parse_price('otcbtc', data[d])
            store.set(key, json.dumps(res))
        time.sleep(1)
    pass
Ejemplo n.º 5
0
def _bigone_tick():
    while True:
        url = 'https://big.one/api/v2/tickers'
        response = requests.request("GET", url)
        if 'html' not in response.text:
            res_json = json.loads(response.text)
            for r in res_json:
                key = 'tick/bigone/%s' % (r['market_id'].replace('-',
                                                                 '').lower())
                res = parse_price('bigone', r)
                store.set(key, json.dumps(res))
            time.sleep(0.5)
        else:
            return
    pass
Ejemplo n.º 6
0
def _kucoin_tick():
    sym_dic = cfg.get_symbols('kucoin')
    symbols = []
    for k in sym_dic:
        for i in sym_dic[k]:
            tradeStr = '%s-%s' % (i, k)
            symbols.append(tradeStr.upper())
    while True:
        for sym in symbols:
            url = 'https://api.kucoin.com/v1/%s/open/tick' % (sym)
            response = requests.request("GET", url)
            tick_data = json.loads(response.text)
            key = 'tick/kucoin/%s' % (sym.replace('-', '').lower())
            res = parse_price('kucoin', tick_data)
            store.set(key, json.dumps(res))
            time.sleep(0.05)
    pass
Ejemplo n.º 7
0
def _bibox_tick():
    sym_dic = cfg.get_symbols('bibox')
    symbols = []
    for k in sym_dic:
        for i in sym_dic[k]:
            tradeStr = '%s_%s' % (i, k)
            symbols.append(tradeStr.upper())
    while True:
        for sym in symbols:
            url = 'https://api.bibox.com/v1/mdata?cmd=ticker&pair=%s' % (sym)
            response = requests.request("GET", url)
            res = json.loads(response.text)
            #dep = json.loads(response.text)
            key = 'tick/%s/%s' % ('bibox', sym.replace('_', '').lower())
            res = parse_price('bibox', res)
            store.set(key, json.dumps(res))
            time.sleep(0.2)
    pass
Ejemplo n.º 8
0
def _fcoin_tick():
    ws = create_connection("wss://api.fcoin.com/v2/ws")
    sym_dic = cfg.get_symbols('fcoin')
    symbols = []
    for k in sym_dic:
        for i in sym_dic[k]:
            tradeStr = 'ticker.%s%s' % (i, k)
            symbols.append(tradeStr)
    item = {}
    item['cmd'] = 'sub'
    item['id'] = '2'
    item['args'] = symbols
    sub_str = json.dumps(item)
    ws.send(sub_str)
    while (1):
        result = ws.recv()
        json_res = json.loads(result)
        if 'ticker' in json_res:
            key = 'tick/fcoin/%s' % (json_res['type'].split('.')[1])
            res = parse_price('fcoin', json_res)
            store.set(key, json.dumps(res))
    pass
Ejemplo n.º 9
0
def _zb_tick():
    # 连接zb行情
    ws = create_connection("wss://api.zb.cn:9999/websocket")
    sym_dic = cfg.get_symbols('zb')
    symbols = []
    for k in sym_dic:
        for i in sym_dic[k]:
            tradeStr = '%s%s' % (i, k)
            symbols.append(tradeStr)
    for s in symbols:
        item = {}
        item['event'] = 'addChannel'
        item['channel'] = '%s_ticker' % (s)
        sub_str = json.dumps(item)
        ws.send(sub_str)
    while (1):
        result = ws.recv()
        res_json = json.loads(result)
        key = 'tick/zb/%s' % (res_json['channel'].split('_')[0])
        res = parse_price('zb', res_json)
        store.set(key, json.dumps(res))
    pass
Ejemplo n.º 10
0
def _huobi_tick():
    # 连接火币行情
    #ws = create_connection("wss://api.huobi.br.com/ws")
    ws = create_connection("wss://api.huobipro.com/ws")
    sym_dic = cfg.get_symbols('huobi')
    symbols = []
    for k in sym_dic:
        for i in sym_dic[k]:
            tradeStr = '%s%s' % (i, k)
            symbols.append(tradeStr)
    i = 0
    for s in symbols:
        item = {}
        item['sub'] = 'market.%s.detail' % (s)
        item['id'] = 'id30' + str(i)
        sub_str = json.dumps(item)
        ws.send(sub_str)
        i += 1
    while (1):
        compressData = ws.recv()
        if not isinstance(compressData, bytes):
            print(type(compressData))
            break
        result = gzip.decompress(compressData).decode('utf-8')
        if result[:7] == '{"ping"':
            ts = result[8:21]
            pong = '{"pong":' + ts + '}'
            ws.send(pong)
        else:
            res = json.loads(result)
            if 'tick' in res:
                coin_pair = res['ch'].strip().split('.')[1]
                key = 'tick/huobi/%s' % (coin_pair)
                res = parse_price('huobi', res)
                store.set(key, json.dumps(res))
    pass