def candels(self,pair,timeframe,section='hist',period={'months':-1},f_paras=None,start=None,end=None, limit=100,sort=-1,return_url=False): """ curl "https://api.bitfinex.com/v2/candles/trade:TimeFrame:Symbol/Section" """ if pair[0]=='f': base.check_empty(f_paras,'period cant be empty when requesting funding market data') pair=pair+':'+f_paras if section=='hist': if not end: end=base.get_currenttime_asstr() end=int(base.str_toTimestamp(end)*1000) if not start: start=base.date_togapn(datetime.datetime.fromtimestamp(end/1000),dateformat="%Y%m%d %H:%M:%S",**period) start=int(base.str_toTimestamp(start)*1000) limit=min(int((end-start)/(int(timeframe[:-1])*1000* (60 if timeframe[-1]=='m' else (60*60 if timeframe[-1]=='h' else 24*60*60)))),1000) url=self.form_url(self.pathdict['KLINE'] % (timeframe,pair,section), parameters={'start':start,'end':end,'limit':limit,'sort':sort}, version='v2') if return_url: return url res=self.get_data(url) # print(res) return self.candels_format(res,pair)
def klines(self,pair,timeframe,period={'months':-1}, start=None,end=None,loop_gap=1): end=base.get_currenttime_asstr() if not end else end start=base.date_togapn(end,dateformat="%Y%m%d %H:%M:%S",**period) if start is None else start start,end=[int(base.str_toTimestamp(x)) for x in [start,end]] unit_val=int(re.findall('[0-9]+',timeframe)[0]) if timeframe[-1]=='m': unit=60*unit_val elif timeframe[-1]=='h': unit=60*60*unit_val elif timeframe[-1]=='D': unit=60*60*24*unit_val else: unit=60*60*24*30*unit_val gap=(end-start)/(int(timeframe[:-1])*1000*(60 if timeframe[-1]=='m' else (60*60 if timeframe[-1]=='h' else 24*60*60))) klinedata=[] while gap>0: limit=min(1000,gap) if limit<1: return None end=start+unit*limit*1000 klinedata.append(self.candels(pair,timeframe,start=start,end=end,limit=limit)) gap=gap-limit start=start+unit*limit*1000 time.sleep(6) if len(klinedata)==0: return None else: return pd.concat(klinedata)
def deals(self,pair,start=None,end=None,period={'months':-1},loop_gap=2): end=base.get_currenttime_asstr() if not end else end start=base.date_togapn(end,dateformat="%Y%m%d %H:%M:%S",**period) if start is None else start start,end=[int(base.str_toTimestamp(x)*1000) for x in [start,end]] dealdata=[] try: while start<end: dealdf=self.trades(pair,start=start,end=end,limit=1000) dealdata.append(dealdf) end=time.mktime(dealdf.index[0].timetuple())*1000 time.sleep(loop_gap) except Exception: return pd.concat(dealdata) return pd.concat(dealdata) if dealdata else None
pair = 'xrp_usd' #pais=['tBATUSD','tMNAUSD', # 'tFUNUSD','tTNBUSD','tSPKUSD','tRCNUSD','tRLCUSD'] pairname = 'XRPUSD' #'tBATUSD','tMNAUSD', # 'tFUNUSD','tTNBUSD','tSPKUSD','tRCNUSD','tRLCUSD'] exchangename = 'okex' #--抓取tf timeframe = '1hour' contract_type = 'quarter' #--抓取时间段 #start,end='20180616','20180715' start, end = '20180628', '20180925' period = {'months': -3} end_period = {} #检查/计算时间 if not end: end = base.get_currenttime_asstr() if not start: start = base.date_togapn(end, dateformat="%Y%m%d %H:%M:%S", **end_period) selectfields = [ 'time', 'pair', 'open', 'high', 'low', 'close', 'volume', 'pct_change' ] #====数据库配置 usedb = 'data_okex' collnam = 'KLINE' + timeframe