# -*- coding:utf-8 -*- """ Created on 2015/09/30 @author: Jacob He @contact: [email protected] """ from lib.stock.DbCache import DbCache if __name__ == '__main__': #init the fundament data c = DbCache() c.check_db_cache(1989)
def get_stock_growth(): """ 获取股票3年的增长率和最近几个季度的增长率 Return -------- DataFrame code :股票代码 name :股票名称 1year:最近一年增长率 2year:最近二年增长率 3year:最近三年增长率 4year:最近四年增长率 1quarter:最近一季度增长率 2quarter:最近二季度增长率 3quarter:最近三季度增长率 4quarter:最近四季度增长率 """ fd = DbCache() stock_list = fd.get_stock_basics() df = pd.DataFrame(index=stock_list.index, columns=['name','industry','area','1quarter','2quarter','3quarter','4quarter','1year','2year','3year','4year']) df['name'] = stock_list['name'] df['industry'] = stock_list['industry'] df['area'] = stock_list['area'] cur = datetime.datetime.now() curyear = cur.year fir_year_growth_data = fd.get_growth_data(curyear-1,4) if fir_year_growth_data.empty: curyear = curyear - 1 fir_year_growth_data = fd.get_growth_data(curyear-1,4) sec_year_growth_data = fd.get_growth_data(curyear-2,4) thir_year_growth_data = fd.get_growth_data(curyear-3,4) for_year_growth_data = fd.get_growth_data(curyear-4,4) for i in fir_year_growth_data.index: df.loc[fir_year_growth_data.loc[i,'code'],'1year'] = fir_year_growth_data.loc[i,'nprg'] for i in sec_year_growth_data.index: df.loc[sec_year_growth_data.loc[i,'code'],'2year'] = sec_year_growth_data.loc[i,'nprg'] for i in thir_year_growth_data.index: df.loc[thir_year_growth_data.loc[i,'code'],'3year'] = thir_year_growth_data.loc[i,'nprg'] for i in for_year_growth_data.index: df.loc[for_year_growth_data.loc[i,'code'],'4year'] = for_year_growth_data.loc[i,'nprg'] curmonth = cur.month if curmonth in(4,5,6): curquarter = 1 curyear = cur.year elif curmonth in (7,8,9): curquarter = 2 curyear = cur.year elif curmonth in (10,11,12): curquarter = 3 curyear = cur.year elif curmonth in(1,2,3): curquarter = 4 curyear = cur.year - 1 fir_quarter_growth_data = fd.get_growth_data(curyear,curquarter) if fir_quarter_growth_data.empty: curyear,curquarter = _sub_quarter(curyear,curquarter) fir_quarter_growth_data = fd.get_growth_data(curyear,curquarter) curyear2,curquarter2 = _sub_quarter(curyear,curquarter) sec_quarter_growth_data = fd.get_growth_data(curyear2,curquarter2) curyear3,curquarter3 = _sub_quarter(curyear2,curquarter2) thir_quarter_growth_data = fd.get_growth_data(curyear3,curquarter3) curyear4,curquarter4 = _sub_quarter(curyear3,curquarter3) for_quarter_growth_data = fd.get_growth_data(curyear4,curquarter4) for i in fir_quarter_growth_data.index: df.loc[fir_quarter_growth_data.loc[i,'code'],'1quarter'] = fir_quarter_growth_data.loc[i,'nprg'] for i in sec_quarter_growth_data.index: df.loc[sec_quarter_growth_data.loc[i,'code'],'2quarter'] = sec_quarter_growth_data.loc[i,'nprg'] for i in thir_quarter_growth_data.index: df.loc[thir_quarter_growth_data.loc[i,'code'],'3quarter'] = thir_quarter_growth_data.loc[i,'nprg'] for i in for_quarter_growth_data.index: df.loc[for_quarter_growth_data.loc[i,'code'],'4quarter'] = for_quarter_growth_data.loc[i,'nprg'] return df