def _get_data_by_spider(symbol, ktype, start, end): """ 根据爬虫地址获取 """ client = bitmexClient.Client(conf.BITMEX_URL_HISTORY) params = { "symbol": symbol, "resolution": ktype, "from": start, "to": end, } data_json = client.get(params) df = tool.init_empty_df(SYMBOL_COLS) if data_json['s'] == 'ok': open_arr = data_json['o'] close_arr = data_json['c'] high_arr = data_json['h'] low_arr = data_json['l'] time_arr = data_json['t'] volume_arr = data_json['v'] for index in range(0, len(time_arr)): row_dict = dict() row_dict['date'] = tradetime.transfer_unixtime( time_arr[index], ktype) row_dict['open'] = float(open_arr[index]) row_dict['close'] = float(close_arr[index]) row_dict['high'] = float(high_arr[index]) row_dict['low'] = float(low_arr[index]) row_dict['volume'] = volume_arr[index] df = df.append(row_dict, ignore_index=True) if len(df) > 0: df["volume"] = df["volume"].astype('float64') return df
def list(symbol, count, start, filter_dict=None): """ 交易历史列表 """ client = bitmexClient.Client(conf.BITMEX_URL_ORDER) params = { "symbol": symbol, "count": count, "start": start, "reverse": True } if filter_dict is not None: params['filter'] = filter_dict data_json = client.get(params) df = tool.init_empty_df(None) for one in data_json: row_dict = dict() row_dict['date'] = tradetime.transfer_iso_datetime( one['timestamp'], "M") row_dict['symbol'] = one['symbol'] row_dict['side'] = one['side'] row_dict['type'] = one['ordType'] row_dict['status'] = one['ordStatus'] row_dict['price'] = one['price'] row_dict['order'] = one['simpleOrderQty'] row_dict['cum'] = one['simpleCumQty'] row_dict['id'] = one['orderID'] df = df.append(row_dict, ignore_index=True) return df
def cancel(oid): """ 取消订单 """ client = bitmexClient.Client(conf.BITMEX_URL_ORDER) content = { "orderID": oid, } return client.delete(content)
def cancel_all(symbol): """ 取消全部订单 """ client = bitmexClient.Client(conf.BITMEX_URL_ORDER_CANCEL_ALL) content = { "symbol": symbol, } return client.delete(content)
def cancel_all_after(timeout): """ 标记删除 timeout: ms """ client = bitmexClient.Client(conf.BITMEX_URL_ORDER_CANCEL_ALL_AFTER) content = { "timeout": timeout, } return client.post(content)
def book(symbol, depth): client = bitmexClient.Client(conf.BITMEX_URL_ORDERBOOK) params = {"symbol": symbol, "depth": depth} data_json = client.get(params) df = tool.init_empty_df(None) for one in data_json: row_dict = dict() row_dict['price'] = one['price'] row_dict['side'] = one['side'] row_dict['size'] = one['size'] df = df.append(row_dict, ignore_index=True) return df
def amend_contract(oid, qty, price=None): """ 修改订单(基于contract) """ client = bitmexClient.Client(conf.BITMEX_URL_ORDER) content = { "orderID": oid, "orderQty": qty, } if price is not None: content["price"] = price return client.put(content)
def amend_simple(oid, simple_qty, price=None): """ 修改订单(基于simple) """ client = bitmexClient.Client(conf.BITMEX_URL_ORDER) content = { "orderID": oid, "simpleOrderQty": simple_qty, } if price is not None: content["price"] = price return client.put(content)
def wallet(currency=conf.BITMEX_CURRENCY_XBT): """ 钱包状态 """ client = bitmexClient.Client(conf.BITMEX_URL_WALLET) params = { "currency": currency, } data_json = client.get(params) row_dict = dict() row_dict['date'] = tradetime.transfer_iso_datetime(data_json['timestamp'], "M") row_dict['amount'] = round(data_json['amount'] / 1000 / 1000 / 100, 4) return row_dict
def create_contract(symbol, side, qty, otype=TYPE_LIMIT, price=None): """ 创建订单(基于contract) """ client = bitmexClient.Client(conf.BITMEX_URL_ORDER) content = { "symbol": symbol, "side": side, "orderQty": qty, "ordType": otype, } if otype == TYPE_LIMIT or otype == TYPE_STOP_LIMIT or otype == TYPE_LIMIT_IF_TOUCH: content["price"] = price return client.post(content)
def _get_data_by_api(symbol, bin_size, count, start=None, end=None): client = bitmexClient.Client(conf.BITMEX_URL_TRADE_BUCKETED) params = { "symbol": symbol, "binSize": bin_size, "count": count, "partial": False, "reverse": True, } if start is not None: params['startTime'] = start if end is not None: params['endTime'] = end data_json = client.get(params) df = _transfer_json_to_df(data_json) return df
def position(count): """ 获取仓位列表 """ # TODO 优化成支持多个symbol client = bitmexClient.Client(conf.BITMEX_URL_POSITION) params = { "count": count, } data_json = client.get(params) row_dict = dict() row_dict['date'] = tradetime.transfer_iso_datetime( data_json[0]['openingTimestamp'], "M") row_dict['symbol'] = data_json[0]['symbol'] row_dict['is_open'] = data_json[0]['isOpen'] row_dict['price'] = data_json[0]['avgEntryPrice'] row_dict['current'] = data_json[0]['currentQty'] return row_dict
def wallet_history(count, start): """ 钱包历史列表 """ client = bitmexClient.Client(conf.BITMEX_URL_WALLET_HISTORY) params = {"count": count, "start": start, "reverse": True} data_json = client.get(params) df = tool.init_empty_df(WALLET_COLS) for one in data_json: row_dict = dict() row_dict['status'] = one['transactStatus'] row_dict['address'] = one['address'] row_dict['amount'] = one['amount'] row_dict['fee'] = one['fee'] row_dict['balance'] = one['walletBalance'] row_dict['date'] = tradetime.transfer_iso_datetime( one['timestamp'], "M") df = df.append(row_dict, ignore_index=True) return df