def request_kline_rest(symbol, period, since):
    try:

        periodSec = get_period_interval(period)
        rangeHour = get_hour_span(since)
        url = 'https://data.gateio.io/api2/1/candlestick2/%s?group_sec=%s&range_hour=%s' % (
            symbol, periodSec, rangeHour)
        mysqlutil.log('gate', url)
        socket.setdefaulttimeout(20)
        headers = {
            "Content-type":
            "application/x-www-form-urlencoded",
            'User-Agent':
            "Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0",
        }
        req = request.Request(url, None, headers)
        response = request.urlopen(req)
        mysqlutil.log('gate', 'response code >>>', response.code)
        content = response.read().decode('utf-8')

        mysqlutil.log('gate', 'content >>>', content)
        content = json.loads(content)
        mysqlutil.log('gate', 'data len >>>', len(content))
        if 'code' in content:
            mysqlutil.log('gate', 'error url >>>', url)
            # 返回请求太频繁的错误
            if content['code'] == 40:
                time.sleep(60)
            return content
        data = content['data']
        if len(data) > 0:
            mysqlutil.log('gate', 'data >>>', '[' + str(data[0]) + ', ...]')
            mysqlutil.log('gate', 'data start >>>',
                          util.getLocaleDateStrBy13(int(data[0][0])))
            mysqlutil.log(
                'gate', 'data end >>>',
                util.getLocaleDateStrBy13(int(data[len(data) - 1][0])))
        return content
    except:
        util.printExcept()
        return False
示例#2
0
def request_kline_rest(symbol, period, startTime=0, endTime=0, size=1000):
    try:

        url = 'https://api.binance.com/api/v1/klines?symbol=%s&interval=%s&limit=%d'
        if startTime == 0 or endTime == 0:
            url = url % (symbol, period, size)
        else:
            url = (url + '&startTime=%d&endTime=%d') % (symbol, period, size,
                                                        startTime, endTime)
        # 测试接口是否正常
        # url = 'https://api.binance.com/api/v1/time'
        # url = 'http://ip.cn'
        mysqlutil.log('binance', url)
        socket.setdefaulttimeout(20)
        headers = {
            "Content-type":
            "application/x-www-form-urlencoded",
            'User-Agent':
            "Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0",
        }
        req = request.Request(url, None, headers)
        response = request.urlopen(req)
        mysqlutil.log('binance', 'response code >>>', response.code)
        content = response.read().decode('utf-8')

        content = json.loads(content)
        mysqlutil.log('binance', 'data len >>>', len(content))
        if len(content) > 0:
            mysqlutil.log('binance', 'data >>>')
            mysqlutil.log('binance', '   ', '[' + str(content[0]) + ', ...]')
            mysqlutil.log('binance', 'data start >>>',
                          util.getLocaleDateStrBy13(content[0][0]))
            mysqlutil.log(
                'binance', 'data end >>>',
                util.getLocaleDateStrBy13(content[len(content) - 1][0]))
        return content
    except:
        util.printExcept()
        time.sleep(61)
        return False
示例#3
0
def request_kline_rest(symbol, period, since=0, size=1000):
    try:

        url = 'http://api.zb.cn/data/v1/kline?market=%s&type=%s&since=%s&size=%s' % (
            symbol, period, since, size)
        mysqlutil.log('zb', url)
        socket.setdefaulttimeout(20)
        headers = {
            "Content-type":
            "application/x-www-form-urlencoded",
            'User-Agent':
            "Mozilla/5.0 (Windows NT 6.1; rv:32.0) Gecko/20100101 Firefox/32.0",
        }
        req = request.Request(url, None, headers)
        response = request.urlopen(req)
        mysqlutil.log('zb', 'response code >>>', response.code)
        content = response.read().decode('utf-8')

        # mysqlutil.log('zb', 'content >>>', content)
        content = json.loads(content)
        data = content['data']
        mysqlutil.log('zb', 'data symbol >>>', content['symbol'])
        mysqlutil.log('zb', 'data moneyType >>>', content['moneyType'])
        mysqlutil.log('zb', 'data len >>>', len(data))
        if len(data) > 0:
            mysqlutil.log('zb', 'data >>>')
            mysqlutil.log('zb', '   ', '[' + str(data[0]) + ', ...]')
            mysqlutil.log('zb', 'data start >>>',
                          util.getLocaleDateStrBy13(data[0][0]))
            mysqlutil.log('zb', 'data end >>>',
                          util.getLocaleDateStrBy13(data[len(data) - 1][0]))
        return content
    except:
        util.printExcept()
        time.sleep(61)
        return False
示例#4
0
def get_okex_kline_history(period):
    global runCount, count, total, done, doneToken, pendingPeriods, periods

    startDate = datetime.datetime.now()

    db = mysqlutil.init()
    cursor = db.cursor()

    try:

        symbols = CommonApi.get_symbols(TABLE, format='dict')

        symbolCount = 0

        for sk in symbols:

            symbol = symbols[sk]
            base = symbols[sk]['base']
            quote = symbols[sk]['quote']

            symbolCount += 1
            mysqlutil.log('okex', startDate, period, 'symbol >>>', sk,
                          symbolCount, '/', len(symbols))

            # 获取最近的一条数据的时间戳
            sql = '''
                SELECT ts FROM xcm_okex_kline 
                WHERE symbol=%s AND period=%s 
                ORDER BY ts DESC LIMIT 0,1
            '''
            cursor.execute(sql, (sk, period))
            rows = cursor.fetchall()
            lastTimestamp = 0
            if len(rows) == 0:
                since = ''
            else:
                lastTimestamp = rows[0][0]
                # mysqlutil.log('okex', startDate, period, 'last timestamp >>>', str(rows[0][0]))
                mysqlutil.log('okex', startDate, period, 'last datetime >>>',
                              util.getLocaleDateStrBy13(rows[0][0]))
                since = str(rows[0][0] + get_period_interval(period))

            mysqlutil.log('okex', startDate, period, 'period >>>', period)
            if since != '':
                mysqlutil.log('okex', startDate, period, 'since datetime >>>',
                              util.getLocaleDateStrBy13(int(since)))
            kdata = request_okex_kline_rest(sk, period, since)
            if kdata is False:
                continue
            elif 'error_code' in kdata:
                mysqlutil.log('okex', startDate, period,
                              '\033[0;30;41m error_code >>>',
                              kdata['error_code'], '\033[0m\n')
                continue
            else:
                mysqlutil.log('okex', startDate, period, 'kdata len >>>',
                              len(kdata))
                mysqlutil.log('okex', startDate, period, 'kdata >>>', kdata)
                mysqlutil.log('okex', startDate, period,
                              'kdata start datetime >>>',
                              util.getLocaleDateStrBy13(kdata[0][0]))

            newTimestamps = []

            for k in kdata:

                newTimestamp = k[0]
                # mysqlutil.log('okex', 'newTimestamp >>>', newTimestamp)
                # TODO: 之所以重复,似乎是同一个时间间隔,有新的交易发生。需要考虑将此重复时间的数据更新到数据库中。
                if lastTimestamp == newTimestamp or newTimestamp in newTimestamps:
                    mysqlutil.log('okex', startDate, period,
                                  '\033[0;30;47m duplicated timestamp >>>',
                                  k[0], '\033[0m')
                    mysqlutil.log('okex', startDate, period,
                                  '\033[0;30;47m duplicated timestamp >>>',
                                  util.getLocaleDateStrBy13(k[0]), '\033[0m')
                    continue

                newTimestamps.append(newTimestamp)

                nowTime = time.time()
                nowTime *= 10000000

                sql = '''
                    INSERT INTO xcm_okex_kline (
                        _id, symbol,base,quote,period,
                        ts,open,high,low,close,vol)
                    VALUES (
                        %s,%s,%s,%s,%s,
                        %s,%s,%s,%s,%s,%s)'''
                param = (nowTime, sk, base, quote, period, k[0], k[1], k[2],
                         k[3], k[4], k[5])
                cursor.execute(sql, param)

                count += 1
                total += 1

            db.commit()
            mysqlutil.log('okex', startDate, period, 'begin date >>>',
                          beginDate)
            mysqlutil.log('okex', startDate, period, 'start date >>>',
                          startDate)
            mysqlutil.log('okex', startDate, period, 'current date >>>',
                          datetime.datetime.now())
            mysqlutil.log('okex', startDate, period,
                          'insert done >>> {:,}'.format(count))
            mysqlutil.log('okex', startDate, period, 'period done >>>', done)
            mysqlutil.log('okex', startDate, period, 'period doneToken >>>',
                          doneToken)
            mysqlutil.log('okex', startDate, period, 'period pending >>>',
                          pendingPeriods)
            mysqlutil.log('okex', startDate, period,
                          'total >>> {:,}'.format(total))
            mysqlutil.log('okex', 'runCount >>>', runCount, '\n')

        done.append(period)
        doneToken[period] = 1
        pendingPeriods.discard(period)

    except:
        util.printExcept()
    finally:
        cursor.close()
        db.close()

    mysqlutil.log('okex', startDate, period, 'start date >>>', startDate)
    mysqlutil.log('okex', startDate, period, 'end date >>>',
                  datetime.datetime.now())
    mysqlutil.log('okex', startDate, period, 'period done >>>', done)
    mysqlutil.log('okex', startDate, period, 'period doneToken >>>', doneToken)
    mysqlutil.log('okex', startDate, period, 'period pending >>>',
                  pendingPeriods)
    mysqlutil.log('okex', startDate, period, 'total >>> {:,}'.format(total))

    if len(done) == len(periods):
        runCount += 1

    mysqlutil.log('okex', 'runCount >>>', runCount, '\n')
def get_kline_history(period):
    global runCount, count, total, done, doneToken, pendingPeriods

    startDate = str(datetime.datetime.now())

    db = mysqlutil.init()
    cursor = db.cursor()

    try:

        sql = 'SELECT symbol, base, quote FROM xcm_gate_symbol'
        cursor.execute(sql)
        symbols = cursor.fetchall()

        symbolCount = 0

        nowTime13 = int(time.time() * 1000)

        for r in symbols:

            symbol = r[0]
            base = r[1]
            quote = r[2]

            symbolCount += 1
            mysqlutil.log('gate', startDate, period, 'symbol >>>', symbol,
                          symbolCount, '/', len(symbols))

            # 获取最近的一条数据的时间戳
            sql = '''
                SELECT ts FROM xcm_gate_kline_history 
                WHERE symbol=%s AND period=%s 
                ORDER BY ts DESC LIMIT 0,1
            '''
            cursor.execute(sql, (symbol, period))
            rows = cursor.fetchall()
            lastTimestamp = 0
            if len(rows) == 0:
                since = util.getTimeStampReturn13('2017-12-01 00:00:00')
            else:
                lastTimestamp = rows[0][0]
                # mysqlutil.log('gate', startDate, period, 'last timestamp >>>', str(rows[0][0]))
                mysqlutil.log('gate', startDate, period, 'last datetime >>>',
                              util.getLocaleDateStrBy13(rows[0][0]))
                since = rows[0][0] + get_period_interval(period)

            mysqlutil.log('gate', startDate, period, 'period >>>', period)
            if since != '':
                mysqlutil.log('gate', startDate, period, 'since datetime >>>',
                              util.getLocaleDateStrBy13(int(since)))
            kdata = request_kline_rest(symbol, period, since)
            if kdata is False:
                continue
            elif 'code' in kdata:
                mysqlutil.log('gate', startDate, period,
                              '\033[0;30;41m code >>>', kdata['code'],
                              kdata['message'], '\033[0m\n')
                continue
            elif 'result' in kdata and kdata['result']:
                kdata = kdata['data']
                mysqlutil.log('gate', startDate, period, 'kdata len >>>',
                              len(kdata))
                if len(kdata):
                    mysqlutil.log('gate', startDate, period, 'kdata >>>')
                    mysqlutil.log('gate', '   ',
                                  '[' + str(kdata[0]) + ', ...]')
                    mysqlutil.log('gate', startDate, period,
                                  'kdata start datetime >>>',
                                  util.getLocaleDateStrBy13(int(kdata[0][0])))

            newTimestamps = []

            for k in kdata:

                newTimestamp = k[0]
                # mysqlutil.log('gate', 'newTimestamp >>>', newTimestamp)
                if lastTimestamp == newTimestamp or newTimestamp in newTimestamps:
                    mysqlutil.log('gate', startDate, period,
                                  '\033[0;30;47m duplicated timestamp >>>',
                                  k[0], '\033[0m')
                    mysqlutil.log('gate', startDate, period,
                                  '\033[0;30;47m duplicated timestamp >>>',
                                  util.getLocaleDateStrBy13(k[0]), '\033[0m')
                    continue

                newTimestamps.append(newTimestamp)

                nowTime = time.time()
                nowTime *= 10000000

                sql = '''
                    INSERT INTO xcm_gate_kline_history (
                        _id, symbol, base, quote, period,
                        ts, volume, close, high, low, open)
                    VALUES (
                        %s, %s, %s, %s, %s,
                        %s, %s, %s, %s, %s, %s)'''
                param = (nowTime, symbol, base, quote, period, k[0], k[1],
                         k[2], k[3], k[4], k[5])

                try:

                    # print('gate', 'sql >>>', sql%param)
                    cursor.execute(sql, param)

                    count += 1
                    total += 1
                except pymysql.err.IntegrityError:
                    # util.printExcept()
                    mysqlutil.log('gate', startDate, period,
                                  '\033[0;30;41m pymysql.err.IntegrityError',
                                  '\033[0m\n')
                    mysqlutil.log('gate', startDate, period, sql % param)

            db.commit()

            mysqlutil.log('gate', '\033[0;30;43m', startDate, period,
                          'begin date >>>', beginDate, '\033[0m')
            mysqlutil.log('gate', '\033[0;30;43m', startDate, period,
                          'start date >>>', startDate, '\033[0m')
            mysqlutil.log('gate', '\033[0;30;43m', startDate,
                          period, 'current date >>>',
                          str(datetime.datetime.now()), '\033[0m')
            mysqlutil.log('gate', startDate, period,
                          'insert done >>> {:,}'.format(count))
            mysqlutil.log('gate', startDate, period, 'period done >>>',
                          str(done))
            mysqlutil.log('gate', startDate, period, 'period doneToken >>>',
                          str(doneToken))
            mysqlutil.log('gate', startDate, period, 'period pending >>>',
                          str(pendingPeriods))
            mysqlutil.log('gate', '\033[0;30;42m', startDate, period,
                          'total >>> {:,}'.format(total), '\033[0m')
            mysqlutil.log('gate', 'runCount >>>', runCount, '\n')

        done.append(period)
        doneToken[period] = 1
        pendingPeriods.discard(period)

    except:
        util.printExcept()
    finally:
        cursor.close()
        db.close()

    mysqlutil.log('gate', '\033[0;30;43m', startDate, period, 'begin date >>>',
                  beginDate, '\033[0m')
    mysqlutil.log('gate', '\033[0;30;43m', startDate, period, 'start date >>>',
                  startDate, '\033[0m')
    mysqlutil.log('gate', '\033[0;30;43m', startDate, period, 'end date >>>',
                  str(datetime.datetime.now()), '\033[0m')
    mysqlutil.log('gate', startDate, period, 'period done >>>', done)
    mysqlutil.log('gate', startDate, period, 'period doneToken >>>',
                  str(doneToken))
    mysqlutil.log('gate', startDate, period, 'period pending >>>',
                  str(pendingPeriods))
    mysqlutil.log('gate', '\033[0;30;42m', startDate, period,
                  'total >>> {:,}'.format(total), ' \033[0m')

    if len(done) == len(periods):
        runCount += 1

    mysqlutil.log('gate', 'runCount >>>', runCount, '\n')
示例#6
0
def get_kline_history(period):
    global runCount, count, total, done, doneToken, pendingPeriods

    startDate = str(datetime.datetime.now())

    db = mysqlutil.init()
    cursor = db.cursor()

    try:

        sql = 'SELECT symbol, base, quote FROM xcm_zb_symbol'
        cursor.execute(sql)
        symbols = cursor.fetchall()

        symbolCount = 0

        nowTime13 = int(time.time() * 1000)

        # 初始化线程标签
        threadLock = _thread.allocate_lock()
        threadLock.acquire()
        runThreadLock[period]['lock'] = threadLock
        threadTime = int(time.time() * 1000)
        runThreadTime.append(threadTime)
        runThreadLock[period]['time'] = threadTime

        while wait_all_thread_running() == False:
            time.sleep(5)

        print('ready to run >>>', period)

        for r in symbols:

            symbol = r[0]
            base = r[1]
            quote = r[2]

            symbolCount += 1
            mysqlutil.log('zb', startDate, period, 'symbol >>>', symbol,
                          symbolCount, '/', len(symbols))

            # 获取最近的一条数据的时间戳
            sql = '''
                SELECT ts FROM xcm_zb_kline_history 
                WHERE symbol=%s AND period=%s 
                ORDER BY ts DESC LIMIT 0,1
            '''
            cursor.execute(sql, (symbol, period))
            rows = cursor.fetchall()
            lastTimestamp = 0
            if len(rows) == 0:
                since = util.getTimeStampReturn13('2017-12-01 00:00:00')
            else:
                lastTimestamp = rows[0][0]
                # mysqlutil.log('zb', startDate, period, 'last timestamp >>>', str(rows[0][0]))
                mysqlutil.log('zb', startDate, period, 'last datetime >>>',
                              util.getLocaleDateStrBy13(rows[0][0]))
                since = rows[0][0] + get_period_interval_return_min(
                    period) * 1000

            mysqlutil.log('zb', startDate, period, 'period >>>', period)
            mysqlutil.log('zb', startDate, period, 'since datetime >>>',
                          util.getLocaleDateStrBy13(int(since)))

            while True:
                print('thread period lock >>>', period,
                      runThreadLock[period]['lock'].locked())
                if (runThreadLock[period]['lock'].locked()
                        == True) and (lock_and_run(period, threadTime)):
                    kdata = request_kline_rest(symbol, period, since)
                    print('\033[1;31;42m thread release >>>', period,
                          '\033[0m')
                    runThreadLock[period]['lock'].release()
                    # 更新线程时间
                    runThreadTime.remove(threadTime)
                    threadTime = int(time.time() * 1000)
                    runThreadTime.append(threadTime)
                    runThreadLock[period]['time'] = threadTime
                    print('runThreadTime >>>', runThreadTime)
                    break
                else:
                    # 给线程重新加锁
                    if runThreadLock[period]['lock'].locked() == False:
                        threadLock = _thread.allocate_lock()
                        threadLock.acquire()
                        runThreadLock[period]['lock'] = threadLock
                    time.sleep(10)

            if kdata is False:
                continue
            elif 'code' in kdata:
                mysqlutil.log('zb', startDate, period,
                              '\033[0;30;41m error code >>>', kdata['code'],
                              'message >>>', kdata['message'], '\033[0m\n')
                continue
            elif 'error' in kdata:
                mysqlutil.log('zb', startDate, period,
                              '\033[0;30;41m error >>>', kdata['error'],
                              '\033[0m\n')
                continue
            elif 'result' in kdata:
                mysqlutil.log('zb', startDate, period,
                              '\033[0;30;41m result >>>', kdata['result'],
                              'message >>>', kdata['message'], '\033[0m\n')
                continue
            elif 'data' in kdata:
                kdata = kdata['data']
                mysqlutil.log('zb', startDate, period, 'kdata len >>>',
                              len(kdata))
                if len(kdata) > 0:
                    mysqlutil.log('zb', startDate, period, 'kdata >>>')
                    mysqlutil.log('zb', '   ', '[' + str(kdata[0]) + ', ...]')
                    mysqlutil.log('zb', startDate, period,
                                  'kdata start datetime >>>',
                                  util.getLocaleDateStrBy13(kdata[0][0]))
            else:
                mysqlutil.log('zb', startDate, period, 'kdata >>>', kdata)

            newTimestamps = []

            for k in kdata:

                newTimestamp = k[0]
                # mysqlutil.log('zb', 'newTimestamp >>>', newTimestamp)
                if lastTimestamp == newTimestamp or newTimestamp in newTimestamps:
                    mysqlutil.log('zb', startDate, period,
                                  '\033[0;30;47m duplicated timestamp >>>',
                                  k[0], '\033[0m')
                    mysqlutil.log('zb', startDate, period,
                                  '\033[0;30;47m duplicated timestamp >>>',
                                  util.getLocaleDateStrBy13(k[0]), '\033[0m')
                    continue

                newTimestamps.append(newTimestamp)

                nowTime = time.time()
                nowTime *= 10000000

                sql = '''
                    INSERT INTO xcm_zb_kline_history (
                        _id, symbol, base, quote, period,
                        ts, open, high, low, close, amount)
                    VALUES (
                        %s, %s, %s, %s, %s,
                        %s, %s, %s, %s, %s, %s)'''
                param = (nowTime, symbol, base, quote, period, k[0], k[1],
                         k[2], k[3], k[4], k[5])
                # print('sql >>>', sql%param)
                cursor.execute(sql, param)

                count += 1
                total += 1

            db.commit()

            mysqlutil.log('zb', '\033[0;30;43m', startDate, period,
                          'begin date >>>', beginDate, '\033[0m')
            mysqlutil.log('zb', '\033[0;30;43m', startDate, period,
                          'start date >>>', startDate, '\033[0m')
            mysqlutil.log('zb', '\033[0;30;43m', startDate,
                          period, 'current date >>>',
                          str(datetime.datetime.now()), '\033[0m')
            mysqlutil.log('zb', startDate, period,
                          'insert done >>> {:,}'.format(count))
            mysqlutil.log('zb', startDate, period, 'period done >>>',
                          str(done))
            mysqlutil.log('zb', startDate, period, 'period doneToken >>>',
                          str(doneToken))
            mysqlutil.log('zb', startDate, period, 'period pending >>>',
                          str(pendingPeriods))
            mysqlutil.log('zb', '\033[0;30;42m', startDate, period,
                          'total >>> {:,}'.format(total), '\033[0m')
            mysqlutil.log('zb', 'runCount >>>', runCount, '\n')

        done.append(period)
        doneToken[period] = 1
        pendingPeriods.discard(period)

    except:
        util.printExcept()
    finally:
        cursor.close()
        db.close()

    mysqlutil.log('zb', '\033[0;30;43m', startDate, period, 'begin date >>>',
                  beginDate, '\033[0m')
    mysqlutil.log('zb', '\033[0;30;43m', startDate, period, 'start date >>>',
                  startDate, '\033[0m')
    mysqlutil.log('zb', '\033[0;30;43m', startDate, period, 'end date >>>',
                  str(datetime.datetime.now()), '\033[0m')
    mysqlutil.log('zb', startDate, period, 'period done >>>', done)
    mysqlutil.log('zb', startDate, period, 'period doneToken >>>',
                  str(doneToken))
    mysqlutil.log('zb', startDate, period, 'period pending >>>',
                  str(pendingPeriods))
    mysqlutil.log('zb', '\033[0;30;42m', startDate, period,
                  'total >>> {:,}'.format(total), ' \033[0m')

    if len(done) == len(periods):
        runCount += 1

    mysqlutil.log('zb', 'runCount >>>', runCount, '\n')
示例#7
0
def get_kline_history(period):
    global runCount, count, total, done, doneToken, pendingPeriods

    startDate = str(datetime.datetime.now())

    db = mysqlutil.init()
    cursor = db.cursor()

    try:

        sql = 'SELECT symbol, base_asset, quote_asset FROM xcm_binance_symbol'
        cursor.execute(sql)
        symbols = cursor.fetchall()

        symbolCount = 0

        nowTime13 = int(time.time() * 1000)

        for r in symbols:

            symbol = r[0]
            base = r[1]
            quote = r[2]

            symbolCount += 1
            mysqlutil.log('binance', startDate, period, 'symbol >>>', symbol,
                          symbolCount, '/', len(symbols))

            # 获取最近的一条数据的时间戳
            sql = '''
                SELECT open_time FROM xcm_binance_kline_history 
                WHERE symbol=%s AND period=%s 
                ORDER BY open_time DESC LIMIT 0,1
            '''
            cursor.execute(sql, (symbol, period))
            rows = cursor.fetchall()
            lastTimestamp = 0
            if len(rows) == 0:
                since = 1
            else:
                lastTimestamp = rows[0][0]
                # mysqlutil.log('binance', startDate, period, 'last timestamp >>>', str(rows[0][0]))
                mysqlutil.log('binance', startDate, period,
                              'last datetime >>>',
                              util.getLocaleDateStrBy13(rows[0][0]))
                since = rows[0][0] + get_period_interval(period) * 1000

            mysqlutil.log('binance', startDate, period, 'period >>>', period)
            if since != '':
                mysqlutil.log('binance', startDate, period,
                              'since datetime >>>',
                              util.getLocaleDateStrBy13(int(since)))
            kdata = request_kline_rest(symbol, period, since, nowTime13)
            if kdata is False:
                continue
            elif 'error_code' in kdata:
                mysqlutil.log('binance', startDate, period,
                              '\033[0;30;41m error_code >>>',
                              kdata['error_code'], '\033[0m\n')
                continue
            else:
                mysqlutil.log('binance', startDate, period, 'kdata len >>>',
                              len(kdata))
                if len(kdata):
                    mysqlutil.log('binance', startDate, period, 'kdata >>>')
                    mysqlutil.log('binance', '   ',
                                  '[' + str(kdata[0]) + ', ...]')
                    mysqlutil.log('binance', startDate, period,
                                  'kdata start datetime >>>',
                                  util.getLocaleDateStrBy13(kdata[0][0]))

            newTimestamps = []

            for k in kdata:

                newTimestamp = k[0]
                # mysqlutil.log('binance', 'newTimestamp >>>', newTimestamp)
                if lastTimestamp == newTimestamp or newTimestamp in newTimestamps:
                    mysqlutil.log('binance', startDate, period,
                                  '\033[0;30;47m duplicated timestamp >>>',
                                  k[0], '\033[0m')
                    mysqlutil.log('binance', startDate, period,
                                  '\033[0;30;47m duplicated timestamp >>>',
                                  util.getLocaleDateStrBy13(k[0]), '\033[0m')
                    continue

                newTimestamps.append(newTimestamp)

                nowTime = time.time()
                nowTime *= 10000000

                sql = '''
                    INSERT INTO xcm_binance_kline_history (
                        _id, symbol,base_asset,quote_asset,period,
                        open_time,open,high,low,close,volume,close_time,
                        quote_asset_volume,num_of_trades,buy_base_asset,buy_quote_asset,f_ignore)
                    VALUES (
                        %s,%s,%s,%s,%s,
                        %s,%s,%s,%s,%s,%s,%s,
                        %s,%s,%s,%s,%s)'''
                param = (nowTime, symbol, base, quote, period, k[0], k[1],
                         k[2], k[3], k[4], k[5], k[6], k[7], k[8], k[9], k[10],
                         k[11])
                # print('binance', 'sql >>>', sql%param)
                cursor.execute(sql, param)

                count += 1
                total += 1

            db.commit()

            mysqlutil.log('binance', '\033[0;30;43m', startDate, period,
                          'begin date >>>', beginDate, '\033[0m')
            mysqlutil.log('binance', '\033[0;30;43m', startDate, period,
                          'start date >>>', startDate, '\033[0m')
            mysqlutil.log('binance', '\033[0;30;43m', startDate,
                          period, 'current date >>>',
                          str(datetime.datetime.now()), '\033[0m')
            mysqlutil.log('binance', startDate, period,
                          'insert done >>> {:,}'.format(count))
            mysqlutil.log('binance', startDate, period, 'period done >>>',
                          str(done))
            mysqlutil.log('binance', startDate, period, 'period doneToken >>>',
                          str(doneToken))
            mysqlutil.log('binance', startDate, period, 'period pending >>>',
                          str(pendingPeriods))
            mysqlutil.log('binance', '\033[0;30;42m', startDate, period,
                          'total >>> {:,}'.format(total), '\033[0m')
            mysqlutil.log('binance', 'runCount >>>', runCount, '\n')

        done.append(period)
        doneToken[period] = 1
        pendingPeriods.discard(period)

    except:
        util.printExcept()
    finally:
        cursor.close()
        db.close()

    mysqlutil.log('binance', '\033[0;30;43m', startDate, period,
                  'begin date >>>', beginDate, '\033[0m')
    mysqlutil.log('binance', '\033[0;30;43m', startDate, period,
                  'start date >>>', startDate, '\033[0m')
    mysqlutil.log('binance', '\033[0;30;43m', startDate, period,
                  'end date >>>', str(datetime.datetime.now()), '\033[0m')
    mysqlutil.log('binance', startDate, period, 'period done >>>', done)
    mysqlutil.log('binance', startDate, period, 'period doneToken >>>',
                  str(doneToken))
    mysqlutil.log('binance', startDate, period, 'period pending >>>',
                  str(pendingPeriods))
    mysqlutil.log('binance', '\033[0;30;42m', startDate, period,
                  'total >>> {:,}'.format(total), ' \033[0m')

    if len(done) == len(periods):
        runCount += 1

    mysqlutil.log('binance', 'runCount >>>', runCount, '\n')