def getShMargins(cursor): # try: startTime = '1990-01-01' endTime = '2020-01-01' # df = ts.sh_margins(start=startTime, end=endTime) df = ts.sh_margins(start=startTime, end=endTime) # 处理缺失值 df = df.fillna(0) print(df) dfLen = len(df) uuidList = [] # 添加uuid for l in range(0, dfLen): uuidList.append(uuid.uuid1()) df['uuid'] = uuidList for k in range(0, dfLen): df2 = df[k:k + 1] cursor.execute( "insert into stock_sh_margins(uuid, op_date, rzye, rzmre, " "rqyl, rqylje, rqmcl, rzrqjyzl ) " "values(:uuid, to_date(:op_date, 'yyyy-MM-dd'), :rzye, :rzmre, " ":rqyl, :rqylje, :rqmcl, :rzrqjyzl)", (str(list(df2['uuid'])[0]), str(list(df2['opDate'])[0]), round(float(df2['rzye']), 4), round(float(df2['rzmre']), 4), round(float(df2['rqyl']), 4), round(float(df2['rqylje']), 4), round(float(df2['rqmcl']), 4), round(float(df2['rzrqjyzl']), 4))) cursor.execute("commit")
def get_financing_securities_sh(start_date=None, end_date=None, retry_count=RETRY_COUNT, pause=PAUSE): logger.info('Begin get financing securities sh data, start_date is %s,' ' end_date is %s.' % (start_date, end_date)) data_dicts = [] try: data_df = ts.sh_margins(start=start_date, end=end_date, retry_count=retry_count, pause=pause) except Exception: logger.exception( 'Error get financing securities sh data, start_date is %s, ' 'end_date is %s.' % (start_date, end_date)) else: if data_df is None or data_df.empty: logger.warn( 'Empty get financing securities sh data, start_date is %s,' ' end_date is %s.' % (start_date, end_date)) else: data_dicts = [{ 'op_date': row[0], 'rzye': row[1], 'rzmre': row[2], 'rqyl': row[3], 'rqylje': row[4], 'rqmcl': row[5], 'rzrqjyzl': row[6] } for row in data_df.values] logger.info( 'Success get financing securities sh data, start_date is %s,' ' end_date is %s.' % (start_date, end_date)) return data_dicts
def get_margin(start, end): sh_margins = ts.sh_margins(start=start, end=end) sz_margins = ts.sz_margins(start=start, end=end) margins = pd.concat([sh_margins, sz_margins]) margins['rzmre_z'] = (margins['rzmre'] - margins['rzmre'].mean()) / margins['rzmre'].std() margins['rqmcl_z'] = (margins['rqmcl'] - margins['rqmcl'].mean()) / margins['rqmcl'].std() return margins
def getShMargins(cursor): # try: startTime = '1990-01-01' endTime = '2020-01-01' # df = ts.sh_margins(start=startTime, end=endTime) df = ts.sh_margins(start=startTime, end=endTime) # 处理缺失值 df = df.fillna(0) #print(df) dfLen = len(df) uuidList = [] # 添加uuid for l in range(0, dfLen): uuidList.append(uuid.uuid1()) df['uuid'] = uuidList for k in range(0, dfLen): df2 = df[k:k + 1] try: cursor.execute( "insert into stock_sh_margins(uuid, op_date, rzye, rzmre, " "rqyl, rqylje, rqmcl, rzrqjyzl ) " "values('%s', '%s', '%.4f', '%.4f', " "'%.4f', '%.4f', '%.4f', '%.4f')" % (str(list(df2['uuid'])[0]), str(list(df2['opDate'])[0]), round(float(df2['rzye']), 4), round(float(df2['rzmre']), 4), round(float(df2['rqyl']), 4), round(float( df2['rqylje']), 4), round(float( df2['rqmcl']), 4), round(float(df2['rzrqjyzl']), 4))) cursor.execute("commit") except Exception as e: # pass print(e)
def get_rzrq_sh(date_start, date_end): """ 获取沪市融资融券汇总数据 Parameters -------- date_start 开始日期,e.g. '2010-03-31' date_end 截止日期,e.g. '2017-03-28' Return -------- DataFrame date 日期 rzye_sh 融资余额上海(元) rzmre_sh 融资买入额上海(元) rqyl_sh 融券余量上海 rqylje_sh 融券余量金额上海(元) rqmcl_sh 融券卖出量上海 rzrqye_sh 融资融券余额上海(元) """ rzrq = ts.sh_margins(start=date_start, end=date_end) # Rename Columns columns_map = { 'opDate': 'date', 'rzye': 'rzye_sh', 'rzmre': 'rzmre_sh', 'rqyl': 'rqyl_sh', 'rqylje': 'rqylje_sh', 'rqmcl': 'rqmcl_sh', 'rzrqjyzl': 'rzrqye_sh' } rzrq.rename(columns=columns_map, inplace=True) return rzrq
def sz_margin_general(start, end, **kwargs): if end < _rzrq_start: return None args = { "start": start.strftime("%Y-%m-%d"), "end": end.strftime("%Y-%m-%d") } return ts.sh_margins(**args)
def setShMargins(self,endTime =None,number =1, isSave = False,tableName = REFERENCE_SH_MARGINS): [startTime,endTime] = getStartTime(endTime = endTime, number=number) try: df = ts.sh_margins(startTime,endTime) if isSave is True: df.to_sql(tableName,self.engine_sql, if_exists='append') except IOError,e: print e
def _sh_margin(): start = '1990-01-01' sh = ts.sh_margins(start) sh.rename(columns={ 'opDate': 'date', 'rqylje': 'rqye', 'rzrqjyzl': 'rzrqye' }, inplace=True) return sh
def get_sh_margins(f, reset_flag=False): """ 获取沪市的融资融券 """ df = ts.sh_margins(pause=conf.REQUEST_BLANK) df = df.sort_values(by=["opDate"]) if reset_flag is False: tool.merge_df_dataset(f, conf.HDF5_FUNDAMENTAL_SH_MARGINS, df) else: tool.create_df_dataset(f, conf.HDF5_FUNDAMENTAL_SH_MARGINS, df) return
def draw_rzye(): data = ts.sh_margins(start='2015-06-20', end='2015-10-28') data['opDate'] = data['opDate'].map(str2date2) data = data.sort_index(ascending=True) data['rzye'] = data['rzye']/100000000 data['rzmre'] = data['rzmre']/10000000 data['rqyl'] = data['rqyl']/1000000 data['rqmcl'] = data['rqmcl']/1000000 length = -200 plt.plot(data['opDate'][length:],data['rzye'][length:],'r',label='rzye') plt.plot(data['opDate'][length:],data['rzmre'][length:],'g',label='rzmre') plt.show()
def main(table): start='2016-01-01' end='2016-05-07' if table == 'sh_margins': df = ts.sh_margins(start=start, end=end) else: df = ts.sz_margins(start=start, end=end) df.to_sql(name='tablename_temp', con=dbcon.engine, if_exists = 'replace', index=False) connection = dbcon.engine.connect() sql = """INSERT OR IGNORE INTO %s SELECT * FROM tablename_temp""" % table connection.execute(text(sql)) connection.execute(text('DROP TABLE tablename_temp'))
def get_sh_margins(start=None, end=None): """ 投资参考 系列 融资融券(沪市):沪市融资融券汇总数据 """ df = ts.sh_margins(start, end) print(df) if df is not None: res = df.to_sql(invest_stock_ref_sh_margins, engine, if_exists='replace') msg = 'ok' if res is None else res print('获取沪市融资融券汇总数据: 开始时间:{0} 结束时间:{1}: {2}'.format(start, end, msg) + '\n') else: print('获取沪市融资融券汇总数据: 开始时间:{0} 结束时间:{1}: {2}'.format(start, end, 'None') + '\n')
def margin_sh_smry(engine, sdate, edate): tbl = "invest_margin_sh_smry" tsl.log(tbl + " start...") try: df = ts.sh_margins(sdate, edate) df = df.set_index('opDate', drop='true') df.to_sql(tbl, engine, if_exists='append') print tsl.log(tbl + " done") except BaseException, e: print print e tsl.log(tbl + " error")
def draw_rzye(): data = ts.sh_margins(start='2015-06-20', end='2015-10-28') data['opDate'] = data['opDate'].map(str2date2) data = data.sort_index(ascending=True) data['rzye'] = data['rzye'] / 100000000 data['rzmre'] = data['rzmre'] / 10000000 data['rqyl'] = data['rqyl'] / 1000000 data['rqmcl'] = data['rqmcl'] / 1000000 length = -200 plt.plot(data['opDate'][length:], data['rzye'][length:], 'r', label='rzye') plt.plot(data['opDate'][length:], data['rzmre'][length:], 'g', label='rzmre') plt.show()
def job_2(): try: print("I'm working......投资参考数据") # 分配预案 profit_data = ts.profit_data(year, top=1000) data = pd.DataFrame(profit_data) data.to_sql('profit_data',engine,index=True,if_exists='replace') print("分配预案......done") # 业绩预告 forecast_data = ts.forecast_data(year,1) data = pd.DataFrame(forecast_data) data.to_sql('forecast_data',engine,index=True,if_exists='replace') print("业绩预告......done") # 限售股解禁 xsg_data = ts.xsg_data() data = pd.DataFrame(xsg_data) data.to_sql('xsg_data',engine,index=True,if_exists='replace') print("限售股解禁......done") # 基金持股 fund_holdings = ts.fund_holdings(year, 1) data = pd.DataFrame(fund_holdings) data.to_sql('fund_holdings',engine,index=True,if_exists='replace') print("基金持股......done") # 新股数据 new_stocks = ts.new_stocks() data = pd.DataFrame(new_stocks) data.to_sql('new_stocks',engine,index=True,if_exists='replace') print("新股数据......done") # 融资融券(沪市) sh_margins = ts.sh_margins() data = pd.DataFrame(sh_margins) data.to_sql('sh_margins',engine,index=True,if_exists='replace') print("融资融券(沪市)......done") # 融资融券(深市) sz_margins = ts.sz_margins() data = pd.DataFrame(sz_margins) data.to_sql('sz_margins',engine,index=True,if_exists='replace') print("融资融券(深市)......done") except Exception as e: print(e)
def sh_margins(request): try: start = request.POST.get('start', '') #选填 end = request.POST.get('end', '') #选填 data = ts.sh_margins(start, end) res = { 'columns': [ '信用交易日期', '本日融资余额(元)', '本日融资买入额(元)', '本日融券余量', '本日融券余量金额(元)', '本日融券卖出量', '本日融资融券余额(元)' ], 'data': json.loads(json.dumps(data.values, cls=DateEncoder)) } except (BaseException): return HttpResponse(BaseException) else: return HttpResponse(json.dumps(res), content_type="application/json")
def show_margins(startdate, enddate, isSH): if isSH == 'sh': df = ts.sh_margins(start=startdate, end=enddate) # 反序 df1 = df[::-1] y1 = df1['rzye'] y2 = df1['rqylje'] # 数值太大,降低用与比较 y = (y1 - y2)/100000000 x = df.index xlist = x.tolist() ylist = y.tolist() else: # 取深市 df = ts.sz_margins(start=startdate, end=enddate) y1 = df['rzye'] y2 = df['rqye'] y = (y1 - y2)/100000000 xlist = np.arange(0, len(df)).tolist() ylist = y.tolist() #取上证同期指数 if isSH == 'sh': shdf = ts.get_hist_data('sh',start=startdate,end=enddate) shdf1 = shdf[::-1] ysh = shdf1['close'] yy = ysh + ysh else: szdf = ts.get_hist_data('sz',start=startdate,end=enddate) szdf1 = szdf[::-1] ysh = szdf1['close'] yy = (ysh + ysh)/10 if len(ysh) > len(xlist): yshlist = yy[1:].tolist() else: yshlist = yy.tolist() plt.plot(xlist, ylist,label="rzye") # 放大上证指数,用于对比 sz plt.plot(xlist,yshlist,label="sh*2,sz*2") plt.legend() plt.show()
def getData(self, mongo, func): if (func == 'profit_data'): df = fd.profit_data(top=self.top) elif (func == 'xsg_data'): df = fd.xsg_data() elif (func == 'new_stocks'): df = fd.new_stocks() elif (func == 'sh_margins'): df = ts.sh_margins(self.start, self.end) elif (func == 'sz_margins'): df = ts.sz_margins(self.start, self.end) elif (func == 'sz_margin_details'): df = ts.sz_margin_details(self.end) else: df = {} print("func:") print(func) print("type:") print(type(df)) insert_string = df.to_json(orient='records') items = json.loads(insert_string)
import talib import numpy as np import pandas as pd import tushare as ts # 沪市融资融券汇总数据 # opDate:信用交易日期 rzye:本日融资余额(元) rzmre: 本日融资买入额(元) rqyl: 本日融券余量 # rqylje: 本日融券余量金额(元) rqmcl: 本日融券卖出量 rzrqjyzl:本日融资融券余额(元) open('rzrq_sz1.csv', 'w') df = ts.sh_margins(start='2015-01-01', end='2015-06-01') df.to_csv('rzrq_sz1.csv') open('rzrq_sz2.csv', 'w') df = ts.sh_margin_details(start='2015-06-01', end='2015-12-31') df.to_csv('rzrq_sz2.csv') # 深市融资融券汇总数据 open('rzrq_ss1.csv', 'w') df = ts.sz_margins(start='2015-01-01', end='2015-06-01') df.to_csv('rzrq_ss1.csv') open('rzrq_ss2.csv', 'w') df = ts.sz_margins(start='2015-06-01', end='2015-12-31') df.to_csv('rzrq_ss2.csv') # 沪市融资融券明细数据 # opDate:信用交易日期,stockCode:标的证券代码,securityAbbr:标的证券简称,rzye:本日融资余额(元),rzmre: 本日融资买入额(元) # rzche:本日融资偿还额(元),rqyl: 本日融券余量,rqmcl: 本日融券卖出量,rqchl: 本日融券偿还量 # open('rzrq.csv', 'w') # df = ts.sh_margin_details(start='2015-01-01', end='2015-06-31', symbol='601989') # df.to_csv('rzrq.csv')
def margin_trading(self, stock): today = datetime.today().strftime("%Y-%m-%d") data = ts.sh_margins(start='2015-08-20', end=today)
import pymongo import json # 600547:山东黄金 # 600362:江西铜业 # 600312:平高电气 # 600499:科达洁能 # 603993:洛阳钼业 db = "InvestInfos" coll = "sh_margins" ty = "600547" market = 'SH' conn = pymongo.MongoClient('127.0.0.1', port=27017) if ty == 'all': df = ts.sh_margins(start='2017-01-01', end='2017-12-22') else: df = ts.sh_margin_details(start='2016-01-01', end='2017-01-01', symbol=ty) # index data columns(14 columns) dicIndex = json.loads(df.to_json(orient='split')) for i, ind in enumerate(dicIndex['index']): jsonstr = { '_id': dicIndex['data'][i][0] + "-" + ty, 'type': ty, 'market': market, dicIndex['columns'][0]: dicIndex['data'][i][0], dicIndex['columns'][1]: dicIndex['data'][i][1], dicIndex['columns'][2]: dicIndex['data'][i][2], dicIndex['columns'][3]: dicIndex['data'][i][3], dicIndex['columns'][4]: dicIndex['data'][i][4], dicIndex['columns'][5]: dicIndex['data'][i][5],
print each if each in resultfm20.sort(columns='bvalues', ascending=False).head(300).index: if each in resultfm10.sort(columns='bvalues', ascending=False).head(300).index: if each in resultfm5.sort(columns='bvalues', ascending=False).head(300).index: final[each] = [resultfm5['bvalues'].loc[each], resultfm10['bvalues'].loc[each], resultfm20['bvalues'].loc[each], resultfm30['bvalues'].loc[each]] print 'get' finfm = pd.DataFrame(final) finfm1=finfm.T finfm1.to_csv('fin_0504.csv') fm = td.get_i_ma(20.0, '20160303','2016-05-04', 300) # 取两融数据 df = ts.sh_margins(start='2015-01-01',end='2016-05-04') df = ts.sh_margin_details(start='2016-01-01',end='2016-05-04') df = ts.sh_margin_details(start='2015-01-01', end='2015-04-19', symbol='601989') df = ts.sz_margins(start='2015-06-01',end='2016-05-04') # 只能一天天取 df = ts.sz_margin_details('2015-04-20') # 取 沪深 日,周,分钟 df = ts.get_hist_data('sh', ktype='W') df = ts.get_hist_data('sz', ktype='60') df = ts.get_hist_data('cyb') df = ts.get_hist_data('zxb') # 龙虎榜 df = ts.top_list('2016-05-04') # 计算macd
#!/usr/bin/env python import tushare as ts import sys begin_date = sys.argv[1] end_date = sys.argv[2] #begin_date = '2015-01-01' #end_date = '2015-08-09' df = ts.sh_margins(start=begin_date, end=end_date) df.to_csv('../../data/sh_rzrq_all.txt')
from datetime import datetime class StockInfo(object): def __init__(self, stock): self.dl = DownLoad() def reference_info(self, stock): data = ts.profit_data(top=60) data.sort('shares', ascending=False) print data[data.shares >= 10] def forcast_data(self, stock): data = ts.forecast_data(2015, 3) print data[-100:] def new_stock(self, stock): pass def margin_trading(self, stock): today = datetime.today().strftime("%Y-%m-%d") data = ts.sh_margins(start='2015-08-20', end=today) if __name__ == '__main__': data = ts.sh_margins(start='2015-10-20', end='2015-10-28') data['rzye'] = data['rzye'] / 100000000 data['rzmre'] = data['rzmre'] / 100000000 data['rqyl'] = data['rqyl'] / 1000000 data['rqmcl'] = data['rqmcl'] / 1000000
def sh_margins(self, conns, start, end): self.base = Base() self.financial_data = conns['financial_data'] self.df = ts.sh_margins(start, end) self.base.batchwri(self.df, 'rongzirongquan_sh', self.financial_data)
投资参考数据: 一级:业绩预告、分配预案、限售解禁 二级:基金持股、融资融券 其它:新股上市 ''' #业绩预告(2017年第4季度) Forecast_data = ts.forecast_data(2017, 4) #分配预案(最近公布60条数据) Profit_data = ts.profit_data(top=60) #限售解禁(2017-12解禁股票) Xsg_data = ts.xsg_data(year=2017, month=12) #基金持股(2017年第4季度) Fund_holdings = ts.fund_holdings(2017, 3) #融资融券 Sh_margins = ts.sh_margins(start='2017-12-01', end='2017-12-15') #总的 Sh_margin_details = ts.sh_margin_details(start='2017-12-01', end='2017-12-15', symbol='601989') #指定股票 Sz_margins = ts.sz_margins(start='2017-12-01', end='2017-12-14') #总 Sz_margin_details = ts.sz_margin_details('2017-12-14') #个股列表 #新股上市 New_stocks = ts.new_stocks() ''' 数据储存:csv,mysql ''' #csv import os filename = 'D:/VNPY/bigfile.csv' for code in ['000875', '600848', '000981']:
retry_count:当网络异常后重试次数,默认为3 pause:重试时停顿秒数,默认为0 返回值说明: opDate:信用交易日期 rzye:本日融资余额(元) rzmre: 本日融资买入额(元) rqyl: 本日融券余量 rqylje: 本日融券余量金额(元) rqmcl: 本日融券卖出量 rzrqjyzl:本日融资融券余额(元) ''' ts.sh_margins(start='2017-01-01', end='2017-12-15') ''' 沪市融资融券明细数据 参数说明: date:日期 format:YYYY-MM-DD 默认为空’‘,数据返回最近交易日明细数据 symbol:标的代码,6位数字e.g.600848,默认为空’‘ start:开始日期 format:YYYY-MM-DD 默认为空’‘ end:结束日期 format:YYYY-MM-DD 默认为空’‘ retry_count:当网络异常后重试次数,默认为3 pause:重试时停顿秒数,默认为0 返回值说明: opDate:信用交易日期
from download import DownLoad from datetime import datetime class StockInfo(object): def __init__(self, stock): self.dl = DownLoad() def reference_info(self, stock): data = ts.profit_data(top=60) data.sort('shares',ascending=False) print data[data.shares>=10] def forcast_data(self,stock): data = ts.forecast_data(2015,3) print data[-100:] def new_stock(self, stock): pass def margin_trading(self, stock): today = datetime.today().strftime("%Y-%m-%d") data = ts.sh_margins(start='2015-08-20', end=today) if __name__ == '__main__': data = ts.sh_margins(start='2015-10-20', end='2015-10-28') data['rzye'] = data['rzye']/100000000 data['rzmre'] = data['rzmre']/100000000 data['rqyl'] = data['rqyl']/1000000 data['rqmcl'] = data['rqmcl']/1000000
def load_data(): start_date=get_date_add_days(get_max_date_sh_margins(),1) print(start_date,end_date) rs=ts.sh_margins(start=start_date, end=end_date) pd.DataFrame.to_sql(rs, table_name, con=conn, flavor='mysql', if_exists='append', index=False)
import matplotlib.pyplot as plt import tushare as ts from jaqs.data import DataApi if __name__ == '__main__': _start_date = '2014-01-01' _end_date = '2018-02-02' phone = '13706519134' token = 'eyJhbGciOiJIUzI1NiJ9.eyJjcmVhdGVfdGltZSI6IjE1MTM0ODE2NTM4MTAiLCJpc3MiOiJhdXRoMCIsImlkIjoiMTM3MDY1MTkxMzQifQ.6wOPdEDGnSBwMzb3dDVp_8LPBT0UbHqnFFvCc15bL_U' api = DataApi("tcp://data.tushare.org:8910") df, msg = api.login(phone, token) #获取融资余额 df1 = ts.sh_margins(start=_start_date, end=_end_date) df1.index = pd.to_datetime(df1.opDate) df1.sort_index(inplace=True) #获取上证日收盘价 df_sh, msg = api.daily(symbol='000001.SH', start_date=_start_date, end_date = _end_date, fields = '', freq = '1d') df_sh.trade_date = pd.Series(df_sh.trade_date, index=df_sh.index, dtype=str) df_sh.index = pd.to_datetime(df_sh.trade_date) df_sh.sort_index(inplace=True) df_a = df1['rzye']/100000000 df_b = df_sh['close'] fig = plt.figure() ax1 = fig.add_subplot(111) ax1.set_xlabel('Daily')
#coding=utf-8 ''' Created on 2015年6月4日 @author: Administrator ''' import tushare as ts # 分配预案 #每到季报、年报公布的时段,就经常会有上市公司利润分配预案发布,而一些高送转高分红的股票往往会成为市场炒作的热点。 df = ts.profit_data(top=60) df.sort('shares',ascending=False) df[df.shares>=10]#选择每10股送转在10以上的 # 业绩预告 ts.forecast_data(2014,2)#获取2014年中报的业绩预告数据 # 限售股解禁 # 以月的形式返回限售股解禁情况,通过了解解禁股本的大小,判断股票上行的压力。可通过设定年份和月份参数获取不同时段的数据。 ts.xsg_data() # 新股数据 ts.new_stocks() # 融资融券 ts.sh_margins(start='2015-01-01', end='2015-04-19') ts.sz_margins(start='2015-01-01', end='2015-04-19') #如果不设symbol参数或者开始和结束日期时段设置过长,数据获取可能会比较慢,建议分段分步获取,比如一年为一个周期 ts.sh_margin_details(start='2015-01-01', end='2015-04-19', symbol='601989') ts.sz_margin_details('2015-04-20')
def getShMargins(self): file_name = self.startDate+'_'+self.endDate+'_sh_margins.csv' path = self.index + self.index_sh_margins + file_name data = ts.sh_margins(start = self.startDate, end = self.endDate) data.to_csv(path, encoding='utf-8') print(file_name)
#coding=utf-8 ''' Created on 2015年7月3日 @author: Administrator ''' import tushare as ts startDate = '2015-06-01' endDate = '2015-07-02' df_sh = ts.sh_margins(start=startDate, end=endDate) df_sh.to_csv(r'd:\stock\shMargin.csv',index = False) df_sz = ts.sz_margins(start=startDate, end=endDate) df_sz.to_csv(r'd:\stock\szMargin.csv',index = False) print 'finished'
async def do_main_task(): day = time.strftime("%Y-%m-%d", time.localtime()) print(day) day = "2016-02-26" # r = await asyncio.sleep(1) cursor = db.tasks.find({"date":day}) status = 0 for document in cursor: status = document["status"] # 获取指数 if status < 1: print("task 1") df = ts.get_index() indexes = json.loads(df.to_json(orient='records')) for index in indexes: # print(index) index["date"] = day db.index_data.insert(index) status = 1 db.tasks.update({"date":day}, {"date":day, "status":status}, upsert=True) # 刷新股票列表,刷新股票交易数据 if status < 2: print("task 2") df = ts.get_stock_basics() df.reset_index(level=0, inplace=True) df = df[['code','name','industry','area','pe','outstanding','totals','totalAssets','liquidAssets','fixedAssets','reserved','reservedPerShare','bvps','pb','timeToMarket']] stocks = json.loads(df.to_json(orient='records')) print("getting stock list") for stock in stocks: # print(stock) db.stocks.update_one({"code":stock["code"]}, {"$set":stock}, upsert=True) for stock in stocks: one_data = db.his_data.find_one({"code":stock["code"], "date":day}) if one_data != None: continue try: df = ts.get_hist_data(stock["code"], start=day, end=day) h_data = json.loads(df.to_json(orient='records')) if len(h_data) > 0: print("get %s his data " % stock["code"]) h_data = h_data[0] h_data["date"] = day h_data["code"] = stock["code"] db.his_data.update_one({"code":stock["code"], "date":day}, {"$set":h_data}, upsert=True) except Exception as e: print(e) status = 2 db.tasks.update({"date":day}, {"date":day, "status":status}, upsert=True) if status < 3 : up_top = db.his_data.count({"date":day, "p_change":{"$gt":9.8}}) down_top = db.his_data.count({"date":day, "p_change":{"$lt":-9.8}}) up_num = db.his_data.count({"date":day, "p_change":{"$gte":0}}) down_num = db.his_data.count({"date":day, "p_change":{"$lt":0}}) db.day_stat.update_one({"date":day}, {"$set":{"date":day, "up_top":up_top, "down_top":down_top, "up_num":up_num, "down_num":down_num}}, upsert=True) status = 3 db.tasks.update({"date":day}, {"date":day, "status":status}, upsert=True) if status < 4 : df = ts.sh_margins(start=day, end=day) sh_margin = json.loads(df.to_json(orient='records')) # print(sh_margin) sh_margin = sh_margin[0] db.day_stat.update_one({"date":day}, {"$set":{"sh_rzye":sh_margin["rzye"]}}, upsert=True) status = 4 db.tasks.update({"date":day}, {"date":day, "status":status}, upsert=True) print(status)
def dealMarginData(self, startDate, endDate): dbtable_sh = self.magin_sh dbtable_sz = self.magin_sz engine = self.engine #生成目录字典 allTimedf = self.getAllTrdingDate(startDate, endDate) if len(allTimedf) > 0: allTimelist = list(allTimedf) #获取每天融资融券数据 for atl in allTimelist: #上海融资融券数据 marginSh_df = pd.DataFrame() #深圳融资融券数据 marginSz_df = pd.DataFrame() #深圳融资融券数据 marginTmp_df = pd.DataFrame() #获取沪市融资融券汇总数据 allsh_df = ts.sh_margins(start=atl, end=atl) marginTmp_df['mt_rzye'] = allsh_df['rzye'] marginTmp_df['mt_rzmre'] = allsh_df['rzmre'] marginTmp_df['mt_rzche'] = 0 marginTmp_df['mt_rqyl'] = allsh_df['rqyl'] marginTmp_df['mt_rqylje'] = allsh_df['rqylje'] marginTmp_df['mt_rqmcl'] = allsh_df['rqmcl'] marginTmp_df['mt_rqchl'] = 0 marginTmp_df['mt_rzrqjyzl'] = allsh_df['rzrqjyzl'] marginTmp_df['mt_code'] = '000002' marginTmp_df['mt_name'] = u'上海A股' marginTmp_df['mt_date'] = atl marginSh_df = marginSh_df.append(marginTmp_df) #获取沪市融资融券个股数据 allsh_df = ts.sh_margin_details(start=atl, end=atl) marginTmp_df = pd.DataFrame() marginTmp_df['mt_code'] = allsh_df['stockCode'] marginTmp_df['mt_name'] = allsh_df['securityAbbr'] marginTmp_df['mt_date'] = allsh_df['opDate'] marginTmp_df['mt_rzye'] = allsh_df['rzye'] marginTmp_df['mt_rzmre'] = allsh_df['rzmre'] marginTmp_df['mt_rzche'] = allsh_df['rzche'] marginTmp_df['mt_rqyl'] = allsh_df['rqyl'] marginTmp_df['mt_rqylje'] = 0 marginTmp_df['mt_rqmcl'] = allsh_df['rqmcl'] marginTmp_df['mt_rqchl'] = allsh_df['rqchl'] marginTmp_df['mt_rzrqjyzl'] = 0 marginSh_df = marginSh_df.append(marginTmp_df) marginSh_series = marginSh_df['mt_date'].astype('str') marginSh_index = marginSh_series.values marginSh_df = marginSh_df.set_index(marginSh_index) #获取深市融资融券汇总数据 allsz_df = ts.sz_margins(start=atl, end=atl) marginTmp_df = pd.DataFrame() marginTmp_df['mt_rzmre'] = allsz_df['rzmre'] marginTmp_df['mt_rzye'] = allsz_df['rzye'] marginTmp_df['mt_rqmcl'] = allsz_df['rqmcl'] marginTmp_df['mt_rqyl'] = allsz_df['rqyl'] marginTmp_df['mt_rqye'] = allsz_df['rqye'] marginTmp_df['mt_rzrqye'] = allsz_df['rzrqye'] marginTmp_df['mt_code'] = '399107' marginTmp_df['mt_name'] = u'深圳A股' marginTmp_df['mt_date'] = atl marginSz_df = marginSz_df.append(marginTmp_df) #获取深市融资融券个股数据 allsz_df = ts.sz_margin_details(atl) #清空tmp表 marginTmp_df = pd.DataFrame() marginTmp_df['mt_code'] = allsz_df['stockCode'] marginTmp_df['mt_name'] = allsz_df['securityAbbr'] marginTmp_df['mt_date'] = allsz_df['opDate'] marginTmp_df['mt_rzmre'] = allsz_df['rzmre'] marginTmp_df['mt_rzye'] = allsz_df['rzye'] marginTmp_df['mt_rqmcl'] = allsz_df['rqmcl'] marginTmp_df['mt_rqyl'] = allsz_df['rqyl'] marginTmp_df['mt_rqye'] = allsz_df['rqye'] marginTmp_df['mt_rzrqye'] = allsz_df['rzrqye'] marginSz_df = marginSz_df.append(marginTmp_df) marginSz_series = marginSz_df['mt_date'].astype('str') marginSz_index = marginSz_series.values marginSz_df = marginSz_df.set_index(marginSz_index) marginSh_df.to_sql(dbtable_sh, con=engine, if_exists='append') marginSz_df.to_sql(dbtable_sz, con=engine, if_exists='append') print atl m = 1
import tushare as ts # 获取分配预案数据 df = ts.profit_data(top=60) # 业绩预告 # 按年度、季度获取业绩预告数据,接口提供从1998年以后每年的业绩预告数据,需指定年度、季度两个参数。 fd = ts.forecast_data(2014, 2) print(fd) # 限售股解禁 # 以月的形式返回限售股解禁情况,通过了解解禁股本的大小,判断股票上行的压力。可通过设定年份和月份参数获取不同时段的数据。 xsg = ts.xsg_data() # 基金持股 # 获取每个季度基金持有上市公司股票的数据。 fh = ts.fund_holdings(2014, 4) print(fh) # 新股数据 ns = ts.new_stocks() print(ns) # 融资融券(沪市) # 沪市的融资融券数据从上海证券交易所网站直接获取,提供了有记录以来的全部汇总和明细数据。根据上交所网站提示:数据根据券商申报的数据汇总,由券商保证数据的真实、完整、准确。 sm = ts.sh_margins(start='2015-01-01', end='2015-04-19') print(sm) # 融资融券(深市) ts.sz_margins(start='2015-01-01', end='2015-04-19')
import sys sys.path.append('/home/renlei/work/stock/tushare' ) import tushare as ts # This program accept 3 parameters: # python3 index_sh.py <start_date> <end_date> <data_file> start = sys.argv[1] end = sys.argv[2] data_file = sys.argv[3] results = ts.sh_margins(start=start, end=end ) results.to_json(data_file, orient='records' )
import pymysql import re if __name__ == '__main__': cons = ts.get_apis() # 建立数据库连接,剔除已入库的部分 db = pymysql.connect(host='127.0.0.1', user='******', passwd='admin', db='stock', charset='utf8') cursor = db.cursor() try: temp_day = ts.sh_margins(start='2016-01-01', end='2018-02-15') for i in range(temp_day.shape[0]): resu0 = list(temp_day.ix[i]) print(resu0) try: sql_insert = "insert into stock_rzrq(state_dt,ctype,rzye,rqye)values('%s','%s','%.2f','%.2f')" % ( str(resu0[0]), 'SH', float(resu0[1]), float(resu0[4])) cursor.execute(sql_insert) db.commit() except Exception as exp: print(exp) continue except Exception as ex: print(ex) try: