def QA_fetch_get_stock_info(code, ip=None, port=None): '股票基本信息' ip, port = get_mainmarket_ip(ip, port) api = TdxHq_API() market_code = _select_market_code(code) with api.connect(ip, port): return api.to_df(api.get_finance_info(market_code, code))
def test_getAPI(self): """pytdx connect """ from QUANTAXIS.QAUtil import QA_util_get_trade_gap from QUANTAXIS.QAFetch.base import _select_market_code code = '600000' frequence = 9 days = 365 * 1.2 start = datetime.datetime.now() - datetime.timedelta(days) end = datetime.datetime.now() - datetime.timedelta(0) api = TDX.tdxapi print(type(api)) if api.connect(TDX.ip, TDX.port, time_out=0.7): print(type(api)) start_date = str(start)[0:10] today_ = datetime.date.today() lens = QA_util_get_trade_gap(start_date, today_) self.assertTrue(lens > 10, "时间间隔太短:{}".format(lens)) alist = [ api.to_df( api.get_security_bars(frequence, _select_market_code(code), code, (int(lens / 800) - i) * 800, 800)) for i in range(int(lens / 800) + 1) ] api.disconnect() data = pd.concat(alist, axis=0, sort=False) self.assertTrue(len(data) > 1, "返回值为空")
def QA_fetch_get_stock_analysis(code): """ 'zyfw', 主营范围 'jyps'#经营评述 'zygcfx' 主营构成分析 date 主营构成 主营收入(元) 收入比例cbbl 主营成本(元) 成本比例 主营利润(元) 利润比例 毛利率(%) 行业 /产品/ 区域 hq cp qy """ market = 'sh' if _select_market_code(code) == 1 else 'sz' null = 'none' data = eval(requests.get(BusinessAnalysis_url.format( market, code), headers=headers_em).text) zyfw = pd.DataFrame(data.get('zyfw', None)) jyps = pd.DataFrame(data.get('jyps', None)) zygcfx = data.get('zygcfx', []) temp = [] for item in zygcfx: try: data_ = pd.concat([pd.DataFrame(item['hy']).assign(date=item['rq']).assign(classify='hy'), pd.DataFrame(item['cp']).assign( date=item['rq']).assign(classify='cp'), pd.DataFrame(item['qy']).assign(date=item['rq']).assign(classify='qy')]) temp.append(data_) except: pass try: res_zyfcfx = pd.concat(temp).set_index( ['date', 'classify'], drop=False) except: res_zyfcfx = None return zyfw, jyps, res_zyfcfx
def QA_fetch_get_stock_day(code, start_date, end_date, if_fq='00', frequence='day', ip=None, port=None): """获取日线及以上级别的数据 Arguments: code {str:6} -- code 是一个单独的code 6位长度的str start_date {str:10} -- 10位长度的日期 比如'2017-01-01' end_date {str:10} -- 10位长度的日期 比如'2018-01-01' Keyword Arguments: if_fq {str} -- '00'/'bfq' -- 不复权 '01'/'qfq' -- 前复权 '02'/'hfq' -- 后复权 '03'/'ddqfq' -- 定点前复权 '04'/'ddhfq' --定点后复权 frequency {str} -- day/week/month/quarter/year 也可以是简写 D/W/M/Q/Y ip {str} -- [description] (default: None) ip可以通过select_best_ip()函数重新获取 port {int} -- [description] (default: {None}) Returns: pd.DataFrame/None -- 返回的是dataframe,如果出错比如只获取了一天,而当天停牌,返回None Exception: 如果出现网络问题/服务器拒绝, 会出现socket:time out 尝试再次获取/更换ip即可, 本函数不做处理 """ ip, port = get_mainmarket_ip(ip, port) api = TdxHq_API() with api.connect(ip, port, time_out=0.7): if frequence in ['day', 'd', 'D', 'DAY', 'Day']: frequence = 9 elif frequence in ['w', 'W', 'Week', 'week']: frequence = 5 elif frequence in ['month', 'M', 'm', 'Month']: frequence = 6 elif frequence in ['quarter', 'Q', 'Quarter', 'q']: frequence = 10 elif frequence in ['y', 'Y', 'year', 'Year']: frequence = 11 start_date = str(start_date)[0:10] today_ = datetime.date.today() lens = QA_util_get_trade_gap(start_date, today_) data = pd.concat([api.to_df(api.get_security_bars(frequence, _select_market_code( code), code, (int(lens / 800) - i) * 800, 800)) for i in range(int(lens / 800) + 1)], axis=0) # 这里的问题是: 如果只取了一天的股票,而当天停牌, 那么就直接返回None了 if len(data) < 1: return None data = data[data['open'] != 0] data = data.assign(date=data['datetime'].apply(lambda x: str(x[0:10])), code=str(code),\ date_stamp=data['datetime'].apply(lambda x: QA_util_date_stamp(str(x)[0:10])))\ .set_index('date', drop=False, inplace=False) data = data.drop(['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1)[start_date:end_date] if if_fq in ['00','bfq']: return data else: print('CURRENTLY NOT SUPPORT REALTIME FUQUAN') return None
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
def QA_fetch_get_security_bars(code, _type, lens, ip=best_ip['stock'], port=7709): """按bar长度推算数据 Arguments: code {[type]} -- [description] _type {[type]} -- [description] lens {[type]} -- [description] Keyword Arguments: ip {[type]} -- [description] (default: {best_ip}) port {[type]} -- [description] (default: {7709}) Returns: [type] -- [description] """ api = TdxHq_API() with api.connect(ip, port): data = pd.concat([api.to_df(api.get_security_bars(_select_type(_type), _select_market_code( code), code, (i - 1) * 800, 800)) for i in range(1, int(lens / 800) + 2)], 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).tail(lens) if data is not None: return data else: return None
def QA_fetch_get_stock_min(code, start, end, frequence='1min', ip=best_ip['stock'], port=7709): 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): data = pd.concat([api.to_df(api.get_security_bars(frequence, _select_market_code( str(code)), str(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(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] return data.assign(datetime=data['datetime'].apply(lambda x: str(x)))
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
def QA_fetch_get_stock_xdxr(code, ip=best_ip['stock'], port=7709): '除权除息' 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
def QA_fetch_get_stock_info(code, ip=best_ip['stock']['ip'], port=best_ip['stock']['port']): '股票基本信息' api = TdxHq_API() market_code = _select_market_code(code) with api.connect(ip, port): return api.to_df(api.get_finance_info(market_code, code))
def QA_fetch_get_stock_latest(code, ip=best_ip['stock'], 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)
def QA_fetch_depth_market_data(code=['000001', '000002'], ip=best_ip['stock'], 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', 'active1', 'active2', 'last_close', 'code', 'open', 'high', 'low', 'price', 'cur_vol', 's_vol', 'b_vol', 'vol', 'ask1', 'ask_vol1', 'bid1', 'bid_vol1', 'ask2', 'ask_vol2', 'bid2', 'bid_vol2', 'ask3', 'ask_vol3', 'bid3', 'bid_vol3', 'ask4', 'ask_vol4', 'bid4', 'bid_vol4', 'ask5', 'ask_vol5', 'bid5', 'bid_vol5']] return data.set_index(['datetime', 'code'], drop=False, inplace=False)
def QA_fetch_get_stock_info(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): return api.to_df(api.get_finance_info(market_code, code))
def getMin(cls, code, start, end, if_fq='00', frequence=8): """获取分钟级别的数据 Args: code: 代码 6位长度的str start: 10位长度的日期字符串 比如'2017-01-01' end: 10位长度的日期字符串 比如'2018-01-01' if_fq: frequence: Returns: pd.DataFrame/None -- 返回的是dataframe,如果出错比如只获 取了一天,而当天停牌,返回None """ # type_ = '' start = str(start)[0:10] today_ = datetime.date.today() lens = QA_util_get_trade_gap(start, today_) _, type_, multiplicator = cls.getReverseFrequence(frequence) lens = lens * multiplicator if lens > 20800: lens = 20800 with cls.tdxapi.connect(cls.ip, cls.port) as api: data = pd.concat([ api.to_df( api.get_security_bars( frequence, _select_market_code(str(code)), str(code), (int(lens / 800) - i) * 800, 800)) for i in range(int(lens / 800) + 1) ], axis=0, sort=False) data = data \ .drop(['year', 'month', 'day', 'hour', 'minute'], axis=1, inplace=False) \ .assign(datetime=pd.to_datetime(data['datetime']), code=str(code), date=data['datetime'].apply(lambda x: str(x)[0:10]), date_stamp=data['datetime'].apply( lambda x: QA_util_date_stamp(x)), time_stamp=data['datetime'].apply( lambda x: QA_util_time_stamp(x)), type=type_).set_index('datetime', drop=False, inplace=False)[start:end] return data.assign( datetime=data['datetime'].apply(lambda x: str(x)))
def QA_fetch_get_stock_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): data = pd.concat([api.to_df(api.get_security_bars(frequence, _select_market_code( str(code)), str(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(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] return data.assign(datetime=data['datetime'].apply(lambda x: str(x)))
def __QA_fetch_get_stock_transaction(code, day, retry, api): batch_size = 2000 # 800 or 2000 ? 2000 maybe also works data_arr = [] max_offset = 21 cur_offset = 0 while cur_offset <= max_offset: one_chunk = api.get_history_transaction_data( _select_market_code(str(code)), str(code), cur_offset * batch_size, batch_size, QA_util_date_str2int(day)) if one_chunk is None or one_chunk == []: break data_arr = one_chunk + data_arr cur_offset += 1 data_ = api.to_df(data_arr) for _ in range(retry): if len(data_) < 2: return __QA_fetch_get_stock_transaction(code, day, 0, api) else: return data_.assign(date=day).assign(datetime=pd.to_datetime(data_['time'].apply(lambda x: str(day) + ' ' + x)))\ .assign(code=str(code)).assign(order=range(len(data_.index))).set_index('datetime', drop=False, inplace=False)
def QA_fetch_get_stock_transaction_realtime(code, ip=best_ip['stock'], port=7709): '实时逐笔成交 包含集合竞价 buyorsell 1--sell 0--buy 2--盘前' api = TdxHq_API() 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)
def QA_fetch_get_security_bars(code, _type, lens, ip=None, port=None): """按bar长度推算数据 Arguments: code {[type]} -- [description] _type {[type]} -- [description] lens {[type]} -- [description] Keyword Arguments: ip {[type]} -- [description] (default: {best_ip}) port {[type]} -- [description] (default: {7709}) Returns: [type] -- [description] """ 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_security_bars(_select_type(_type), _select_market_code( code), code, (i - 1) * 800, 800)) for i in range(1, int(lens / 800) + 2)], 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).tail(lens) if data is not None: return data else: return None
def QA_fetch_get_stock_latest(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 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)
def QA_fetch_get_stock_realtime(code=['000001', '000002'], 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() __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', 'active1', 'active2', 'last_close', 'code', 'open', 'high', 'low', 'price', 'cur_vol', 's_vol', 'b_vol', 'vol', 'ask1', 'ask_vol1', 'bid1', 'bid_vol1', 'ask2', 'ask_vol2', 'bid2', 'bid_vol2', 'ask3', 'ask_vol3', 'bid3', 'bid_vol3', 'ask4', 'ask_vol4', 'bid4', 'bid_vol4', 'ask5', 'ask_vol5', 'bid5', 'bid_vol5']] return data.set_index('code', drop=False, inplace=False)
def QA_fetch_depth_market_data(code=['000001', '000002'], 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() __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', 'active1', 'active2', 'last_close', 'code', 'open', 'high', 'low', 'price', 'cur_vol', 's_vol', 'b_vol', 'vol', 'ask1', 'ask_vol1', 'bid1', 'bid_vol1', 'ask2', 'ask_vol2', 'bid2', 'bid_vol2', 'ask3', 'ask_vol3', 'bid3', 'bid_vol3', 'ask4', 'ask_vol4', 'bid4', 'bid_vol4', 'ask5', 'ask_vol5', 'bid5', 'bid_vol5']] return data.set_index(['datetime', 'code'], drop=False, inplace=False)
def QA_fetch_get_stock_day(code, start_date, end_date, if_fq='00', frequence='day', ip=None, port=None): """获取日线及以上级别的数据 Arguments: code {str:6} -- code 是一个单独的code 6位长度的str start_date {str:10} -- 10位长度的日期 比如'2017-01-01' end_date {str:10} -- 10位长度的日期 比如'2018-01-01' Keyword Arguments: if_fq {str} -- '00'/'bfq' -- 不复权 '01'/'qfq' -- 前复权 '02'/'hfq' -- 后复权 '03'/'ddqfq' -- 定点前复权 '04'/'ddhfq' --定点后复权 frequency {str} -- day/week/month/quarter/year 也可以是简写 D/W/M/Q/Y ip {str} -- [description] (default: None) ip可以通过select_best_ip()函数重新获取 port {int} -- [description] (default: {None}) Returns: pd.DataFrame/None -- 返回的是dataframe,如果出错比如只获取了一天,而当天停牌,返回None Exception: 如果出现网络问题/服务器拒绝, 会出现socket:time out 尝试再次获取/更换ip即可, 本函数不做处理 """ 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, time_out=0.7): if frequence in ['day', 'd', 'D', 'DAY', 'Day']: frequence = 9 elif frequence in ['w', 'W', 'Week', 'week']: frequence = 5 elif frequence in ['month', 'M', 'm', 'Month']: frequence = 6 elif frequence in ['quarter', 'Q', 'Quarter', 'q']: frequence = 10 elif frequence in ['y', 'Y', 'year', 'Year']: frequence = 11 start_date = str(start_date)[0:10] today_ = datetime.date.today() lens = QA_util_get_trade_gap(start_date, today_) data = pd.concat([api.to_df(api.get_security_bars(frequence, _select_market_code( code), code, (int(lens / 800) - i) * 800, 800)) for i in range(int(lens / 800) + 1)], axis=0) # 这里的问题是: 如果只取了一天的股票,而当天停牌, 那么就直接返回None了 if len(data) < 1: return None data = data[data['open'] != 0] if if_fq in ['00', 'bfq']: 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) return data.drop(['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1)[start_date:end_date].assign(date=data['date'].apply(lambda x: str(x)[0:10])) elif if_fq in ['01', 'qfq']: xdxr_data = QA_fetch_get_stock_xdxr(code) bfq_data = data.assign(date=pd.to_datetime(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) bfq_data = bfq_data.drop( ['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1) # if xdxr_data is not None: info = xdxr_data[xdxr_data['category'] == 1] bfq_data['if_trade'] = True data = pd.concat([bfq_data, info[['category']] [bfq_data.index[0]:]], axis=1) data['date'] = data.index data['if_trade'].fillna(value=False, inplace=True) data = data.fillna(method='ffill') data = pd.concat([data, info[['fenhong', 'peigu', 'peigujia', 'songzhuangu']][bfq_data.index[0]:]], axis=1) data = data.fillna(0) data['preclose'] = (data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia']) / (10 + data['peigu'] + data['songzhuangu']) data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1)[::-1].cumprod() data['open'] = data['open'] * data['adj'] data['high'] = data['high'] * data['adj'] data['low'] = data['low'] * data['adj'] data['close'] = data['close'] * data['adj'] data['preclose'] = data['preclose'] * data['adj'] data = data[data['if_trade']] return data.drop(['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1)[data['open'] != 0].assign(date=data['date'].apply(lambda x: str(x)[0:10]))[start_date:end_date] else: bfq_data['preclose'] = bfq_data['close'].shift(1) bfq_data['adj'] = 1 return bfq_data[start_date:end_date] elif if_fq in ['03', 'ddqfq']: xdxr_data = QA_fetch_get_stock_xdxr(code) info = xdxr_data[xdxr_data['category'] == 1] bfq_data = data\ .assign(date=pd.to_datetime(data['datetime'].apply(lambda x: 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) bfq_data['if_trade'] = True data = pd.concat([bfq_data, info[['category']] [bfq_data.index[0]:end_date]], axis=1) data['date'] = data.index data['if_trade'].fillna(value=False, inplace=True) data = data.fillna(method='ffill') data = pd.concat([data, info[['fenhong', 'peigu', 'peigujia', 'songzhuangu']][bfq_data.index[0]:end_date]], axis=1) data = data.fillna(0) data['preclose'] = (data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia']) / (10 + data['peigu'] + data['songzhuangu']) data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1)[::-1].cumprod() data['open'] = data['open'] * data['adj'] data['high'] = data['high'] * data['adj'] data['low'] = data['low'] * data['adj'] data['close'] = data['close'] * data['adj'] data['preclose'] = data['preclose'] * data['adj'] data = data[data['if_trade']] return data.drop(['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1)[data['open'] != 0].assign(date=data['date'].apply(lambda x: str(x)[0:10]))[start_date:end_date] elif if_fq in ['02', 'hfq']: xdxr_data = QA_fetch_get_stock_xdxr(code) info = xdxr_data[xdxr_data['category'] == 1] bfq_data = data\ .assign(date=pd.to_datetime(data['datetime'].apply(lambda x: 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) bfq_data['if_trade'] = True data = pd.concat([bfq_data, info[['category']] [bfq_data.index[0]:]], axis=1) data['date'] = data.index data['if_trade'].fillna(value=False, inplace=True) data = data.fillna(method='ffill') data = pd.concat([data, info[['fenhong', 'peigu', 'peigujia', 'songzhuangu']][bfq_data.index[0]:]], axis=1) data = data.fillna(0) data['preclose'] = (data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia']) / (10 + data['peigu'] + data['songzhuangu']) data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1).cumprod() data['open'] = data['open'] / data['adj'] data['high'] = data['high'] / data['adj'] data['low'] = data['low'] / data['adj'] data['close'] = data['close'] / data['adj'] data['preclose'] = data['preclose'] / data['adj'] data = data[data['if_trade']] return data.drop(['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1)[data['open'] != 0].assign(date=data['date'].apply(lambda x: str(x)[0:10]))[start_date:end_date] elif if_fq in ['04', 'ddhfq']: xdxr_data = QA_fetch_get_stock_xdxr(code) info = xdxr_data[xdxr_data['category'] == 1] bfq_data = data\ .assign(date=pd.to_datetime(data['datetime'].apply(lambda x: 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) bfq_data['if_trade'] = True data = pd.concat([bfq_data, info[['category']] [bfq_data.index[0]:end_date]], axis=1) data['date'] = data.index data['if_trade'].fillna(value=False, inplace=True) data = data.fillna(method='ffill') data = pd.concat([data, info[['fenhong', 'peigu', 'peigujia', 'songzhuangu']][bfq_data.index[0]:end_date]], axis=1) data = data.fillna(0) data['preclose'] = (data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia']) / (10 + data['peigu'] + data['songzhuangu']) data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1).cumprod() data['open'] = data['open'] / data['adj'] data['high'] = data['high'] / data['adj'] data['low'] = data['low'] / data['adj'] data['close'] = data['close'] / data['adj'] data['preclose'] = data['preclose'] / data['adj'] data = data[data['if_trade']] return data.drop(['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1)[data['open'] != 0].assign(date=data['date'].apply(lambda x: str(x)[0:10]))[start_date:end_date]
def QA_fetch_get_stock_day(code, start_date, end_date, if_fq='00', frequence='day', ip=best_ip['stock'], port=7709): """获取日线及以上级别的数据 Arguments: code {str:6} -- code 是一个单独的code 6位长度的str start_date {str:10} -- 10位长度的日期 比如'2017-01-01' end_date {str:10} -- 10位长度的日期 比如'2018-01-01' Keyword Arguments: if_fq {str} -- '00'/'bfq' -- 不复权 '01'/'qfq' -- 前复权 '02'/'hfq' -- 后复权 '03'/'ddqfq' -- 定点前复权 '04'/'ddhfq' --定点后复权 frequency {str} -- day/week/month/quarter/year 也可以是简写 D/W/M/Q/Y ip {str} -- [description] (default: best_ip['stock']) ip可以通过select_best_ip()函数重新获取 port {int} -- [description] (default: {7709}) Returns: pd.DataFrame/None -- 返回的是dataframe,如果出错比如只获取了一天,而当天停牌,返回None Exception: 如果出现网络问题/服务器拒绝, 会出现socket:time out 尝试再次获取/更换ip即可, 本函数不做处理 """ api = TdxHq_API() with api.connect(ip, port, time_out=0.7): if frequence in ['day', 'd', 'D', 'DAY', 'Day']: frequence = 9 elif frequence in ['w', 'W', 'Week', 'week']: frequence = 5 elif frequence in ['month', 'M', 'm', 'Month']: frequence = 6 elif frequence in ['quarter', 'Q', 'Quarter', 'q']: frequence = 10 elif frequence in ['y', 'Y', 'year', 'Year']: frequence = 11 start_date = str(start_date)[0:10] today_ = datetime.date.today() lens = QA_util_get_trade_gap(start_date, today_) data = pd.concat([api.to_df(api.get_security_bars(frequence, _select_market_code( code), code, (int(lens / 800) - i) * 800, 800)) for i in range(int(lens / 800) + 1)], axis=0) # 这里的问题是: 如果只取了一天的股票,而当天停牌, 那么就直接返回None了 if len(data) < 1: return None data = data[data['open'] != 0] if if_fq in ['00', 'bfq']: 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) return data.drop(['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1)[start_date:end_date].assign(date=data['date'].apply(lambda x: str(x)[0:10])) elif if_fq in ['01', 'qfq']: xdxr_data = QA_fetch_get_stock_xdxr(code) bfq_data = data.assign(date=pd.to_datetime(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) bfq_data = bfq_data.drop( ['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1) # if xdxr_data is not None: info = xdxr_data[xdxr_data['category'] == 1] bfq_data['if_trade'] = True data = pd.concat([bfq_data, info[['category']] [bfq_data.index[0]:]], axis=1) data['date'] = data.index data['if_trade'].fillna(value=False, inplace=True) data = data.fillna(method='ffill') data = pd.concat([data, info[['fenhong', 'peigu', 'peigujia', 'songzhuangu']][bfq_data.index[0]:]], axis=1) data = data.fillna(0) data['preclose'] = (data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia']) / (10 + data['peigu'] + data['songzhuangu']) data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1)[::-1].cumprod() data['open'] = data['open'] * data['adj'] data['high'] = data['high'] * data['adj'] data['low'] = data['low'] * data['adj'] data['close'] = data['close'] * data['adj'] data['preclose'] = data['preclose'] * data['adj'] data = data[data['if_trade']] return data.drop(['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1)[data['open'] != 0].assign(date=data['date'].apply(lambda x: str(x)[0:10]))[start_date:end_date] else: bfq_data['preclose'] = bfq_data['close'].shift(1) bfq_data['adj'] = 1 return bfq_data[start_date:end_date] elif if_fq in ['03', 'ddqfq']: xdxr_data = QA_fetch_get_stock_xdxr(code) info = xdxr_data[xdxr_data['category'] == 1] bfq_data = data\ .assign(date=pd.to_datetime(data['datetime'].apply(lambda x: 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) bfq_data['if_trade'] = True data = pd.concat([bfq_data, info[['category']] [bfq_data.index[0]:end_date]], axis=1) data['date'] = data.index data['if_trade'].fillna(value=False, inplace=True) data = data.fillna(method='ffill') data = pd.concat([data, info[['fenhong', 'peigu', 'peigujia', 'songzhuangu']][bfq_data.index[0]:end_date]], axis=1) data = data.fillna(0) data['preclose'] = (data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia']) / (10 + data['peigu'] + data['songzhuangu']) data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1)[::-1].cumprod() data['open'] = data['open'] * data['adj'] data['high'] = data['high'] * data['adj'] data['low'] = data['low'] * data['adj'] data['close'] = data['close'] * data['adj'] data['preclose'] = data['preclose'] * data['adj'] data = data[data['if_trade']] return data.drop(['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1)[data['open'] != 0].assign(date=data['date'].apply(lambda x: str(x)[0:10]))[start_date:end_date] elif if_fq in ['02', 'hfq']: xdxr_data = QA_fetch_get_stock_xdxr(code) info = xdxr_data[xdxr_data['category'] == 1] bfq_data = data\ .assign(date=pd.to_datetime(data['datetime'].apply(lambda x: 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) bfq_data['if_trade'] = True data = pd.concat([bfq_data, info[['category']] [bfq_data.index[0]:]], axis=1) data['date'] = data.index data['if_trade'].fillna(value=False, inplace=True) data = data.fillna(method='ffill') data = pd.concat([data, info[['fenhong', 'peigu', 'peigujia', 'songzhuangu']][bfq_data.index[0]:]], axis=1) data = data.fillna(0) data['preclose'] = (data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia']) / (10 + data['peigu'] + data['songzhuangu']) data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1).cumprod() data['open'] = data['open'] / data['adj'] data['high'] = data['high'] / data['adj'] data['low'] = data['low'] / data['adj'] data['close'] = data['close'] / data['adj'] data['preclose'] = data['preclose'] / data['adj'] data = data[data['if_trade']] return data.drop(['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1)[data['open'] != 0].assign(date=data['date'].apply(lambda x: str(x)[0:10]))[start_date:end_date] elif if_fq in ['04', 'ddhfq']: xdxr_data = QA_fetch_get_stock_xdxr(code) info = xdxr_data[xdxr_data['category'] == 1] bfq_data = data\ .assign(date=pd.to_datetime(data['datetime'].apply(lambda x: 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) bfq_data['if_trade'] = True data = pd.concat([bfq_data, info[['category']] [bfq_data.index[0]:end_date]], axis=1) data['date'] = data.index data['if_trade'].fillna(value=False, inplace=True) data = data.fillna(method='ffill') data = pd.concat([data, info[['fenhong', 'peigu', 'peigujia', 'songzhuangu']][bfq_data.index[0]:end_date]], axis=1) data = data.fillna(0) data['preclose'] = (data['close'].shift(1) * 10 - data['fenhong'] + data['peigu'] * data['peigujia']) / (10 + data['peigu'] + data['songzhuangu']) data['adj'] = (data['preclose'].shift(-1) / data['close']).fillna(1).cumprod() data['open'] = data['open'] / data['adj'] data['high'] = data['high'] / data['adj'] data['low'] = data['low'] / data['adj'] data['close'] = data['close'] / data['adj'] data['preclose'] = data['preclose'] / data['adj'] data = data[data['if_trade']] return data.drop(['fenhong', 'peigu', 'peigujia', 'songzhuangu', 'if_trade', 'category'], axis=1)[data['open'] != 0].assign(date=data['date'].apply(lambda x: str(x)[0:10]))[start_date:end_date]
def getDay(cls, code, start_date, end_date, if_fq='00', frequence=9): """获取日线及以上级别的数据 Args: code {str:6} -- code 是一个单独的code 6位长度的str start_date {str:10} -- 10位长度的日期 比如'2017-01-01' end_date {str:10} -- 10位长度的日期 比如'2018-01-01' if_fq {str} -- '00'/'bfq' -- 不复权 '01'/'qfq' -- 前复权 '02'/'hfq' -- 后复权 '03'/'ddqfq' -- 定点前复权 '04'/'ddhfq' --定点后复权 frequency {int} -- K线周期 0 5分钟K线 1 15分钟K线 2 30分钟K线 3 1小时K线 4 日K线 5 周K线 6 月K线 7 1分钟 8 1分钟K线 9 日K线 10 季K线 11 年K线 ip {str} -- [description] (default: None) ip可以通过select_best_ip()函数重新获取 port {int} -- [description] (default: {None}) Returns: pd.DataFrame/None -- 返回的是dataframe,如果出错比如只获 取了一天,而当天停牌,返回None Exception: 如果出现网络问题/服务器拒绝, 会出现socket:time out 尝试再次获取/更换ip即可, 本函数不做处理 """ try: with cls.tdxapi.connect(cls.ip, cls.port, time_out=0.7) as api: start_date = str(start_date)[0:10] today_ = datetime.date.today() lens = QA_util_get_trade_gap(start_date, today_) data = pd.concat([ api.to_df( api.get_security_bars( frequence, _select_market_code(code), code, (int(lens / 800) - i) * 800, 800)) for i in range(int(lens / 800) + 1) ], axis=0, sort=False) # 这里的问题是: 如果只取了一天的股票,而当天停牌, 那么就直接返回None了 if len(data) < 1: return None data = data[data['open'] != 0] data = data.assign( date=data['datetime'].apply(lambda x: str(x[0:10])), code=str(code), date_stamp=data['datetime'].apply( lambda x: QA_util_date_stamp(str(x)[0:10]))) \ .set_index('date', drop=False, inplace=False) end_date = str(end_date)[0:10] data = data.drop( ['year', 'month', 'day', 'hour', 'minute', 'datetime'], axis=1)[start_date:end_date] if if_fq in ['00', 'bfq']: return data else: print('CURRENTLY NOT SUPPORT REALTIME FUQUAN') return None # xdxr = QA_fetch_get_stock_xdxr(code) # if if_fq in ['01','qfq']: # return QA_data_make_qfq(data,xdxr) # elif if_fq in ['02','hfq']: # return QA_data_make_hfq(data,xdxr) except Exception as e: if isinstance(e, TypeError): print( '1、Tushare内置的pytdx版本和QUANTAXIS使用的pytdx 版本不同, 请重新安装pytdx以解决此问题.{}:{}' .format(cls.ip, cls.port)) print('pip uninstall pytdx\npip install pytdx') print('2、或者此时间段无数据。') else: print(e)