import pyodbc ? import time
conn = pyodbc.connect(r'DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\WindData\WindRawData.mdb') crsr = conn.cursor() import tushare as ts ts.set_token('XXXXXXX') pro = ts.pro_api()
m_startdate='20210430' m_enddate='20210731'
dfx = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,exchange,list_status,delist_date,list_date,market')
N_numb=0? for II in range(0,len(dfx)): ? ? m_stockcode=dfx.iloc[II]['ts_code'].strip() ? ? m_stockcode=m_stockcode[ :6] ? ? N_numb=N_numb+1 ? ?? if m_startdate > '19900101': ?? ? ? ? m_stockname=dfx.iloc[II]['name'].strip()
? ? ? df_a = pro.adj_factor(ts_code=m_stockcode, start_date=m_startdate) ? ??? ? df_b = pro.daily(ts_code=m_stockcode, start_date=m_startdate) ? ??? if (df_a is None) or ?(df_b is None): ? ? ? ?print("data error!") ? ? ? else: ? ? ? ?df=pd.merge(left=df_a,right=df_b,on=['ts_code','trade_date'],sort=True) ? ? ? ?ts_code=df['ts_code'].tolist() ? ? ? ?ts_tradedate=df['trade_date'].tolist() ? ? ? ?ts_open=df['open'].tolist() ? ? ? ?ts_high=df['high'].tolist() ? ? ? ?ts_low=df['low'].tolist() ? ? ? ?ts_close=df['close'].tolist() ? ? ? ?ts_pre_close=df['pre_close'].tolist() ? ? ? ?ts_volume=df['vol'].tolist() ? ? ? ?ts_amount=df['amount'].tolist() ? ? ? ?ts_change=df['change'].tolist() ? ? ? ?ts_pct_chg=df['pct_chg'].tolist() ? ? ? ?ts_dvdfactor=df['adj_factor'].tolist() ? ? ? ?count=0 ? ? ? ?for jj in range(len(ts_code)): ? ? ? ? ? ?sql=""" insert into stockprice(stockcode,stockname,tradedate,open,high,low,close,pre_close,volume,amount, \ ? ? ? ? ? ?change,pct_chg,dvd_factor) values ('%s','%s','%s',%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""" ? ? ? ? ? ?crsr.execute(sql%(ts_code[jj],m_stockname,ts_tradedate[jj],ts_open[jj],ts_high[jj],ts_low[jj],ts_close[jj], \ ? ? ? ? ? ?ts_pre_close[jj],ts_volume[jj],ts_amount[jj],ts_change[jj],ts_pct_chg[jj],ts_dvdfactor[jj]))
crsr.commit() crsr.close() conn.close() ?
|