Пример #1
0
    def closeAll(self, symbol, standard_token=None):
        """以市价单的方式全平某个合约的当前仓位,若交易所支持批量下单,使用批量下单接口

        Parameters
        ----------
        symbols : str
            所要平仓的合约代码,多个合约代码用逗号分隔开。
        direction : str, optional
            账户统一到某个币本位上

        Return
        ------
        vtOrderIDs: list of str
        包含平仓操作发送的所有订单ID的列表
        """
        if not standard_token:
            return []
        vtOrderIDs = []
        base_currency, quote_currency = symbol.split("-")
        if base_currency == standard_token:
            path = f'/api/spot/v3/accounts/{str.lower(quote_currency)}'
            side = 'buy'
        elif quote_currency == standard_token:
            path = f'/api/spot/v3/accounts/{str.lower(base_currency)}'
            side = 'sell'
        else:
            return []  # 币对双方都不是指定的本位
        request = Request('GET',
                          path,
                          params=None,
                          callback=None,
                          data=None,
                          headers=None)

        request = self.sign2(request)
        request.extra = {"instrument_id": symbol, "side": side}
        url = self.makeFullUrl(request.path)
        response = requests.get(url,
                                headers=request.headers,
                                params=request.params)
        data = response.json()
        rsp = self.onCloseAll(data, request)
        # rsp: [{'client_oid': '', 'order_id': '2433076975049728', 'result': True}]
        # failed: [{'code': 30024, 'message': 'Parameter value filling error'}]
        for result in rsp:
            if "code" in result.keys():
                self.gateway.writeLog(f'换币失败:{result}', logging.ERROR)
            elif "result" in result.keys():
                if result['result']:
                    vtOrderIDs.append(result['order_id'])
                    self.gateway.writeLog(f'换币成功:{result}')

        return vtOrderIDs
Пример #2
0
    def cancelAll(self, symbol=None, orders=None):
        """撤销所有挂单,若交易所支持批量撤单,使用批量撤单接口

        Parameters
        ----------
        symbol : str, optional
            用逗号隔开的多个合约代码,表示只撤销这些合约的挂单(默认为None,表示撤销所有合约的所有挂单)
        orders : str, optional
            用逗号隔开的多个vtOrderID.
            若为None,先从交易所先查询所有未完成订单作为待撤销订单列表进行撤单;
            若不为None,则对给出的对应订单中和symbol参数相匹配的订单作为待撤销订单列表进行撤单。
        Return
        ------
        vtOrderIDs: list of str
        包含本次所有撤销的订单ID的列表
        """
        vtOrderIDs = []
        # 未完成(未成交和部分成交)
        req = {
            'instrument_id': symbol,
        }
        path = f'/api/spot/v3/orders_pending'
        request = Request('GET',
                          path,
                          params=req,
                          callback=None,
                          data=None,
                          headers=None)
        request = self.sign2(request)
        request.extra = orders
        url = self.makeFullUrl(request.path)
        response = requests.get(url,
                                headers=request.headers,
                                params=request.params)
        data = response.json()

        if data:
            rsp = self.onCancelAll(data, request)
            # failed rsp: {'code': 33027, 'message': 'Order has been revoked or revoked'}
            if "code" in rsp.keys():
                self.gateway.writeLog(f"交易所返回{symbol}撤单失败:{rsp['message']}",
                                      logging.ERROR)
                return []
            # rsp: {'eth-usdt':
            # {'result': True, 'client_oid': '',
            # 'order_id': ['2432470701654016', '2432470087389184', '2432469715472384']}}
            for sym, result in rsp.items():
                if result['result']:
                    vtOrderIDs += result['order_id']
                    self.gateway.writeLog(
                        f"交易所返回{sym}撤单成功: ids: {result['order_id']}")
        return vtOrderIDs
Пример #3
0
    def closeAll(self, symbol, direction=None):
        """以市价单的方式全平某个合约的当前仓位,若交易所支持批量下单,使用批量下单接口

        Parameters
        ----------
        symbols : str
            所要平仓的合约代码,多个合约代码用逗号分隔开。
        direction : str, optional
            所要平仓的方向,()
默认为None,即在两个方向上都进行平仓,否则只在给出的方向上进行平仓
        Return
        ------
        vtOrderIDs: list of str
        包含平仓操作发送的所有订单ID的列表
        """
        vtOrderIDs = []
        req = {
            'instrument_id': symbol,
        }
        path = f'/api/swap/v3/{symbol}/position/'
        request = Request('GET',
                          path,
                          params=req,
                          callback=None,
                          data=None,
                          headers=None)

        request = self.sign2(request)
        request.extra = direction
        url = self.makeFullUrl(request.path)
        response = requests.get(url,
                                headers=request.headers,
                                params=request.params)
        data = response.json()
        if data['holding']:
            rsp = self.onCloseAll(data, request)
            # failed:{'error_message': 'Incorrect order size', 'result': 'true',
            #      'error_code': '35012', 'order_id': '-1'}
            # rsp: {'error_message': '', 'result': 'true', 'error_code': '0',
            #  'order_id': '66-a-4ec048f15-0'}
            for result in rsp:
                if not result['error_message']:
                    vtOrderIDs.append(result['order_id'])
                    self.gateway.writeLog(f'平仓成功:{result}')
                else:
                    self.gateway.writeLog(f'平仓失败:{result}', logging.ERROR)
        return vtOrderIDs
Пример #4
0
 def cancelAll(self, symbol=None, orders=None):
     """撤销所有挂单,若交易所支持批量撤单,使用批量撤单接口
     Parameters
     ----------
     symbol : str, optional
         用逗号隔开的多个合约代码,表示只撤销这些合约的挂单(默认为None,表示撤销所有合约的所有挂单)
     orders : str, optional
         用逗号隔开的多个vtOrderID.
         若为None,先从交易所先查询所有未完成订单作为待撤销订单列表进行撤单;
         若不为None,则对给出的对应订单中和symbol参数相匹配的订单作为待撤销订单列表进行撤单。
     Return
     ------
     vtOrderIDs: list of str
     包含本次所有撤销的订单ID的列表
     """
     vtOrderIDs = []
     symbol = self.contractMapReverse[symbol]
     # 未完成(包含未成交和部分成交)
     req = {'instrument_id': symbol, 'state': 6}
     path = f'/api/futures/v3/orders/{symbol}'
     request = Request('GET',
                       path,
                       params=req,
                       callback=None,
                       data=None,
                       headers=None)
     request = self.sign2(request)
     request.extra = orders
     url = self.makeFullUrl(request.path)
     response = requests.get(url,
                             headers=request.headers,
                             params=request.params)
     data = response.json()
     if data['result'] and data['order_info']:
         data = self.onCancelAll(data['order_info'], request)
         #{'result': True,
         # 'order_ids': ['2432685818596352', '2432686510479360'],
         # 'instrument_id': 'ETH-USD-190329'}
         if data['result']:
             vtOrderIDs += str(data['order_ids'])
             self.gateway.writeLog(
                 f"交易所返回{str(data['instrument_id'])} 撤单成功: ids: {str(data['order_ids'])}"
             )
     return vtOrderIDs
Пример #5
0
    def cancelAll(self, symbol=None, orders=None):
        """撤销所有挂单,若交易所支持批量撤单,使用批量撤单接口

        Parameters
        ----------
        symbol : str, optional
            用逗号隔开的多个合约代码,表示只撤销这些合约的挂单(默认为None,表示撤销所有合约的所有挂单)
        orders : str, optional
            用逗号隔开的多个vtOrderID.
            若为None,先从交易所先查询所有未完成订单作为待撤销订单列表进行撤单;
            若不为None,则对给出的对应订单中和symbol参数相匹配的订单作为待撤销订单列表进行撤单。
        Return
        ------
        vtOrderIDs: list of str
        包含本次所有撤销的订单ID的列表
        """
        vtOrderIDs = []
        # 未完成(包含未成交和部分成交)
        req = {'state': 6}
        path = f'/api/swap/v3/orders/{symbol}'
        request = Request('GET',
                          path,
                          params=req,
                          callback=None,
                          data=None,
                          headers=None)
        request = self.sign2(request)
        request.extra = orders
        url = self.makeFullUrl(request.path)
        response = requests.get(url,
                                headers=request.headers,
                                params=request.params)
        data = response.json()
        if data['order_info']:
            data = self.onCancelAll(data['order_info'], request)
            # {'client_oids': [],
            # 'ids': ['66-7-4ebc9281f-0', '66-8-4ebc91cfa-0'],
            # 'instrument_id': 'ETH-USD-SWAP', 'result': 'true'}
            if data['result'] == 'true':
                vtOrderIDs += data['ids']
                self.gateway.writeLog(f"交易所返回{symbol}撤单成功: ids: {data['ids']}")
        return vtOrderIDs
Пример #6
0
    def closeAll(self, symbol, direction=None):
        """以市价单的方式全平某个合约的当前仓位,若交易所支持批量下单,使用批量下单接口
        Parameters
        ----------
        symbols : str
            所要平仓的合约代码,多个合约代码用逗号分隔开。
        direction : str, optional
            所要平仓的方向,(默认为None,即在两个方向上都进行平仓,否则只在给出的方向上进行平仓)
        Return
        ------
        vtOrderIDs: list of str
        包含平仓操作发送的所有订单ID的列表
        """
        vtOrderIDs = []
        symbol = self.contractMapReverse[symbol]
        req = {
            'instrument_id': symbol,
        }
        path = f'/api/futures/v3/{symbol}/position/'
        request = Request('GET',
                          path,
                          params=req,
                          callback=None,
                          data=None,
                          headers=None)

        request = self.sign2(request)
        request.extra = direction
        url = self.makeFullUrl(request.path)
        response = requests.get(url,
                                headers=request.headers,
                                params=request.params)
        data = response.json()
        if data['result'] and data['holding']:
            data = self.onCloseAll(data, request)
            for result in data:
                if result['result']:
                    vtOrderIDs.append(result['order_id'])
                    self.gateway.writeLog(f'平仓成功:{result}')
        return vtOrderIDs