一段代码:用ccxt封装一个函数获取各交易所各交易对的日线数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013613428/article/details/85784800

以下代码通过ccxt获取各交易所各交易对的日线数据,大家可以简单的修改,改为获取分钟级别的k线数据,并返回合适的数据格式(这里只返回时间和close价格)

需要依赖的库:

import ccxt
import pandas as pd
from datetime import datetime
import numpy as np

创建交易所:

def create_exchange(exchange_name):
    exchange = getattr(ccxt, exchange_name)()
    exchange.proxies = {
        'http': 'socks5://127.0.0.1:1080',
        'https': 'socks5h://127.0.0.1:1080'
    }
    if isMac():
        exchange.proxies = {
            'http': 'http://127.0.0.1:1080',
            'https': 'https://127.0.0.1:1080'
        }
    exchange.load_markets()
    return exchange

获取交易对:

def get_symbol(exchange_name):
	exchange = create_exchange(exchange_name)
    return exchange.symbols

获取交易对日线数据:

def get_daily_candles(exchange_name, symbol, start):
    params = {'partial': False}
    exchange = create_exchange(exchange_name)
    since = int(start.timestamp()*1000)
    candles = exchange.fetch_ohlcv(symbol, '1d', since, 365, params)
    df = pd.DataFrame(candles, columns=['timestamp','open','high','low','close','volume'])
    # df.set_index(pd.DatetimeIndex(df['timestamp'].map(lambda x:exchange.iso8601(x))),inplace=True)
    df['timestamp'] = df['timestamp'].map(lambda x:exchange.iso8601(x)[0:10])
    return np.array(df[['timestamp','close']]).tolist()

没有更多推荐了,返回首页