量化交易:创建签名、从okex平台获取ticker数据和k线数据

import hashlib
import json
import pandas as pd
from urllib.request import Request, urlopen

pd.set_option('expand_frame_repr', False)  # 当列太多时不换行


def create_trade_sign(params, api_key, secret_key):
    """
    创建交易签名
    :return: md5加密数据
    """
    sign = ''
    for key in sorted(params.keys()):
        sign += '&' + key + '=' + str(params[key])
    return hashlib.md5(sign.encode('utf-8')).hexdigest()


if __name__ == '__main__':
    symbol = 'btc2ckusd'
    params = {'symbol': symbol}
    api_key = "5561c051fa52ad68322b90b06646c10ddcda3529845"
    secret_key = "5b6a-6b13d-944c4-9017
    # print(create_trade_sign(params, api_key, secret_key))


def get_url_data(url, retry_times=3):
    """API接口中获取数据
    :param url: API接口
    :param retry_times:最大尝试次数
    :return: API json格式数据
    """
    while True:
        try:
            headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
            request = Request(url=url, headers=headers)
            b_data = urlopen(request, timeout=10).read()
            str_data = b_data.decode('utf-8')
            json_data = json.loads(str_data)
            return json_data
        except Exception as http_error:
            if retry_times > 0:
                return get_url_data(url, retry_times=retry_times-1)
            else:
                print('尝试失败次数过多,已经放弃,错误信息:%s' % http_error)
                return None


if __name__ == '__main__':
    url = 'https://www.okex.com/api/v1/ticker.do?symbol=ltc_usdt'
    # print(get_url_data(url))
    url = 'https://www.okex.com/api/v1/kline.do?symbol=btc_usdt&type=30min'
    # print(get_url_data(url))


def get_ticker_from_okex(symbol_list):
    """
    从交易所获取ticker数据
    :param symbol_list: symbol列表
    :return: ticker数据
    """
    df = pd.DataFrame()
    base_url = 'https://www.okex.com/api/v1/ticker.do?symbol='
    for symbol in symbol_list:
        url = base_url + symbol
        json_data = get_url_data(url)
        if json_data is None:
            continue
        _df = pd.DataFrame(json_data, dtype='float')
        _df = _df[['ticker']].T
        _df['symbol'] = symbol
        df = df.append(_df, ignore_index=True)
        # df.to_hdf('data/okex_ticker.h5', key='all_data', mode='w')
        df = df[['symbol', 'last', 'buy', 'sell', 'high', 'low', 'vol']]
    return df


if __name__ == '__main__':
    symbol_list = ['btc_usdt', 'ltc_usdt']
    # print(get_ticker_from_okex(symbol_list))


def get_candle_from_okex(symbol, kline_type='1min'):
    url = 'https://www.okex.com/api/v1/kline.do?symbol=%s&type=%s' % (symbol, kline_type)  # 构建url
    json_data = get_url_data(url)
    if json_data is None:
        return None
    df = pd.DataFrame(json_data, dtype='float')
    df.rename(columns={0: 'candle_begin_time', 1: 'Open', 2: 'High', 3: 'Low', 4: 'Close', 5: 'Volume'}, inplace=True)  # 对df的每一列进行重新命名
    df['candle_begin_time'] = pd.to_datetime(df['candle_begin_time'], unit='ms')
    df['candle_begin_time_beijing'] = df['candle_begin_time'] + pd.Timedelta(hours=8)
    del df['candle_begin_time']
    df = df[['candle_begin_time_beijing', 'Open', 'High', 'Low', 'Close', 'Volume']]
    print(df)


if __name__ == '__main__':
    symbol = 'btc_usdt'
    get_candle_from_okex(symbol)

猜你喜欢

转载自blog.csdn.net/Darkman_EX/article/details/81534865