Ejemplo n.º 1
0
def list_to_df(list_kline):
    # 遍历加入date
    for i in range(len(list_kline)):
        list_kline[i][0] = int(list_kline[i][0] / 1000)
        date = Base.timestamp_to_date(list_kline[i][0])
        list_kline[i].append(date)
    # 创建df
    df = pd.DataFrame(list_kline)
    # 去掉不需要数据
    df.drop([6, 7, 8, 9, 10, 11], axis=1, inplace=True)
    # 设置列名
    df.columns = ['timestamp', 'open', 'high', 'low', 'close', 'vol', 'date']
    # 转换数据类型
    df['timestamp'] = df['timestamp'].astype(int)
    df['open'] = df['open'].astype(float)
    df['high'] = df['high'].astype(float)
    df['low'] = df['low'].astype(float)
    df['close'] = df['close'].astype(float)
    df['vol'] = df['vol'].astype(float)
    df['date'] = df['date'].astype(str)
    # 掉换列位置
    list_columns = ['date', 'timestamp', 'open', 'high', 'low', 'close', 'vol']
    df = df[list_columns]
    # 设置日期为索引
    df.set_index('date', inplace=True)
    return df
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)