量化学习(二)历史数据获取

回顾:上回书说到,我们已经获取所有股票列表并写入数据库当中,我们可以循环遍历所有股票代码获取我们想得到的历史数据
获取股票历史数据

import akshare as ak
import sqlalchemy
import datetime
import pandas as pd


def create_mysql_engine():
    """
    创建数据库引擎对象
    :return: 新创建的数据库引擎对象
    """

    # 引擎参数信息
    host = '192.168.9.110'
    user = 'root'
    passwd = 'A_quant88'
    port = '3306'
    db = 'quantt'

    # 创建数据库引擎对象
    mysql_engine = sqlalchemy.create_engine(
        'mysql+pymysql://{0}:{1}@{2}:{3}'.format(user, passwd, host, port),
        poolclass=sqlalchemy.pool.NullPool
    )

    # 如果不存在数据库quantt则创建

    mysql_engine.execute("CREATE DATABASE IF NOT EXISTS {0} ".format(db))

    # 创建连接数据库db_quant的引擎对象
    db_engine = sqlalchemy.create_engine(
        'mysql+pymysql://{0}:{1}@{2}:{3}/{4}?charset=utf8'.format(user, passwd, host, port, db),
        poolclass=sqlalchemy.pool.NullPool
    )

    # 返回引擎对象
    return db_engine

def get_stock_codes(update=False):

    """
    获取A股代码列表

    若参数update为False,表示从数据库中读取股票列表
    若数据库中不存在股票列表的表,或者update为True,则下载指定日期date的交易股票列表

    :param update: 是否更新股票列表,默认为False
    :return: A股代码的列表
    """

    # 创建数据库引擎对象
    engine = create_mysql_engine()

    # 数据库中股票代码的表名
    table_name = 'stock_codes'

    # 数据库中不存在股票代码表,或者需要更新股票代码表
    if table_name not in sqlalchemy.inspect(engine).get_table_names() or update:

        # 查询股票数据
        stock_zh_spot_df = ak.stock_zh_a_spot_em()  ## 获取实时数据
        stock_zh_spot_data = stock_zh_spot_df[stock_zh_spot_df['名称'] != '']  ## 去除名称为空值的数据
        codes_names = stock_zh_spot_data[['代码', '名称']]
        # 将股票代码写入数据库
        codes_names.to_sql(name=table_name, con=engine, if_exists='replace', index=False, index_label=False)
        # 返回股票列表
        return codes_names['代码'].tolist()

    # 从数据库中读取股票代码列表
    else:
        # 待执行的sql语句
        sql_cmd = 'SELECT {} FROM {}'.format('代码', table_name)
        # 读取sql,返回股票列表
        return pd.read_sql(sql=sql_cmd, con=engine)['代码'].tolist()

def create_data(stock_codes, period = "daily",start_date = '20230227', end_date = datetime.date.today().strftime('%Y%m%d'),
                    adj = 'hfq'):

    """
    下载指定日期内,指定股票的日线数据

    :param stock_codes: 待下载数据的股票代码
    :param start_date: 日线开始日期1990-12-19
    :param end_date: 日线结束日期
    :param adj: 默认返回不复权的数据; qfq: 返回前复权后的数据; hfq: 返回后复权后的数据
    :return: None
    """

    # 下载股票循环
    for code in stock_codes:
        print('正在下载{}...'.format(code))

        # 下载日线数据
        data_df = ak.stock_zh_a_hist(symbol=code, period=period, start_date=start_date, end_date=end_date,
                                adjust=adj)  ## 日度数据,后复权
        if data_df.empty:
            print('--------------empty------------------' + code)
            continue
        print(data_df)

if __name__ == '__main__':
    stock_codes = get_stock_codes()
    create_data(stock_codes)

单线程保存到数据库替换函数

def create_data(stock_codes, period = "daily",start_date = '19901219', end_date = '20230228',
                    adj = 'hfq'):

    """
    下载指定日期内,指定股票的日线数据

    :param stock_codes: 待下载数据的股票代码
    :param start_date: 日线开始日期1990-12-19
    :param end_date: 日线结束日期
    :param adj: 默认返回不复权的数据; qfq: 返回前复权后的数据; hfq: 返回后复权后的数据
    :return: None
    """
    # 创建数据库引擎对象
    engine = create_mysql_engine()
    # 下载股票循环
    for index,code in enumerate(stock_codes):
        print('({}/{})正在创建{}...'.format(index + 1, len(stock_codes), code))
        data_df = ak.stock_zh_a_hist(symbol=code, period=period, start_date=start_date, end_date=end_date,
                                         adjust=adj)  ## 日度数据,后复权
        if data_df.empty:
            print('--------------empty------------------'+ code)
            continue
                # 删除重复数据
        data_df.drop_duplicates(['日期'], inplace=True)
            # 写入数据库
        table_name = '{}'.format(code)
        data_df.to_sql(name=table_name, con=engine, if_exists='replace', index=True, index_label='id')

        print('({}/{})创建完成{}...'.format(index + 1, len(stock_codes), code))

多线程获取股票历史数据录入数据库

import akshare as ak
import sqlalchemy
import datetime
import multiprocessing
import pandas as pd



def create_mysql_engine():
    """
    创建数据库引擎对象
    :return: 新创建的数据库引擎对象
    """

    # 引擎参数信息
    host = '127.0.0.1'
    user = 'root'
    passwd = 'A_quant88'
    port = '3306'
    db = 'quantf'

    # 创建数据库引擎对象
    mysql_engine = sqlalchemy.create_engine(
        'mysql+pymysql://{0}:{1}@{2}:{3}'.format(user, passwd, host, port),
        poolclass=sqlalchemy.pool.NullPool
    )

    # 如果不存在数据库db_quant则创建

    mysql_engine.execute("CREATE DATABASE IF NOT EXISTS {0} ".format(db))

    # 创建连接数据库db_quant的引擎对象
    db_engine = sqlalchemy.create_engine(
        'mysql+pymysql://{0}:{1}@{2}:{3}/{4}?charset=utf8'.format(user, passwd, host, port, db),
        poolclass=sqlalchemy.pool.NullPool
    )

    # 返回引擎对象
    return db_engine

def get_stock_codes(update=False):

    """
    获取指定日期的A股代码列表

    若参数update为False,表示从数据库中读取股票列表
    若数据库中不存在股票列表的表,或者update为True,则下载指定日期date的交易股票列表


    :param update: 是否更新股票列表,默认为False
    :return: A股代码的列表
    """

    # 创建数据库引擎对象
    engine = create_mysql_engine()

    # 数据库中股票代码的表名
    table_name = 'stock_codes'

    # 数据库中不存在股票代码表,或者需要更新股票代码表
    if table_name not in sqlalchemy.inspect(engine).get_table_names() or update:

        # 查询股票数据
        stock_zh_spot_df = ak.stock_zh_a_spot_em()  ## 获取实时数据
        stock_zh_spot_data = stock_zh_spot_df[stock_zh_spot_df['名称'] != '']  ## 去除名称为空值的数据
        codes_names = stock_zh_spot_data[['代码', '名称']]
        # 将股票代码写入数据库
        codes_names.to_sql(name=table_name, con=engine, if_exists='replace', index=False, index_label=False)
        # 返回股票列表
        return codes_names['代码'].tolist()

    # 从数据库中读取股票代码列表
    else:
        # 待执行的sql语句
        sql_cmd = 'SELECT {} FROM {}'.format('代码', table_name)
        # 读取sql,返回股票列表
        return pd.read_sql(sql=sql_cmd, con=engine)['代码'].tolist()
def create_data(stock_codes, period = "daily",start_date = '19901219', end_date = '20230228',
                    adj = 'hfq'):

    """
    下载指定日期内,指定股票的日线数据

    :param stock_codes: 待下载数据的股票代码
    :param start_date: 日线开始日期1990-12-19
    :param end_date: 日线结束日期 end_date = datetime.date.today().strftime('%Y%m%d')
    :param adjustflag: 复权选项 1:后复权  2:前复权  3:不复权  默认为前复权
    :return: None
    """
    # 创建数据库引擎对象
    engine = create_mysql_engine()
    # 下载股票循环
    for index,code in enumerate(stock_codes):
        print('({}/{})正在创建{}...'.format(index + 1, len(stock_codes), code))
        data_df = ak.stock_zh_a_hist(symbol=code, period=period, start_date=start_date, end_date=end_date,
                                         adjust=adj)  ## 日度数据,后复权
        if data_df.empty:
            print('--------------empty------------------'+ code)
            continue
                # 删除重复数据
        data_df.drop_duplicates(['日期'], inplace=True)
            # 写入数据库
        table_name = '{}'.format(code)
        data_df.to_sql(name=table_name, con=engine, if_exists='replace', index=True, index_label='id')

        print('({}/{})创建完成{}...'.format(index + 1, len(stock_codes), code))
'''
        try:
            data_df = ak.stock_zh_a_hist(symbol=code, period=period, start_date=start_date, end_date=end_date,
                                         adjust=adj)  ## 日度数据,后复权
            if data_df.empty:
                print('--------------empty------------------'+ code)
                continue
                # 删除重复数据
            data_df.drop_duplicates(['日期'], inplace=True)
            # 写入数据库
            table_name = '{}'.format(code)
            data_df.to_sql(name=table_name, con=engine, if_exists='replace', index=True, index_label='id')
            print('({}/{})创建完成{}...'.format(index + 1, len(stock_codes), code))
        except Exception as e:
            print(e)'''

def get_code_group(process_num, stock_codes):
    """
    获取代码分组,用于多进程计算,每个进程处理一组股票

    :param process_num: 进程数
    :param stock_codes: 待处理的股票代码
    :return: 分组后的股票代码列表,列表的每个元素为一组股票代码的列表
    """

    # 创建空的分组
    code_group = [[] for i in range(process_num)]

    # 按余数为每个分组分配股票
    for index, code in enumerate(stock_codes):
        code_group[index % process_num].append(code)

    return code_group


def multiprocessing_func(func, args):
    """
    多进程调用函数

    :param func: 函数名
    :param args: func的参数,类型为元组,第0个元素为进程数,第1个元素为股票代码列表
    :return: 包含各子进程返回对象的列表
    """

    # 用于保存各子进程返回对象的列表
    results = []

    # 创建进程池
    with multiprocessing.Pool(processes=args[0]) as pool:
        # 多进程异步计算
        for codes in get_code_group(args[0], args[1]):
            results.append(pool.apply_async(func, args=(codes, *args[2:],)))
        # 阻止后续任务提交到进程池
        pool.close()
        # 等待所有进程结束
        pool.join()

    return results


def create_data_mp(stock_codes, process_num=4,
                   period = "daily",start_date = '19901219', end_date = '20230228', adj='hfq'):
    """
    使用多进程创建指定日期内,指定股票的日线数据,计算扩展因子

    :param stock_codes: 待创建数据的股票代码
    :param process_num: 进程数
    :param from_date: 日线开始日期
    :param to_date: 日线结束日期
    :param adjustflag: 复权选项 1:后复权  2:前复权  3:不复权  默认为前复权
    :return: None
    """

    multiprocessing_func(create_data, (process_num, stock_codes, period,start_date, end_date, adj))

if __name__ == '__main__':
    stock_codes = get_stock_codes()
    create_data_mp(stock_codes)
#     print(stock_codes)

注:实际获取建议:将stock_codes分拆放到多个数据库,通过代码多次下载后合并或分库使用

delete  from  quantc.stock_codes where 代码  IN (select 代码 from quant.stock_codes union all select 代码 from quanta.stock_codes union all select 代码 from quantb.stock_codes);
delete  from  quantd.stock_codes where 代码  IN (select 代码 from quant.stock_codes union all select 代码 from quanta.stock_codes union all select 代码 from quantb.stock_codes union all select 代码 from quantc.stock_codes);
delete  from  quante.stock_codes where 代码  IN (select 代码 from quant.stock_codes union all select 代码 from quanta.stock_codes union all select 代码 from quantb.stock_codes union all select 代码 from quantc.stock_codes union all select 代码 from quantd.stock_codes);
delete  from  quantf.stock_codes where 代码  IN (select 代码 from quant.stock_codes union all select 代码 from quanta.stock_codes union all select 代码 from quantb.stock_codes union all select 代码 from quantc.stock_codes union all select 代码 from quantd.stock_codes union all select 代码 from quante.stock_codes);

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值