def get_trade_rank(self, date): url = 'http://www.dce.com.cn/publicweb/quotesdata/exportMemberDealPosiQuotesBatchData.html' year, month, day = split_date(date, '%Y-%m-%d') data = { "year": year, "month": month - 1, # 脑残程序员的设计 "day": day, "batchExportFlag": 'batch', } response = self.do_request(url, data, "POST", type='binary') zip_ref = zipfile.ZipFile(io.BytesIO(response)) df_list = [] for finfo in zip_ref.infolist(): file = zip_ref.open(finfo, 'r') df = self._parse_trade_file(file) df_list.append(df) df_result = _concat_df(df_list) df_result['date'] = date df_result.reset_index(level=['名次'], inplace=True) _rename_df(df_result) return df_result, ""
def _get_url_by_date(self, date): year, month, day = split_date(date, '%Y-%m-%d') date_int = int(date_convert(date, '%Y-%m-%d', "%Y%m%d")) url = 'http://old.czce.com.cn/portal/DFSStaticFiles/Future/%d/%d/FutureDataHolding.txt' url_old = 'http://old.czce.com.cn/portal/exchange/%d/datatradeholding/%d.txt' if date < '2015-10-01': return url_old % (year, date_int) else: return url % (year, date_int)
def get_trade_rank(self, date): url = 'http://www.czce.com.cn/portal/DFSStaticFiles/Future/%d/%d/FutureDataHolding.txt' year, month, day = split_date(date, '%Y-%m-%d') date_int = int(date_convert(date, '%Y-%m-%d', "%Y%m%d")) url = url % (year, date_int) response = self.do_request(url, None, "GET", type='binary') df = self._parse_trade_file(io.StringIO(response.decode('gbk'))) df['date'] = date _rename_df(df) return df, ""
def _get_trade_rank_by_product(self, date, product): url = 'http://www.cffex.com.cn/sj/ccpm/%04d%02d/%02d/%s.xml' year, month, day = split_date(date, '%Y-%m-%d') url = url % (year, month, day, product) response = self.do_request(url, None, "GET") root = ElementTree.fromstring(response) data_list = [] for dataElements in root: if dataElements.tag != 'data': continue data = {} for subElement in dataElements: key = subElement.tag value = subElement.text if key in [ 'instrumentid', 'datatypeid', 'rank', 'shortname', 'volume', 'varvolume' ]: data[key] = value # print(data[key]) # print(data) data_list.append(data) df = pd.DataFrame(data_list) datatype_map = { "0": "成交量", "1": "持买单量", "2": "持卖单量", } df_list = [] for type, name in datatype_map.items(): # warnning df_tmp = df[df['datatypeid'] == type].copy() df_tmp['rank'] = df_tmp['rank'].apply(lambda x: int(x)) df_tmp.rename(columns={"instrumentid": "symbol"}, inplace=True) df_tmp.rename(columns={"rank": "名次"}, inplace=True) df_tmp.rename(columns={"shortname": name + "期货公司"}, inplace=True) df_tmp.rename(columns={"volume": name}, inplace=True) df_tmp.rename(columns={"varvolume": name + "增减"}, inplace=True) # print(df_tmp) df_tmp.drop(['datatypeid'], axis=1, inplace=True) df_tmp.set_index(['symbol', '名次'], inplace=True) df_list.append(df_tmp) return _merge_df(df_list)
def get_trade_rank(self, date): url = 'http://old.czce.com.cn/portal/DFSStaticFiles/Future/%d/%d/FutureDataHolding.txt' url_old = 'http://old.czce.com.cn/portal/exchange/%d/datatradeholding/%d.txt' year, month, day = split_date(date, '%Y-%m-%d') date_int = int(date_convert(date, '%Y-%m-%d', "%Y%m%d")) if date_int < 20151001: url_old = url_old % (year, date_int) response = self.do_request(url_old, None, "GET", type='binary') df = self._parse_trade_file(io.StringIO(response.decode('gbk')), old=True) df['date'] = date _rename_df(df) else: url = url % (year, date_int) response = self.do_request(url, None, "GET", type='binary') df = self._parse_trade_file(io.StringIO(response.decode('gbk'))) df['date'] = date _rename_df(df) df = df[df.variety == df.symbol] return transform(df), ""