def get_yjyg(): df = ts.forecast_data(2018, 2) df.to_sql('yjyg', engine, if_exists='replace', index=False, index_label='code')
def getForecast(cursor): for i in range(1992, 2017+1): for j in range(1, 4+1): try: print(i, j) df = ts.forecast_data(i, j) # 处理缺失值 df = df.stack().replace('--', '0').unstack() # print(df) dfLen = len(df) # print(dfLen) uuidList = [] # 添加uuid yearList = [] # 添加年份 quarterList = [] # 添加季度 for l in range(0, dfLen): uuidList.append(uuid.uuid1()) yearList.append(str(i)) quarterList.append(str(j)) df['uuid'] = uuidList df['year'] = yearList df['quarter'] = quarterList for k in range(0, dfLen): df2 = df[k:k+1] cursor.execute("insert into stock_forecast(uuid, code, name, type, report_date, pre_eps, range, year, quarter) " "values(:uuid, :code, :name, :type, to_date(:report_date, 'yyyy-MM-dd'), :pre_eps, :range, :year, :quarter)", (str(list(df2['uuid'])[0]), str(list(df2['code'])[0]), str(list(df2['name'])[0]), str(list(df2['type'])[0]), str(list(df2['report_date'])[0]), round(float(df2['pre_eps']), 4), str(list(df2['range'])[0]), str(list(df2['year'])[0]), str(list(df2['quarter'])[0])) ) cursor.execute("commit") except Exception: pass
def _fetch_forecast(): for year in range(2004, 2018): for quarter in range(1, 5): print(year, ' year ', 'quarter ', quarter) fcast = ts.forecast_data(year, quarter) fcast.rename(columns={'report_date': 'date'}, inplace=True) forecast.insert(fcast.to_dict('record'))
def forecast_data(self, year, quarter): tsdata = ts.forecast_data( year=year, quarter=quarter, ) jsdata = To_Json(tsdata) return jsdata
def __init__(self): self.__yearRange = 5 #5年 从2012到2016 self.__yearBegin = datetime.now().year-self.__yearRange #self.__getYearReportOnline() self.getThingsEveryday() #print(yearbegin) self.__pdYearReport = [pd.read_excel('D:/zzz/joinquant/data4stock/' + \ str(self.__yearBegin+i) + 'y.xls','Report',na_values=['NA']) \ for i in range(self.__yearRange)] self.__pdYearProfit = [pd.read_excel('D:/zzz/joinquant/data4stock/' + \ str(self.__yearBegin+i) + 'Profit.xls', sheet_name='Profit',na_values=['NA'])\ for i in range(self.__yearRange)] self.__stocksL = set(self.__pdYearReport[3].code.values) self.__stocksNow = set(self.__pdYearReport[4].code.values) # 出了年报的股票 self.__stocksNotNow = set(self.__stocksL - self.__stocksNow) #self.__stockNotNew = self.__stockBasics[(self.__stockBasics.timeToMarket < 20160101) & # (self.__stockBasics.timeToMarket != 0)].index.values.astype(int) #self.__stockNotNew #print(self.__stockNotNew.index.values.astype(int)) #每天更新一次 pdYPL = self.__pdYearProfit[3] pdYRL = self.__pdYearReport[3] self.__pdYearReportNear = self.__pdYearReport[4].append(pdYRL[pdYRL.code.isin(self.__stocksNotNow)]) self.__pdYearProfitNear = self.__pdYearProfit[4].append(pdYPL[pdYPL.code.isin(self.__stocksNotNow)]) self.__minute_update = -1 self.__forecastLastYear = ts.forecast_data(datetime.now().year-1, 4) self.__forecastLastYear['code'] = self.__forecastLastYear['code'].astype(int) #print(self.__pdYearReportNear[self.__pdYearReportNear.code==600522]) #print(self.__pdYearReportNear[self.__pdYearReportNear.code == 600067]) #pdtest.to_excel('D:/zzz/joinquant/data4stock/' + str('test') + 'Profit.xls', sheet_name='Profit') print('\n')
def save_forecast_data(): forecast_cols = ['股票代码', '预告类型', '预告日期', '变动范围'] fd = ts.forecast_data(forcast_year, forcast_season) del fd['name'] del fd['pre_eps'] #fd.columns = forecast_cols; fd.to_csv("forecast_%s.csv" % dateStr, encoding="utf-8", index=False)
def __init__(self): self.__yearRange = 5 #5年 从2012到2016 self.__yearBegin = datetime.now().year - self.__yearRange #self.__getYearReportOnline() self.__flagUpdateReport = False flag = input('Update report now(Y/N):') if flag == 'Y' or flag == 'y': self.__flagUpdateReport = True self.getThingsEveryday() #print(yearbegin) self.__pdYearReport = [pd.read_excel('./' + \ str(self.__yearBegin+i) + 'y.xls','Report',na_values=['NA']) \ for i in range(self.__yearRange)] self.__pdYearProfit = [pd.read_excel('./' + \ str(self.__yearBegin+i) + 'Profit.xls', sheet_name='Profit',na_values=['NA'])\ for i in range(self.__yearRange)] self.__pdYearGrowth = [pd.read_excel('./' + \ str(self.__yearBegin+i) + 'Growth.xls','Growth',na_values=['NA']) \ for i in range(self.__yearRange)] self.__stocksL = set(self.__pdYearGrowth[3].code.values) self.__stocksNow = set(self.__pdYearGrowth[4].code.values) # 出了年报的股票 self.__stocksNotNow = set(self.__stocksL - self.__stocksNow) #self.__stockNotNew = self.__stockBasics[(self.__stockBasics.timeToMarket < 20160101) & # (self.__stockBasics.timeToMarket != 0)].index.values.astype(int) #self.__stockNotNew #print(self.__stockNotNew.index.values.astype(int)) #每天更新一次 pdYPL = self.__pdYearProfit[3] pdYRL = self.__pdYearReport[3] self.__pdYearReportNear = self.__pdYearReport[4].append( pdYRL[pdYRL.code.isin(self.__stocksNotNow)]) self.__pdYearProfitNear = self.__pdYearProfit[4].append( pdYPL[pdYPL.code.isin(self.__stocksNotNow)]) self.__minute_update = -1 self.__forecastLastYear = ts.forecast_data(datetime.now().year - 1, 4) self.__forecastLastYear['code'] = self.__forecastLastYear[ 'code'].astype(int) #print(self.__pdYearReportNear[self.__pdYearReportNear.code==600522]) #print(self.__pdYearReportNear[self.__pdYearReportNear.code == 600067]) #pdtest.to_excel('D:/zzz/joinquant/data4stock/' + str('test') + 'Profit.xls', sheet_name='Profit') print('\n') for i in range(self.__yearRange): self.__pdYearReport[i].rename( columns={'profits_yoy': 'yoy' + str(i)}, inplace=True) self.pdYear4Report = self.__pdYearReport[0].copy() for i in range(1, self.__yearRange - 1): self.pdYear4Report = self.pdYear4Report.merge( self.__pdYearReport[i], on='code') # 4年成长能力 for i in range(self.__yearRange): #5年 从2012到2016 0-4 self.__pdYearGrowth[i].rename(columns={'nprg': 'yoy' + str(i)}, inplace=True) self.pdYear4Growth = self.__pdYearGrowth[0].copy() for i in range(1, self.__yearRange - 1): self.pdYear4Growth = self.pdYear4Growth.merge( self.__pdYearGrowth[i], on='code')
def forecast_data(year): for i in range(4): predts = ts.forecast_data(year, i + 1) if predts is not None: predts.to_sql('reference_forecast_data', engine, flavor='mysql', if_exists='append')
def forecast_info(year, month): ''' 业绩预告 ''' try: return ts.forecast_data(year, month) except: print('error, month=[1,4], please check your parameter')
def get_stock_forecast(nd, jd): """ 获取业绩预告 """ try: res = ts.forecast_data(nd, jd) return res except: return None
def download_forecast_data(self, year=2017, quarter=1): """ """ #print('downloading the forecast_data info...') try: data_2017_1 = ts.forecast_data(2017, 1) filename = 'forecast_data_2017_1' data_2017_1.to_excel(self.working_folder + filename + '.xlsx', encoding='GBK') except: print('download forecast_data_2017_1 failed') try: data_2017_2 = ts.forecast_data(2017, 2) filename = 'forecast_data_2017_2' data_2017_2.to_excel(self.working_folder + filename + '.xlsx', encoding='GBK') except: print('download forecast_data_2017_2 failed') try: data_2017_3 = ts.forecast_data(2017, 3) filename = 'forecast_data_2017_3' data_2017_3.to_excel(self.working_folder + filename + '.xlsx', encoding='GBK') except: print('download forecast_data_2017_3 failed') try: data_2017_4 = ts.forecast_data(2017, 4) filename = 'forecast_data_2017_4' data_2017_4.to_excel(self.working_folder + filename + '.xlsx', encoding='GBK') except: print('download forecast_data_2017_4 failed') data = data_2017_4.append(data_2017_3).append(data_2017_2).append( data_2017_1) data.drop_duplicates(subset=['code'], keep='first', inplace=True) filename = 'forecast_data' data.to_excel(self.working_folder + filename + '.xlsx', encoding='GBK') return
def download_forecast(year, quarter): try: df = ts.forecast_data(year, quarter) df.set_index('code', inplace=True) utils.df2csv(df, _forecast_file_path(year, quarter)) except Exception as e: logger.error( 'download_forecast(year[{}], quarter[{}]) failed.{}'.format( year, quarter, error.exception_string(e))) raise error.ServerException(error.SERVER_ERR_DOWNLOAD_FAILED, error.exception_string(e))
def getvrdata(isinit='no'): mylogger = getmylogger() curday = datetime.date.today() print(curday) curyear = curday.year curmonth = curday.month quarter = (curmonth - 1) // 3 + 1 # prdict = {'profitdivi': '分配预案', 'forecast': '业绩预告', 'xsg': '限售股解禁', 'fundholdings': '基金持股', 'newstocks': '新股'} prdict = {'fundholdings': '基金持股'} for dv in prdict: vrtbname = dv vrinfo = prdict[vrtbname] try: if dv == "profitdivi": df = ts.profit_data(year=curyear, top=100) if isinit == 'no' and df is not None: df = df[df['report_date'] >= str(curday)] elif dv == "forecast": df = ts.forecast_data(curyear, quarter) if isinit == 'no' and df is not None: # print(df[df['report_date'].isin(['2019-12-28'])]) df = df[df['report_date'] >= str(curday)] elif dv == "xsg": df = ts.xsg_data() if isinit == 'no' and df is not None: curym = datetime.date.today().strftime("%Y-%m") df = df[df['date'].str.contains(curym)] elif dv == "fundholdings": if quarter == 1: quarter = 4 curyear -= 1 else: quarter -= 1 df = ts.fund_holdings(curyear, quarter) if isinit == 'no' and df is not None: lastyq = str(curyear) + '-' + str(quarter) df = df[df['date'].str.contains(lastyq)] if df is not None: newcolumns = ['nums', 'count', 'nlast', 'name', 'amount', 'date', 'ratio', 'code', 'clast'] df.columns = newcolumns elif dv == "newstocks": df = ts.new_stocks() if isinit == 'no' and df is not None: df = df[df['ipo_date'] >= str(curday)] else: mylogger.info("没有执行命令。") if df is not None: tosql(df, vrtbname, "append", vrinfo, mylogger) else: mylogger.info("没有%s数据。" % vrinfo) except Exception: tracelog = traceback.format_exc() mylogger.info("获取数据异常。") mylogger.info(tracelog)
def year_forecast_data(year): DTS=[] for i in range(4): preDTS=ts.forecast_data(year,i+1) DTS.append(preDTS) try: DTS = pd.concat(DTS) except ValueError: DTS = None return DTS
def __init__(self): TestArr = [["601366","利群股份","预升","2018-04-16","0.1700","30.33"], ["000018","神州长城","预增","2018-04-16","0.0583","140%~170%"], ["000626","远大控股","预亏","2018-04-16","0.1602","0"], ["000430" ,'张家界',"预亏","2018-04-14","0.0330",'-241.78%~-237.06%'], ["300107" ,'张家界2',"预亏","2018-04-19","0.0320",'-30%']] if(Test): self.forecastDF = DataFrame(TestArr, columns=["code", "name","type","report_date", "pre_eps","range"]) else: self.forecastDF = ts.forecast_data(2018,1) self.currentPrice = None
def updatepredict(): predictdatalist=ts.forecast_data(2014,1) predictdata=pd.DataFrame(predictdatalist) conn= ms.connect(host='localhost',port = 3306,user='******', passwd='123456',db ='investment',charset="utf8") cur = conn.cursor() values=[] for index,row in predictdata.iterrows(): values.append((row['code'],row['name'],row['type'],row['report_date'],row['pre_eps'],row['range'])) cur.executemany("insert into predict20141 (code,name,type,report_date,pre_eps,ran) values(%s,%s,%s,%s,%s,%s)",values) conn.commit() cur.close() conn.close()
def test_forecast(self): # code, 代码 # name, 名称 # type, 业绩变动类型【预增、预亏等】 # report_date, 发布日期 # pre_eps, 上年同期每股收益 # range, 业绩变动范围 self.setUp() # 获取2016年中报的业绩预告数据 df = ts.forecast_data(2017, 1) insert_string = df.to_json(orient='records') items = json.loads(insert_string) self.mongo.db.forecast.insert(items)
def updateForecastDate(con, year: int, quarter: int): import share.model.dao.reference.ForecastData as Model logging.debug("Updating day updateForecastDate of {}Q{}".format( year, quarter)) df = ts.forecast_data(year, quarter) res = [] for _, row in df.iterrows(): obj = Model.rowToORM(row, year=year, quarter=quarter) if obj is not None: res.append(obj) Base.metadata.create_all(con.engine) con.save_all(res) return
def setForecastData(self,year=None,quarter=None, number = 1, isSave = False,tableName = REFERENCE_FORECAST_DATA): [year, quarter] = getYearQuarter(year, quarter) for i in range(0,number): try: df = ts.forecast_data(year, quarter) df['date'] =str(year) + str(quarter) if isSave is True: df.to_sql(tableName,self.engine_sql, if_exists='append') quarter = quarter - 1 if quarter < 1: quarter = 4 year = year - 1 except IOError,e: print e
def forecast_history(engine, year, quarter): tbl = "invest_forecast_history" tsl.log(tbl + " start...") try: df = ts.forecast_data(year, quarter) df['year'] = year df['quarter'] = quarter df = df.set_index('code', 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 forecast_data(request): try: year = request.POST.get('year', '') #必填 quarter = request.POST.get('quarter', '') #必填 data = ts.forecast_data(int(year), int(quarter)) res = { 'columns': ['code', 'name', 'type', 'report_date', 'pre_eps', 'range'], '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 get_data_date(year, quarter): cmd = '''delete from %s where `year`='%s' and quarter='%s' ''' % ( table_name, year, quarter) print cmd run_mysql_cmd(cmd=cmd, conn=conn) #先删除指定年和指定季度的数据 rs = ts.forecast_data(year=int(year), quarter=int(quarter)) rs['year'] = year rs['quarter'] = quarter rs = rs.drop_duplicates() #去除重复的数据,没想到还有重复的,心塞塞,这个api不咋地啊,挖地兔 pd.DataFrame.to_sql(rs, table_name, con=conn, flavor='mysql', if_exists='append', index=False) return rs
def forecast(engine, year, quarter): tbl = "invest_forecast" tsl.log(tbl + " start...") try: df = ts.forecast_data(year, quarter) if len(df) != 0: df['year'] = year df['quarter'] = quarter df = df.reset_index() df.to_sql(tbl, engine, if_exists='replace') print tsl.log(tbl + " done") except BaseException, e: print print e tsl.log(tbl + " error")
def getForecast(cursor): for i in range(1992, 2017 + 1): for j in range(1, 4 + 1): try: print(i, j) df = ts.forecast_data(i, j) # 处理缺失值 df = df.stack().replace('--', '0').unstack() #print(df.ix[129]) #print(df) #print(df.ix[131]) ##处理'None' # df=df.replace('None',0) dfLen = len(df) # print(dfLen) uuidList = [] # 添加uuid yearList = [] # 添加年份 quarterList = [] # 添加季度 for l in range(0, dfLen): uuidList.append(uuid.uuid1()) yearList.append(str(i)) quarterList.append(str(j)) df['uuid'] = uuidList df['year'] = yearList df['quarter'] = quarterList #print("xxxxx") for k in range(0, dfLen): df2 = df[k:k + 1] sql = ( "insert into stock_forecast(uuid, code, name, type, report_date, pre_eps, `range`, year, quarter) " "value ('%s', '%s', '%s', '%s', '%s','%.4f', '%s','%s', '%s')" % (str(list(df2['uuid'])[0]), str(list(df2['code'])[0]), str(list(df2['name'])[0]), str(list(df2['type'])[0]), str(list(df2['report_date'])[0]), round(float(df2['pre_eps']), 4), str(list(df2['range'])[0]), str(list( df2['year'])[0]), str(list(df2['quarter'])[0]))) print(sql) cursor.execute(sql) cursor.execute("commit") except Exception as e: print(e) pass
def update_forcast_quarter_report_data( self ): if not os.path.exists( self.__forcast_quarter_report_file_name ): list_date_quarter = Utils.parse_date_to_ymd( self.__date ) df_forcast_quarter_report_data = pd.DataFrame() for year in range( 2003, int( list_date_quarter[0] ) + 1 ): for quarter in range( 1, 5 ): try: df_tmp = ts.forecast_data( year, quarter ) except: ERROR( 'exception occurs when update forcast quarter report data of year {0} quarter {1}'.format( year, quarter ) ) else: df_forcast_quarter_report_data = df_forcast_quarter_report_data.append( df_tmp ) LOG( 'update forcast quarter report data of year {0} quarter {1}'.format( year, quarter ) ) if year == int( list_date_quarter[0] ) and quarter == list_date_quarter[3]: break Utils.save_data( df_forcast_quarter_report_data, self.__forcast_quarter_report_file_name, 'forcast quarter report' )
def get_ref_forecast_data(year, quarter): """ 投资参考 系列 按年度、季度获取业绩预告数据,接口提供从1998年以后每年的业绩预告数据, 需指定年度、季度两个参数 """ df = ts.forecast_data(year, quarter) print(df) if df is not None: if len(df) > 0: df.insert(0, 'year', year) df.insert(1, 'quarter', quarter) res = df.to_sql(invest_stock_ref_forecast, engine, if_exists='replace') msg = 'ok' if res is None else res print('获取业绩预告数据: {0} {1}: {2}'.format(year, quarter, msg) + '\n') else: print('获取业绩预告数据: {0} {1}: {2}'.format(year, quarter, 'None') + '\n')
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 get_performance_forecast(year, quarter): """得到业绩预告 Args: year: 年份, YYYY格式数字 quarter: 季度, 只能是1, 2, 3, 4的数字 Returns: data_dicts: 字段的列表, return None if have exception, return empty if no data """ _check_input(year, quarter) logger.info( 'Begin get performance forecast data, the year is: %s, quarter is: %s' % (year, quarter)) try: data_df = ts.forecast_data(year=year, quarter=quarter) except Exception as e: logging.exception( 'Error get performance forecast data, the year is: %s, quarter is: %s' % (year, quarter)) return None else: data_dicts = [] if data_df is None or data_df.empty: logger.warn( 'Empty get performance forecast data, the year is: %s, quarter is: %s' % (year, quarter)) else: data_dicts = [{ 'code': row[0], 'name': row[1], 'year': year, 'quarter': quarter, 'type': row[2], 'report_date': row[3], 'pre_eps': row[4], 'range': row[5], 'insert_date': today_line } for row in data_df.values] logger.info( 'Success get performance forecast data, the year is: %s, quarter is: %s' % (year, quarter)) return data_dicts
def forecast(self): today = datetime.date.today() year = int(today.strftime('%Y')) month = int(today.strftime('%m')) if month > 3: quarter = month / 3 else: quarter = 1 if month == 3 else 4 data = ts.forecast_data(year, quarter) code_list = list(data.code) type_list = list(data.type) for code in self.valid_code: if code in code_list: index = code_list.index(code) if type_list[index] in [ '预降'.decode('utf-8'), '预减'.decode('utf-8'), '预亏'.decode('utf-8') ]: self.valid_code.remove(code) self.expect_loss_code.append(code)
def updatepredict(): predictdatalist = ts.forecast_data(2014, 1) predictdata = pd.DataFrame(predictdatalist) conn = ms.connect(host='localhost', port=3306, user='******', passwd='123456', db='investment', charset="utf8") cur = conn.cursor() values = [] for index, row in predictdata.iterrows(): values.append((row['code'], row['name'], row['type'], row['report_date'], row['pre_eps'], row['range'])) cur.executemany( "insert into predict20141 (code,name,type,report_date,pre_eps,ran) values(%s,%s,%s,%s,%s,%s)", values) conn.commit() cur.close() conn.close()
def fetchByYearQuarter(self, mongo, func): years = range(self.begin_year, self.end_year_notinclude) quarters = range(1, 1 + self.quarter) for year in years: for quarter in quarters: print(str(year) + " " + str(quarter)) if (func == 'forecast_data'): df = ts.forecast_data(year, quarter) elif (func == 'fund_holdings'): df = ts.fund_holdings(year, quarter) else: print(func) df = {} tmpJson = json.loads(df.to_json(orient='records')) for i in range(len(tmpJson)): tmpJson[i][u'year'] = int(year) tmpJson[i][u'quarter'] = int(quarter) coll = mongo.reference[func] coll2 = mongo.reference[str(func) + '_' + str(year)] coll2.insert(tmpJson) coll.insert(tmpJson)
def ProfitExp(): year=dt.date.today().year month=dt.date.today().month if month>9: season=4 elif month>6: season=3 elif month>3: season=2 else: season=1 if season==1: year=year-1 season=4 else: season=season-1 dp=ts.forecast_data(year,season) dp=dp.sort_values('range',ascending=False) return [u"\n近期预披露靠前的结果:",dp[:30]]
def main(): code = [] time = [] profit1 = [] profit2 = [] df = ts.forecast_data(2016, 2) for i in range(len(df)): if df['pre_eps'][i] > 0.1: code.append(df['code'][i]) time.append(df['report_date'][i]) for i in range(len(code)): start_time = datetime.datetime.strptime(time[i], '%Y-%m-%d') end_time1 = start_time + datetime.timedelta(days=30) end_time2 = start_time + datetime.timedelta(days=60) endstr1 = end_time1.strftime('%Y-%m-%d') endstr2 = end_time2.strftime('%Y-%m-%d') data = ts.get_k_data(start=time[i], end=endstr1, ktype='D', autype='qfq', code=code[i]) try: profit1.append(data['close'][data.index[-1]] / data['close'][data.index[0]] - 1) except: profit1.append(np.nan) data = ts.get_k_data(start=time[i], end=endstr2, ktype='D', autype='qfq', code=code[i]) try: profit2.append(data['close'][data.index[-1]] / data['close'][data.index[0]] - 1) except: profit2.append(np.nan) output = {'code': code, 'profit1': profit1, 'profit2': profit2} a = pd.DataFrame(output) a.to_csv('D:/pre_profit.csv')
def check_forecast(self, upgrade=False): """ 检查该季度内是否发布业绩预告,放在report里面 Parameters ---------- self.code : stock code self.year : 年份 self.quarter : 季度 Returns ------- if not search forecast , return None. code : name : type : 预升 report_date : pre_eps : range: """ _path = '../report/forecast_%s-%s.csv' % (self.year, self.quarter) if os.path.exists(_path) and (upgrade == False): all_forecast = pandas.read_csv(_path, encoding='gbk', header=0) all_forecast.code = all_forecast.code.map( lambda x: str(x).zfill(6)) #all_forecast=all_forecast.set_index(['code']) else: print('there is not files,watting download %s' % self.year) all_forecast = ts.forecast_data(int(self.year), int(self.quarter)) all_forecast.to_csv(_path, encoding='gbk') if self.code in all_forecast.code.values: stock_forecast = all_forecast[all_forecast['code'] == self.code] return stock_forecast #print (stock_forecast) else: print('< %s has not forecast >' % self.code) return None
def baseAPI(): # df=ts.get_hist_data('002524',start='2017-01-01',end='2017-04-24') # 这个函数只能获取近3年的数据 # 目前这个版本是从最新开始 【0】 # print(df) # print(df['close'].sum()) # stock_info=ts.get_stock_basics() # print(stock_info) # stock_info.to_csv('2.csv',encoding='gbk') # n_df=pd.read_csv('2.csv',encoding='gbk') # n_df.to_excel('2.xls',encoding='gbk') # print(n_df) # 这样子居然搞定了。 # dx_1=ts.get_hist_data('603111',start='2017-01-28',end='2017-04-22') # print(dx_1) # print(len(dx_1)) # ts.get_sz50s() # print(dx_1['close'][0]) ''' print(stock_info.dtypes) cols=stock_info.columns for col in cols: if stock_info[col].dtype == 'O': print("O in " ,col) del stock_info[col] print(stock_info) stock_info.to_excel('new.xls') ''' # 编码出错 # stock_info.to_excel('base.xls',encoding='gb2312') ''' data=stock_info.ix['300141']['timeToMarket'] print(data) print(type(data)) data=str(data) print(type(data)) print(data[1:4]) print(data[4:6]) print(data) date_format=data[0:4]+'-'+data[4:6]+'-'+data[6:8] print(date_format) delta=60*7/5 day0=datetime.date(datetime.date.today().year,datetime.date.today().month,datetime.date.today().day) day30=day0-datetime.timedelta(delta) print(day30) day30=day30.strftime("%Y-%m-%d") day0=day0.strftime("%Y-%m-%d") ''' # df1=ts.new_stocks() # print(df1) # df2=ts.new_stocks(2) # print(df2) # df3=ts.new_stocks(3) # print(df3) # sz_index=ts.get_k_data('399001',index=True,start='2017-01-10',end='2017-04-28') ''' sz_index=ts.get_k_data('300141') print(sz_index) print(sz_index.ix[sz_index['date']=='2014-05-06','high'].values[0]) ''' ''' df = ts.get_realtime_quotes(['600848', '000980', '000981']) #一次过返回3个数据 print(df) ''' # bar 函数 # conn = ts.get_apis() # df =ts.bar('000022',conn,start_date='2000-01-01',adj='qfq') # print(df) # print(df.dtypes) df = ts.get_today_all() print(df) exit() filename = datetime.datetime.now().strftime('%Y-%m-%d') + '.xls' df.to_excel(filename) forecast_filename = '2017-12.xls' forecast_df = ts.forecast_data(2017, 4) print(forecast_df) forecast_df.to_excel(forecast_filename) ts.stock_pledged()
#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 get_achievement(self): fc = ts.forecast_data(2016, 4) print fc
for code, stockRow in df.iterrows(): print("pre divi " + stockRow['code']) print(type(stockRow['shares'])) spamwriter.writerow([stockRow['code'], stockRow['name'], stockRow['year'], stockRow['report_date'], stockRow["divi"], stockRow["shares"]]) preDivi_csv.close() preEarning_csv = open('PreEarningResult.csv', 'wb+') spamwriter1 = csv.writer(preEarning_csv,dialect='excel') spamwriter1.writerow(['stock', 'name', 'type', 'report_date', "pre_eps", "range"]) tmp = str('111') print(tmp.decode('utf-8').encode('gb2312')) df1 = ts.forecast_data(2016,3) for code1, stockRow1 in df1.iterrows(): print("pre earning " + stockRow1['code']) print("name type " + type(stockRow1['name']).__name__) print("type type " + type(stockRow1['type']).__name__) print("pre_eps type " + type(stockRow1['pre_eps']).__name__) print(stockRow1['range']) print("range type " + type(stockRow1['range']).__name__) spamwriter1.writerow([stockRow1['code'], stockRow1['name'], stockRow1['type'].encode('gb2312'), stockRow1['report_date'], stockRow1["pre_eps"], tmp.decode('utf-8').encode('gb2312')]) preEarning_csv.close()
def forcast_data(self,stock): data = ts.forecast_data(2015,3) print data[-100:]