Skip to content

Commit

Permalink
0.6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmysoa committed Dec 4, 2016
1 parent 9bb6a2e commit fa5e837
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -181,6 +181,12 @@ Quick Start
Change Logs
-----------

0.6.2 2016/12/03
==========
- 新增十大股东和十大流通股接口 top10_holders
- 新增全球实时指数列表接口 global_realtime
- 修复部分bug

0.6.1 2016/11/22
===========
- 修正get_k_databug
Expand Down
10 changes: 7 additions & 3 deletions tushare/__init__.py
@@ -1,4 +1,4 @@
__version__ = '0.6.1'
__version__ = '0.6.2'
__author__ = 'Jimmy Liu'

"""
Expand Down Expand Up @@ -52,7 +52,8 @@
xsg_data, fund_holdings,
new_stocks, sh_margins,
sh_margin_details,
sz_margins, sz_margin_details)
sz_margins, sz_margin_details,
top10_holders)

"""
for shibor
Expand Down Expand Up @@ -100,4 +101,7 @@
"""
for futures API
"""
from tushare.futures.intlfutures import (get_intlfuture)
from tushare.futures.intlfutures import (get_intlfuture)


from tushare.stock.globals import (global_realtime)
18 changes: 17 additions & 1 deletion tushare/stock/cons.py
Expand Up @@ -27,7 +27,7 @@
'sse': 'www.sse.com.cn', 'szse': 'www.szse.cn',
'oss': '218.244.146.57', 'idxip':'115.29.204.48',
'shibor': 'www.shibor.org', 'mbox':'www.cbooo.cn',
'tt': 'gtimg.cn'}
'tt': 'gtimg.cn', 'gw': 'gw.com.cn'}
PAGES = {'fd': 'index.phtml', 'dl': 'downxls.php', 'jv': 'json_v2.php',
'cpt': 'newFLJK.php', 'ids': 'newSinaHy.php', 'lnews':'rollnews_ch_out_interface.php',
'ntinfo':'vCB_BulletinGather.php', 'hs300b':'000300cons.xls',
Expand Down Expand Up @@ -119,6 +119,8 @@
INDEX_COLS = ['code', 'name', 'change', 'open', 'preclose', 'close', 'high', 'low', 'volume', 'amount']
HIST_FQ_COLS = ['date', 'open', 'high', 'close', 'low', 'volume', 'amount', 'factor']
SINA_DD_COLS = ['code', 'name', 'time', 'price', 'volume', 'preprice', 'type']
GLOBAL_HQ_SYMBOL = 'sh000001,hkHSI,znb_UKX,znb_DAX,znb_INDEXCF,znb_CAC,znb_SMI,znb_FTSEMIB,znb_MADX,znb_OMX,znb_SPX,znb_HEX,znb_OSEAX,znb_ISEQ,znb_AEX,znb_ICEXI,znb_NKY,znb_TWSE,znb_FSSTI,znb_KOSPI,znb_FBMKLCI,znb_SET,znb_JCI,znb_PCOMP,znb_KSE100,znb_SENSEX,znb_VNINDEX,znb_CSEALL,znb_SASEIDX,znb_SPTSX,znb_MEXBOL,znb_IBOV,znb_MERVAL,znb_AS51,znb_NZSE50FG,znb_CASE,znb_JALSH,sz399001,znb_INDU,znb_CCMP'
GLOBAL_HQ_COLS = ['symbol', 'name', 'price', 'chga', 'chgp', 'datetime']
HIST_FQ_FACTOR_COLS = ['code','value']
DATA_GETTING_TIPS = '[Getting data:]'
DATA_GETTING_FLAG = '#'
Expand Down Expand Up @@ -274,3 +276,17 @@ def _check_lhb_input(last):
raise TypeError(LHB_MSG)
else:
return True


def _code_to_symbol(code):
"""
生成symbol代码标志
"""
if code in INDEX_LABELS:
return INDEX_LIST[code]
else:
if len(code) != 6 :
return ''
else:
return 'sh%s'%code if code[:1] in ['5', '6', '9'] else 'sz%s'%code

3 changes: 3 additions & 0 deletions tushare/stock/ref_vars.py
Expand Up @@ -18,6 +18,9 @@
MAR_SH_HZ_TAIL_URL = '&pageHelp.pageNo=%s&pageHelp.beginPage=%s&pageHelp.endPage=%s'
TERMINATED_URL = '%s%s/%s?jsonCallBack=jsonpCallback%s&isPagination=true&sqlId=COMMON_SSE_ZQPZ_GPLB_MCJS_ZZSSGGJBXX_L&pageHelp.pageSize=50&_=%s'
SUSPENDED_URL = '%s%s/%s?jsonCallBack=jsonpCallback%s&isPagination=true&sqlId=COMMON_SSE_ZQPZ_GPLB_MCJS_ZTSSGS_L&pageHelp.pageSize=50&_=%s'
TOP10_HOLDERS_URL = '%swebf10.%s/SDGD/SD%sGD%s.js'
TOP10_SUMM_COLS = ['quarter', 'amount', 'changed' ,'props']
TOP10_PER_COLS = ['quarter', 'name', 'hold', 'h_pro', 'sharetype', 'status']
TERMINATED_T_COLS = ['COMPANY_CODE', 'COMPANY_ABBR', 'LISTING_DATE', 'CHANGE_DATE']
LHB_KINDS = ['ggtj', 'yytj', 'jgzz', 'jgmx']
LHB_GGTJ_COLS = ['code', 'name', 'count', 'bamount', 'samount', 'net', 'bcount', 'scount']
Expand Down
59 changes: 55 additions & 4 deletions tushare/stock/reference.py
Expand Up @@ -9,7 +9,6 @@
from __future__ import division
from tushare.stock import cons as ct
from tushare.stock import ref_vars as rv
from tushare.util import dateu as dt
import pandas as pd
import time
import lxml.html
Expand All @@ -19,6 +18,8 @@
from pandas.compat import StringIO
from tushare.util import dateu as du
from tushare.util.netbase import Client
from bokeh.charts.builders.line_builder import Line
from _csv import Error

try:
from urllib.request import urlopen, Request
Expand Down Expand Up @@ -232,8 +233,8 @@ def xsg_data(year=None, month=None,
count:解禁数量(万股)
ratio:占总盘比率
"""
year = dt.get_year() if year is None else year
month = dt.get_month() if month is None else month
year = du.get_year() if year is None else year
month = du.get_month() if month is None else month
for _ in range(retry_count):
time.sleep(pause)
try:
Expand Down Expand Up @@ -690,9 +691,59 @@ def sz_margin_details(date='', retry_count=3, pause=0.001):
raise IOError(ct.NETWORK_URL_ERROR_MSG)


def top10_holders(code=None, year=None, quarter=None, gdtype='0',
retry_count=3, pause=0.001):
if code is None:
return None
else:
code = ct._code_to_symbol(code)
gdtype = 'LT' if gdtype == '1' else ''
qdate = ''
if (year is not None) & (quarter is not None):
qdate = du.get_q_date(year, quarter)
for _ in range(retry_count):
time.sleep(pause)
try:
request = Request(rv.TOP10_HOLDERS_URL%(ct.P_TYPE['http'], ct.DOMAINS['gw'],
gdtype, code.upper()))
lines = urlopen(request, timeout = 10).read()
lines = lines.decode('utf8') if ct.PY3 else lines
reg = re.compile(r'= \'\[(.*?)\]\';')
lines = reg.findall(lines)[0]
jss = json.loads('[%s]' %lines)
summ = []
data = pd.DataFrame()
for row in jss:
qt = row['jzrq']
hold = row['ljcy']
change = row['ljbh']
props = row['ljzb']
arow = [qt, hold, change ,props]
summ.append(arow)
ls = row['sdgdList']
dlist = []
for inrow in ls:
sharetype = inrow['gbxz']
name = inrow['gdmc']
hold = inrow['cgs']
h_pro = inrow['zzgs']
status = inrow['zjqk']
dlist.append([qt, name, hold, h_pro, sharetype, status])
ddata = pd.DataFrame(dlist, columns=rv.TOP10_PER_COLS)
data = data.append(ddata, ignore_index=True)
df = pd.DataFrame(summ, columns=rv.TOP10_SUMM_COLS)
if qdate != '':
df = df[df.quarter == qdate]
data = data[data.quarter == qdate]
except Exception as e:
print(e)
else:
return df, data
raise IOError(ct.NETWORK_URL_ERROR_MSG)


def _random(n=13):
from random import randint
start = 10**(n-1)
end = (10**n)-1
return str(randint(start, end))

16 changes: 15 additions & 1 deletion tushare/util/dateu.py
Expand Up @@ -56,6 +56,12 @@ def get_now():
return time.strftime('%Y-%m-%d %H:%M:%S')


def int2time(timestamp):
datearr = datetime.datetime.utcfromtimestamp(timestamp)
timestr = datearr.strftime("%Y-%m-%d %H:%M:%S")
return timestr


def diff_day(start=None, end=None):
d1 = datetime.datetime.strptime(end, '%Y-%m-%d')
d2 = datetime.datetime.strptime(start, '%Y-%m-%d')
Expand Down Expand Up @@ -109,4 +115,12 @@ def tt_dates(start='', end=''):
return dates



def _random(n=13):
from random import randint
start = 10**(n-1)
end = (10**n)-1
return str(randint(start, end))

def get_q_date(year=None, quarter=None):
dt = {'1': '-03-31', '2': '-06-30', '3': '-09-30', '4': '-12-31'}
return '%s%s'%(str(year), dt[str(quarter)])

0 comments on commit fa5e837

Please sign in to comment.