def get_ST(self): # 暂停上市 zt = ts.get_suspended() print zt # 终止上市 zz = ts.get_terminated() print zz
def classification(class_types): if class_types == 'industry': industry_classified = ts.get_industry_classified() industry_classified.to_csv('D:\\ts\\classification\\industry_classified.csv', encoding='gbk') elif class_types == 'concept': concept_classified = ts.get_concept_classified() concept_classified.to_csv('D:\\ts\\classification\\concept_classified.csv', encoding='gbk') elif class_types == 'area': area_classified = ts.get_area_classified() area_classified.to_csv('D:\\ts\\classification\\area_classified.csv', encoding='gbk') elif class_types == 'sme': sme_classified = ts.get_sme_classified() sme_classified.to_csv('D:\\ts\\classification\\sme_classified.csv', encoding='gbk') elif class_types == 'gem': gem_classified = ts.get_gem_classified() gem_classified.to_csv('D:\\ts\\classification\\gem_classified.csv', encoding='gbk') elif class_types == 'st': st_classified = ts.get_st_classified() st_classified.to_csv('D:\\ts\\classification\\st_classified.csv', encoding='gbk') elif class_types == 'hs300': hs300s = ts.get_hs300s() hs300s.to_csv('D:\\ts\\classification\\hs300s.csv', encoding='gbk') elif class_types == 'sz50': sz50s = ts.get_sz50s() sz50s.to_csv('D:\\ts\\classification\\sz50s.csv', encoding='gbk') elif class_types == 'zz500': zz500s = ts.get_zz500s() zz500s.to_csv('D:\\ts\\classification\\zz500s.csv', encoding='gbk') elif class_types == 'terminated': terminated = ts.get_terminated() terminated.to_csv('D:\\ts\\classification\\terminated.csv', encoding='gbk') elif class_types == 'suspended': suspended = ts.get_suspended() suspended.to_csv('D:\\ts\\classification\\suspended.csv', encoding='gbk')
def QA_SU_save_stock_terminated(client=DATABASE): ''' 获取已经被终止上市的股票列表,数据从上交所获取,目前只有在上海证券交易所交易被终止的股票。 collection: code:股票代码 name:股票名称 oDate:上市日期 tDate:终止上市日期 :param client: :return: None ''' # 🛠todo 已经失效从wind 资讯里获取 # 这个函数已经失效 print("!!! tushare 这个函数已经失效!!!") df = QATs.get_terminated() #df = QATs.get_suspended() print( " Get stock terminated from tushare,stock count is %d (终止上市股票列表)" % len(df) ) coll = client.stock_terminated client.drop_collection(coll) json_data = json.loads(df.reset_index().to_json(orient='records')) coll.insert(json_data) print(" 保存终止上市股票列表 到 stock_terminated collection, OK")
tables = cur.fetchall() selectab = re.compile(r'\w*\w*') tabnames = selectab.findall(str(tables)) res = tab_name in tabnames return res stk_concept_class = ts.get_concept_classified() stk_area_class = ts.get_area_classified() stk_sme_class = ts.get_sme_classified() stk_gem_class = ts.get_gem_classified() stk_st_class = ts.get_st_classified() stk_hs300_class = ts.get_hs300s() stk_sz50_class = ts.get_sz50s() stk_zz500_class = ts.get_zz500s() stk_terminated_class = ts.get_terminated() stk_sus_class = ts.get_suspended() while True: #hostname = input('Please enter the hostname: ') pw = input('Please enter the password: '******'Please enter the number of datebase: ') try: conn = mysql.connector.connect(host='10.23.0.2', port=3306, user='******', password=pw, database=dbname) cur = conn.cursor() break except:
def get_stock_terminated(): return ts.get_terminated()
from sqlalchemy import create_engine import tushare as ts stock_basics = ts.get_stock_basics() #所有股票列表 stock_basic industry = ts.get_industry_classified() # 行业分类 industry concept = ts.get_concept_classified() # 概念分类 concept area = ts.get_area_classified() # 地域分类 area #sme = ts.get_sme_classified() # 中小板 sme #gem = ts.get_gem_classified() # 创业板 gem #st = ts.get_st_classified() # ST风险警示板 st hs300s = ts.get_hs300s() # 沪深300 hs300s sz50s = ts.get_sz50s() # 上证50 sz50s zz500s = ts.get_zz500s() # 中证500 zz500s terminated = ts.get_terminated() # 终止上市 terminated suspended = ts.get_suspended() # 暂停上市 suspended engine = create_engine('mysql://*****:*****@10.20.116.107/CN_BASSOON?charset=utf8') #sme.to_sql('sme', engine, if_exists='append') #gem.to_sql('gem', engine, if_exists='append') #st.to_sql('st', engine, if_exists='append') hs300s.to_sql('hs300s', engine, if_exists='append') sz50s.to_sql('sz50s', engine, if_exists='append') zz500s.to_sql('zz500s', engine, if_exists='append') terminated.to_sql('terminated', engine, if_exists='append') suspended.to_sql('suspended', engine, if_exists='append') stock_basics.to_sql('stock_basics', engine, if_exists='append') industry.to_sql('industry', engine, if_exists='append') concept.to_sql('concept', engine, if_exists='append') area.to_sql('area', engine, if_exists='append')
def getTerminated(): jsonFile = os.path.join(config.listsRootPath, "stockTerminated.json") data = ts.get_terminated() writeFile(jsonFile, data, 'records', False)
def get_terminated_info(file_path): terminated_info = ts.get_terminated() terminated_info.to_csv(file_path, encoding='utf-8') print '\ndownload terminated info finished\n'
def init_terminated_class(self): df = ts.get_terminated() df.to_csv(self.terminated_class_file)
def stock_list_update(self,type='all'): ''' 股票基本面板更新 :param type: :return: ''' type_list =['all', 'industry', 'concept', 'area', 'sme', 'gem', 'st', 'hs300s', 'ss50s', 'zz500s', 'terminated', 'suspended'] #输入类型检查 if not isinstance(type,str): raise TypeError,'stock_type must be string' elif isinstance(type,str) and type not in type_list: raise ValueError,'%s is not a valid type' % (type) #更新数据 if type == 'all': temp = ts.get_stock_basics() path = os.path.join(StockDataControler().stock_basis_init(), 'global.csv') temp.to_csv(path) return temp if type == 'industry': temp = ts.get_industry_classified() #temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'industry.csv') temp.to_csv(path) if type == 'concept': temp = ts.get_concept_classified() #temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'concept.csv') temp.to_csv(path) if type == 'area': temp = ts.get_area_classified() #temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'area.csv') temp.to_csv(path) if type == 'sme': temp = ts.get_sme_classified() #temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'sme.csv') temp.to_csv(path) if type == 'gem': temp = ts.get_gem_classified() #temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'gem.csv') temp.to_csv(path) if type == 'st': temp = ts.get_st_classified() # temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'st.csv') temp.to_csv(path) if type == 'hs300s': temp = ts.get_hs300s() temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'hs300s.csv') temp.to_csv(path) if type == 'sz50s': temp = ts.get_sz50s() temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'sz50s.csv') temp.to_csv(path) if type == 'zz500s': temp = ts.get_zz500s() temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'zz500s.csv') temp.to_csv(path) if type == 'terminated': temp = ts.get_terminated() temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'terminated.csv') temp.to_csv(path) if type == 'suspended': temp = ts.get_suspended() temp['name'] = temp['name'].map(lambda x: x.encode('utf-8')) path = os.path.join(StockDataControler().stock_basis_init(), 'suspended.csv') temp.to_csv(path)
import tushare as ts import storage as storage storage.save_sql(ts.get_industry_classified(), "basics/industry_classified") storage.save_sql(ts.get_concept_classified(), "basics/concept_classified") storage.save_sql(ts.get_area_classified(), "basics/area_classified") storage.save_sql(ts.get_st_classified(), "basics/st_classified") storage.save_sql(ts.get_hs300s(), "basics/hs300s") storage.save_sql(ts.get_sz50s(), "basics/sz50s") storage.save_sql(ts.get_zz500s(), "basics/zz500s") storage.save_sql(ts.get_terminated(), "basics/terminated") storage.save_sql(ts.get_suspended(), "basics/suspended") storage.save_sql(ts.get_sme_classified(), "basics/sme_classified") storage.save_sql(ts.get_gem_classified(), "basics/gem_classified")
def terminateddb(): terminated = ts.get_terminated() print(terminated, sep=' ', end='\n', file=sys.stdout, flush=False) terminated.to_sql('terminated_data', ENGINE)
print("#0", df) df = ts.get_hist_data('600848',start='2016-08-05',end='2016-08-23') print("#1", df) df = ts.get_hist_data('600848',start='2016-08-05',end='2016-08-23', ktype='W') print("#2", df) df = ts.get_hist_data('600848',start='2016-08-05',end='2016-08-23', ktype='M') print("#3", df) df = ts.get_hist_data('600848',start='2016-08-05',end='2016-08-23', ktype='5') print("#4", df) df = ts.get_hist_data('600848',start='2016-08-05',end='2016-08-23', ktype='15') print("#5", df) df = ts.get_hist_data('600848',start='2016-08-05',end='2016-08-23', ktype='30') print("#6", df) df = ts.get_hist_data('600848',start='2016-08-05',end='2016-08-23', ktype='60') print("#7", df) df = ts.get_index() printD("ts.get_index", df) df = ts.get_suspended() printD("ts.get_suspended()", df) df = ts.get_sz50s() printD("ts.get_sz50s", df) df = ts.get_terminated() printD("ts.get_terminated", df) df = ts.get_zz500s() printD("ts.get_zz500s", df)
def classified(): #在现实交易中,经常会按行业统计股票的涨跌幅或资金进出,本接口按照sina财经对沪深股票进行的行业分类,返回所有股票所属行业的信息。 #注意:有的股票属于两个行业:如000587 industry = ts.get_industry_classified() industry = industry.drop_duplicates() industry_new = data_pro(industry) industry_new.rename(columns={'c_name': 'industry'}, inplace=True) #返回股票概念的分类数据,现实的二级市场交易中,经常会以”概念”来炒作,在数据分析过程中,可根据概念分类监测资金等信息的变动情况。 concept = ts.get_concept_classified() concept = concept.drop_duplicates() concept_new = data_pro(concept) concept_new.rename(columns={'c_name': 'concept'}, inplace=True) #按地域对股票进行分类,即查找出哪些股票属于哪个省份。 area = ts.get_area_classified() area = area.drop_duplicates() area_new = data_pro(area) #获取中小板股票数据,即查找所有002开头的股票 sme = ts.get_sme_classified() sme['belong_bk'] = '中小板' #获取创业板股票数据,即查找所有300开头的股票 gem = ts.get_gem_classified() gem['belong_bk'] = '创业板' #获取风险警示板股票数据,即查找所有st股票 st = ts.get_st_classified() st['belong_bk'] = '风险警示板' bk = pd.concat([sme, gem, st], join='outer', axis=0) bk = bk.drop_duplicates() bk = data_pro(bk) # 获取沪深300当前成份股及所占权重 hs300s = ts.get_hs300s() hs300s = hs300s.iloc[:, 1:-1] hs300s['belong_cfg'] = '沪深300成份股' # 获取上证50成份股 sz50s = ts.get_sz50s() sz50s = sz50s.iloc[:, 1:] sz50s['belong_cfg'] = '上证50成份股' # 获取中证500成份股 zz500s = ts.get_zz500s() zz500s = zz500s.iloc[:, 1:-1] zz500s['belong_cfg'] = '中证500成份股' cfg = pd.concat([hs300s, sz50s, zz500s], join='outer', axis=0) cfg = cfg.drop_duplicates() cfg = data_pro(cfg) # 获取已经被终止上市的股票列表,数据从上交所获取,目前只有在上海证券交易所交易被终止的股票。 terminated = ts.get_terminated() if terminated != None: terminated['终止上市'] = '终止上市' # 获取被暂停上市的股票列表,数据从上交所获取,目前只有在上海证券交易所交易被终止的股票。 suspended = ts.get_suspended() if suspended != None: suspended['暂停上市'] = '暂停上市' # 数据拼接 table_list = [industry_new, concept_new, area_new, bk, cfg] for i in range(len(table_list)): if i == 0: classified_data = table_list[i] else: classified_data = pd.merge(classified_data, table_list[i], how='outer', on=['code', 'name']) classified_data = classified_data.fillna('未知') #一个代码对应多个名称的数据整理 classified_temp = classified_data.groupby(['code']) classified_new = classified_temp.agg(lambda x: '|'.join(x)) classified_dict = { 'industry': '所属行业', 'name': '名称', 'concept': '概念', 'area': '区域', 'belong_bk': '所属板块', 'belong_cfg': '所属成分股' } classified_new.columns = classified_new.columns.map(classified_dict) industry_list = classified_new['所属行业'] industry_list = [i.split('|')[0] for i in industry_list] classified_new['所属行业'] = industry_list return classified_new
'''获取中证500成份股 返回值说明: code:股票代码 name:股票名称''' print('中证500成份股') print(ts.get_zz500s()) '''获取已经被终止上市的股票列表,数据从上交所获取,目前只有在上海证券交易所交易被终止的股票。 返回值说明: code:股票代码 name:股票名称 oDate:上市日期 tDate:终止上市日期''' print('终止上市股票列表') print(ts.get_terminated()) ''''获取被暂停上市的股票列表,数据从上交所获取,目前只有在上海证券交易所交易被终止的股票。 返回值说明: code:股票代码 name:股票名称 oDate:上市日期 tDate:暂停上市日期''' print('暂停上市的股票列表') print(ts.get_suspended())
gc = ts.get_gem_classified() print(gc) # 风险警示板分类 # 获取风险警示板股票数据,即查找所有st股票 st = ts.get_st_classified() print(st) # 沪深300成份及权重 # 获取沪深300当前成份股及所占权重 # FIXME 有bug h3 = ts.get_hs300s() print(h3) # 上证50成份股 # 获取上证50成份股 # FIXME 有bug ts.get_sz50s() # 中证500成份股 # FIXME 有bug ts.get_zz500s() # 终止上市股票列表 t = ts.get_terminated() print(t) # 暂停上市股票列表 # 获取被暂停上市的股票列表,数据从上交所获取,目前只有在上海证券交易所交易被终止的股票。 s = ts.get_suspended() print(s)
def init(engine, session): tsl.log("stock_industry start...") df = ts.get_industry_classified() df.to_sql('stock_industry',engine,if_exists='replace') print tsl.log("stock_industry done") tsl.log("stock_concept start...") df = ts.get_concept_classified() df.to_sql('stock_concept',engine,if_exists='replace') print tsl.log("stock_concept done") tsl.log("stock_area start...") df = ts.get_area_classified() df.to_sql('stock_area',engine,if_exists='replace') tsl.log("stock_area done") tsl.log("stock_sme start...") df = ts.get_sme_classified() df.to_sql('stock_sme',engine,if_exists='replace') tsl.log("stock_sme done") tsl.log("stock_gem start...") df = ts.get_gem_classified() df.to_sql('stock_gem',engine,if_exists='replace') tsl.log("stock_gem done") tsl.log("stock_risk_warning start...") df = ts.get_st_classified() df.to_sql('stock_risk_warning',engine,if_exists='replace') tsl.log("stock_risk_warning done") tsl.log("stock_hs300 start...") df = ts.get_hs300s() df.to_sql('stock_hs300',engine,if_exists='replace') tsl.log("stock_hs300 done") tsl.log("stock_sz50 start...") df = ts.get_sz50s() df.to_sql('stock_sz50',engine,if_exists='replace') tsl.log("stock_sz50 done") tsl.log("stock_zz500 start...") df = ts.get_zz500s() df.to_sql('stock_zz500',engine,if_exists='replace') tsl.log("stock_zz500 done") tsl.log("stock_stop_list start...") df = ts.get_terminated() df.to_sql('stock_stop_list',engine,if_exists='replace') tsl.log("stock_stop_list done") tsl.log("stock_pause_list start...") df = ts.get_suspended() df.to_sql('stock_pause_list',engine,if_exists='replace') tsl.log("stock_pause_list done") tsl.log("call update_stock_info start...") session.execute('call update_stock_info') tsl.log("call update_stock_info done")
def classification_type(class_types): if class_types == 'classification_industry': industry_classified = ts.get_industry_classified('sw') industry_classified.to_sql('classification_industry', engine, flavor='mysql', if_exists='replace') elif class_types == 'concept': concept_classified = ts.get_concept_classified() concept_classified.to_sql('classification_concept', engine, flavor='mysql', if_exists='replace') elif class_types == 'area': area_classified = ts.get_area_classified() area_classified.to_sql('classification_area', engine, flavor='mysql', if_exists='replace') elif class_types == 'sme': sme_classified = ts.get_sme_classified() sme_classified.to_sql('classification_sme', engine, flavor='mysql', if_exists='replace') elif class_types == 'gem': gem_classified = ts.get_gem_classified() gem_classified.to_sql('classification_gem', engine, flavor='mysql', if_exists='replace') elif class_types == 'st': st_classified = ts.get_st_classified() st_classified.to_sql('classification_st', engine, flavor='mysql', if_exists='replace') elif class_types == 'hs300': hs300s = ts.get_hs300s() hs300s.to_sql('classification_hs300s', engine, flavor='mysql', if_exists='replace') elif class_types == 'sz50': sz50s = ts.get_sz50s() sz50s.to_sql('classification_sz50s', engine, flavor='mysql', if_exists='replace') elif class_types == 'zz500': zz500s = ts.get_zz500s() zz500s.to_sql('classification_zz500s', engine, flavor='mysql', if_exists='replace') elif class_types == 'terminated': terminated = ts.get_terminated() terminated.to_sql('classification_terminated', engine, flavor='mysql', if_exists='replace') elif class_types == 'suspended': suspended = ts.get_suspended() suspended.to_sql('classification_suspended', engine, flavor='mysql', if_exists='replace')
def load_company_industry_info(): #下载加载行业分类数据 try: rs=ts.get_industry_classified() sql.write_frame(rs, "company_industry_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载公司行业分类信息ok") except: print("下载公司行业分类信息出错") #下载加载概念分类数据 try: rs=ts.get_concept_classified() sql.write_frame(rs, "company_concept_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("载公司概念分类信息ok") except: print("下载公司概念分类信息出错") #下载加载地域分类数据 try: rs=ts.get_area_classified() sql.write_frame(rs, "company_area_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载公司区域分类信息ok") except: print("下载公司区域分类信息出错") #下载加载中小板分类数据 try: rs=ts.get_sme_classified() sql.write_frame(rs, "company_sme_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载中小板分类数据ok") except: print("下载中小板分类数据出错") #下载加载创业板分类数据 try: rs=ts.get_gem_classified() sql.write_frame(rs, "company_gem_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载创业板分类数据ok") except: print("下载创业板分类数据出错") #下载加载st板分类数据 try: rs=ts.get_st_classified() sql.write_frame(rs, "company_st_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载st板分类数据ok") except: print("下载st板分类数据出错") #下载加载沪深300板分类数据 try: rs=ts.get_hs300s() sql.write_frame(rs, "company_hs300_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载沪深300板分类数据ok") except: print("下载加载沪深300板分类数据出错") #下载加载上证50板分类数据 try: rs=ts.get_sz50s() sql.write_frame(rs, "company_sz50_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载上证50板分类数据ok") except: print("下载加载上证50板分类数据出错") #下载加载中证500板分类数据 try: rs=ts.get_zz500s() sql.write_frame(rs, "company_zz500_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载中证500板分类数据ok") except: print("下载加载中证500板分类数据出错") #下载加载终止上市分类数据 try: rs=ts.get_terminated() sql.write_frame(rs, "company_terminated_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载终止上市分类数据ok") except: print("下载加载终止上市分类数据出错") #下载加载暂停上市分类数据 try: rs=ts.get_suspended() sql.write_frame(rs, "company_suspended_classified", con=conn_company_classified , flavor='mysql', if_exists='replace',index=True) print("下载加载暂停上市分类数据ok") except: print("下载加载暂停上市分类数据出错")
def get_stock_chengfeng(self): df = ts.get_sz50s() # print df terminal_stock = ts.get_terminated() print terminal_stock
def get_stock_chengfeng(self): df = ts.get_sz50s() # print(df) terminal_stock = ts.get_terminated() print(terminal_stock)
time.sleep(1) # 沪深300 hss = ts.get_hs300s() time.sleep(1) # 上证50 szs = ts.get_sz50s() time.sleep(1) # 中证500 zzs = ts.get_zz500s() time.sleep(1) # 终止上市 tss = ts.get_terminated() time.sleep(1) # 暂停上市 ss = ts.get_terminated() time.sleep(1) # 股本 stock_basic = ts.get_stock_basics() time.sleep(1) news_basic = news.merge(inds, on='code', how='left').merge(concepts, on='code', how='left').merge(areas, on='code', how='left')