def download_economy(): #货币供应量 ts.get_money_supply().to_csv(path + 'money_supply.csv') #季度GDP ts.get_gdp_quarter().to_csv(path + 'gdp_quarter.csv') #年度GDP ts.get_gdp_year().to_csv(path + 'gdp_year.csv') #CPI ts.get_cpi().to_csv(path + 'cpi.csv') #存款准备金率 ts.get_rrr().to_csv(path + 'rrr.csv')
def add_reserve_base_rate_page(canvas_para): """ 函数功能:在pdf中增加准备金基率 :param canvas_para: :return: """ c = canvas_para c.setFont("song", 10) c.drawString(10, letter[1] - 20, '存款准备金基率') c.setLineWidth(3) c.line(10, letter[1] - 24, letter[0] - 10, letter[1] - 24) # 画银行准备金基率 df_rbr = ts.get_rrr().replace('--', np.nan) # df_rbr['date'] = df_rbr.apply(lambda x: stdMonthDate2ISO(x['month']), axis=1) # 提取相关数据 pot_before = extract_point_from_df_date_x(df_rbr, 'date', 'before') pot_now = extract_point_from_df_date_x(df_rbr, 'date', 'now') pot_changed = extract_point_from_df_date_x(df_rbr, 'date', 'changed') data_rbr = [tuple(pot_now)] data_rbr_note = ['准备金基率'] money_drawing = gen_lp_drawing(data=data_rbr, data_note=data_rbr_note, height=letter[1] * 0.2) renderPDF.draw(drawing=money_drawing, canvas=c, x=10, y=letter[1] * 0.7) c.showPage() return c
def get_rrr(year, quarter): try: df = ts.get_rrr() engine = create_engine('mysql://*****:*****@127.0.0.1/stock?charset=utf8') df.to_sql('rrr', engine, if_exists='append') print "message" except Exception, e: e.message
def get_rrr_info(): df = ts.get_rrr() if df is not None: res = df.to_sql(microE_rrr, engine, if_exists='replace') msg = 'ok' if res is None else res print('获取存款准备金率: ' + msg + '\n') else: print('获取存款准备金率: ' + 'None' + '\n')
def return_rrr(self): ''' 存款准备金率 ''' df = ts.get_rrr() detail = {} for col in df.columns: lt = df[col].values.tolist() lt.reverse() detail[col] = lt self.reply(detail=detail)
def stat_all(tmp_datetime): # 存款利率 data = ts.get_deposit_rate() common.insert_db(data, "ts_deposit_rate", False, "`date`,`deposit_type`") # 贷款利率 data = ts.get_loan_rate() common.insert_db(data, "ts_loan_rate", False, "`date`,`loan_type`") # 存款准备金率 data = ts.get_rrr() common.insert_db(data, "ts_rrr", False, "`date`") # 货币供应量 data = ts.get_money_supply() common.insert_db(data, "ts_money_supply", False, "`month`") # 货币供应量(年底余额) data = ts.get_money_supply_bal() common.insert_db(data, "ts_money_supply_bal", False, "`year`") # 国内生产总值(年度) data = ts.get_gdp_year() common.insert_db(data, "ts_gdp_year", False, "`year`") # 国内生产总值(季度) data = ts.get_gdp_quarter() common.insert_db(data, "ts_get_gdp_quarter", False, "`quarter`") # 三大需求对GDP贡献 data = ts.get_gdp_for() common.insert_db(data, "ts_gdp_for", False, "`year`") # 三大产业对GDP拉动 data = ts.get_gdp_pull() common.insert_db(data, "ts_gdp_pull", False, "`year`") # 三大产业贡献率 data = ts.get_gdp_contrib() common.insert_db(data, "ts_gdp_contrib", False, "`year`") # 居民消费价格指数 data = ts.get_cpi() common.insert_db(data, "ts_cpi", False, "`month`") # 工业品出厂价格指数 data = ts.get_ppi() common.insert_db(data, "ts_ppi", False, "`month`") #############################基本面数据 http://tushare.org/fundamental.html # 股票列表 data = ts.get_stock_basics() print(data.index) common.insert_db(data, "ts_stock_basics", True, "`code`")
def download_economy(): path = './data/' df = ts.get_money_supply() df.to_csv(path+'money_supply.csv') ts.get_gdp_quarter().to_csv(path+'gdp_quarter.csv') ts.get_gdp_year().to_csv(path + 'gdp_year.csv') ts.get_cpi().to_csv(path+'cpi.csv') # ts.get_hist_data('sz').to_csv(path + 'sz.csv') # ts.get_hist_data('sh').to_csv(path + 'sh.csv') # import time import datetime # now_year = time.localtime().tm_year # now_mon = time.localtime().tm_mon # now_day = time.localtime().tm_mday years = 3 start = datetime.datetime.today().date() + datetime.timedelta(-365*years) end = datetime.datetime.today().date() ts.get_k_data('399001', start=str(start), index=True).to_csv(path + 'sz.csv') #默认2年 , ts.get_k_data('000001', start=str(start), index=True).to_csv(path + 'sh.csv') # ts.get_rrr().to_csv(path + 'rrr.csv')
def download_economy(): import tushare as ts path = 'C:/Users/Administrator/stockPriditionProjects/data/' ts.get_money_supply().to_csv(path + 'money_supply.csv') ts.get_gdp_quarter().to_csv(path + 'gdp_quarter.csv') ts.get_gdp_year().to_csv(path + 'gdp_year.csv') ts.get_cpi().to_csv(path + 'cpi.csv') # ts.get_hist_data('sz').to_csv(path + 'sz.csv') # ts.get_hist_data('sh').to_csv(path + 'sh.csv') # import time import datetime # now_year = time.localtime().tm_year # now_mon = time.localtime().tm_mon # now_day = time.localtime().tm_mday years = 3 start = datetime.datetime.today().date() + datetime.timedelta(-365 * years) end = datetime.datetime.today().date() ts.get_k_data('399001', start=str(start), index=True).to_csv(path + 'sz.csv') #默认2年 , ts.get_k_data('000001', start=str(start), index=True).to_csv(path + 'sh.csv') #存款准备金率 ts.get_rrr().to_csv(path + 'rrr.csv')
def get_macro(): Macro={} Macro['Depo']=ts.get_deposit_rate() Macro['Loan']=ts.get_loan_rate() Macro['RRR']=ts.get_rrr() Macro['MoneySupply']=ts.get_money_supply() Macro['MoneyBalance']=ts.get_money_supply_bal() Macro['GDPYOY']=ts.get_gdp_year() Macro['GDPQOQ']=ts.get_gdp_quarter() Macro['GDPFOR']=ts.get_gdp_for() Macro['GDPPULL']=ts.get_gdp_pull() Macro['GDPCON']=ts.get_gdp_contrib() Macro['CPI']=ts.get_cpi() Macro['PPI']=ts.get_ppi() Macro['SHIBO']=ts.shibor_data() return Macro
def get_required_reserves_rate(): """得到存款准备金率""" logger.info('Begin get RequiredReservesRate.') try: data_df = ts.get_rrr() except Exception as e: logger.exception('Error get RequiredReservesRate.') return None else: data_dicts = [] if data_df.empty: logger.warn('Empty get RequiredReservesRate.') else: data_dicts = [{'date': row[0], 'before': row[1], 'now': row[2], 'changed': row[3]} for row in data_df.values] logger.info('Success get RequiredReservesRate.') return data_dicts
def core_function(self, type): self.set_data() print(type) mongo = MongoClient("127.0.0.1", 27017) if (type == 'gdp_year'): print("gdp_year") df = fd.get_gdp_year() elif (type == 'gdp_quarter'): print("gdp_quarter") df = fd.get_gdp_quarter() elif (type == 'gdp_for'): print("gdp_for") df = fd.get_gdp_for() elif (type == 'gdp_pull'): print("gdp_pull") df = fd.get_gdp_pull() elif (type == 'get_money_supply_bal'): print("get_money_supply_bal") df = fd.get_money_supply_bal() elif (type == 'gdp_contrib'): print("gdp_contrib") df = fd.get_gdp_contrib() elif (type == 'get_cpi'): print("get_cpi") df = ts.get_cpi() elif (type == 'get_ppi'): print("get_ppi") df = ts.get_ppi() elif (type == 'get_rrr'): print("get_rrr") df = ts.get_rrr() elif (type == 'money_supply'): print("money_supply") df = ts.get_money_supply() elif (type == 'money_supply_bal'): print("money_supply_bal") df = ts.get_money_supply_bal() else: df = {} print(df) insert_string = df.to_json(orient='records') items = json.loads(insert_string) mongo.macro.gdp_year.insert(items)
def __init__(self, instrument, timewindow): self.__instrument = instrument self.__timewindow = int(timewindow) self.__starttime = ( datetime.datetime.now() - datetime.timedelta(days=self.__timewindow)).strftime('%Y-%m-%d') # __starttime is max time of entry point self.__endtime = datetime.datetime.now().strftime('%Y-%m-%d') basics = ts.get_stock_basics() profit = ts.get_profit_data(2014, 3) #code is not the index report = ts.get_report_data(2014, 3) operation = ts.get_operation_data(2014, 3) growth = ts.get_growth_data(2014, 3) debtpay = ts.get_debtpaying_data(2014, 3) cashflow = ts.get_cashflow_data(2014, 3) # country wise rrr = ts.get_rrr() money_supply = ts.get_money_supply() cpi = ts.get_cpi()
def __call__(self, conns): self.base = Base() self.financial_data = conns['financial_data'] '''存款利率''' deposit_rate = ts.get_deposit_rate() self.base.batchwri(deposit_rate, 'deposit_rate', self.financial_data) '''贷款利率''' loan_rate = ts.get_loan_rate() self.base.batchwri(loan_rate, 'loan_rate', self.financial_data) '''存款准备金率''' rrr = ts.get_rrr() self.base.batchwri(rrr, 'RatioOfDeposit', self.financial_data) '''货币供应量''' money_supply = ts.get_money_supply() self.base.batchwri(money_supply, 'money_supply', self.financial_data) '''货币供应量(年底余额)''' money_supply_bal = ts.get_money_supply_bal() self.base.batchwri(money_supply_bal, 'money_supply_bal', self.financial_data) '''国内生产总值(年度)''' gdp_year = ts.get_gdp_year() self.base.batchwri(gdp_year, 'gdp_year', self.financial_data) '''国内生产总值(季度)''' gdp_quarter = ts.get_gdp_quarter() self.base.batchwri(gdp_quarter, 'gdp_quarter', self.financial_data) '''三大需求对GDP贡献''' gdp_for = ts.get_gdp_for() self.base.batchwri(gdp_for, 'gdp_for', self.financial_data) '''三大产业对GDP拉动''' gdp_pull = ts.get_gdp_pull() self.base.batchwri(gdp_pull, 'gdp_pull', self.financial_data) '''三大产业贡献率''' gdp_contrib = ts.get_gdp_contrib() self.base.batchwri(gdp_contrib, 'gdp_contrib', self.financial_data) '''居民消费价格指数''' cpi = ts.get_cpi() self.base.batchwri(cpi, 'cpi', self.financial_data) '''工业品出场价格指数''' ppi = ts.get_ppi() self.base.batchwri(ppi, 'ppi', self.financial_data)
import tushare as ts # 存款利率 # 结构不规整 dr = ts.get_deposit_rate() print(dr) # 贷款利率 # 结构不规整 lr = ts.get_loan_rate() print(lr) # 存款准备金率 rrr = ts.get_rrr() print(rrr) # 货币供应量 ms = ts.get_money_supply() print(ms) # 货币供应量(年底余额) ms = ts.get_money_supply_bal() print(ms) # 国内生产总值(年度) gy = ts.get_gdp_year() print(gy)
def rrr(): return ts.get_rrr()
# -*- coding: utf-8 -*- import tushare as ts # 宏观经济数据 #存款利率 saving_rate = ts.get_deposit_rate() #贷款利率 loan_rate = ts.get_loan_rate() #存款准备金率 reserve_rate = ts.get_rrr() #货币供应量 m_supply = ts.get_money_supply() #货币供应量(年底余额) m_supply_y = ts.get_money_supply_bal() #GDP gdp_y = ts.get_gdp_year() gdp_q = ts.get_gdp_quarter() #三大需求对GDP贡献 gdp_for = ts.get_gdp_for() #三大产业对GDP拉动 gdp_pull = ts.get_gdp_pull() #三大产业贡献率 gdp_contrib = ts.get_gdp_contrib() cpi = ts.get_cpi()
def getRRRRate(self): file_name = 'rrr_rate.csv' path = self.index + self.index_rrr + file_name data = ts.get_rrr() data.to_csv(path, encoding='utf-8') print(file_name)
def macro_type(macros_type): if macros_type == 'deposit_rate': deposit_rate = ts.get_deposit_rate() if deposit_rate is not None: deposit_rate.to_sql('macros_deposit_rate', engine, flavor='mysql', if_exists='replace') elif macros_type == 'loan_rate': loan_rate = ts.get_loan_rate() if loan_rate is not None: loan_rate.to_sql('macros_loan_rate', engine, flavor='mysql', if_exists='replace') elif macros_type == 'rrr': rrr = ts.get_rrr() if rrr is not None: rrr.to_sql('macros_rrr', engine, flavor='mysql', if_exists='replace') elif macros_type == 'money_supply': money_supply = ts.get_money_supply() if money_supply is not None: money_supply.to_sql('macros_money_supply', engine, flavor='mysql', if_exists='replace') elif macros_type == 'money_supply_bal': money_supply_bal = ts.get_money_supply_bal() if money_supply_bal is not None: money_supply_bal.to_sql('macros_money_supply_bal', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_year': gdp_year = ts.get_gdp_year() if gdp_year is not None: gdp_year.to_sql('macros_gdp_year', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_quater': gdp_quater = ts.get_gdp_quarter() if gdp_quater is not None: gdp_quater.to_sql('macros_gdp_quater', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_for': gdp_for = ts.get_gdp_for() if gdp_for is not None: gdp_for.to_sql('macros_gdp_for', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_pull': gdp_pull = ts.get_gdp_pull() if gdp_pull is not None: gdp_pull.to_sql('macros_gdp_pull', engine, flavor='mysql', if_exists='replace') elif macros_type == 'gdp_contrib': gdp_contrib = ts.get_gdp_contrib() if gdp_contrib is not None: gdp_contrib.to_sql('macros_gdp_contrib', engine, flavor='mysql', if_exists='replace') elif macros_type == 'cpi': cpi = ts.get_cpi() if cpi is not None: cpi.to_sql('macros_cpi', engine, flavor='mysql', if_exists='replace') elif macros_type == 'ppi': ppi = ts.get_ppi() if ppi is not None: ppi.to_sql('macros_ppi', engine, flavor='mysql', if_exists='replace')
''' i didn't multiprocess this one since the performance seems acceptable ''' import tushare as ts import pandas as pd if __name__ =='__main__': deposit_rate=ts.get_deposit_rate() deposit_rate .to_csv('D:\\ts\\macro\\deposit_rate.csv',encoding='gbk') loan_rate=ts.get_loan_rate() loan_rate.to_csv('D:\\ts\\macro\\loan_rate.csv', encoding='gbk') rrr=ts.get_rrr() rrr.to_csv('D:\\ts\\macro\\rrr.csv', encoding='gbk') money_supply=ts.get_money_supply() money_supply.to_csv('D:\\ts\\macro\\money_supply.csv', encoding='gbk') money_supply_bal=ts.get_money_supply_bal() money_supply_bal.to_csv('D:\\ts\\macro\\money_supply_bal.csv', encoding='gbk') gdp_year=ts.get_gdp_year() gdp_year.to_csv('D:\\ts\\macro\\gdp_year.csv', encoding='gbk') gdp_quater=ts.get_gdp_quarter() gdp_quater.to_csv('D:\\ts\\macro\\gdp_quater.csv', encoding='gbk') gdp_for=ts.get_gdp_for()
def getrrrdb(): rrr = ts.get_rrr() rrr.to_sql('rrr_data',ENGINE,if_exists='append')
def setRRR(self, isSave=False, tableName=MACROECONOMIC_RRR): df = ts.get_rrr() if isSave is True: df.to_sql(tableName, self.engine_sql, if_exists='append') return df
def init(engine, session): tbl = "macro_deposit" tsl.log(tbl + " start...") df = ts.get_deposit_rate() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_loan" tsl.log(tbl + " start...") df = ts.get_loan_rate() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_rrr" tsl.log(tbl + " start...") df = ts.get_rrr() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_money_supply" tsl.log(tbl + " start...") df = ts.get_money_supply() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_money_supply_year" tsl.log(tbl + " start...") df = ts.get_money_supply_bal() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_year" tsl.log(tbl + " start...") df = ts.get_gdp_year() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_quarter" tsl.log(tbl + " start...") df = ts.get_gdp_quarter() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_for" tsl.log(tbl + " start...") df = ts.get_gdp_for() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_pull" tsl.log(tbl + " start...") df = ts.get_gdp_pull() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_gdp_contrib" tsl.log(tbl + " start...") df = ts.get_gdp_contrib() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_cpi" tsl.log(tbl + " start...") df = ts.get_cpi() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "macro_ppi" tsl.log(tbl + " start...") df = ts.get_ppi() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done") tbl = "gold_and_foreign_reserves" tsl.log(tbl + " start...") df = ts.get_gold_and_foreign_reserves() df.to_sql(tbl,engine,if_exists='replace') tsl.log(tbl + " done")
import tushare as ts import matplotlib.pyplot as plt import matplotlib as mpl import mpl_finance as mpf import numpy as np import pandas as pd lra = ts.get_rrr() print(lra[:1]) c = lra.iloc[:, 2] print(c.values) n = np.shape(c.values) print(n[0]) t = np.linspace(1, n[0], n[0]) plt.plot(t, c.values) plt.show()
rate:利率(%) ''' df = ts.get_loan_rate() print(df) # 存款准备金率 ''' 返回值说明: date :变动日期 before :调整前存款准备金率(%) now:调整后存款准备金率(%) changed:调整幅度(%) ''' df = ts.get_rrr() #货币供应量 ''' 返回值说明: month :统计时间 m2 :货币和准货币(广义货币M2)(亿元) m2_yoy:货币和准货币(广义货币M2)同比增长(%) m1:货币(狭义货币M1)(亿元) m1_yoy:货币(狭义货币M1)同比增长(%) m0:流通中现金(M0)(亿元) m0_yoy:流通中现金(M0)同比增长(%) cd:活期存款(亿元) cd_yoy:活期存款同比增长(%) qm:准货币(亿元)
def job_5(): try: print("I'm working......宏观经济数据") # 存款利率 deposit_rate = ts.get_deposit_rate() data = pd.DataFrame(deposit_rate) data.to_sql('deposit_rate',engine,index=True,if_exists='replace') print("存款利率......done") # 贷款利率 loan_rate = ts.get_loan_rate() data = pd.DataFrame(loan_rate) data.to_sql('loan_rate',engine,index=True,if_exists='replace') print("贷款利率......done") # 存款准备金率 rrr = ts.get_rrr() data = pd.DataFrame(rrr) data.to_sql('rrr',engine,index=True,if_exists='replace') print("存款准备金率......done") # 货币供应量 money_supply = ts.get_money_supply() data = pd.DataFrame(money_supply) data.to_sql('money_supply',engine,index=True,if_exists='replace') print("货币供应量......done") # 货币供应量(年底余额) money_supply_bal = ts.get_money_supply_bal() data = pd.DataFrame(money_supply_bal) data.to_sql('money_supply_bal',engine,index=True,if_exists='replace') print("货币供应量(年底余额)......done") # 国内生产总值(年度) gdp_year = ts.get_gdp_year() data = pd.DataFrame(gdp_year) data.to_sql('gdp_year',engine,index=True,if_exists='replace') print("国内生产总值(年度)......done") # 国内生产总值(季度) gdp_quarter = ts.get_gdp_quarter() data = pd.DataFrame(gdp_quarter) data.to_sql('gdp_quarter',engine,index=True,if_exists='replace') print("国内生产总值(季度)......done") # 三大需求对GDP贡献 gdp_for = ts.get_gdp_for() data = pd.DataFrame(gdp_for) data.to_sql('gdp_for',engine,index=True,if_exists='replace') print("三大需求对GDP贡献......done") # 三大产业对GDP拉动 gdp_pull = ts.get_gdp_pull() data = pd.DataFrame(gdp_pull) data.to_sql('gdp_pull',engine,index=True,if_exists='replace') print("三大产业对GDP拉动......done") # 三大产业贡献率 gdp_contrib = ts.get_gdp_contrib() data = pd.DataFrame(gdp_contrib) data.to_sql('gdp_contrib',engine,index=True,if_exists='replace') print("三大产业贡献率......done") # 居民消费价格指数 cpi = ts.get_cpi() data = pd.DataFrame(cpi) data.to_sql('cpi',engine,index=True,if_exists='replace') print("居民消费价格指数......done") # 工业品出厂价格指数 ppi = ts.get_ppi() data = pd.DataFrame(ppi) data.to_sql('ppi',engine,index=True,if_exists='replace') print("工业品出厂价格指数......done") except Exception as e: print(e)
def load_macro_economy(): # 下载存款利率 try: rs = ts.get_deposit_rate() pd.DataFrame.to_sql(rs, "deposit_rate", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载存款利率ok") except: print("下载存款利率出错") # 下载贷款利率 try: rs = ts.get_loan_rate() pd.DataFrame.to_sql(rs, "loan_rate", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载贷款利率ok") except: print("下载贷款利率出错") # 下载存款准备金率 try: rs = ts.get_rrr() pd.DataFrame.to_sql(rs, "rrr", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载存款准备金率ok") except: print("下载存款准备金率出错") # 下载货币供应量 try: rs = ts.get_money_supply() pd.DataFrame.to_sql(rs, "money_supply", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载货币供应量ok") except: print("下载货币供应量出错") # 下载货币供应量(年底余额) try: rs = ts.get_money_supply_bal() pd.DataFrame.to_sql( rs, "money_supply_bal", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True ) print("下载货币供应量(年底余额)ok") except: print("下载货币供应量(年底余额)出错") # 下载国内生产总值(年度) try: rs = ts.get_gdp_year() pd.DataFrame.to_sql(rs, "gdp_year", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载国内生产总值(年度)ok") except: print("下载国内生产总值(年度)出错") # 下载国内生产总值(季度) try: rs = ts.get_gdp_quarter() pd.DataFrame.to_sql(rs, "gdp_quarter", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载国内生产总值(季度)ok") except: print("下载国内生产总值(季度)出错") # 下载三大需求对GDP贡献 try: rs = ts.get_gdp_for() pd.DataFrame.to_sql(rs, "gdp_for", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载三大需求对GDP贡献ok") except: print("下载三大需求对GDP贡献出错") # 下载三大产业对GDP拉动 try: rs = ts.get_gdp_pull() pd.DataFrame.to_sql(rs, "gdp_pull", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载三大产业对GDP拉动ok") except: print("下载三大产业对GDP拉动出错") # 下载三大产业贡献率 try: rs = ts.get_gdp_contrib() pd.DataFrame.to_sql(rs, "gdp_contrib", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载三大产业贡献率ok") except: print("下载三大产业贡献率出错") # 下载居民消费价格指数 try: rs = ts.get_cpi() pd.DataFrame.to_sql(rs, "gdp_cpi", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载居民消费价格指数ok") except: print("下载居民消费价格指数出错") # 下载工业品出厂价格指数 try: rs = ts.get_ppi() pd.DataFrame.to_sql(rs, "gdp_ppi", con=conn_macro_economy, flavor="mysql", if_exists="replace", index=True) print("下载工业品出厂价格指数ok") except: print("下载工业品出厂价格指数出错")
def macrodata(start=None, end=None): """macroeconomics data : Shibor | Reserve Ratio | M2 | GDP | CPI | Loan Rate. the data start from 2006-10-08. Cause it is when the shibor data start on Tushare. parameters: --------- start: a string present a date indicates the return data start from. for example: '2011-01-22' end : refer to start return: -------- pandas.DataFrame """ T = __T T.rename(columns={'calendarDate': 'date'}, inplace=True) today = dt.today() today = dt.strftime(today, '%Y-%m-%d') T = T[T.date > '2006-04-01'] T = T[T.date <= today] loan = ts.get_loan_rate() #date loan_type rate loan = loan[loan.date > '2006-06-01'] rate = pd.DataFrame() for i in range(len(loan)): if loan.iloc[i].loan_type == '短期贷款(六个月以内)': rate = rate.append(loan.iloc[i][['date', 'rate']]) rrr = ts.get_rrr()[['date', 'now']] # 准备金率 rrr.rename(columns={'now': 'rrr'}, inplace=True) rrr = rrr[rrr.date > '2006-08-01'] dataScale = ['15', '09'] dataindex = 0 def makeDate(raw_date): raw_date = str(raw_date) raw_date = raw_date.split('.') if int(raw_date[1]) <= 9: raw_date[1] = '0' + raw_date[1] return raw_date[0] + '-' + raw_date[1] + '-' + dataScale[dataindex] def makeQuarter(raw_date): raw_date = str(raw_date) raw_date = raw_date.split('.') p = raw_date[1] q = {'1': '01', '2': '04', '3': '07', '4': '10'} return raw_date[0] + '-' + q[p] + '-' + '15' supply = ts.get_money_supply()[['month', 'm2_yoy']] #15th. supply.rename(columns={'month': 'date', 'm2_yoy': 'm2'}, inplace=True) if type(supply.iloc[1]['date']) == type(''): supply = supply[supply.date > '2006.6'] else: supply = supply[supply.date > 2006.6] supply['date'] = supply['date'].apply(makeDate) dataindex = dataindex + 1 m2 = supply gdp = ts.get_gdp_quarter()[['quarter', 'gdp_yoy']] # gdp will available at 15th. gdp.rename(columns={'quarter': 'date', 'gdp_yoy': 'gdp'}, inplace=True) if type(gdp.iloc[1]['date']) == type(''): gdp = gdp[gdp.date > '2006.1'] else: gdp = gdp[gdp.date > 2006.1] gdp['date'] = gdp['date'].apply(makeQuarter) cpi = ts.get_cpi() #month , cpi, available at 9th. cpi.rename(columns={'month': 'date'}, inplace=True) if type(cpi.iloc[1]['date']) == type(''): cpi = cpi[cpi.date > '2006.6'] else: cpi = cpi[cpi.date > 2006.6] cpi['date'] = cpi['date'].apply(makeDate) lastvalue = 0.0 def setValue(v): nonlocal lastvalue if pd.isnull(v) or v == 'None': return lastvalue else: lastvalue = v return lastvalue T = T.merge(cpi, on='date', how='left') lastvalue = cpi.iloc[-1]['cpi'] T['cpi'] = T['cpi'].apply(setValue) T = T.merge(gdp, on='date', how='left') lastvalue = gdp.iloc[-1]['gdp'] T['gdp'] = T['gdp'].apply(setValue) T = T.merge(m2, on='date', how='left') lastvalue = m2.iloc[-1]['m2'] T['m2'] = T['m2'].apply(setValue) T = T.merge(rrr, on='date', how='left') lastvalue = rrr.iloc[-1]['rrr'] T['rrr'] = T['rrr'].apply(setValue) T = T.merge(rate, on='date', how='left') lastvalue = rate.iloc[-1]['rate'] T['rate'] = T['rate'].apply(setValue) # T=shibor.merge(T,on='date',how='left') T = T[T.isOpen > 0.5] T = T[T.date >= '2006-10-08'] del T['isOpen'] if start and end: T = T[T.date >= start] T = T[T.date <= end] T[['rrr']] = T[['rrr']].astype(float) T[['m2']] = T[['m2']].astype(float) T[['rate']] = T[['rate']].astype(float) T[['gdp']] = T[['gdp']].astype(float) T[['cpi']] = T[['cpi']].astype(float) T = T.reset_index() del T['index'] return T