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 get_financing_securities_sz(start_date=None, end_date=None, retry_count=RETRY_COUNT, pause=PAUSE): logger.info('Begin get financing securities sz data, start_date is %s,' ' end_date is %s.' % (start_date, end_date)) data_dicts = [] try: data_df = ts.sz_margins(start=start_date, end=end_date, retry_count=retry_count, pause=pause) except Exception: logger.exception( 'Error get financing securities sz 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 sz data, start_date is %s,' ' end_date is %s.' % (start_date, end_date)) else: data_dicts = [{ 'rzmre': row[0], 'rzye': row[1], 'rqmcl': row[2], 'rqyl': row[3], 'rqye': row[4], 'rzrqye': row[5], 'op_date': row[6] } for row in data_df.values] logger.info( 'Success get financing securities sz data, start_date is %s,' ' end_date is %s.' % (start_date, end_date)) return data_dicts
def getSzMargins(cursor): # try: for i in range(2010, 2017 + 1): df = ts.sz_margins(start=str(i) + '-01-01', end=str(i) + '-12-31', pause=0.01) # 处理缺失值 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_sz_margins(uuid, op_date, rzmre, rzye, " "rqmcl, rqyl, rqye, rzrqye ) " "values(:uuid, to_date(:op_date, 'yyyy-MM-dd'), :rzmre, :rzye, " ":rqmcl, :rqyl, :rqye, :rzrqye)", (str(list(df2['uuid'])[0]), str(list(df2['opDate'])[0]), round(float(df2['rzmre']), 4), round(float(df2['rzye']), 4), round(float(df2['rqmcl']), 4), round(float(df2['rqyl']), 4), round(float(df2['rqye']), 4), round(float(df2['rzrqye']), 4))) cursor.execute("commit")
def setSzMargins(self,endTime =None,number =1, isSave = False,tableName = REFERENCE_SH_MARGINS): [startTime,endTime] = getStartTime(endTime = endTime, number=number) try: df = ts.sz_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_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.sz_margins(**args)
def get_sz_margins(f, reset_flag=False): """ 获取深市的融资融券 """ df = ts.sz_margins(pause=conf.REQUEST_BLANK) df = df.sort_values(by=["opDate"]) if reset_flag is False: tool.merge_df_dataset(f, conf.HDF5_FUNDAMENTAL_SZ_MARGINS, df) else: tool.create_df_dataset(f, conf.HDF5_FUNDAMENTAL_SZ_MARGINS, df) return
def dump_rzrq(): today = datetime.date.today() delta = datetime.timedelta(days=360) ndays_before = today - delta print("%s, %s", today, ndays_before) df = ts.sz_margins(start=ndays_before.strftime('%Y-%m-%d'), end=today.strftime('%Y-%m-%d')) df.to_sql('rzrq_total', engine, if_exists='append', index=False, index_label='opDate')
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 margin_sz_smry(engine, sdate, edate): tbl = "invest_margin_sz_smry" tsl.log(tbl + " start...") try: df = ts.sz_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 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 _sz_margin(): end = today() start = end - timedelta(days=300) sz = pd.DataFrame() while True: print('\n') print(start, end) cur = ts.sz_margins(start, end) # print(cur) if cur is None or cur.empty: break sz = pd.concat([cur, sz]) end = start - timedelta(days=1) start = end - timedelta(days=300) sz.rename(columns={'opDate': 'date'}, inplace=True) return sz
def sz_margins(request): try: start = request.POST.get('start', '') #选填 end = request.POST.get('end', '') #选填 data = ts.sz_margins(start, end) res = { 'columns': [ '信用交易日期', '本日融资余额(元)', '本日融资买入额(元)', '本日融券余量', '本日融券余量金额(元)', '本日融券卖出量', '本日融资融券余额(元)' ], 'data': json.loads(json.dumps(data.values[:, ::-1], cls=DateEncoder)) } except (BaseException): return HttpResponse(BaseException) else: return HttpResponse(json.dumps(res), content_type="application/json")
def get_sz_margins(start, end): """ 投资参考 系列 融资融券(深市):深市融资融券汇总数据 """ df = ts.sz_margins(start, end) print(df) if df is not None: if start is None: start = get_week_today() if end is None: start = datetime.date.today() if len(df) > 0: df.insert(0, 'start', start) df.insert(1, 'end', end) res = df.to_sql(invest_stock_ref_sz_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 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)
def get_rzrq_sz(date_start, date_end): """ 获取深市融资融券汇总数据 Parameters -------- date_start 开始日期,e.g. '2010-03-31' date_end 截止日期,e.g. '2017-03-28' Return -------- DataFrame date 日期 rzye_sz 融资余额深圳(元) rzmre_sz 融资买入额深圳(元) rqyl_sz 融券余量深圳 rqylje_sz 融券余量金额深圳(元) rqmcl_sz 融券卖出量深圳 rzrqye_sz 融资融券余额深圳(元) """ rzrq = ts.sz_margins(start=date_start, end=date_end) # Rename Columns columns_map = { 'opDate': 'date', 'rzmre': 'rzmre_sz', 'rzye': 'rzye_sz', 'rqmcl': 'rqmcl_sz', 'rqyl': 'rqyl_sz', 'rqye': 'rqylje_sz', 'rzrqye': 'rzrqye_sz' } rzrq.rename(columns=columns_map, inplace=True) print(rzrq.head(10)) # Swith Columns rzye_sz = rzrq.pop('rzye_sz') rzrq.insert(1, 'rzye_sz', rzye_sz) rqmcl_sz = rzrq.pop('rqmcl_sz') rzrq.insert(5, 'rqmcl_sz', rqmcl_sz) return rzrq
def updateMarginsSZ(con, start: datetime = None, end: datetime = None): import share.model.dao.reference.MarginSZ as Model dateFormat = "%Y-%m-%d" if start is None: start = datetime.now() if end is None: end = datetime.now() logging.debug("Updating MarginsSZ from {start} to {end}".format( start=start.strftime(dateFormat), end=end.strftime(dateFormat))) df = ts.sz_margins(start=start.strftime(dateFormat), end=end.strftime(dateFormat), retry_count=32) res = [] for _, row in df.iterrows(): obj = Model.rowToORM(row) if obj is not None: res.append(obj) Base.metadata.create_all(con.engine) con.save_all(res) return
def getSzMargins(self): file_name = self.startDate+'_'+self.endDate+'_sz_margins.csv' path = self.index + self.index_sz_margins + file_name data = ts.sz_margins(start = self.startDate, end = self.endDate) data.to_csv(path, encoding='utf-8') print(file_name)
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
def sz_margins(self, conns, start, end): self.base = Base() self.financial_data = conns['financial_data'] self.df = ts.sz_margins(start, end) self.base.batchwri(self.df, 'rongzirongquan_sz', self.financial_data)
#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'
#!/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.sz_margins(start=begin_date, end=end_date) df.to_csv('../../data/sz_rzrq_all.txt')
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 short_win = 12 long_win = 26 macd_win = 9
''' #业绩预告(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']: df = ts.get_hist_data(code) if os.path.exists(filename): df.to_csv(filename, mode='a',
#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')
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') # # 深市明细数据 # open('rzrq.csv')
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')
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: temp_day2 = ts.sz_margins(start='2016-01-01', end='2016-12-31') temp_day2mid = np.array(temp_day2) for j in range(len(temp_day2mid)): resu2 = temp_day2mid[j] print(resu2) try: sql_insert2 = "insert into stock_rzrq(state_dt,ctype,rzye,rqye)values('%s','%s','%.2f','%.2f')" % ( str(resu2[0]), 'SZ', float(resu2[2]), float(resu2[5])) cursor.execute(sql_insert2) db.commit() except Exception as exp: print(exp) continue except Exception as ex: print(ex)