#-*- coding: utf-8 -*-
importpandas as pdfrom jqdatasdk import *
importtimefrom sqlalchemy importcreate_engineimportdatetime#====================JoinQuant股票列表======================================================================================================================
defallStock():print("----------------------------------")print("开始从JoinQuant接口获取股票列表数据")
stock_list= list(get_all_securities(types=['stock']).index)print(stock_list)returnstock_list#====================JoinQuant竞价数据======================================================================================================================
def jjQuant(stock, engine, *args):print("----------------------------------")print("开始从JoinQuant接口获取股票竞价数据")#创建空DataFrame
bids =pd.DataFrame()for stk instock_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接口程序执行完成")