示例#1
0
def api_topcoins():
    count = request.args.get('c', '')

    client = settings['db_adapter']
    service = kline_service(client, settings)

    to_time = long(time.time())
    from_time = to_time - 24 * 3600
    rank = service.query_symbol_daily_rank(from_time, to_time, int(count))

    if rank:
        top_coins = [{
            "name": coin[1],
            "symbol": "%susdt" % coin[1].lower(),
            "latest_price": coin[2],
            "rank": coin[3],
            "fullname": coin[4],
            "market_cap_usd": coin[5],
            "percent_change_24h": coin[6],
            "volume_usd_24h": coin[7]
        } for coin in rank]

        return json.dumps(top_coins)
    else:
        return json.dumps({})
示例#2
0
def api_monitor():
    client = settings['db_adapter']
    service = kline_service(client, settings)

    result = []
    result.append(settings['symbols'])
    monitors = service.query_monitor()
    if monitors:
        monitor_item = [{
            "time": monitor[0],
            "market": monitor[1],
            "symbol": monitor[2],
            "update_time": monitor[3]
        } for monitor in monitors]
        result.append(monitor_item)
        return json.dumps(result)
    else:
        return json.dumps(result)
示例#3
0
def api_validate():
    start_time = request.args.get('s', '')
    end_time = request.args.get('e', '')
    market = request.args.get('m', '')
    symbol = request.args.get('b', '')
    client = settings['db_adapter']
    service = kline_service(client, settings)

    validates = service.query_validation(long(start_time), long(end_time),
                                         market, symbol)
    if validates:
        validate_item = [{
            "time": validate[0],
            "market": validate[1],
            "symbol": validate[2]
        } for validate in validates]
        result = []
        for val in validate_item:
            if len(result) == 0:
                result.append({
                    'market': val['market'],
                    'symbol': val['symbol'],
                    'count': 1
                })
            else:
                match = None
                for itm in result:
                    if (itm['market'] == val['market']
                            and itm['symbol'] == val['symbol']):
                        match = itm
                        break
                if match is None:
                    result.append({
                        'market': val['market'],
                        'symbol': val['symbol'],
                        'count': 1
                    })
                else:
                    match['count'] = match['count'] + 1
        return json.dumps(result)
    else:
        return json.dumps([{}])
示例#4
0
def api_index():
    index_type = request.args.get('t', '')
    size = request.args.get('w', '')

    client = settings['db_adapter']
    service = kline_service(client, settings)

    data = service.query_latest_index(index_type, int(size))
    series = data['series'][0]
    values = []

    for row_index in range(len(series['values'])):
        value = {}
        value['direction'] = 'up'
        for column_index in range(len(series['columns'])):
            value[series['columns']
                  [column_index]] = series['values'][row_index][column_index]
        values.append(value)

    return json.dumps(values)
示例#5
0
def api_trend():
    symbol = request.args.get('s', '')

    client = settings['db_adapter']
    support_markets = settings['markets'].keys()
    support_symbols = settings['symbols']['default']
    to_time = int(time.time())
    from_time = to_time - 4 * 24 * 3600

    if not symbol in support_symbols:
        return 'not supported'

    service = kline_service(client, settings)
    kline = service.get_trend_by_symbol(symbol, from_time, to_time, 'D')

    if kline and kline.has_key('series'):
        columns = kline['series'][0]['columns']
        for i in range(0, len(columns)):
            if columns[i] == 'time':
                time_index = i
            elif columns[i] == 'price':
                price_index = i

        timezone_offset = settings['timezone_offset']

        ticks = kline['series'][0]['values']
        ticks.sort(lambda x, y: cmp(x[time_index], y[time_index]))

        kline = [[
            get_timestamp_str_date(tick[time_index], 0),
            float(tick[price_index])
        ] for tick in ticks if not (tick[price_index] == None)]

        return json.dumps(kline)
    else:
        return json.dumps({})
示例#6
0
def api_kline():
    market = request.args.get('m', '')
    symbol = request.args.get('s', '')
    size = request.args.get('w', '')
    resolution = request.args.get('r', '')

    client = settings['db_adapter']
    support_markets = settings['markets'].keys()
    support_symbols = settings['symbols']['default']

    if (not symbol == 'index' and not symbol == 'innovation') and (
            not market in support_markets or not symbol in support_symbols):
        return 'not supported'

    if (not size or size == ''):
        size = int(settings['kline']['size'])
    else:
        size = int(size)

    if (not resolution or resolution == ''):
        resolution = '1'

    service = kline_service(client, settings)
    kline = service.get_kline_by_market_symbol(market, symbol, resolution,
                                               size)

    if kline and kline.has_key('series'):
        columns = kline['series'][0]['columns']
        for i in range(0, len(columns)):
            if columns[i] == 'time':
                time_index = i
            elif columns[i] == 'open':
                open_index = i
            elif columns[i] == 'close':
                close_index = i
            elif columns[i] == 'low':
                low_index = i
            elif columns[i] == 'high':
                high_index = i
            elif columns[i] == 'volume':
                volume_index = i

        timezone_offset = settings['timezone_offset']

        ticks = kline['series'][0]['values']
        ticks.sort(lambda x, y: cmp(x[time_index], y[time_index]))

        kline = [
            [
                get_timestamp_str_short(tick[time_index], 0),
                float(tick[open_index]),
                float(tick[close_index]),
                float(tick[low_index]),
                float(tick[high_index]),
                float(tick[volume_index])
            ] for tick in ticks
            if not (tick[open_index] == None or tick[close_index] == None
                    or tick[high_index] == None or tick[low_index] == None)
        ]

        return json.dumps(kline)
    else:
        return json.dumps({})
示例#7
0
def tv_history():
    symbol = request.args.get('symbol', '')
    resolution = request.args.get('resolution', '')
    from_time = request.args.get('from', '')
    to_time = request.args.get('to', '')

    client = settings['db_adapter']
    support_markets = settings['markets'].keys()
    support_symbols = settings['symbols']['default']

    service = kline_service(client, settings)
    kline = service.get_tvkline_by_market_symbol(symbol, long(from_time),
                                                 long(to_time), resolution,
                                                 settings['kline']['size'])

    if kline and kline.has_key('series'):
        columns = kline['series'][0]['columns']
        for i in range(0, len(columns)):
            if columns[i] == 'time':
                time_index = i
            elif columns[i] == 'open':
                open_index = i
            elif columns[i] == 'close':
                close_index = i
            elif columns[i] == 'low':
                low_index = i
            elif columns[i] == 'high':
                high_index = i
            elif columns[i] == 'volume':
                volume_index = i

        timezone_offset = settings['timezone_offset']

        ticks = kline['series'][0]['values']
        ticks.sort(lambda x, y: cmp(x[time_index], y[time_index]))

        kline = {
            "s": "ok",
            "t": [],  #timestamp
            "o": [],  #open
            "c": [],  #close
            "h": [],  #high
            "l": [],  #low
            "v": []  #volume
        }

        for index in range(len(ticks)):
            tick = ticks[index]

            # remove the timeslot contains no data
            if tick[open_index] == None or tick[close_index] == None or tick[
                    high_index] == None or tick[low_index] == None:
                continue

            kline["t"].append(tick[time_index])
            kline["o"].append(tick[open_index])
            kline["c"].append(tick[close_index])
            kline["h"].append(tick[high_index])
            kline["l"].append(tick[low_index])
            kline["v"].append(tick[volume_index])

    else:
        #kline = { "s": "no_data", "nextTime": long(time.time() + 60) }
        kline = {"s": "no_data"}

    return json.dumps(kline)
示例#8
0
def api_datasync():
    table = request.args.get('tb', '')
    start = request.args.get('st', '')
    end = request.args.get('et', '')
    client = settings['db_adapter']
    service = kline_service(client, settings)

    result = []
    datasyncs = service.query_datasync(table, long(start), long(end))
    if datasyncs:
        datasync_item = []
        if table == "market_ticks":
            datasync_item = [{
                "time": datasync[0],
                "amount": datasync[1],
                "close": datasync[2],
                "count": datasync[3],
                "high": datasync[4],
                "low": datasync[5],
                "market": datasync[6],
                "open": datasync[7],
                "period": datasync[8],
                "symbol": datasync[9],
                "timezone_offset": datasync[10],
                "volume": datasync[11]
            } for datasync in datasyncs]
        elif table == "k10_index":
            datasync_item = [{
                "time": datasync[0],
                "close": datasync[1],
                "high": datasync[2],
                "low": datasync[3],
                "open": datasync[4],
                "period": datasync[5],
                "symbol": datasync[6],
                "volume": datasync[7]
            } for datasync in datasyncs]
        elif table == "k30_index":
            datasync_item = [{
                "time": datasync[0],
                "close": datasync[1],
                "high": datasync[2],
                "low": datasync[3],
                "open": datasync[4],
                "period": datasync[5],
                "symbol": datasync[6],
                "volume": datasync[7]
            } for datasync in datasyncs]
        elif table == "k10_daily_rank":
            datasync_item = [{
                "time": datasync[0],
                "id": datasync[1],
                "market_cap_usd": datasync[2],
                "max_supply": datasync[3],
                "name": datasync[4],
                "percent_change_1h": datasync[5],
                "percent_change_24h": datasync[6],
                "percent_change_7d": datasync[7],
                "period": datasync[8],
                "price_usd": datasync[9],
                "rank": datasync[10],
                "symbol": datasync[11],
                "total_supply": datasync[12],
                "volume_usd_24h": datasync[13]
            } for datasync in datasyncs]
        return json.dumps(datasync_item)
    else:
        return json.dumps(result)