本文整理汇总了Python中tushare.util.dateu.today函数的典型用法代码示例。如果您正苦于以下问题:Python today函数的具体用法?Python today怎么用?Python today使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了today函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: top_detail
def top_detail(code, date=None, retry_count= 3, pause= 0.001):
"""
根据指定日期,指定股票的龙虎榜交易细节
Parameters
--------
code: string
指定股票代码
date: string
指定日期
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
Return
--------
code:代码
name:名称
bamount:累积买入额(万)
bcount:买入次数
samount:累积卖出额(万)
scount:卖出次数
net:净额(万)
"""
if code is None or code == "":
return None
if date is None:
if du.get_hour() < 18:
date = du.last_tddate()
else:
date = du.today()
else:
if(du.is_holiday(date)):
return None
time.sleep(pause)
try:
request = Request(rv.LHB_SINA_URL2%(ct.P_TYPE['http'], ct.DOMAINS['vsf'], code, date))
text = urlopen(request, timeout=10).read()
text = text.decode('GBK')
text = text.split('details=')[1][2:-2]
text = eval(text, type('Dummy', (dict,),
dict(__getitem__ = lambda s, n:n))())
text = json.dumps(text)
text = json.loads(text)
buy_df = pd.DataFrame(text['buy'], columns=rv.LHB_TMP_DETAIL_COLS)
buy_df['type'] = 'buy'
sell_df = pd.DataFrame(text['sell'], columns=rv.LHB_TMP_DETAIL_COLS)
sell_df['type'] = 'sell'
#LHB_DETAIL_COLS = ['code', 'type', 'insCode', 'insName', 'bamount', 'samount', 'net']
df = sell_df.append(buy_df)
df.columns = rv.LHB_DETAIL_COLS
df['bamount'] = df['bamount'].astype(float)
df['samount'] = df['samount'].astype(float)
df['net'] = df['net'].astype(float)
df['date'] = date
except Exception as e:
print e
else:
return df
raise IOError(ct.NETWORK_URL_ERROR_MSG)
示例2: sh_margins
def sh_margins(start=None, end=None, retry_count=3, pause=0.001):
"""
获取沪市融资融券数据列表
Parameters
--------
start:string
开始日期 format:YYYY-MM-DD 为空时取去年今日
end:string
结束日期 format:YYYY-MM-DD 为空时取当前日期
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
Return
------
DataFrame
opDate:信用交易日期
rzye:本日融资余额(元)
rzmre: 本日融资买入额(元)
rqyl: 本日融券余量
rqylje: 本日融券余量金额(元)
rqmcl: 本日融券卖出量
rzrqjyzl:本日融资融券余额(元)
"""
start = du.today_last_year() if start is None else start
end = du.today() if end is None else end
if du.diff_day(start, end) < 0:
return None
start, end = start.replace("-", ""), end.replace("-", "")
data = pd.DataFrame()
ct._write_head()
df = _sh_hz(data, start=start, end=end, retry_count=retry_count, pause=pause)
return df
示例3: top_list
def top_list(date = None, retry_count=3, pause=0.001):
"""
获取每日龙虎榜列表
Parameters
--------
date:string
明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
Return
------
DataFrame
code:代码
name :名称
pchange:涨跌幅
amount:龙虎榜成交额(万)
buy:买入额(万)
bratio:占总成交比例
sell:卖出额(万)
sratio :占总成交比例
reason:上榜原因
date :日期
"""
if not date:
if du.get_hour() < 18:
date = du.last_tddate()
else:
date = du.today()
else:
if(du.is_holiday(date)):
return None
for _ in range(retry_count):
time.sleep(pause)
try:
request = Request(rv.LHB_URL%(ct.P_TYPE['http'], ct.DOMAINS['em'], date))
text = urlopen(request, timeout=10).read()
text = text.decode('GBK')
html = lxml.html.parse(StringIO(text))
res = html.xpath("//table[@id=\"dt_1\"]")
sarr = [etree.tostring(node).decode('utf-8') for node in res]
sarr = ''.join(sarr)
df = pd.read_html(sarr)[0]
df.columns = [i for i in range(1,12)]
df = df.apply(_f_rows, axis=1)
df = df.fillna(method='ffill')
df = df.drop([1, 4], axis=1)
df.columns = rv.LHB_COLS
df = df.drop_duplicates()
df['code'] = df['code'].astype(int)
df['code'] = df['code'].map(lambda x: str(x).zfill(6))
df['date'] = date
except:
pass
else:
return df
raise IOError(ct.NETWORK_URL_ERROR_MSG)
示例4: CalculateRealTimeMA
def CalculateRealTimeMA(stocknumber,n):
'''
计算实时均价,包含实时现价作为当天收盘价,使用前复权。开盘前不要用,建议只开盘时使用
stocknumber 股票代码 类型int
n 几日均价 类型int
'''
nowtime = int(time.strftime('%H',time.localtime(time.time())))
today = du.today()
if du.is_holiday(today) or nowtime > 16 or nowtime < 9:
return CalculateMA(stocknumber,n)
else:
price = ts.get_realtime_quotes(str(stocknumber))['price'].values
behandma = CalculateMA(stocknumber,n-1)
for i in price:
c = behandma*(n-1) + float(i)
return float(c)/n
示例5: sz_margins
def sz_margins(start=None, end=None, retry_count=3, pause=0.001):
"""
获取深市融资融券数据列表
Parameters
--------
start:string
开始日期 format:YYYY-MM-DD 默认为上一周的今天
end:string
结束日期 format:YYYY-MM-DD 默认为今日
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
Return
------
DataFrame
opDate:信用交易日期(index)
rzmre: 融资买入额(元)
rzye:融资余额(元)
rqmcl: 融券卖出量
rqyl: 融券余量
rqye: 融券余量(元)
rzrqye:融资融券余额(元)
"""
data = pd.DataFrame()
if start is None and end is None:
end = du.today()
start = du.day_last_week()
if start is None or end is None:
ct._write_msg(rv.MAR_SZ_HZ_MSG2)
return None
try:
date_range = pd.date_range(start=start, end=end, freq='B')
if len(date_range)>261:
ct._write_msg(rv.MAR_SZ_HZ_MSG)
else:
ct._write_head()
for date in date_range:
data = data.append(_sz_hz(str(date.date()), retry_count, pause) )
except:
ct._write_msg(ct.DATA_INPUT_ERROR_MSG)
else:
return data
示例6: day_boxoffice
def day_boxoffice(date=None, retry_count=3, pause=0.001):
"""
获取单日电影票房数据
数据来源:EBOT艺恩票房智库
Parameters
------
date:日期,默认为上一日
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
return
-------
DataFrame
AvgPrice 平均票价
AvpPeoPle 场均人次
BoxOffice 单日票房(万)
BoxOffice_Up 环比变化 (%)
IRank 排名
MovieDay 上映天数
MovieName 影片名
SumBoxOffice 累计票房(万)
WomIndex 口碑指数
"""
for _ in range(retry_count):
time.sleep(pause)
try:
if date is None:
date = 0
else:
date = int(du.diff_day(du.today(), date)) + 1
request = Request(ct.BOXOFFICE_DAY%(ct.P_TYPE['http'], ct.DOMAINS['mbox'],
ct.BOX, date, _random()))
lines = urlopen(request, timeout = 10).read()
if len(lines) < 15: #no data
return None
except Exception as e:
print(e)
else:
js = json.loads(lines.decode('utf-8') if ct.PY3 else lines)
df = pd.DataFrame(js['data1'])
df = df.drop(['MovieImg', 'BoxOffice1', 'MovieID', 'Director', 'IRank_pro'], axis=1)
return df
示例7: get_today_ticks
def get_today_ticks(code=None, retry_count=3, pause=0.001):
"""
获取当日分笔明细数据
Parameters
------
code:string
股票代码 e.g. 600848
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
return
-------
DataFrame 当日所有股票交易数据(DataFrame)
属性:成交时间、成交价格、价格变动,成交手、成交金额(元),买卖类型
"""
if code is None or len(code)!=6 :
return None
symbol = _code_to_symbol(code)
date = du.today()
for _ in range(retry_count):
time.sleep(pause)
try:
request = Request(ct.TODAY_TICKS_PAGE_URL % (ct.P_TYPE['http'], ct.DOMAINS['vsf'],
ct.PAGES['jv'], date,
symbol))
data_str = urlopen(request, timeout=10).read()
data_str = data_str.decode('GBK')
data_str = data_str[1:-1]
data_str = eval(data_str, type('Dummy', (dict,),
dict(__getitem__ = lambda s, n:n))())
data_str = json.dumps(data_str)
data_str = json.loads(data_str)
pages = len(data_str['detailPages'])
data = pd.DataFrame()
ct._write_head()
for pNo in range(1, pages+1):
data = data.append(_today_ticks(symbol, date, pNo,
retry_count, pause), ignore_index=True)
except Exception as er:
print(str(er))
else:
return data
raise IOError(ct.NETWORK_URL_ERROR_MSG)
示例8: get_nav_history
def get_nav_history(code, start=None, end=None, retry_count=3, pause=0.001, timeout=10):
'''
获取历史净值数据
Parameters
------
code:string
基金代码 e.g. 000001
start:string
开始日期 format:YYYY-MM-DD 为空时取当前日期
end:string
结束日期 format:YYYY-MM-DD 为空时取去年今日
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
timeout: int 默认 10s
请求大量数据时的网络超时
return
-------
DataFrame
date 发布日期 (index)
value 基金净值(股票/混合/QDII型基金) / 年华收益(货币/债券基金)
total 累计净值(股票/混合/QDII型基金) / 万分收益(货币/债券基金)
change 净值增长率(股票/混合/QDII型基金)
'''
start = du.today_last_year() if start is None else start
end = du.today() if end is None else end
# 判断基金类型
ismonetary = False # 是否是债券型和货币型基金
df_fund = get_fund_info(code)
fund_type = df_fund.ix[0]['Type2Name']
if (fund_type.find(u'债券型') != -1) or (fund_type.find(u'货币型') != -1):
ismonetary = True
ct._write_head()
nums = _get_nav_histroy_num(code, start, end, ismonetary)
data = _parse_nav_history_data(
code, start, end, nums, ismonetary, retry_count, pause, timeout)
return data
示例9: lhb_daily_detail
def lhb_daily_detail(code, date, type):
"""
获取个股龙虎榜买5和卖5
:param code: 股票代码
:param date: 日期 yyyy-MM-dd
:param type: SINA龙虎榜类型
:return:
"""
if (date is None):
date = du.today()
request = Request(LHB_SINA_URL % (code, date, type))
text = urlopen(request, timeout=10).read()
text = text.decode('GBK')
text = text.splitlines()[1]
ctxt = pyv8.JSContext()
ctxt.enter()
index = text.find('{') - 1
text = text[index:-1]
temp = ctxt.eval(text)
data = []
date_columns = ['SYMBOL', 'buyAmount', 'sellAmount', 'netAmount', 'comCode', 'comName']
for a in temp.buy:
tempdata = []
for k in date_columns:
tempdata.append(a[k])
tempdata.append('b')
data.append(tempdata)
for a in temp.sell:
tempdata = []
for k in date_columns:
tempdata.append(a[k])
tempdata.append('s')
data.append(tempdata)
df = pd.DataFrame(data, columns=LHB_SINA_COLUMNS)
df['date'] = date
return df
示例10:
import os
import sys
from tushare.util import dateu as du
if "__main__" == __name__:
if du.is_holiday(du.today()):
pass
else:
ret = os.system('/home/work/anaconda/bin/python ./download_daily_data.py -p >>logs/daily.log 2>>logs/daily.err')
if ret != 0:
print >> sys.stderr, 'daily error with exit code %d'%ret
else:
ret = os.system('/home/work/anaconda/bin/python ./hfq2qfq.py -p >>logs/fq.log 2>>logs/fq.err')
if ret != 0:
print >> sys.stderr, 'fq error with exit code %d'%ret
示例11: get_h_data
def get_h_data(code, start=None, end=None, autype='qfq',
index=False, retry_count=3, pause=0.001):
'''
获取历史复权数据
Parameters
------
code:string
股票代码 e.g. 600848
start:string
开始日期 format:YYYY-MM-DD 为空时取当前日期
end:string
结束日期 format:YYYY-MM-DD 为空时取去年今日
autype:string
复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
return
-------
DataFrame
date 交易日期 (index)
open 开盘价
high 最高价
close 收盘价
low 最低价
volume 成交量
amount 成交金额
'''
start = du.today_last_year() if start is None else start
end = du.today() if end is None else end
qs = du.get_quarts(start, end)
qt = qs[0]
ct._write_head()
data = _parse_fq_data(_get_index_url(index, code, qt), index,
retry_count, pause)
if len(qs)>1:
for d in range(1, len(qs)):
qt = qs[d]
ct._write_console()
df = _parse_fq_data(_get_index_url(index, code, qt), index,
retry_count, pause)
data = data.append(df, ignore_index=True)
if len(data) == 0 or len(data[(data.date>=start)&(data.date<=end)]) == 0:
return None
data = data.drop_duplicates('date')
if index:
data = data[(data.date>=start) & (data.date<=end)]
data = data.set_index('date')
data = data.sort_index(ascending=False)
return data
if autype == 'hfq':
data = data.drop('factor', axis=1)
data = data[(data.date>=start) & (data.date<=end)]
for label in ['open', 'high', 'close', 'low']:
data[label] = data[label].map(ct.FORMAT)
data[label] = data[label].astype(float)
data = data.set_index('date')
data = data.sort_index(ascending = False)
return data
else:
if autype == 'qfq':
data = data.drop('factor', axis=1)
df = _parase_fq_factor(code, start, end)
df = df.drop_duplicates('date')
df = df.sort('date', ascending=False)
frow = df.head(1)
rt = get_realtime_quotes(code)
if rt is None:
return None
if ((float(rt['high']) == 0) & (float(rt['low']) == 0)):
preClose = float(rt['pre_close'])
else:
if du.is_holiday(du.today()):
preClose = float(rt['price'])
else:
if (du.get_hour() > 9) & (du.get_hour() < 18):
preClose = float(rt['pre_close'])
else:
preClose = float(rt['price'])
rate = float(frow['factor']) / preClose
data = data[(data.date >= start) & (data.date <= end)]
for label in ['open', 'high', 'low', 'close']:
data[label] = data[label] / rate
data[label] = data[label].map(ct.FORMAT)
data[label] = data[label].astype(float)
data = data.set_index('date')
data = data.sort_index(ascending = False)
return data
else:
for label in ['open', 'high', 'close', 'low']:
data[label] = data[label] / data['factor']
data = data.drop('factor', axis=1)
data = data[(data.date>=start) & (data.date<=end)]
for label in ['open', 'high', 'close', 'low']:
data[label] = data[label].map(ct.FORMAT)
data = data.set_index('date')
data = data.sort_index(ascending=False)
#.........这里部分代码省略.........
示例12: get_k_data
def get_k_data(code=None, start='', end='',
ktype='D', autype='qfq',
index=False,
retry_count=3,
pause=0.001):
"""
获取k线数据
---------
Parameters:
code:string
股票代码 e.g. 600848
start:string
开始日期 format:YYYY-MM-DD 为空时取上市首日
end:string
结束日期 format:YYYY-MM-DD 为空时取最近一个交易日
autype:string
复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
ktype:string
数据类型,D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟,默认为D
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
return
-------
DataFrame
date 交易日期 (index)
open 开盘价
high 最高价
close 收盘价
low 最低价
volume 成交量
amount 成交额
turnoverratio 换手率
code 股票代码
"""
symbol = ct.INDEX_SYMBOL[code] if index else _code_to_symbol(code)
url = ''
dataflag = ''
autype = '' if autype is None else autype
if (start is not None) & (start != ''):
end = du.today() if end is None or end == '' else end
if ktype.upper() in ct.K_LABELS:
fq = autype if autype is not None else ''
if code[:1] in ('1', '5') or index:
fq = ''
kline = '' if autype is None else 'fq'
if (start is None or start == '') & (end is None or end == ''):
urls = [ct.KLINE_TT_URL%(ct.P_TYPE['http'], ct.DOMAINS['tt'],
kline, fq, symbol,
ct.TT_K_TYPE[ktype.upper()], start, end,
fq, _random(17))]
else:
years = du.tt_dates(start, end)
urls = []
for year in years:
startdate = str(year) + '-01-01'
enddate = str(year+1) + '-12-31'
url = ct.KLINE_TT_URL%(ct.P_TYPE['http'], ct.DOMAINS['tt'],
kline, fq+str(year), symbol,
ct.TT_K_TYPE[ktype.upper()], startdate, enddate,
fq, _random(17))
urls.append(url)
dataflag = '%s%s'%(fq, ct.TT_K_TYPE[ktype.upper()])
elif ktype in ct.K_MIN_LABELS:
urls = [ct.KLINE_TT_MIN_URL%(ct.P_TYPE['http'], ct.DOMAINS['tt'],
symbol, ktype, ktype,
_random(16))]
dataflag = 'm%s'%ktype
else:
raise TypeError('ktype input error.')
data = pd.DataFrame()
for url in urls:
data = data.append(_get_k_data(url, dataflag,
symbol, code,
index, ktype,
retry_count, pause),
ignore_index=True)
if ktype not in ct.K_MIN_LABELS:
if ((start is not None) & (start != '')) & ((end is not None) & (end != '')):
if data.empty==False:
data = data[(data.date >= start) & (data.date <= end)]
return data
raise IOError(ct.NETWORK_URL_ERROR_MSG)
示例13: get_h_data
def get_h_data(code, start=None, end=None, autype='qfq',
retry_count=3, pause=0.001):
'''
获取历史复权数据
Parameters
------
code:string
股票代码 e.g. 600848
start:string
开始日期 format:YYYY-MM-DD 为空时取当前日期
end:string
结束日期 format:YYYY-MM-DD 为空时取去年今日
autype:string
复权类型,qfq-前复权 hfq-后复权 None-不复权,默认为qfq
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
return
-------
DataFrame
date 交易日期 (index)
open 开盘价
high 最高价
close 收盘价
low 最低价
volumn 成交量
amount 成交金额
'''
start = du.today_last_year() if start is None else start
end = du.today() if end is None else end
qs = du.get_quarts(start, end)
qt = qs[0]
print ct.FQ_PRINTING%(qt[0], qt[1])
data = _parse_fq_data(ct.HIST_FQ_URL%(ct.P_TYPE['http'], ct.DOMAINS['vsf'],
code, qt[0], qt[1]), retry_count, pause)
if len(qs)>1:
for d in range(1, len(qs)):
qt = qs[d]
print ct.FQ_PRINTING%(qt[0], qt[1])
url = ct.HIST_FQ_URL%(ct.P_TYPE['http'], ct.DOMAINS['vsf'],
code, qt[0], qt[1])
df = _parse_fq_data(url, retry_count, pause)
data = data.append(df, ignore_index=True)
data = data.drop_duplicates('date')
if start is not None:
data = data[data.date>=start]
if end is not None:
data = data[data.date<=end]
if autype == 'hfq':
data = data.drop('factor', axis=1)
for label in ['open', 'high', 'close', 'low']:
data[label] = data[label].map(ct.FORMAT)
data = data.set_index('date')
data = data.sort_index(ascending=False)
return data
else:
for label in ['open', 'high', 'close', 'low']:
data[label] = data[label] / data['factor']
data = data.drop('factor', axis=1)
if autype == 'qfq':
df = _parase_fq_factor(code, start, end)
df = df.drop_duplicates('date')
df = df[df.date>=start]
df = df[df.date<=end]
df = pd.merge(data, df)
df = df.sort('date', ascending=False)
frow = df.head(1)
rate = float(frow['close']) / float(frow['factor'])
df['close_temp'] = df['close']
df['close'] = rate * df['factor']
for label in ['open', 'high', 'low']:
df[label] = df[label] * (df['close'] / df['close_temp'])
df[label] = df[label].map(ct.FORMAT)
df = df.drop(['factor', 'close_temp'], axis=1)
df['close'] = df['close'].map(ct.FORMAT)
df = df.set_index('date')
df = df.sort_index(ascending=False)
return df
else:
for label in ['open', 'high', 'close', 'low']:
data[label] = data[label].map(ct.FORMAT)
data = data.set_index('date')
data = data.sort_index(ascending=False)
return data
示例14: top_list
def top_list(date = None, retry_count=3, pause=0.001):
"""
获取每日龙虎榜列表
Parameters
--------
date:string
明细数据日期 format:YYYY-MM-DD 如果为空,返回最近一个交易日的数据
retry_count : int, 默认 3
如遇网络等问题重复执行的次数
pause : int, 默认 0
重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题
Return
------
DataFrame
code:代码
name :名称
pchange:涨跌幅
amount:龙虎榜成交额(万)
buy:买入额(万)
bratio:占总成交比例
sell:卖出额(万)
sratio :占总成交比例
reason:上榜原因
date :日期
"""
if date is None:
if du.get_hour() < 18:
date = du.last_tddate()
else:
date = du.today()
else:
if(du.is_holiday(date)):
return None
for _ in range(retry_count):
time.sleep(pause)
try:
request = Request(rv.LHB_URL%(ct.P_TYPE['http'], ct.DOMAINS['em'], date, date))
text = urlopen(request, timeout=10).read()
text = text.decode('GBK')
text = text.split('_1=')[1]
text = eval(text, type('Dummy', (dict,),
dict(__getitem__ = lambda s, n:n))())
text = json.dumps(text)
text = json.loads(text)
df = pd.DataFrame(text['data'], columns=rv.LHB_TMP_COLS)
df.columns = rv.LHB_COLS
df['buy'] = df['buy'].astype(float)
df['sell'] = df['sell'].astype(float)
df['amount'] = df['amount'].astype(float)
df['Turnover'] = df['Turnover'].astype(float)
df['bratio'] = df['buy'] / df['Turnover']
df['sratio'] = df['sell'] /df['Turnover']
df['bratio'] = df['bratio'].map(ct.FORMAT)
df['sratio'] = df['sratio'].map(ct.FORMAT)
df['date'] = date
for col in ['amount', 'buy', 'sell']:
df[col] = df[col].astype(float)
df[col] = df[col] / 10000
df[col] = df[col].map(ct.FORMAT)
df = df.drop('Turnover', axis=1)
except:
pass
else:
return df
raise IOError(ct.NETWORK_URL_ERROR_MSG)
示例15: tick
def tick(code, conn=None, date='', asset='E', market='', retry_count = 3):
"""
tick数据
Parameters:
------------
code:证券代码,支持股票,ETF/LOF,期货/期权,港股
conn:服务器连接 ,通过ts.api()或者ts.xpi()获得
date:日期
asset:证券品种,E:沪深交易所股票和基金, INDEX:沪深交易所指数, X:其他证券品种,大致如下:
支持的扩展行情包括(asset='X'):
郑州商品期权 OZ 大连商品期权 OD 上海商品期权 OS
上海个股期权 QQ 香港指数 FH 郑州商品 QZ 大连商品 QD 上海期货 QS
香港主板 KH 香港权证 KR 开放式基金 FU 货币型基金 FB
招商理财产品 LC 招商货币产品 LB 国际指数 FW 国内宏观指标 HG 中国概念股 CH
美股知名公司 MG B股转H股 HB 股份转让 SB 股指期货 CZ 香港创业板 KG 香港信托基金 KT
国债预发行 GY 主力期货合约 MA
中证指数 ZZ 港股通 GH
market:市场代码,通过ts.get_markets()获取
Return
----------
DataFrame
date:日期
time:时间
price:成交价
vol:成交量
type:买卖方向,0-买入 1-卖出 2-集合竞价成交
期货 0:开仓 1:多开 -1:空开
期货多一列数据oi_change:增仓数据
"""
code = code.strip().upper()
date = int(date.replace('-', ''))
today = int(str(du.today()).replace('-', ''))
for _ in range(retry_count):
try:
if conn is None:
print(ct.MSG_NOT_CONNECTED)
return None
api, xapi = conn
data = pd.DataFrame()
mkcode = _get_mkcode(code, asset=asset, xapi=xapi) if market == '' else market
con = api if asset in['E', 'INDEX'] else xapi
for i in range(200):
if date == today:
ds = con.get_transaction_data(market=mkcode, code=code, start=i * 300, count=300)
else:
ds = con.get_history_transaction_data(market=mkcode, code=code, date=date, start=i * 300, count=300)
df = api.to_df(ds)
data = data.append(df) if i == 0 else df.append(data, ignore_index=True)
if len(ds) < 300:
break
if asset in['E', 'INDEX']:
data['date'] = date
data['date'] = data['date'].map(lambda x: '%s-%s-%s '%(str(x)[0:4], str(x)[4:6], str(x)[6:8]))
data['datetime'] = data['date'] + data['time']
data = data[['datetime', 'price', 'vol', 'buyorsell']]
data.columns = ['datetime', 'price', 'vol', 'type']
else:
if mkcode in [31, 71]:
if date == today:
data = data.drop(['hour', 'minute', 'nature_name', 'zengcang', 'direction',
'second', 'nature_mark', 'nature_value'], axis=1)
else:
data = data.drop(['hour', 'minute', 'nature_name', 'zengcang', 'direction'], axis=1)
data.loc[data.nature== 512, 'nature' ] = 2
data.loc[data.nature== 256, 'nature' ] = 1
data = data.sort_values('date')
data.columns = ['date', 'price', 'vol', 'type']
elif mkcode in [28, 29, 30, 47, 60]:
if date == today:
data = data.drop(['hour', 'minute', 'nature', 'direction',
'second', 'nature_mark', 'nature_value'], axis=1)
else:
data = data.drop(['hour', 'minute', 'nature', 'direction'], axis=1)
data.columns = ['date', 'price', 'vol', 'oi_change', 'type']
else:
data = data.drop(['hour', 'minute', 'nature_name', 'zengcang', 'direction', 'nature'], axis=1)
except Exception as e:
print(e)
else:
return data