Exemple #1
0
def collect_to_csv(symbol='BTCUSDT', period='1m', timestamp=1528992000):
    # path1m=os.path.abspath('.') + '\\' + period + '_' + str(symbol).lower()+ '\\'
    dir1m = os.path.abspath('.') + '/' + period + '_' + str(
        symbol).lower() + '/'
    while timestamp < time.time() - 86400:
        # 生成币安url
        url = 'https://api.binance.com/api/v1/klines?symbol=' + symbol + '&interval=' + period + '&startTime=' + str(
            timestamp) + '000&limit=1000'
        # 获取json列表
        list_kline = Base.request_get(url)
        # 转换成df格式
        df = list_to_df(list_kline)
        # 保存文件
        if period == '1m':
            if not os.path.exists(dir1m):
                os.mkdir(dir1m)
                log('创建文件夹,以便保存csv文件' + dir1m)
            path = dir1m + str(df.iloc[0, 0]) + ".csv"
            timestamp = df.iloc[-1, 0] + 60
        else:
            path = str(symbol).lower() + 'merge1d.csv'
            timestamp = df.iloc[-1, 0] + 60 * 60 * 24
        df.to_csv(path, encoding="utf-8")
        log(Base.timestamp_to_date(df.iloc[0, 0]) + ' 保存成功 ' + str(path))
        time.sleep(1)
    # 合并分钟
    if period == '1m':
        merge1m(dir1m, symbol)
Exemple #2
0
def collect_to_csv(symbol='BTCUSDT', timestamp=1528992000):
    dir1m = os.path.abspath('.') + '/' + str(symbol).lower() + '/'
    while timestamp < time.time() - 86400:
        # 生成币安url
        url = 'https://api.binance.com/api/v1/klines?symbol=' + symbol + '&interval=1m&startTime=' + str(
            timestamp) + '000&limit=1000'
        # 获取json列表
        list_kline = Base.request_get(url)
        # 转换成df格式
        df = list_to_df(list_kline)
        # 保存文件
        if not os.path.exists(dir1m):
            os.mkdir(dir1m)
            log('创建文件夹,以便保存csv文件' + dir1m)
        path = dir1m + Base.timestamp_to_date_pure(int(df.iloc[0, 0])) + ".csv"
        timestamp = df.iloc[-1, 0] + 60
        df.to_csv(path, encoding="utf-8")
        log(Base.timestamp_to_date(df.iloc[0, 0]) + ' 保存成功 ' + str(path))
        time.sleep(1)