示例#1
0
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')
示例#2
0
def call_money_supply():
    '''
    返回值说明:

    month :统计时间
    m2 :货币和准货币(广义货币M2)(亿元)
    m2_yoy:货币和准货币(广义货币M2)同比增长(%)
    m1:货币(狭义货币M1)(亿元)
    m1_yoy:货币(狭义货币M1)同比增长(%)
    m0:流通中现金(M0)(亿元)
    m0_yoy:流通中现金(M0)同比增长(%)
    cd:活期存款(亿元)
    cd_yoy:活期存款同比增长(%)
    qm:准货币(亿元)
    qm_yoy:准货币同比增长(%)
    ftd:定期存款(亿元)
    ftd_yoy:定期存款同比增长(%)
    sd:储蓄存款(亿元)
    sd_yoy:储蓄存款同比增长(%)
    rests:其他存款(亿元)
    rests_yoy:其他存款同比增长(%)

    '''
    df = ts.get_money_supply()

    return [df.loc[0, 'm0'], df.loc[0, 'm1'], df.loc[0, 'm2']]
示例#3
0
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)
示例#4
0
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同比增长(%)
示例#5
0
 def setMoneySupply(self,
                    isSave=False,
                    tableName=MACROECONOMIC_MONEY_SUPPLY):
     df = ts.get_money_supply()
     if isSave is True:
         df.to_sql(tableName, self.engine_sql, if_exists='append')
     return df
示例#6
0
def get_money_supply_info():
    df = ts.get_money_supply()
    if df is not None:
        res = df.to_sql(microE_money_supply, engine, if_exists='replace')
        msg = 'ok' if res is None else res
        print('获取货币供应量: ' + msg + '\n')
    else:
        print('获取货币供应量: ' + 'None' + '\n')
示例#7
0
文件: dbutil.py 项目: nfsli926/stock
def get_money_supply():
    try:
        df = ts.get_money_supply()
        engine = create_engine('mysql://*****:*****@127.0.0.1/stock?charset=utf8')
        df.to_sql('money_supply', engine, if_exists='append')
        print "message"
    except Exception, e:
        e.message
示例#8
0
文件: m2.py 项目: zArche/ceco
def get_last_12_month_m2_changes():
    df = ts.get_money_supply()
    df = df[0:12]

    k = [i for i in df['month']]
    v = [float(i) for i in df['m2_yoy']]
    k.reverse()
    v.reverse()

    return [k, v]
示例#9
0
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()
示例#10
0
文件: stock.py 项目: ccfr32/www
 def return_money_supply(self):
     '''
     货币供应量
     '''
     df = ts.get_money_supply()
     detail = {}
     for col in df.columns:
         lt = df[col].values.tolist()
         lt.reverse()
         detail[col] = lt
     self.reply(detail=detail)
示例#11
0
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`")
示例#12
0
def add_money_supply_aage(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)

    # 画货币供应量
    money_supply = ts.get_money_supply().replace('--', np.nan)
    money_supply['date'] = money_supply.apply(
        lambda x: stdMonthDate2ISO(x['month']), axis=1)

    # 画货币量曲线图
    m0 = extract_point_from_df_date_x(money_supply, 'date', 'm0')
    m1 = extract_point_from_df_date_x(money_supply, 'date', 'm1')
    m2 = extract_point_from_df_date_x(money_supply, 'date', 'm2')

    data_supply = [tuple(m0), tuple(m1), tuple(m2)]
    data_supply_note = ['m0', 'm1', 'm2']

    money_drawing = gen_lp_drawing(data=data_supply,
                                   data_note=data_supply_note,
                                   height=letter[1] * 0.2)
    renderPDF.draw(drawing=money_drawing, canvas=c, x=10, y=letter[1] * 0.7)

    # 画货币量增长率曲线图
    m0_yoy = extract_point_from_df_date_x(money_supply, 'date', 'm0_yoy')
    m1_yoy = extract_point_from_df_date_x(money_supply, 'date', 'm1_yoy')
    m2_yoy = extract_point_from_df_date_x(money_supply, 'date', 'm2_yoy')

    data_supply_yoy = [tuple(m0_yoy), tuple(m1_yoy), tuple(m2_yoy)]
    data_supply_yoy_note = ['m0增长率', 'm1增长率', 'm2增长率']

    money_yoy_drawing = gen_lp_drawing(data=data_supply_yoy,
                                       data_note=data_supply_yoy_note,
                                       height=letter[1] * 0.2)
    renderPDF.draw(drawing=money_yoy_drawing,
                   canvas=c,
                   x=10,
                   y=letter[1] * 0.4)

    c.showPage()

    return c
示例#13
0
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')
示例#14
0
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
示例#15
0
文件: TsPy.py 项目: sodunux/DataPy
 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()
示例#16
0
    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)
示例#17
0
    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()
示例#18
0
def get_money_supply():
    """得到货币供应量"""
    logger.info('Begin get MoneySupply.')
    try:
        data_df = ts.get_money_supply()
    except Exception as e:
        logger.exception('Error get MoneySupply.')
        return None
    else:
        data_dicts = []
        if data_df.empty:
            logger.warn('Empty get MoneySupply.')
        else:
            data_dicts = [{'month': row[0], 'm2': row[1],
                           'm2_yoy': row[2], 'm1': row[3], 'm1_yoy': row[4],
                           'm0': row[5], 'm0_yoy': row[6], 'cd': row[7], 'cd_yoy': row[8],
                           'qm': row[9], 'qm_yoy': row[10], 'ftd': row[11], 'ftd_yoy': row[12],
                           'sd':row[13], 'sd_yoy':row[14], 'rests': row[15], 'rests_yoy': row[16]}
                          for row in data_df.values]
            logger.info('Success get MoneySupply.')
        return data_dicts
示例#19
0
 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)
示例#20
0
def getMoneySupply(ann_dt_label):
    raw_data = ts.get_money_supply()
    year_month_label = 'year_month'
    raw_data = raw_data.rename({'month': year_month_label}, axis=1)

    raw_data.loc[:, 'year'] = raw_data[year_month_label].apply(
        lambda x: x.split('.')[0]).astype('int')
    raw_data.loc[:, 'month'] = raw_data[year_month_label].apply(
        lambda x: x.split('.')[1]).astype('int')
    raw_data = raw_data.drop(year_month_label, axis=1)

    # ========= change datatype
    raw_data = chgDataType(raw_data, ['year', 'month'])

    # ========= change column names
    raw_data = raw_data.rename({'qm': 'quasi_currency'}, axis=1)

    # ========== announcement date
    tmp_data = raw_data.loc[raw_data['month'] !=
                            12]  # announced month is 1 month later
    tmp_data.loc[:, 'announced_month'] = tmp_data['month'] + 1
    tmp_data.loc[:, 'announced_year'] = tmp_data['year']
    processed_data = tmp_data.copy()

    tmp_data = raw_data.loc[raw_data[
        'month'] == 12]  # December, adding 1 month = January in the next year
    tmp_data.loc[:, 'announced_month'] = 1
    tmp_data.loc[:, 'announced_year'] = tmp_data['year'] + 1
    processed_data = processed_data.append(tmp_data)

    processed_data.loc[:, ann_dt_label] = processed_data.apply(
        lambda x: datetime.strftime(
            datetime(int(x['announced_year']), int(x['announced_month']), 15),
            '%Y-%m-%d'),
        axis=1)
    processed_data = processed_data.drop(['announced_year', 'announced_month'],
                                         axis=1)

    return processed_data
示例#21
0
文件: TsPy.py 项目: sodunux/DataPy
 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()
示例#22
0
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')
示例#23
0
文件: opdata.py 项目: BWZX/opdata
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
示例#24
0
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")
示例#25
0
文件: macro.py 项目: wgb128/anack
def money_supply():
    return ts.get_money_supply()
示例#26
0
文件: m2.py 项目: ao-song/stock
 def download_m2(self):
     m2T = ts.get_money_supply()
     self.__m2 = m2T[m2T.month >= START_MONTH]
     self.__m2.to_csv(FILE_LOCATION, encoding='utf-8')
示例#27
0
def getmoneysupplydb():
	moneysupply = ts.get_money_supply()
	moneysupply.to_sql('moneysupply_data',ENGINE,if_exists='append')
示例#28
0
 def getMoneySupply(self):
     file_name = 'money_supply.csv'
     path = self.index + self.index_money_supply + file_name
     data = ts.get_money_supply()
     data.to_csv(path, encoding='utf-8')
     print(file_name)
示例#29
0
文件: mac.py 项目: ahtanwang/c_stock
#-*- coding: UTF-8 -*- 
import pylab
import pandas as pd    
import MySQLdb
import matplotlib.pyplot as plt
from sqlalchemy import create_engine
import tushare as ts
import numpy as np
import mylib as me

df = ts.get_money_supply() 
df['m1dm2'] = 0.0
df['fm2'] = 0.0
df['fm1'] = 0.0
df['num'] = int(0)

df2 = df.head(12 * 15)
for i in range(df2.index.size):
	m2 = float(df2.loc[i,'m2'])
	m1 = float(df2.loc[i,'m1'])
	df2.loc[i,'m1dm2'] = float(m1 / m2)
	df2.loc[i,'fm2'] = float(df2.loc[i,'m2_yoy'])
	df2.loc[i,'fm1'] = float(df2.loc[i,'m1_yoy'])
	df2.loc[i,'num'] = i

#df2.sort_values('num',  ascending = False)	
print df2.head()
	
df2.to_csv('..\\..\\data\\view\\money.txt')

示例#30
0
# encoding=utf-8
import matplotlib.pyplot as plt
import tushare as ts

from SDK.SDKHeader import *

money_supply_df = ts.get_money_supply()[0:30]
money_supply_df['month_std'] = money_supply_df.apply(lambda x:stdMonthDate(x['month']),axis=1)
money_supply_df = money_supply_df.sort_values(by='month_std',ascending=True)
sh_index = ts.get_hist_data('sh')


# trick to get the axes
fig, ax = plt.subplots(2,1)

# std month date str
month_date = list(map(lambda x:stdMonthDate(x),money_supply_df['month']))
x = range(0,len(money_supply_df['month']))
# plot data
ax[0].plot(x, list(map(lambda x:float(x),money_supply_df['m2_yoy'])), 'ro--', label='m2_yoy')
ax[0].plot(x, list(map(lambda x:float(x),money_supply_df['m1_yoy'])), 'b*--', label='m1_yoy')
ax[0].plot(x, list(map(lambda x:float(x),money_supply_df['m0_yoy'])), 'g*--', label='m0_yoy')
# ax[1].plot(sh_index.index,sh_index.open,'y*--',label='sh_index')

# make and set ticks and ticklabels
ticklabels_0 = month_date
ax[0].set_xticks(x)
ax[0].set_xticklabels(ticklabels_0,rotation=90)
ax[0].set_title('money_supply')
ax[0].legend(loc='best')
ax[0].grid()
示例#31
0
 def getMoneySupply(self):
     logging.debug("begin getMoneySupply")
     moneySupply=tushare.get_money_supply()
     self.saveDb(moneySupply,"money_supply")
     logging.debug("end getMoneySupply")
示例#32
0
#-*- coding: UTF-8 -*-
import pylab
import pandas as pd
import MySQLdb
import matplotlib.pyplot as plt
from sqlalchemy import create_engine
import tushare as ts
import numpy as np
import mylib as me
import mydef as md

global G_DBengine
G_DBengine = create_engine('mysql://*****:*****@127.0.0.1/mystock?charset=utf8')

df = ts.get_money_supply()
df['m1dm2'] = 0.0
df['fm2'] = 0.0
df['fm1'] = 0.0
df['num'] = int(0)

df2 = df.head(12 * 15)
for i in range(df2.index.size):
    m2 = float(df2.loc[i, 'm2'])
    m1 = float(df2.loc[i, 'm1'])
    df2.loc[i, 'm1dm2'] = float(m1 / m2)
    df2.loc[i, 'fm2'] = float(df2.loc[i, 'm2_yoy'])
    df2.loc[i, 'fm1'] = float(df2.loc[i, 'm1_yoy'])
    df2.loc[i, 'num'] = i

df2.sort_values('num', ascending=False)
print df2.head()
示例#33
0
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)
示例#34
0
 def Get_money_supply(self):
     dt = ts.get_money_supply()
     dt.to_csv('货币供应量.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("下载工业品出厂价格指数出错")