def xrxd(self, date='', retry=3, pause=0.001): """ 获取股票除权除息信息 Parameters ------ date: string format:YYYY-MM-DD retry: int, 默认 3 如遇网络等问题重复执行的次数 pause : int, 默认 0 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 return ------ Dict or None nd, 对应年份 fh_sh, 分红数额 djr, 股权登记日 cqr, 除权日 FHcontent, 除权除息信息 """ data = None if not date: date = Utility.getToday() symbol = Utility.symbol(self.__code) for _ in range(retry): time.sleep(pause) url = cf.HISTORY_URL % ('fq', 'qfq', symbol, 'day', date, date, 'qfq', Utility.random(17)) try: request = self._session.get(url, timeout=10) pattern = re.compile(r'({"nd".+?})') result = re.search(pattern, request.text) if result: data = eval(result.group(1)) except Exception as e: print(str(e)) return data raise IOError(cf.NETWORK_URL_ERROR_MSG)
def __parsePage(self, cate='', event=0, num=0, retry=3, pause=0.001): for _ in range(retry): time.sleep(pause) try: rdInt = Utility.random() request = self._session.get(cf.MACRO_URL % (rdInt, cate, event, num, rdInt), timeout=10) if self._PY3: request.encoding = 'gbk' regSym = re.compile(r'\,count:(.*?)\}') datastr = regSym.findall(request.text) datastr = datastr[0] datastr = datastr.split('data:')[1] except Exception as e: print(e) else: return datastr raise IOError(cf.NETWORK_URL_ERROR_MSG)
def marginDetailsAllByDate(self, date, retry=3, pause=0.001): """ 按日期获取两市融资融券明细列表 Parameters -------- date : string 选择日期 format:YYYY-MM-DD retry : int, 默认 3 如遇网络等问题重复执行的次数 pause : int, 默认 0 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 Return ------ DataFrame or List: [{'code':, 'name':, ...}, ...] code: 股票代码 name: 名称 rzye: 当日融资余额(元) rzyezb: 当日融资余额占比(%) rzmre: 当日融资买入额(元) rzche: 当日融资偿还额(元) rzjmre: 当日融资净买入额(元) rqye: 当日融券余额(元) rqyl: 当日融券余量(股) rqmcl: 当日融券卖出量(股) rqchl: 当日融券偿还量(股) rqjmcl: 当日融券净卖出量(股) rzrqye: 当日融资融券余额(元) rzrqyecz: 当日融资融券余额差值(元) """ self._data = pd.DataFrame() self._writeHead() self._data = self.__handleMarginDetailsAllByDate(self._data, date, 1, Utility.random(8), retry, pause) self._data.rename(columns={'scode':'code', 'sname':'name'}, inplace=True) return self._result()
def marginTotal(self, retry=3, pause=0.001): """ 两市合计融资融券历史数据 Parameters -------- retry : int, 默认 3 如遇网络等问题重复执行的次数 pause : int, 默认 0 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 Return ------ DataFrame or List: [{'date':, 'close':, ...}, ...] date: 日期 close: 沪深300收盘点数 zdf: 沪深300收盘涨跌幅(%) rzye: 融资余额(元) rzyezb: 融资余额占比(%) rzmre: 融资买入额(元) rzche: 融资偿还额(元) rzjmre: 融资净买入额(元) rqye: 融券余额(元) rqyl: 融券余量(股) rqmcl: 融券卖出量(股) rqchl: 融券偿还量(股) rqjmcl: 融券净卖出量(股) rzrqye: 融资融券余额(元) rzrqyecz: 融资融券余额差值(元) """ self._data = pd.DataFrame() self._writeHead() self._data = self.__handleMarginTotal(self._data, 1, Utility.random(8), retry, pause) self._data.rename(columns={'tdate':'date'}, inplace=True) return self._result()
def __handleFoundHoldings(self, start, end, pageNo, retry, pause): for _ in range(retry): time.sleep(pause) if pageNo>0: self._writeConsole() try: # http://quotes.money.163.com/hs/marketdata/service/jjcgph.php?host=/hs/marketdata/service/jjcgph.php&page=0&query=start:2018-06-30;end:2018-09-30&order=desc&count=60&type=query&req=73259 request = self._session.get( cf.FUND_HOLDS_URL % (pageNo, start, end, Utility.random(5)), timeout=10 ) if self._PY3: request.encoding = 'utf-8' lines = request.text lines = lines.replace('--', '0') lines = json.loads(lines) data = lines['list'] df = pd.DataFrame(data) df = df.drop(['CODE', 'ESYMBOL', 'EXCHANGE', 'NAME', 'RN', 'SHANGQIGUSHU', 'SHANGQISHIZHI', 'SHANGQISHULIANG'], axis=1) for col in ['GUSHU', 'GUSHUBIJIAO', 'SHIZHI', 'SCSTC27']: df[col] = df[col].astype(float) df['SCSTC27'] = df['SCSTC27']*100 df['GUSHU'] = df['GUSHU']/10000 df['GUSHUBIJIAO'] = df['GUSHUBIJIAO']/10000 df['SHIZHI'] = df['SHIZHI']/10000 df['GUSHU'] = df['GUSHU'].map(cf.FORMAT) df['GUSHUBIJIAO'] = df['GUSHUBIJIAO'].map(cf.FORMAT) df['SHIZHI'] = df['SHIZHI'].map(cf.FORMAT) df['SCSTC27'] = df['SCSTC27'].map(cf.FORMAT) df.columns = cf.FUND_HOLDS_COLS df = df[['code', 'name', 'date', 'nums', 'nlast', 'count', 'clast', 'amount', 'ratio']] except Exception as e: print(e) else: if pageNo == 0: return df, int(lines['pagecount']) else: return df raise IOError(cf.NETWORK_URL_ERROR_MSG)
def history(self, start='', end='', ktype='D', autype='qfq', index=False, retry=3, pause=0.001): """ 获取股票交易历史数据 --------- Parameters: 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: int, 默认 3 如遇网络等问题重复执行的次数 pause : int, 默认 0 重复请求数据过程中暂停的秒数,防止请求间隔时间太短出现的问题 return ------- DataFrame or list: [{'date':, 'open':, ...}, ...] date 交易日期 (index) open 开盘价 high 最高价 close 收盘价 low 最低价 volume 成交量 code 股票代码 """ self._data = pd.DataFrame() url = '' dataflag = '' symbol = cf.INDEX_SYMBOL[self.__code] if index else Utility.symbol(self.__code) autype = '' if autype is None else autype if (start is not None) & (start != ''): end = Utility.getToday() if end is None or end == '' else end if ktype.upper() in cf.K_LABELS: fq = autype if autype is not None else '' if self.__code[:1] in ('1', '5') or index: fq = '' kline = '' if autype is None else 'fq' if ((start is None or start == '') or (end is None or end == '')) or (start == end): # http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?_var=kline_dayqfq¶m=sh600000,day,,,640,qfq&r=0.73327461855388564 urls = [cf.HISTORY_URL % (kline, fq, symbol, cf.TT_K_TYPE[ktype.upper()], start, end, fq, Utility.random(17))] else: years = Utility.ttDates(start, end) urls = [] for year in years: startdate = str(year) + '-01-01' enddate = str(year+1) + '-12-31' # http://web.ifzq.gtimg.cn/appstock/app/fqkline/get?_var=kline_dayqfq2008¶m=sh600000,day,2008-01-01,2009-12-31,640,qfq&r=0.73327461855388564 url = cf.HISTORY_URL % (kline, fq+str(year), symbol, cf.TT_K_TYPE[ktype.upper()], startdate, enddate, fq, Utility.random(17)) urls.append(url) dataflag = '%s%s'%(fq, cf.TT_K_TYPE[ktype.upper()]) # qfqday elif ktype in cf.K_MIN_LABELS: # http://ifzq.gtimg.cn/appstock/app/kline/mkline?param=sh600000,m30,,640&_var=m30_today&r=0.5537154641907898 urls = [cf.HISTORY_MIN_URL % (symbol, ktype, ktype, Utility.random(16))] dataflag = 'm%s'%ktype # m30 else: raise TypeError('ktype input error.') for url in urls: self._data = self._data.append(self.__handleHistory(url, dataflag, symbol, index, ktype, retry, pause), ignore_index=True) if ktype not in cf.K_MIN_LABELS: if ((start is not None) & (start != '')) & ((end is not None) & (end != '')): self._data = self._data[(self._data.date >= start) & (self._data.date <= end)] return self._result() raise IOError(cf.NETWORK_URL_ERROR_MSG)
def realtime(self): """ 获取实时交易数据 getting real time quotes data 用于跟踪交易情况(本次执行的结果-上一次执行的数据) Parameters ------ stockdata(code) code : string, array-like object (list, tuple, Series). return ------- DataFrame or list: [{'name':, 'open':, ...}, ...] 0:name,股票名字 1:open,今日开盘价 2:pre_close,昨日收盘价 3:price,当前价格 4:high,今日最高价 5:low,今日最低价 6:bid,竞买价,即“买一”报价 7:ask,竞卖价,即“卖一”报价 8:volumn,成交量 maybe you need do volumn/100 9:amount,成交金额(元 CNY) 10:b1_v,委买一(笔数 bid volume) 11:b1_p,委买一(价格 bid price) 12:b2_v,“买二” 13:b2_p,“买二” 14:b3_v,“买三” 15:b3_p,“买三” 16:b4_v,“买四” 17:b4_p,“买四” 18:b5_v,“买五” 19:b5_p,“买五” 20:a1_v,委卖一(笔数 ask volume) 21:a1_p,委卖一(价格 ask price) ... 30:date,日期; 31:time,时间; """ self._data = pd.DataFrame() symbols_list = '' if isinstance(self.__code, list) or isinstance(self.__code, set) or isinstance(self.__code, tuple) or isinstance(self.__code, pd.Series): for code in self.__code: symbols_list += Utility.symbol(code) + ',' else: symbols_list = Utility.symbol(self.__code) symbols_list = symbols_list[:-1] if len(symbols_list) > 8 else symbols_list # http://hq.sinajs.cn/rn=4879967949085&list=sh600000,sh600004 request = self._session.get( cf.LIVE_DATA_URL % (Utility.random(), symbols_list), timeout=10 ) request.encoding = 'gbk' reg = re.compile(r'\="(.*?)\";') data = reg.findall(request.text) regSym = re.compile(r'(?:sh|sz)(.*?)\=') syms = regSym.findall(request.text) data_list = [] syms_list = [] for index, row in enumerate(data): if len(row)>1: data_list.append([astr for astr in row.split(',')]) syms_list.append(syms[index]) if len(syms_list) == 0: return None self._data = pd.DataFrame(data_list, columns=cf.LIVE_DATA_COLS) self._data = self._data.drop('s', axis=1) self._data['code'] = syms_list ls = [cls for cls in self._data.columns if '_v' in cls] for txt in ls: self._data[txt] = self._data[txt].map(lambda x : x[:-2]) # 价格、成交量等转换为浮点类型 for col in ['open', 'pre_close', 'price', 'high', 'low', 'bid', 'ask', 'volume','amount', 'b1_v', 'b1_p', 'b2_v', 'b2_p', 'b3_v', 'b3_p', 'b4_v', 'b4_p', 'b5_v', 'b5_p', 'a1_v', 'a1_p', 'a2_v', 'a2_p', 'a3_v', 'a3_p', 'a4_v', 'a4_p', 'a5_v', 'a5_p']: self._data[col] = self._data[col].astype(float) return self._result()