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_cpi_page(canvas_para, length): """ 函数功能:增加CPI页 :param canvas_para: :return: """ c = canvas_para cpi_df = ts.get_cpi() cpi_df['month'] = cpi_df.apply(lambda x: stdMonthDate(x['month']), axis=1) cpi_df = cpi_df.sort_values( by='month', ascending=False).head(length).sort_values(by='month', ascending=True) cpi = extract_point_from_df_date_x(df_origin=cpi_df, date_col='month', y_col='cpi', timeAxis='month') gdp_pull_drawing = gen_lp_drawing([tuple(cpi)], data_note=['CPI增长率'], time_axis='month') renderPDF.draw(drawing=gdp_pull_drawing, canvas=c, x=10, y=letter[1] * 0.6) c.showPage() return c
def get_economics_state(s_date, e_date): s_year = s_date.split('-')[0] # 起始年份 e_year = e_date.split('-')[0] # 结束年份 s_month = s_date.split('-')[1] # 起始月份 e_month = s_date.split('-')[1] # 结束月份 s_day = s_date.split('-')[2] # 开始日期 e_day = e_date.split('-')[2] # 结束日期 # 获取Shibor拆解利率数据 shibor_df = get_shibor(s_year) for y in range(int(s_year)+1, int(e_year)+1): shibor_df = pd.concat([shibor_df, get_shibor(y)]) return shibor_df # 获取每月CPI数据 cpi_df = ts.get_cpi() dates = pd.date_range(s_year + s_month + s_day, e_year + e_month + e_day) # 生成整个时间区间内的日期 cpi = pd.DataFrame(data={'cpi': 0}, index=dates) # 初始化处理后的CPI数据框 cpi.index.names = ['date'] for i in cpi_df.month: # 根据所需要的区间遍历每月CPI数据 year = i.split('.')[0] month = i.split('.')[1] if len(month) == 1: # 如果月份数小于10,则需要补0 month = '0' + month cpi[year+'-'+month] = cpi_df[cpi_df['month'] == i].iloc[0, 1] # 获取每月货币供应量数据 money_df = ts.get_money_supply() money_supply = pd.DataFrame(data={'money': 0}, index=dates) money_supply.index.names = ['date'] for i in money_df.month: # 根据所需要的区间遍历每月CPI数据 year = i.split('.')[0] month = i.split('.')[1] if len(month) == 1: # 如果月份数小于10,则需要补0 month = '0' + month money_supply[year+'-'+month] = money_df[money_df['month'] == i].iloc[0, 2] # 广义货币M2同比增长(%)
def macro_info_to_sql(): create_classify_table() a = ts.get_cpi() b = ts.get_ppi() c = ts.get_money_supply() c = c.iloc[:, [0, 1, 3, 5]] b = b.iloc[:, [0, 2]] result = pd.merge(a, b, how='left', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False) result = pd.merge(result, c, how='left', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False) df_to_mysql('anack_macro_data', result)
def get_cpi(): try: df = ts.get_cpi() engine = create_engine('mysql://*****:*****@127.0.0.1/stock?charset=utf8') df.to_sql('cpi', engine, if_exists='append') print "message" except Exception, e: e.message
def get_gdp_cpi_info(): df = ts.get_cpi() if df is not None: res = df.to_sql(microE_cpi, engine, if_exists='replace') msg = 'ok' if res is None else res print('获取居民消费价格指数: ' + msg + '\n') else: print('获取居民消费价格指数: ' + 'None' + '\n')
def test(): ts.get_sz50s() ts.get_hs300s() ts.get_zz500s() ts.realtime_boxoffice() ts.get_latest_news() ts.get_notices(tk) ts.guba_sina() ts.get_cpi() ts.get_ppi() ts.get_stock_basics() ts.get_concept_classified() ts.get_money_supply() ts.get_gold_and_foreign_reserves() ts.top_list() #每日龙虎榜列表 ts.cap_tops() #个股上榜统计 ts.broker_tops() #营业部上榜统计 ts.inst_tops() # 获取机构席位追踪统计数据 ts.inst_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 get_cpi(self): dat=ts.get_cpi() dat.index = pd.to_datetime(dat['month']) dat = dat.drop(['month'], axis=1) dat['cpi']=dat['cpi']/100 dat=dat['2018':'2008'] #选取时间片 dat=dat.sort_index() #倒序 dat['cpi2']=dat['cpi'].cumprod(axis=0) #计算累乘 dat.to_csv("cpi.csv") dat.plot() plt.show()
def return_cpi(self): ''' 居民消费价格指数 ''' df = ts.get_cpi() detail = {} for col in df.columns: lt = df[col].values.tolist() lt.reverse() detail[col] = lt self.reply(detail=detail)
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_cpi(): """居民消费价格指数""" logger.info('Begin get CPI.') try: data_df = ts.get_cpi() except Exception as e: logger.exception('Error get CPI.') return None else: data_dicts = [] if data_df.empty: logger.warn('Empty get CPI.') else: data_dicts = [{'month': row[0], 'cpi': row[1]} for row in data_df.values] logger.info('Success get CPI.') return data_dicts
def cpi_vs_m2(self): #此处的计算有误,获得的数据是同比,而本函数计算是环比增长的方法 dat_cpi = ts.get_cpi() dat_m2 = ts.get_money_supply() dat_cpi.index=pd.to_datetime(dat_cpi['month']) dat_m2.index=pd.to_datetime(dat_m2['month']) dat_cpi = dat_cpi['2018':'2008'] dat_m2 = dat_m2['2018':'2008'] dat_m2['cpi']=dat_cpi['cpi'] dat = dat_m2[['m0','m1','m2','cpi']] dat = dat.sort_index() dat['cpi']= dat['cpi']/100 dat['cpi'] = dat['cpi'].cumprod(axis=0) dat = dat.astype(dtype='float64') dat = (dat - dat.min()) / (dat.max() - dat.min()) dat.plot() plt.show()
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 get_all_price(code_list): '''''process all stock''' df = ts.get_realtime_quotes(STOCK) print df df = ts.get_latest_news() print df df = ts.get_cpi() print df df = ts.get_stock_basics() print df df = ts.get_sz50s() print df df = ts.get_hist_data('600848') print df
def get_cpi(start_month, end_month): """ 取得居民消费价格指数 @param start_month: 开始月份YYYY.M @param end_month: 结束月份YYYY.M @return: 指定月份范围的CPI数据集 """ # 日期格式 date_format = '%Y.%m' # 文件路径 file_path = './cache/ts/ts_cpi.csv' # 如果存在数据文件,则直接读取 if os.path.exists(file_path): logcm.print_info('读取缓存数据...') # 读取文件 df_cpi = pd.read_csv(file_path, dtype={ 'month': str, 'cpi': float, 'month_num': int }) else: # 取得CPI数据 df_cpi = ts.get_cpi() # 把日期转成Num month_num_list = datecm.date_list_to_num(df_cpi["month"], date_format) # 插入新的列 df_cpi.insert(2, 'month_num', month_num_list) # 保存到文件 df_cpi.to_csv(file_path, index=False) # 对开始结束日期计算 start = datecm.date_to_num(start_month, date_format) end = datecm.date_to_num(end_month, date_format) # 根据开始结束时间数据筛选 df_result = df_cpi.ix[df_cpi.month_num >= start, :] df_result = df_result.ix[df_cpi.month_num <= end, :] # 按照日期数值排正序 df_result = df_result.sort_values(by=['month_num']) # 返回数据集 return df_result
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)
def gdp_vs_m2(self): dat_cpi = ts.get_cpi() dat_m2 = ts.get_money_supply() dat_gdp = ts.get_gdp_year() dat_gdp.index =pd.to_datetime(dat_gdp['year'], format='%Y') dat_gdp=dat_gdp.to_period('A') dat_gdp = dat_gdp['gdp'] dat_gdp = dat_gdp['2017':'2000'] dat_gdp=dat_gdp.sort_index() #dat_gdp = (dat_gdp - dat_gdp.min()) / (dat_gdp.max() - dat_gdp.min()) dat_cpi.index=pd.to_datetime(dat_cpi['month']) dat_m2.index=pd.to_datetime(dat_m2['month']) dat_cpi = dat_cpi['2017':'2000'] dat_m2 = dat_m2['2017':'2000'] dat_m2['cpi']=dat_cpi['cpi'] dat = dat_m2[['m0','m1','m2','cpi']] dat = dat.sort_index() dat['cpi']= dat['cpi']/100 dat['cpi'] = dat['cpi'].cumprod(axis=0) dat = dat.astype(dtype='float64') #dat = (dat - dat.min()) / (dat.max() - dat.min()) dat=dat.resample("AS").first() dat= dat.to_period('A') #dat=dat.resample("AS").sum() dat['gdp']=dat_gdp #dat['m2-gdp']=dat['m2']-dat['gdp'] #dat = (dat - dat.min()) / (dat.max() - dat.min()) #dat['m2-gdp']=dat['m2']-dat['gdp'] dat['m0']=dat['m0']/dat['m0']['2000'] dat['m1']=dat['m1']/dat['m1']['2000'] dat['m2']=dat['m2']/dat['m2']['2000'] dat['cpi']=dat['cpi']/dat['cpi']['2000'] dat['gdp']=dat['gdp']/dat['gdp']['2000'] dat['m2-gdp']=dat['m2']-dat['gdp'] dat=dat.drop(['cpi','m0','m1'], axis=1) print(dat) #dat['m2-gdp'].plot() dat.plot() plt.show()
pi :第一产业献率(%) si :第二产业献率(%) industry:其中工业献率(%) ti :第三产业献率(%) ''' ts.get_gdp_contrib() # 居民消费价格指数 ''' 返回值说明: month :统计月份 cpi :价格指数 ''' ts.get_cpi() # 工业品出厂价格指数 ''' 返回值说明: month :统计月份 ppiip :工业品出厂价格指数 ppi :生产资料价格指数 qm:采掘工业价格指数 rmi:原材料工业价格指数 pi:加工工业价格指数 cg:生活资料价格指数 food:食品类价格指数 clothing:衣着类价格指数 roeu:一般日用品价格指数
import tushare as ts print(ts.get_cpi())
def capture_stock_data(): capture_date = datetime.datetime.now().strftime("%Y%m%d") save_dir = "/home/dandelion/stock_data/" + capture_date if not os.path.exists(save_dir): os.mkdir(save_dir) print("The save directory is created successfully!\n", save_dir) print("The save directory is already exist!\n", save_dir) # ======================Daily Command================================================================ # get the boxoffcie data of the last day and save as csvfile named as the capture command ts.day_boxoffice().to_csv( save_dir + "/" + capture_date + "_day_boxoffice.csv", header=True, sep=",", index=False, ) print("day_boxoffice data capture completed!") # get the cinema data of the last day and save as csvfile named as the capture command ts.day_cinema().to_csv( save_dir + "/" + capture_date + "_day_cinema.csv", header=True, sep=",", index=False, ) print("day_cinema data capture completed!") ts.month_boxoffice().to_csv( save_dir + "/" + capture_date + "_month_boxoffice.csv", header=True, sep=",", index=False, ) print("month_boxoffice data capture completed!") ts.realtime_boxoffice().to_csv( save_dir + "/" + capture_date + "_realtime_boxoffice.csv", header=True, sep=",", index=False, ) print("realtime_boxoffice data capture completed!") # get the stock data index of the last day and save as csvfile named as the capture command ts.get_index().to_csv( save_dir + "/" + capture_date + "_get_index.csv", header=True, sep=",", index=False, ) print("get_index data capture completed!") # get the history cpi data and save as csvfile named as the capture command ts.get_cpi().to_csv( save_dir + "/" + capture_date + "_get_cpi.csv", header=True, sep=",", index=False, ) print("get_cpi data capture completed!") # get the history gdp data by month and save as csvfile named as the capture command ts.get_gdp_year().to_csv( save_dir + "/" + capture_date + "_get_gdp_year.csv", header=True, sep=",", index=False, ) print("get_gdp_year data capture completed!") # get today all stock data and save as csvfile named as the capture command # ts.get_today_all().to_csv(save_dir+'/'+capture_date+'_get_today_all.csv',header=True,sep=',',index=False) # get detail information of the top brokers today and save as csvfile named as the capture command ts.broker_tops().to_csv( save_dir + "/" + capture_date + "_broker_tops.csv", header=True, sep=",", index=False, ) print("broker_tops data capture completed!") # get detail information of the top brokers today and save as csvfile named as the capture command ts.cap_tops().to_csv( save_dir + "/" + capture_date + "_cap_tops.csv", header=True, sep=",", index=False, ) print("cap_tops data capture completed!") ts.get_area_classified().to_csv( save_dir + "/" + capture_date + "_get_area_classified.csv", header=True, sep=",", index=False, ) print("get_area_classified data capture completed!") # ts.get_balance_sheet(code='').to_csv(save_dir+'/'+capture_date+'_get_balance_sheet.csv',header=True,sep=',',index=False) # print('get_balance_sheet data capture completed!') # ts.get_cash_flow(code='').to_csv(save_dir+'/'+capture_date+'_get_cash_flow.csv',header=True,sep=',',index=False) # print('get_cash_flow data capture completed!') ts.get_day_all().to_csv( save_dir + "/" + capture_date + "_get_day_all.csv", header=True, sep=",", index=False, ) print("get_day_all data capture completed!") ts.get_cashflow_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_cashflow_data.csv", header=True, sep=",", index=False, ) print("get_cashflow_data data capture completed!") ts.get_concept_classified().to_csv( save_dir + "/" + capture_date + "_get_concept_classified.csv", header=True, sep=",", index=False, ) print("get_concept_classified data capture completed!") ts.get_debtpaying_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_debtpaying_data.csv", header=True, sep=",", index=False, ) print("get_debtpaying_data data capture completed!") ts.get_deposit_rate().to_csv( save_dir + "/" + capture_date + "_get_deposit_rate.csv", header=True, sep=",", index=False, ) print("get_deposit_rate data capture completed!") ts.get_gdp_contrib().to_csv( save_dir + "/" + capture_date + "_get_gdp_contrib.csv", header=True, sep=",", index=False, ) ts.get_gdp_for().to_csv( save_dir + "/" + capture_date + "_get_gdp_for.csv", header=True, sep=",", index=False, ) ts.get_gdp_pull().to_csv( save_dir + "/" + capture_date + "_get_gdp_pull.csv", header=True, sep=",", index=False, ) ts.get_gdp_quarter().to_csv( save_dir + "/" + capture_date + "_get_gdp_quarter.csv", header=True, sep=",", index=False, ) print("get_gdp_ data capture completed!") # ts.get_gdp_year().to_csv(save_dir+'/'+capture_date+'_get_gdp_year.csv',header=True,sep=',',index=False) ts.get_gem_classified().to_csv( save_dir + "/" + capture_date + "_get_gem_classified.csv", header=True, sep=",", index=False, ) ts.get_gold_and_foreign_reserves().to_csv( save_dir + "/" + capture_date + "_get_gold_and_foreign_reserves.csv", header=True, sep=",", index=False, ) ts.get_growth_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_growth_data.csv", header=True, sep=",", index=False, ) ts.get_industry_classified().to_csv( save_dir + "/" + capture_date + "_get_industry_classified.csv", header=True, sep=",", index=False, ) ts.get_hs300s().to_csv( save_dir + "/" + capture_date + "_get_hs300s.csv", header=True, sep=",", index=False, ) ts.get_sz50s().to_csv( save_dir + "/" + capture_date + "_get_sz50s.csv", header=True, sep=",", index=False, ) ts.get_zz500s().to_csv( save_dir + "/" + capture_date + "_get_zz500s.csv", header=True, sep=",", index=False, ) ts.get_operation_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_operation_data.csv", header=True, sep=",", index=False, ) ts.get_stock_basics().to_csv( save_dir + "/" + capture_date + "_get_stock_basics.csv", header=True, sep=",", index=False, ) ts.get_report_data(2018, 3).to_csv( save_dir + "/" + capture_date + "_get_report_data.csv", header=True, sep=",", index=False, ) ts.inst_detail().to_csv( save_dir + "/" + capture_date + "_inst_detail.csv", header=True, sep=",", index=False, ) ts.inst_tops().to_csv( save_dir + "/" + capture_date + "_inst_tops.csv", header=True, sep=",", index=False, ) print("inst_tops data capture completed!") ts.new_stocks().to_csv( save_dir + "/" + capture_date + "_new_stocks.csv", header=True, sep=",", index=False, ) print("new_stocks data capture completed!") ts.top_list().to_csv( save_dir + "/" + capture_date + "_top_list.csv", header=True, sep=",", index=False, ) print("top_list data capture completed!")
plt.show() ## 相关指标 short_indices(cum_delta ,0.03) ## 计算收益 gain = 1+signal1.shift(1)*df[0] ## 画图 sz.close.plot() (sz.close[0]*gain.cumprod()).plot() plt.show() short_indices(gain.cumprod(),0.03) cpi = tushare.get_cpi() cpi.index = cpi["month"].apply(pandas.to_datetime) dff = pandas.DataFrame() dff["cpi"] = cpi["cpi"].shift(1) dff["index_gain"] = delta dff = dff.dropna().sort_index() dff.corr() signal= dff["cpi"]>(dff["cpi"].shift(1)) gain = 1+dff["index_gain"]*(signal.shift(1)) sz.close.plot() (sz.close[0]*gain.cumprod()).plot() plt.show()
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Feb 2 20:29:25 2019 @author: xsxsz """ import pandas as pd import tushare as ts df = ts.realtime_boxoffice() print(df) print('--------------') df = ts.get_cpi() print(df) print('--------------') df = ts.get_stock_basics() print(df) print('--------------') df.to_csv('./stock.csv')
def call_cpi(): df = ts.get_cpi() return df.loc[0, 'cpi']
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() gdp_for.to_csv('D:\\ts\\macro\\gdp_for.csv', encoding='gbk') gdp_pull=ts.get_gdp_pull() gdp_pull.to_csv('D:\\ts\\macro\\gdp_pull.csv', encoding='gbk') gdp_contrib=ts.get_gdp_contrib() gdp_contrib.to_csv('D:\\ts\\macro\\gdp_contrib.csv', encoding='gbk') cpi=ts.get_cpi() cpi.to_csv('D:\\ts\\macro\\cpi.csv', encoding='gbk') ppi=ts.get_ppi() ppi.to_csv('D:\\ts\\macro\\ppi.csv', encoding='gbk') print 'all done'
def cpi(): return ts.get_cpi()
#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() ppi = ts.get_ppi() df = ts.shibor_data() #取当前年份的数据 #df = ts.shibor_data(2014) #取2014年的数据 df.sort('date', ascending=False).head(10) df = ts.shibor_quote_data() #取当前年份的数据 #df = ts.shibor_quote_data(2014) #取2014年的数据 df.sort('date', ascending=False).head(10) #shibo均值 df = ts.shibor_ma_data() #取当前年份的数据 #df = ts.shibor_ma_data(2014) #取2014年的数据 df.sort('date', ascending=False).head(10)
""" # @File : macro_eco_data.py # @Author: Chen Zhen # python version: 3.7 # @Date : 2020/3/28 # @Desc : use tushare to crawl maro economics data """ import tushare as ts print(ts.get_cpi()) # 得到cpi数据 print(ts.get_hist_data('600519')) # 得到茅台的股票交易数据
def getCPI(self): file_name = 'cpi.csv' path = self.index + self.index_cpi + file_name data = ts.get_cpi() data.to_csv(path, encoding='utf-8') print(file_name)
def Get_cpi(self): dt = ts.get_cpi() dt.to_csv('居民消费价格指数cpi.csv') print(dt)
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("下载工业品出厂价格指数出错")