def fund(self): fd = ts.Fund() df = fd.FundDiv(ticker='184688', adjustedType='D', beginDate='20000101', field='secShortName,effectDate,publishDate,dividendAfTax,dividendBfTax') print(df) dd = ts.fund_holdings(2015, 4) print(dd[['name', 'nums', 'clast', 'amount']])
def _getFundStocksData(self,year,quater): try: res = ts.fund_holdings(year,quater) except IOError as e: print str(year)+' '+str(quater)+'季度没有数据。' return None print str(year)+' '+str(quater)+'季度数据读取成功!' return res
def year_fund_holdings(year): for i in range(4): try: preDTS = ts.fund_holdings(year, i + 1) preDTS.to_csv('D:\\ts\\tzck\\fund_holdings_%s_%s.csv'%(year,i+1),encoding='gbk') except : next
def updateholds(): holdsdatalist=ts.fund_holdings(2014,2,10,1) holdsdata=pd.DataFrame(holdsdatalist) conn= ms.connect(host='localhost',port = 3306,user='******', passwd='123456',db ='investment',charset="utf8") cur = conn.cursor() values=[] for index,row in holdsdata.iterrows(): values.append((row['code'],row['name'],row['date'],row['nums'],row['nlast'],row['count'],row['clast'],row['amount'],row['ratio'])) cur.executemany('insert into holds20142 (code,name,date,nums,nlast,count,clast,amount,ratio) values(%s,%s,%s,%s,%s,%s,%s,%s,%s)',values) conn.commit() cur.close() conn.close()
def j_fund(_year, _quarter, _db): begin = get_micro_second() log_debug('year: %d, quarter: %d', _year, _quarter) try: df = ts.fund_holdings(_year, _quarter) except Exception: log_error("warn:error: %s/%s fund() exception!", _year, _quarter) return -4 # calc cost time log_info("fund [%s, %s] costs %d us", _year, _quarter, get_micro_second()-begin) if df is None : log_error("warn: df is None, next") return -1 if df.empty: log_error("warn: df is empty, next") return -2 if len(df) <= 1: log_error("warn: df is empty, next") return -3 # pd.options.display.max_rows = 1000 log_debug(df) begin = get_micro_second() j_fund_to_db(df, _db) log_info("to_db costs %d us", get_micro_second() - begin) return
def stockholder(): data = ts.fund_holdings(2019, 4) data.to_sql('stockholder', con=con, if_exists='append', index=None)
HS300S.to_csv('./ASHR/DATA/HS300S.csv', index = False) # SZ 50 SZ50S = ts.get_sz50s() SZ50S.to_csv('./ASHR/DATA/SZ50S.csv', index = False) # ZZ 500 ZZ500S = ts.get_zz500s() ZZ500S.to_csv('./ASHR/DATA/ZZ500S.csv', index = False) ################# # Fund Holdings # ################# # TODO Data is available quarterly FundHolding = ts.fund_holdings(CURRENT.year, np.floor((CURRENT.month+2)/3)) #################### # Financial Report # #################### # FinancialReport: EPS, EPS_YOY, ROE, net_profits, profits_yoy # ProfitData: ROE, net_profit_ratio, gross_profit_rate, EPS, bips (business income per share) # GrowthData: mbrg (main business rate growth), nprg (net profit), # nav, targ (total asset), epsg, seg (shareholder's eqty) # DebtPayingData: currentratio, quickratio, cashratio, icratio (interest coverage) # TODO Data is available quarterly # TODO Compare data for FinancialReport and ProfitData
import tushare as ts import requests import os df = ts.fund_holdings(2017, 3) df.clast = df.clast.astype("float") df2 = df.set_index("code") def chajing(s): return s[:2].upper() + "#" + s[2:] def huifushuzi(w): return w[3:9] def huifu(w): return w[0:2].lower() + w[3:9] def zhao(gpdm): x = ts.get_k_data(gpdm, autype="hfq") if type(x) == type(None): return if len(x) < 150: return if x.close[len(x) - 100] * 1.2 > x.close[len(x) - 1]: return gpdm
def invoke_fund_holdings(year, quarter): invoke_fund_holdings = ts.fund_holdings(year=year, quarter=quarter) return invoke_fund_holdings
def getInvestData(): filepath = 'f:\\stockdata\\投资参考数据\\' index = 0 # 分配预案 # 参数说明: # year: 预案公布的年份,默认为2014 # top: 取最新n条数据,默认取最近公布的25条 # retry_count:当网络异常后重试次数,默认为3 # pause: 重试时停顿秒数,默认为0 # 返回值说明: # code: 股票代码 # name: 股票名称 # year: 分配年份 # report_date: 公布日期 # divi: 分红金额(每10股) # shares: 转增和送股数(每10股) df = ts.profit_data(top=60) # df.sort('shares', ascending=False) saveCsv(df, filepath, investList, index) index += 1 # 业绩预告 # 参数说明: # year: int 年度 e.g: 2014 # quarter: int 季度: 1、2、3、4,只能输入这4个季度 # 返回值说明: # code, 代码 # name, 名称 # type, 业绩变动类型【预增、预亏等】 # report_date, 发布日期 # pre_eps, 上年同期每股收益 # range, 业绩变动范围 # 获取2014年中报的业绩预告数据 df = ts.forecast_data(2014, 2) saveCsv(df, filepath, investList, index) index += 1 # 限售股解禁 # 参数说明: # year: 年份, 默认为当前年 # month: 解禁月份,默认为当前月 # retry_count:当网络异常后重试次数,默认为3 # pause: 重试时停顿秒数,默认为0 # 返回值说明: # code:股票代码 # name:股票名称 # date: 解禁日期 # count: 解禁数量(万股) # ratio: 占总盘比率 df = ts.xsg_data() saveCsv(df, filepath, investList, index) index += 1 # 基金持股 # 参数说明: # year: 年份, 默认为当前年 # quarter: 季度(只能输入1,2,3,4 # 这个四个数字) # retry_count:当网络异常后重试次数,默认为3 # pause: 重试时停顿秒数,默认为0 # 返回值说明: # code:股票代码 # name:股票名称 # date: 报告日期 # nums: 基金家数 # nlast: 与上期相比(增加或减少了) # count: 基金持股数(万股) # clast: 与上期相比 # amount: 基金持股市值 # ratio: 占流通盘比率 df = ts.fund_holdings(2014, 4) saveCsv(df, filepath, investList, index) index += 1 # 新股上市 # 参数说明: # retry_count:当网络异常后重试次数,默认为3 # pause: 重试时停顿秒数,默认为0 # 返回值说明: # code:股票代码 # name:股票名称 # ipo_date: 上网发行日期 # issue_date: 上市日期 # amount: 发行数量(万股) # markets: 上网发行数量(万股) # price: 发行价格(元) # pe: 发行市盈率 # limit: 个人申购上限(万股) # funds:募集资金(亿元) # ballot: 网上中签率( %) df = ts.new_stocks() saveCsv(df, filepath, investList, index) index += 1 # 沪市融资融券汇总数据 # 参数说明: # start: 开始日期 # format:YYYY - MM - DD # 为空时取去年今日 # end: 结束日期 # format:YYYY - MM - DD # 为空时取当前日期 # retry_count:当网络异常后重试次数,默认为3 # pause: 重试时停顿秒数,默认为0 # 返回值说明: # opDate: 信用交易日期 # rzye: 本日融资余额(元) # rzmre: 本日融资买入额(元) # rqyl: 本日融券余量 # rqylje: 本日融券余量金额(元) # rqmcl: 本日融券卖出量 # rzrqjyzl: 本日融资融券余额(元) # df = ts.sh_margins(start='2015-01-01', end='2015-04-19') # saveCsv(df, filepath, investList, index) index += 1 # 沪市融资融券明细数据 # 参数说明: # 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: 信用交易日期 # stockCode: 标的证券代码 # securityAbbr: 标的证券简称 # rzye: 本日融资余额(元) # rzmre: 本日融资买入额(元) # rzche: 本日融资偿还额(元) # rqyl: 本日融券余量 # rqmcl: 本日融券卖出量 # rqchl: 本日融券偿还量 # df = ts.sh_margin_details(start='2015-01-01', end='2015-04-19', symbol='601989') # #如果不设symbol参数或者开始和结束日期时段设置过长,数据获取可能会比较慢,建议分段分步获取,比如一年为一个周期 # saveCsv(df, filepath, investList, index) index += 1 # 深市融资融券汇总数据 # 参数说明: # start: 开始日期 # format:YYYY - MM - DD # 为空时取去年今日 # end: 结束日期 # format:YYYY - MM - DD # 为空时取当前日期 # retry_count:当网络异常后重试次数,默认为3 # pause: 重试时停顿秒数,默认为0 # 返回值说明: # opDate: 信用交易日期(index) # rzmre: 融资买入额(元) # rzye: 融资余额(元) # rqmcl: 融券卖出量 # rqyl: 融券余量 # rqye: 融券余量(元) # rzrqye: 融资融券余额(元) # df = ts.sz_margins(start='2015-01-01', end='2015-04-19') # saveCsv(df, filepath, investList, index) index += 1 # 沪市融资融券明细数据 # 参数说明: # 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:信用交易日期 # stockCode:标的证券代码 # securityAbbr:标的证券简称 # rzye:本日融资余额(元) # rzmre: 本日融资买入额(元) # rzche:本日融资偿还额(元) # rqyl: 本日融券余量 # rqmcl: 本日融券卖出量 # rqchl: 本日融券偿还量 # df = ts.sh_margin_details(start='2015-01-01', end='2015-04-19', symbol='601989') # #如果不设symbol参数或者开始和结束日期时段设置过长,数据获取可能会比较慢,建议分段分步获取,比如一年为一个周期 # saveCsv(df, filepath, investList, index) index += 1
import datetime import tushare as ts import numpy as np import pymysql import calendar if __name__ == '__main__': # 建立数据库连接,剔除已入库的部分 db = pymysql.connect(host='127.0.0.1', user='******', passwd='admin', db='stock', charset='utf8') cursor = db.cursor() year = 2017 for i in range(4): temp_day = ts.fund_holdings(year,i+1) for j in range(temp_day.shape[0]): resu0 = list(temp_day.ix[j]) print(resu0) try: sql_insert = "insert into stock_fundholdings(state_dt,report_dt,stock_code,nums,nlast,count,clast,amount,ratio)values('%s','%s','%s','%i','%i','%.2f','%.2f','%.2f','%.2f')"%(str(resu0[2]),str(resu0[2]),str(resu0[0]),int(resu0[3]),int(resu0[4]),float(resu0[5]),float(resu0[6]),float(resu0[7]),float(resu0[8])) cursor.execute(sql_insert) db.commit() except Exception as exp: print(exp) continue
#%% import pandas as pd import tushare as ts # raw_data1 = pd.read_csv("Data\\Investment-Portfolio-Analysis-Data.csv", index_col='Date', parse_dates=True) # print(pd.DataFrame(raw_data1)) fundHolding_df = pd.DataFrame(ts.fund_holdings(2019, 2)) fundHolding_df = fundHolding_df.sort_values(by='count', ascending=0) # print(fundHolding_df) # film_preDay_df = pd.DataFrame(ts.day_boxoffice()) #%%
import tushare as ts re = ts.fund_holdings(2018, 3) # print(type(re)) # re.to_excal('./tushare_out/fund_holdings.xlsx') # print(re) df = ts.get_hist_data('000875') #直接保存 print(type(df)) df.to_excel('./000875.xlsx') #设定数据位置(从第3行,第6列开始插入数据) # df.to_excel('c:/day/000875.xlsx', startrow=2,startcol=5)
recent_k5_data = ts.get_k_data(target_stock_code, ktype='5') '''最近5min线''' #==============================================================================指数成分============================================================================== '''获取指数成分''' df_hs300 = ts.get_hs300s() #获取沪深300当前成份股及所占权重 df_sz50 = ts.get_sz50s() #获取上证50成份股 df_zz500 = ts.get_zz500s() #获取中证500成份股 #==============================================================================投资参考============================================================================== df_dividend = ts.profit_data(top=60) df_dividend.sort('shares',ascending=False) '''分配预案''' df_forecast = ts.forecast_data(2017,4) '''业绩预告''' df_restrict = ts.xsg_data() '''限售股解禁''' df_fund = ts.fund_holdings(2017, 2) '''基金持股''' df_sh = ts.sh_margin_details(start='2015-01-01', end='2015-04-19', symbol='601989') '''上交所融资融券明细''' df_sz = ts.sz_margins(start='2015-01-01', end='2015-04-19') df_sz_detail = ts.sz_margin_details('2015-04-20') '''深交所融资融券总额''' #==============================================================================新闻============================================================================== ts.get_latest_news() #默认获取最近80条新闻数据,只提供新闻类型、链接和标题 ts.get_latest_news(top=5,show_content=True) #显示最新5条新闻,并打印出新闻内容 '''即时新闻''' ts.get_notices('600028') '''个股新闻''' ts.guba_sina(True) '''新浪股吧''' #==============================================================================海外数据源==============================================================================
df = ts.get_operation_data(year, season) filename = 'management' print('正在下载营运报表......\n') df.to_excel(out_path + filename + filetype) print('\n') df = ts.get_growth_data(year, season) filename = 'growth' print('正在下载成长性报表......\n') df.to_excel(out_path + filename + filetype) print('\n') df = ts.get_debtpaying_data(year, season) filename = 'debt' print('正在下载资产负债表......\n') df.to_excel(out_path + filename + filetype) print('\n') df = ts.get_cashflow_data(year, season) filename = 'cashflow' print('正在下载现金流量表......\n') df.to_excel(out_path + filename + filetype) print('\n') df = ts.fund_holdings(year, season) filename = '基金持股数据' print('正在下载基金持股数据......\n') df.to_excel(out_path + filename + filetype) print('\n')
import pymongo import json # 600547:山东黄金 # 600362:江西铜业 # 600312:平高电气 # 600499:科达洁能 # 603993:洛阳钼业 db = "InvestInfos" coll = "FundHolds" stock = "600547" year = 2017 quart = 1 conn = pymongo.MongoClient('127.0.0.1', port=27017) df = ts.fund_holdings(year, quart) # index data columns(X columns) dicIndex = json.loads(df.to_json(orient='split')) for i, ind in enumerate(dicIndex['index']): jsonstr = { '_id': dicIndex['data'][i][0] + "-" + dicIndex['data'][i][2], 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], dicIndex['columns'][6]: dicIndex['data'][i][6], dicIndex['columns'][7]: dicIndex['data'][i][7], dicIndex['columns'][8]: dicIndex['data'][i][8] }
# -*- coding:utf-8 -*- ''' Created on 2016年5月24日 @author: 04yyl ''' import tushare as ts if __name__ == '__main__': print ts.fund_holdings(2016, 1) # ts.set_token('14a9186596c5cd0a6e74b994434d620c5ddaab9611d686f6e201768e1aa957cb') # # 获取secid # mt = ts.Master() # secid = mt.SecID(assetClass="E",cnSpell="",ticker="300027") # # print secid # fd = ts.Subject() # df = fd.NewsByCompany(field='newsPublishTime,newsTitle,relatedScore,sentiment,newsPublishSite,newsInsertTime',partyID=35390,beginDate='20160524') # # df = df.sort('insertTime', ascending=False) # print df
def downloading_information(time=3, SAVE=True): """ 获取全STOCK 数据 """ year = 2018 quarter = 1 df = pd.read_csv("../list/stock_code_all.csv", encoding='gbk') rt_df = df df["code"] = df['code'].map(lambda x: str(x).zfill(6)) ST_basics = ts.get_stock_basics() ST_basics = ST_basics.drop(["name"], axis=1) ST_basics = ST_basics.reset_index() df = pd.merge(df, ST_basics, how="left", on="code") print("\n\n process downloading the pre-announcement datas for %s -Q%s" % (year, quarter)) pre_announcement = ts.get_report_data(year, quarter + 1) pre_announcement.columns = information_dict["predict"] pre_announcement = pre_announcement.drop(["name"], axis=1) df = pd.merge(df, pre_announcement, how="left", on="code") print("\n\n process downloading the profit datas for %s -Q%s" % (year, quarter)) profit_df = ts.get_profit_data(year, quarter) profit_df = profit_df.drop(["name"], axis=1) df = pd.merge(df, profit_df, how="left", on="code") print("\n\n process downloading the operation datas for %s -Q%s" % (year, quarter)) op_df = ts.get_operation_data(year, quarter) op_df = op_df.drop(["name"], axis=1) df = pd.merge(df, op_df, how="left", on="code") print("\n\n process downloading the growth datas for %s -Q%s" % (year, quarter)) growth_df = ts.get_growth_data(year, quarter) growth_df = growth_df.drop(["name"], axis=1) df = pd.merge(df, growth_df, how="left", on="code") print("\n\n process downloading the debt datas for %s -Q%s" % (year, quarter)) debt_df = ts.get_debtpaying_data(year, quarter) debt_df = debt_df.drop(["name"], axis=1) df = pd.merge(df, debt_df, how="left", on="code") print("\n\n process downloading the cashflow datas for %s -Q%s" % (year, quarter)) cashflow_df = ts.get_cashflow_data(year, quarter) cashflow_df = cashflow_df.drop(["name"], axis=1) df = pd.merge(df, cashflow_df, how="left", on="code") print("\n\n process downloading the funds holding datas for %s -Q%s" % (year, quarter)) fund_df = ts.fund_holdings(year, quarter) fund_df = fund_df.drop(["name"], axis=1) fund_df = fund_df.drop(["date"], axis=1) fund_df.rename(columns={ "num": "fund_nums", "nlast": "fund_nlast", "count": "fund_shares", "clast": "fund_clast", "amount": "fund_amount", "ratio": "fund_ratio", }, inplace=True) df = pd.merge(df, fund_df, how="left", on="code") df = df.drop_duplicates("code", keep='first', inplace=False) df = df.set_index("code") #profit_df=profit_df.set_index("code") #df=pd.concat([df,profit_df],axis=1,join_axes=[df.index]) #df.columns=df.columns +information_dict["predict"].keys[2:] #print (pre_announcement) ''' for year in range(2015,2017): print(year) for Q in range(1,5): print(Q) ST_basics=download_ACH_Q(year,Q,ST_basics) ''' if SAVE == True: import os path = os.path.dirname(os.getcwd()) + '\\report\\' # 存储路径 df.to_csv(path + 'Stock_Information.csv', encoding='gbk', header=True) return df, rt_df
import pandas as pd import sys import time from mongoModel import * # Exchange(name='沪深股市').save() for year in range(2004, 2018): for quarter in range(1, 5): finance = ts.get_report_data(year, quarter) profit = ts.get_profit_data(year, quarter) operation = ts.get_operation_data(year, quarter) # time.sleep(9) debtpay = ts.get_debtpaying_data(year, quarter) cashFlow = ts.get_cashflow_data(year, quarter) fundHold = ts.fund_holdings(year, quarter) forecast = ts.forecast_data(year, quarter) growth = ts.get_growth_data(year, quarter) result = pd.concat([ finance, profit, operation, growth, debtpay, cashFlow, fundHold, forecast ], axis=1) for i in range(len(result)): it = result.iloc[i].to_dict() # print(it) # print(result.loc[i]['code'].all(),'kksksksk ',it['code']) if (type(it['code']) is type(0.9)) or (not it['code']): # print('ssssssssssssssssssssssssssssssssss') continue args = []