Skip to content

Commit

Permalink
Merge pull request waditu#272 from chuihero/master
Browse files Browse the repository at this point in the history
增加获取黄金和外汇储备,get_gold_and_foreign_reserves()
  • Loading branch information
jimmysoa committed Jan 17, 2017
2 parents 9005d27 + 29152f3 commit 30573d4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tushare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
get_gdp_contrib, get_cpi,
get_ppi, get_deposit_rate,
get_loan_rate, get_rrr,
get_money_supply, get_money_supply_bal)
get_money_supply, get_money_supply_bal,
get_gold_and_foreign_reserves)

"""
for classifying data
Expand Down
27 changes: 27 additions & 0 deletions tushare/stock/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,3 +392,30 @@ def get_money_supply_bal():
for i in df.columns:
df[i] = df[i].apply(lambda x:np.where(x is None, '--', x))
return df


def get_gold_and_foreign_reserves():
"""
获取外汇储备
Returns
-------
DataFrame
month :统计时间
gold:黄金储备(万盎司)
foreign_reserves:外汇储备(亿美元)
"""
rdint = vs.random()
request = Request(vs.MACRO_URL % (vs.P_TYPE['http'], vs.DOMAINS['sina'],
rdint, vs.MACRO_TYPE[2], 5, 200,
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.GOLD_AND_FOREIGN_CURRENCY_RESERVES)
for i in df.columns:
df[i] = df[i].apply(lambda x: np.where(x is None, '--', x))
return df
1 change: 1 addition & 0 deletions tushare/stock/macro_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
RRR_COLS = ['date','before','now','changed']
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']

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

0 comments on commit 30573d4

Please sign in to comment.