Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改了宏观经济接口的API,让宏观经济接口可以正常工作。并且增加了获取进出口总额以及固定资产投资量的接口 #1386

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions test/macro_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# -*- coding:utf-8 -*-
# -*- coding:utf-8 -*-
'''
Created on 2015/3/14
Created on 2015/01/24
@author: Jimmy Liu

Modified on 2020/06/28
@contributor: Cole ZHANG
@email: longzonejazz@gmail.com
'''
import unittest
import tushare.stock.macro as fd
Expand Down Expand Up @@ -29,9 +33,6 @@ def test_get_cpi(self):
def test_get_ppi(self):
print(fd.get_ppi())

def test_get_deposit_rate(self):
print(fd.get_deposit_rate())

def test_get_loan_rate(self):
print(fd.get_loan_rate())

Expand All @@ -43,8 +44,13 @@ def test_get_money_supply(self):

def test_get_money_supply_bal(self):
print(fd.get_money_supply_bal())

def test_get_total_import_export(self):
print(fd.get_total_import_export())

def test_get_industry_fixed_investment(self):
print(fd.get_industry_fixed_investment())


if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
unittest.main()
4 changes: 3 additions & 1 deletion tushare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
get_ppi, get_deposit_rate,
get_loan_rate, get_rrr,
get_money_supply, get_money_supply_bal,
get_gold_and_foreign_reserves)
get_gold_and_foreign_reserves,
get_industry_fixed_investment,
get_total_import_export)

"""
for classifying data
Expand Down
134 changes: 100 additions & 34 deletions tushare/stock/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
@author: Jimmy Liu
@group : waditu
@contact: jimmysoa@sina.cn

Modified on 2020/06/28
@contributor: Cole ZHANG
@email: longzonejazz@gmail.com
"""

import pandas as pd
Expand Down Expand Up @@ -248,21 +252,23 @@ def get_deposit_rate():
deposit_type :存款种类
rate:利率(%)
"""
rdint = vs.random()
request = Request(vs.MACRO_URL%(vs.P_TYPE['http'], vs.DOMAINS['sina'],
rdint, vs.MACRO_TYPE[2], 2, 600,
rdint))
text = urlopen(request, timeout=10).read()
text = text.decode('gbk')
regSym = re.compile(r'\,count:(.*?)\}')
datastr = regSym.findall(text)
datastr = datastr[0]
datastr = datastr.split('data:')[1]
js = json.loads(datastr)
df = pd.DataFrame(js, columns=vs.DEPOSIT_COLS)
for i in df.columns:
df[i] = df[i].apply(lambda x:np.where(x is None, '--', x))
return df
return "get_deposit_rate is unavailable"

# rdint = vs.random()
# request = Request(vs.MACRO_URL%(vs.P_TYPE['http'], vs.DOMAINS['sina'],
# rdint, vs.MACRO_TYPE[2], 2, 600,
# rdint))
# text = urlopen(request, timeout=10).read()
# text = text.decode('gbk') if ct.PY3 else text
# regSym = re.compile(r'\,count:(.*?)\}')
# datastr = regSym.findall(text)
# datastr = datastr[0]
# datastr = datastr.split('data:')[1]
# js = json.loads(datastr)
# df = pd.DataFrame(js, columns=vs.DEPOSIT_COLS)
# for i in df.columns:
# df[i] = df[i].apply(lambda x:np.where(x is None, '--', x))
# return df


def get_loan_rate():
Expand All @@ -275,21 +281,22 @@ def get_loan_rate():
loan_type :存款种类
rate:利率(%)
"""
rdint = vs.random()
request = Request(vs.MACRO_URL%(vs.P_TYPE['http'], vs.DOMAINS['sina'],
rdint, vs.MACRO_TYPE[2], 3, 800,
rdint))
text = urlopen(request, timeout=10).read()
text = text.decode('gbk')
regSym = re.compile(r'\,count:(.*?)\}')
datastr = regSym.findall(text)
datastr = datastr[0]
datastr = datastr.split('data:')[1]
js = json.loads(datastr)
df = pd.DataFrame(js, columns=vs.LOAN_COLS)
for i in df.columns:
df[i] = df[i].apply(lambda x:np.where(x is None, '--', x))
return df
return "get_loan_rate is unavailable"
# rdint = vs.random()
# request = Request(vs.MACRO_URL%(vs.P_TYPE['http'], vs.DOMAINS['sina'],
# rdint, vs.MACRO_TYPE[2], 3, 800,
# rdint))
# text = urlopen(request, timeout=10).read()
# text = text.decode('gbk') if ct.PY3 else text
# regSym = re.compile(r'\,count:(.*?)\}')
# datastr = regSym.findall(text)
# datastr = datastr[0]
# datastr = datastr.split('data:')[1]
# js = json.loads(datastr)
# df = pd.DataFrame(js, columns=vs.LOAN_COLS)
# for i in df.columns:
# df[i] = df[i].apply(lambda x:np.where(x is None, '--', x))
# return df


def get_rrr():
Expand All @@ -308,7 +315,7 @@ def get_rrr():
rdint, vs.MACRO_TYPE[2], 4, 100,
rdint))
text = urlopen(request, timeout=10).read()
text = text.decode('gbk')
text = text.decode('gbk') if ct.PY3 else text
regSym = re.compile(r'\,count:(.*?)\}')
datastr = regSym.findall(text)
datastr = datastr[0]
Expand Down Expand Up @@ -349,7 +356,7 @@ def get_money_supply():
rdint, vs.MACRO_TYPE[2], 1, 600,
rdint))
text = urlopen(request, timeout=10).read()
text = text.decode('gbk')
text = text.decode('gbk') if ct.PY3 else text
regSym = re.compile(r'\,count:(.*?)\}')
datastr = regSym.findall(text)
datastr = datastr[0]
Expand Down Expand Up @@ -382,7 +389,7 @@ def get_money_supply_bal():
rdint, vs.MACRO_TYPE[2], 0, 200,
rdint))
text = urlopen(request,timeout=10).read()
text = text.decode('gbk')
text = text.decode('gbk') if ct.PY3 else text
regSym = re.compile(r'\,count:(.*?)\}')
datastr = regSym.findall(text)
datastr = datastr[0]
Expand All @@ -409,7 +416,7 @@ def get_gold_and_foreign_reserves():
rdint, vs.MACRO_TYPE[2], 5, 200,
rdint))
text = urlopen(request,timeout=10).read()
text = text.decode('gbk')
text = text.decode('gbk') if ct.PY3 else text
regSym = re.compile(r'\,count:(.*?)\}')
datastr = regSym.findall(text)
datastr = datastr[0]
Expand All @@ -419,3 +426,62 @@ def get_gold_and_foreign_reserves():
for i in df.columns:
df[i] = df[i].apply(lambda x: np.where(x is None, '--', x))
return df


def get_total_import_export():
"""
获取进出口总额
Returns
-------
DataFrame
year:统计时间
total:进出口总额(美元亿元)
export:出口总额(美元亿元)
import:进口总额(美元亿元)
delta:差额(美元亿元)
"""
rdint = vs.random()
request = Request(vs.MACRO_URL % (vs.P_TYPE['http'], vs.DOMAINS['sina'],
rdint, vs.MACRO_TYPE[3], 0, 400,
rdint))
text = urlopen(request, timeout=10).read()
text = text.decode('gbk')
regSym = re.compile(r'\,count:(.*?)\}')
datastr = regSym.findall(text)
datastr = datastr[0]
datastr = datastr.split(u'\'美元\':')[1]
datastr = datastr.replace("\"", "")
js = json.loads(datastr)
df = pd.DataFrame(js, columns=vs.TOTAL_IMPORT_EXPORT)
for i in df.columns:
df[i] = df[i].apply(lambda x: np.where(x is None, '--', x))
return df


def get_industry_fixed_investment():
"""
获取行业固投完成情况
Returns
-------
DataFrame
month:统计时间
industry:行业
amount:固定资产投资完成额(亿元)
increment:固定资产投资完成额同比增长(%)
"""
rdint = vs.random()
request = Request(vs.MACRO_URL % (vs.P_TYPE['http'], vs.DOMAINS['sina'],
rdint, vs.MACRO_TYPE[4], 10, 10000,
rdint))
text = urlopen(request, timeout=10).read()
text = text.decode('gbk')
regSym = re.compile(r'\,count:(.*?)\}')
datastr = regSym.findall(text)
datastr = datastr[0]
datastr = datastr.split('data:')[1]
js = json.loads(datastr)
df = pd.DataFrame(js, columns=vs.INDUSTRY_FIXED_INVESTMENT)
for i in df.columns:
df[i] = df[i].apply(lambda x: np.where(x is None, '--', x))

return df
6 changes: 4 additions & 2 deletions tushare/stock/macro_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

P_TYPE = {'http':'http://','ftp':'ftp://'}
DOMAINS = {'sina':'sina.com.cn','sinahq':'sinajs.cn','ifeng':'ifeng.com'}
MACRO_TYPE = ['nation','price','fininfo']
MACRO_URL = '%smoney.finance.%s/mac/api/jsonp.php/SINAREMOTECALLCALLBACK%s/MacPage_Service.get_pagedata?cate=%s&event=%s&from=0&num=%s&condition=&_=%s'
MACRO_TYPE = ['nation','price','fininfo','foreign','fixed']
MACRO_URL = '%squotes.%s/mac/api/jsonp_v3.php/SINAREMOTECALLCALLBACK%s/MacPage_Service.get_pagedata?cate=%s&event=%s&from=0&num=%s&condition=&_=%s'
GDP_YEAR_COLS = ['year','gdp','pc_gdp','gnp','pi','si','industry','cons_industry','ti','trans_industry','lbdy']
GDP_QUARTER_COLS = ['quarter','gdp','gdp_yoy','pi','pi_yoy','si','si_yoy','ti','ti_yoy']
GDP_FOR_COLS = ['year','end_for','for_rate','asset_for','asset_rate','goods_for','goods_rate']
Expand All @@ -17,6 +17,8 @@
MONEY_SUPPLY_COLS = ['month','m2','m2_yoy','m1','m1_yoy','m0','m0_yoy','cd','cd_yoy','qm','qm_yoy','ftd','ftd_yoy','sd','sd_yoy','rests','rests_yoy']
MONEY_SUPPLY_BLA_COLS = ['year','m2','m1','m0','cd','qm','ftd','sd','rests']
GOLD_AND_FOREIGN_CURRENCY_RESERVES = ['month','gold','foreign_reserves']
TOTAL_IMPORT_EXPORT = ['year', 'total', 'export', 'import', 'delta']
INDUSTRY_FIXED_INVESTMENT = ['month', 'industry', 'amount', 'increment']

def random(n=13):
from random import randint
Expand Down