财经数据(2)-聚宽数据

1、目标:利用JoinQuant获取竞价数据并实现Mysql存储

参考文献:https://www.joinquant.com/help/api/help?name=JQData#%E6%95%B0%E6%8D%AE%E8%B0%83%E7%94%A8%E6%96%B9%E6%B3%95

Tushare接口:https://tushare.pro/document/2

JoinQuant接口:https://www.joinquant.com/help/api/help?name=Stock

代码如下:

# -*- coding: utf-8 -*-
import pandas as pd
from jqdatasdk import *
import time
from sqlalchemy import create_engine
import datetime


# ====================JoinQuant股票列表======================================================================================================================
def allStock():
    print("----------------------------------")
    print("开始从JoinQuant接口获取股票列表数据")
    stock_list = list(get_all_securities(types=['stock']).index)
    print(stock_list)

    return stock_list


# ====================JoinQuant竞价数据======================================================================================================================
def jjQuant(stock, engine, *args):
    print("----------------------------------")
    print("开始从JoinQuant接口获取股票竞价数据")

    # 创建空DataFrame
    bids = pd.DataFrame()
    for stk in stock_list:
        bidding = get_call_auction(security=stk, start_date=start, end_date=end, fields=['time', 'current', 'money'])
        bids = bids.append(bidding)

    bids['code'] = bids['code'].apply(lambda x: x[:6])
    bids['money'] = bids['money'].apply(lambda x: round(x / 10000, 2))

    bids.to_sql('call_auction', engine, if_exists='append', index=False)

    print(bids)
    print("本次存储JoinQuant股票竞价数据%s条" % bids.shape[0])


# ====================JoinQuant龙虎榜数据======================================================================================================================
def topInst(stcok, engine, *args):
    print("----------------------------------")
    print("开始从JoinQuant接口获取龙虎榜数据")
    top = get_billboard_list(stock_list=stcok, start_date=start, end_date=end)

    top['code'] = top['code'].map(lambda x: x[:6])
    top['buy_value'] = top['buy_value'].map(lambda x: round(x / 10000, 2))
    top['sell_value'] = top['sell_value'].map(lambda x: round(x / 10000, 2))
    top['net_value'] = top['net_value'].map(lambda x: round(x / 10000, 2))

    top = top.drop(['abnormal_code', 'abnormal_name', 'buy_rate', 'sell_rate', 'amount'], axis=1)

    top.to_sql('quant_top_inst', engine, if_exists='append', index=False)

    print(top)
    print("本次存储JoinQuant龙虎榜数据%s条" % top.shape[0])
    print("--------------------------------------------")


# ====================主函数====================================================================================================================================
if __name__ == '__main__':
    print("JoinQuant接口程序开始执行")
    print("--------------------------------------------")
    begin = time.time()

    # 创建Pandas读写数据库引擎
    engine = create_engine('mysql://root:123456@127.0.0.1/quant?charset=utf8')

    # 获取起始及截止日期,如果获取当天数据,则start=end即可
    start = input("时间格式如:1949-10-01,请输入起始日期:")
    end = input("时间格式如:1949-10-01,请输入截止日期:")

    # ID是申请时所填写的手机号;Password为聚宽官网登录密码,新申请用户默认为手机号后6位
    auth('18829345691', '345691')

    # 查询是否登录/连接成功
    is_auth = is_auth()

    stock_list = allStock()                     # 获取JoinQuant股票列表
    jjQuant(stock_list, engine, start, end)     # 获取JoinQuant竞价数据
    topInst(stock_list, engine, start, end)     # 获取JoinQuant龙虎榜数据

    ed = time.time()
    count = get_query_count()
    print('JoinQuant接口程序共执行%0.2f秒.' % ((ed - begin)))
    print("JoinQuant当天剩余可请求数据情况为:"%count)
    print("JoinQuant接口程序执行完成")

  

原文地址:https://www.cnblogs.com/Iceredtea/p/12099967.html