def stk_get_sme_classified(): try: conn = connMysql().createconn() cur = conn.cursor() cur.execute('truncate table stk_sme_classified;') cur.close() conn.close() df = ts.get_sme_classified() df.to_sql('stk_sme_classified', engine, if_exists='append') except: print("获取中小板数据失败")
def stk_basics(): print("\n =======获取股票列表============") try: df = ts.get_stock_basics() conn = connMysql().createconn() cur = conn.cursor() cur.execute('truncate table stk_basics;') cur.close() conn.close() df.to_sql('stk_basics', engine, if_exists='append') except: print("获取股票基本信息失败")
def stk_inst_tops(days=5,table = 'stk_inst_tops_ma5'): print("\n插入数据:" + table) try: conn = connMysql().createconn() cur = conn.cursor() sql = 'truncate table ' + table + ';' cur.execute(sql) cur.close() conn.close() df = ts.inst_tops(days=days) df.to_sql(table,engine,if_exists='append') except: print("\n收集机构席位追踪失败")
def bigTrade(code, dat=str(date.today())): df = ts.get_sina_dd(code=code, date=dat, vol=600) # 默认400手 conn = connMysql().createconn() conn.autocommit(True) cur = conn.cursor() sql = 'delete from stk_dadan_detail where code=' + '\'' + code + '\'' + ' and date=' + '\'' + dat + '\';' cur.execute(sql) try: if (type(df) == pandas.core.frame.DataFrame): df['date'] = dat #print() df.to_sql('stk_dadan_detail', engine, if_exists='append') except: print(code + ': 获取大单数据失败')
def stk_k_line_data(code): print("\n获取K线数据:" + code) conn = connMysql().createconn() conn.autocommit(True) cur = conn.cursor() # sql = 'delete from stk_k_line_data where code=' + '\'' + code + '\'' + ' and date=' + '\'' + dat + '\';' sql = 'delete from stk_k_line_data where code=' + '\'' + code + '\';' #print(sql) cur.execute(sql) df = ts.get_hist_data(code=code) try: if (type(df) == pandas.core.frame.DataFrame): df['code'] = code # print(df) df.to_sql('stk_k_line_data', engine, if_exists='append') except: print(code + ': 获取K线数据失败')
df['v_ma50'] = pd.rolling_mean(df['volume'], 50) df['ma120'] = pd.rolling_mean(df['close'], 120) df['v_ma120'] = pd.rolling_mean(df['volume'], 120) df = df.fillna(0) df.to_sql('stk_hist_data', engine, if_exists='append') except Exception as e: print("获取股票历史数据失败: " + str(stkcode) + str(e)) if (__name__ == '__main__'): engine = create_engine( 'mysql+pymysql://root:[email protected]/tushare?charset=utf8') # sql = 'delete from stk_hist_data where code = ' + '\'' + stkcode + '\';' sql = 'truncate table stk_hist_data;' conn = connMysql().createconn() cur = conn.cursor() cur.execute(sql) cur.close() conn.close() stk_basics() stkList = ts.get_stock_basics() threads = [] x = 0 for stk in stkList.index: # stk_get_hist_data(stkcode=stk) try: print(stk) t = threading.Thread(target=stk_get_hist_data, args=(stk, )) threads.append(t)