本文整理汇总了Python中pytdx.hq.TdxHq_API.connect方法的典型用法代码示例。如果您正苦于以下问题:Python TdxHq_API.connect方法的具体用法?Python TdxHq_API.connect怎么用?Python TdxHq_API.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pytdx.hq.TdxHq_API
的用法示例。
在下文中一共展示了TdxHq_API.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: api
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def api(retry_count=3):
for _ in range(retry_count):
try:
api = TdxHq_API(heartbeat=True)
api.connect(ct._get_server(), ct.T_PORT)
except Exception as e:
print(e)
else:
return api
raise IOError(ct.NETWORK_URL_ERROR_MSG)
示例2: __call__
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def __call__(self):
count = 0
connect = sqlite3.connect(self.sqlitefile)
try:
progress = ProgressBar(self)
api = TdxHq_API()
api.connect(self.ip, self.port)
count = import_time(connect, self.market, self.quotations, api,
self.dest_dir, max_days=self.max_days, progress=progress)
except Exception as e:
print(e)
finally:
connect.commit()
connect.close()
self.queue.put([self.task_name, self.market, 'TIME', None, count])
示例3: QA_fetch_get_stock_xdxr
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def QA_fetch_get_stock_xdxr(code, ip=None, port=None):
'除权除息'
global best_ip
if ip is None and port is None and best_ip['stock']['ip'] is None and best_ip['stock']['port'] is None:
best_ip = select_best_ip()
ip = best_ip['stock']['ip']
port = best_ip['stock']['port']
elif ip is None and port is None and best_ip['stock']['ip'] is not None and best_ip['stock']['port'] is not None:
ip = best_ip['stock']['ip']
port = best_ip['stock']['port']
else:
pass
api = TdxHq_API()
market_code = _select_market_code(code)
with api.connect(ip, port):
category = {
'1': '除权除息', '2': '送配股上市', '3': '非流通股上市', '4': '未知股本变动', '5': '股本变化',
'6': '增发新股', '7': '股份回购', '8': '增发新股上市', '9': '转配股上市', '10': '可转债上市',
'11': '扩缩股', '12': '非流通股缩股', '13': '送认购权证', '14': '送认沽权证'}
data = api.to_df(api.get_xdxr_info(market_code, code))
if len(data) >= 1:
data = data\
.assign(date=pd.to_datetime(data[['year', 'month', 'day']]))\
.drop(['year', 'month', 'day'], axis=1)\
.assign(category_meaning=data['category'].apply(lambda x: category[str(x)]))\
.assign(code=str(code))\
.rename(index=str, columns={'panhouliutong': 'liquidity_after',
'panqianliutong': 'liquidity_before', 'houzongguben': 'shares_after',
'qianzongguben': 'shares_before'})\
.set_index('date', drop=False, inplace=False)
return data.assign(date=data['date'].apply(lambda x: str(x)[0:10]))
else:
return None
示例4: _get_bars
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def _get_bars(self, market, code, ktype):
data = []
tdx_market = self._trans_market(market)
if tdx_market is None:
print("tdx_market == None")
return data
tdx_ktype = self._trans_ktype(ktype)
if tdx_ktype is None:
print("tdx_ktype == None")
return data
try:
ip = self.getParam('ip')
port = self.getParam('port')
except:
ip = '119.147.212.81'
port = 7709
api = TdxHq_API(raise_exception=True)
with api.connect(ip, port):
if (market == 'SH' and code[:3] == '000') \
or (market == 'SZ' and code[:2] == '39'):
for i in range(self._max[ktype]):
data += api.get_index_bars(tdx_ktype, tdx_market, code,
(self._max[ktype]-1-i)*800,800)
else:
for i in range(self._max[ktype]):
data += api.get_security_bars(tdx_ktype, tdx_market, code,
(self._max[ktype]-1-i)*800,800)
return data
示例5: QA_fetch_get_stock_realtime
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def QA_fetch_get_stock_realtime(code=['000001', '000002'], ip=best_ip, port=7709):
api = TdxHq_API()
__data = pd.DataFrame()
code = [code] if type(code) is str else code
async def _get_security_quotes(__code):
"""
"""
assert isinstance(__code, str)
data=await api.get_security_quotes([(__select_market_code(__code), __code)])
data=api.to_df(data)
data['datetime'] = datetime.datetime.now()
return data[['datetime', 'code', 'open', 'high', 'low', 'price']]
with api.connect(ip, port):
coroutines = [asyncio.ensure_future(
_get_security_quotes(code_)) for code_ in code]
print(coroutines)
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
res = loop.run_until_complete(asyncio.gather(*coroutines))
print(res)
return res
示例6: QA_fetch_get_stock_block
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def QA_fetch_get_stock_block(ip=None, port=None):
'板块数据'
global best_ip
if ip is None and port is None and best_ip['stock']['ip'] is None and best_ip['stock']['port'] is None:
best_ip = select_best_ip()
ip = best_ip['stock']['ip']
port = best_ip['stock']['port']
elif ip is None and port is None and best_ip['stock']['ip'] is not None and best_ip['stock']['port'] is not None:
ip = best_ip['stock']['ip']
port = best_ip['stock']['port']
else:
pass
api = TdxHq_API()
with api.connect(ip, port):
data = pd.concat([api.to_df(api.get_and_parse_block_info("block_gn.dat")).assign(type='gn'),
api.to_df(api.get_and_parse_block_info(
"block.dat")).assign(type='yb'),
api.to_df(api.get_and_parse_block_info(
"block_zs.dat")).assign(type='zs'),
api.to_df(api.get_and_parse_block_info("block_fg.dat")).assign(type='fg')])
if len(data) > 10:
return data.assign(source='tdx').drop(['block_type', 'code_index'], axis=1).set_index('code', drop=False, inplace=False).drop_duplicates()
else:
QA_util_log_info('Wrong with fetch block ')
示例7: QA_fetch_get_stock_transaction_realtime
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def QA_fetch_get_stock_transaction_realtime(code, ip=None, port=None):
'实时分笔成交 包含集合竞价 buyorsell 1--sell 0--buy 2--盘前'
global best_ip
if ip is None and port is None and best_ip['stock']['ip'] is None and best_ip['stock']['port'] is None:
best_ip = select_best_ip()
ip = best_ip['stock']['ip']
port = best_ip['stock']['port']
elif ip is None and port is None and best_ip['stock']['ip'] is not None and best_ip['stock']['port'] is not None:
ip = best_ip['stock']['ip']
port = best_ip['stock']['port']
else:
pass
api = TdxHq_API()
try:
with api.connect(ip, port):
data = pd.DataFrame()
data = pd.concat([api.to_df(api.get_transaction_data(
_select_market_code(str(code)), code, (2 - i) * 2000, 2000)) for i in range(3)], axis=0)
if 'value' in data.columns:
data = data.drop(['value'], axis=1)
data = data.dropna()
day = datetime.date.today()
return data.assign(date=str(day)).assign(datetime=pd.to_datetime(data['time'].apply(lambda x: str(day) + ' ' + str(x))))\
.assign(code=str(code)).assign(order=range(len(data.index))).set_index('datetime', drop=False, inplace=False)
except:
return None
示例8: QA_fetch_get_index_min
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def QA_fetch_get_index_min(code, start, end, level='1min', ip=best_ip, port=7709):
api = TdxHq_API()
type_ = ''
if str(level) in ['5', '5m', '5min', 'five']:
level, type_ = 0, '5min'
elif str(level) in ['1', '1m', '1min', 'one']:
level, type_ = 8, '1min'
elif str(level) in ['15', '15m', '15min', 'fifteen']:
level, type_ = 1, '15min'
elif str(level) in ['30', '30m', '30min', 'half']:
level, type_ = 2, '30min'
elif str(level) in ['60', '60m', '60min', '1h']:
level, type_ = 3, '60min'
with api.connect(ip, port):
if str(code)[0] in ['5', '1']: # ETF
data = pd.concat([api.to_df(api.get_security_bars(
level, 1 if str(code)[0] in ['0', '8', '9', '5'] else 0, code, (25 - i) * 800, 800)) for i in range(26)], axis=0)
else:
data = pd.concat([api.to_df(api.get_index_bars(
level, 1 if str(code)[0] in ['0', '8', '9', '5'] else 0, code, (25 - i) * 800, 800)) for i in range(26)], axis=0)
data = data\
.assign(datetime=pd.to_datetime(data['datetime']), code=str(code))\
.drop(['year', 'month', 'day', 'hour', 'minute'], axis=1, inplace=False)\
.assign(date=data['datetime'].apply(lambda x: str(x)[0:10]))\
.assign(date_stamp=data['datetime'].apply(lambda x: QA_util_date_stamp(x)))\
.assign(time_stamp=data['datetime'].apply(lambda x: QA_util_time_stamp(x)))\
.assign(type=type_).set_index('datetime', drop=False, inplace=False)[start:end]
# data
return data.assign(datetime=data['datetime'].apply(lambda x: str(x)))
示例9: QA_fetch_get_index_day
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def QA_fetch_get_index_day(code, start_date, end_date, level='day', ip=best_ip, port=7709):
'指数日线'
api = TdxHq_API()
if level in ['day', 'd', 'D', 'DAY', 'Day']:
level = 9
elif level in ['w', 'W', 'Week', 'week']:
level = 5
elif level in ['month', 'M', 'm', 'Month']:
level = 6
elif level in ['Q', 'Quarter', 'q']:
level = 10
elif level in ['y', 'Y', 'year', 'Year']:
level = 11
with api.connect(ip, port):
if str(code)[0] in ['5', '1']: # ETF
data = pd.concat([api.to_df(api.get_security_bars(
level, 1 if str(code)[0] in ['0', '8', '9', '5'] else 0, code, (25 - i) * 800, 800)) for i in range(26)], axis=0)
else:
data = pd.concat([api.to_df(api.get_index_bars(
level, 1 if str(code)[0] in ['0', '8', '9', '5'] else 0, code, (25 - i) * 800, 800)) for i in range(26)], axis=0)
data = data.assign(date=data['datetime'].apply(lambda x: str(x[0:10]))).assign(code=str(code))\
.assign(date_stamp=data['datetime'].apply(lambda x: QA_util_date_stamp(str(x)[0:10])))\
.set_index('date', drop=False, inplace=False)\
.drop(['year', 'month', 'day', 'hour',
'minute', 'datetime'], axis=1)[start_date:end_date]
return data.assign(date=data['date'].apply(lambda x: str(x)[0:10]))
示例10: ping
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def ping(ip):
__time1 = datetime.datetime.now()
api = TdxHq_API()
try:
with api.connect(ip, 7709):
if len(api.get_security_list(0, 1)) > 800:
return datetime.datetime.now() - __time1
except:
return datetime.timedelta(9, 9, 0)
示例11: ping
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def ping(ip):
api = TdxHq_API()
__time1 = datetime.datetime.now()
try:
with api.connect(ip, 7709):
if len(api.get_security_list(0, 1)) > 800:
return datetime.datetime.now() - __time1
except:
print('Bad REPSONSE %s' % ip)
return datetime.timedelta(9, 9, 0)
示例12: QA_fetch_get_index_min
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def QA_fetch_get_index_min(code, start, end, frequence='1min', ip=None, port=None):
'指数分钟线'
global best_ip
if ip is None and port is None and best_ip['stock']['ip'] is None and best_ip['stock']['port'] is None:
best_ip = select_best_ip()
ip = best_ip['stock']['ip']
port = best_ip['stock']['port']
elif ip is None and port is None and best_ip['stock']['ip'] is not None and best_ip['stock']['port'] is not None:
ip = best_ip['stock']['ip']
port = best_ip['stock']['port']
else:
pass
api = TdxHq_API()
type_ = ''
start_date = str(start)[0:10]
today_ = datetime.date.today()
lens = QA_util_get_trade_gap(start_date, today_)
if str(frequence) in ['5', '5m', '5min', 'five']:
frequence, type_ = 0, '5min'
lens = 48 * lens
elif str(frequence) in ['1', '1m', '1min', 'one']:
frequence, type_ = 8, '1min'
lens = 240 * lens
elif str(frequence) in ['15', '15m', '15min', 'fifteen']:
frequence, type_ = 1, '15min'
lens = 16 * lens
elif str(frequence) in ['30', '30m', '30min', 'half']:
frequence, type_ = 2, '30min'
lens = 8 * lens
elif str(frequence) in ['60', '60m', '60min', '1h']:
frequence, type_ = 3, '60min'
lens = 4 * lens
if lens > 20800:
lens = 20800
with api.connect(ip, port):
if str(code)[0] in ['5', '1']: # ETF
data = pd.concat([api.to_df(api.get_security_bars(
frequence, 1 if str(code)[0] in ['0', '8', '9', '5'] else 0, code, (int(lens / 800) - i) * 800, 800)) for i in range(int(lens / 800) + 1)], axis=0)
else:
data = pd.concat([api.to_df(api.get_index_bars(
frequence, 1 if str(code)[0] in ['0', '8', '9', '5'] else 0, code, (int(lens / 800) - i) * 800, 800)) for i in range(int(lens / 800) + 1)], axis=0)
data = data\
.assign(datetime=pd.to_datetime(data['datetime']), code=str(code))\
.drop(['year', 'month', 'day', 'hour', 'minute'], axis=1, inplace=False)\
.assign(code=code)\
.assign(date=data['datetime'].apply(lambda x: str(x)[0:10]))\
.assign(date_stamp=data['datetime'].apply(lambda x: QA_util_date_stamp(x)))\
.assign(time_stamp=data['datetime'].apply(lambda x: QA_util_time_stamp(x)))\
.assign(type=type_).set_index('datetime', drop=False, inplace=False)[start:end]
# data
return data.assign(datetime=data['datetime'].apply(lambda x: str(x)))
示例13: QA_fetch_get_stock_realtime
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def QA_fetch_get_stock_realtime(code=['000001', '000002'], ip=best_ip, port=7709):
api = TdxHq_API()
__data = pd.DataFrame()
with api.connect(ip, port):
code = [code] if type(code) is str else code
for id_ in range(int(len(code) / 80) + 1):
__data = __data.append(api.to_df(api.get_security_quotes(
[(__select_market_code(x), x) for x in code[80 * id_:80 * (id_ + 1)]])))
__data['datetime'] = datetime.datetime.now()
data = __data[['datetime', 'code', 'open', 'high', 'low', 'price']]
return data.set_index('code', drop=False, inplace=False)
示例14: QA_fetch_get_stock_latest
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def QA_fetch_get_stock_latest(code, ip=best_ip, port=7709):
code = [code] if isinstance(code, str) else code
api = TdxHq_API(multithread=True)
with api.connect(ip, port):
data = pd.concat([api.to_df(api.get_security_bars(
9, __select_market_code(item), item, 0, 1)).assign(code=item) for item in code], axis=0)
return data\
.assign(date=pd.to_datetime(data['datetime']
.apply(lambda x: x[0:10])), date_stamp=data['datetime']
.apply(lambda x: QA_util_date_stamp(str(x[0:10]))))\
.set_index('date', drop=False)\
.drop(['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1)
示例15: _test_speed
# 需要导入模块: from pytdx.hq import TdxHq_API [as 别名]
# 或者: from pytdx.hq.TdxHq_API import connect [as 别名]
def _test_speed(self, ip, port=7709):
api = TdxHq_API(raise_exception=True, auto_retry=False)
_time = datetime.datetime.now()
try:
with api.connect(ip, port, time_out=0.05):
if len(api.get_security_list(0, 1)) > 800:
return (datetime.datetime.now() - _time).total_seconds()
else:
return datetime.timedelta(9, 9, 0).total_seconds()
except Exception as e:
return datetime.timedelta(9, 9, 0).total_seconds()