Python爬取聚宽量化平台沪深300价格数据保存成excel文件

记录一下学习成果。

成果

Python爬取聚宽量化平台沪深300价格数据保存成excel文件_第1张图片
一只股票一个表 命名方式是stock+股票名称

使用语言——Python

注册账号

1.需要去聚宽量化平台jqdata注册一个账号。
2.下载jqdatasdk: pip install jqdatasdk。
3.账号登陆:jqdatasdk.auth('账号','密码')

获取沪深300的成分股名单

HS300_list = jqdatasdk.get_index_stocks('000300.XSHG', date=date)#获取沪指300成分股名称

写入excel

 for stock in HS300_list:
        stock_price = jqdatasdk.get_price(str(stock),end_date = end_date,frequency = 'daily',fields = ['open','close','low','high','volume','money',
                                                                                                       'factor',
                                                                                                       'high_limit','low_limit','avg',
                                                                                                       'pre_close','paused'],panel = False)
        stock_price = pd.DataFrame(stock_price)
        wb = openpyxl.load_workbook('allstocks_in_alldays.xlsx')
        if first_build == True:
            wb.create_sheet(title='stock'+str(stock))#只是建立一次表格

        ws = wb['stock'+str(stock)]
        for row in dataframe_to_rows(stock_price.reset_index(),index= False):
            ws.append(row)
            print(str(stock) + 'is doing now')
        wb.save('allstocks_in_alldays.xlsx')

全部代码

def stocks_all_values(end_date,count,first_build = False):#最后一天 往前几天,是否是第一次建立表格
    date = datedecrease(end_date,count,True)#开始日期的日子
    HS300_list = jqdatasdk.get_index_stocks('000300.XSHG', date=date)#获取沪指300成分股名称
    for stock in HS300_list:
        stock_price = jqdatasdk.get_price(str(stock),end_date = end_date,frequency = 'daily',fields = ['open','close','low','high','volume','money',
                                                                                                       'factor',
                                                                                                       'high_limit','low_limit','avg',
                                                                                                       'pre_close','paused'],panel = False)
        stock_price = pd.DataFrame(stock_price)
        wb = openpyxl.load_workbook('allstocks_in_alldays.xlsx')
        if first_build == True:
            wb.create_sheet(title='stock'+str(stock))#只是建立一次表格

        ws = wb['stock'+str(stock)]
        for row in dataframe_to_rows(stock_price.reset_index(),index= False):
            ws.append(row)
            print(str(stock) + 'is doing now')
        wb.save('allstocks_in_alldays.xlsx')
        def datedecrease(init_date,count,is_decrease=True):
    str_init_date = init_date.strftime('%Y-%m-%d %H:%M:%S')#将datatime 转化为字符串 datetime => string  #strftime 是datetime。date的函数
    #print(str_init_date)
    if is_decrease:
        delta = datetime.timedelta(minutes=count)
        n_days = init_date - delta
       # print(n_days.strftime('%Y-%m-%d %H:%M:%S'))
    else:
        delta = datetime.timedelta(minutes=count)
        n_days = init_date + delta#
        n_days= n_days.strftime('%Y-%m-%d %H:%M:%S')
    # 将字符串转换为dataframe string => datetime
    new_date= datetime.datetime.strptime(str(n_days),'%Y-%m-%d %H:%M:%S')
    # print(new_date)
    return new_date

你可能感兴趣的:(量化分析,python,numpy,开发语言)