Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cancelReplace no data output partial fail #184

Closed
xInactive opened this issue Nov 18, 2022 · 2 comments
Closed

cancelReplace no data output partial fail #184

xInactive opened this issue Nov 18, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@xInactive
Copy link

With cancelReplace, when Cancel Order Succeeds but New Order Placement fails it does not output cancel-data as stated in Github docs. Instead it only throws an Error without the needed cancel-data:

Traceback (most recent call last): File "c:\Users\Pascal\Python Skripts\Binance\Binance.py", line 35, in <module> trade = client.cancel_and_replace(symbol=ticker, side='SELL', type= 'LIMIT_MAKER', cancelReplaceMode= 'ALLOW_FAILURE', quantity= 950, price= 0.7, cancelOrderId= 37359904) File "C:\Users\Pascal\AppData\Local\Programs\Python\Python310\lib\site-packages\binance\spot\trade.py", line 184, in cancel_and_replace return self.sign_request("POST", url_path, params) File "C:\Users\Pascal\AppData\Local\Programs\Python\Python310\lib\site-packages\binance\api.py", line 83, in sign_request return self.send_request(http_method, url_path, payload) File "C:\Users\Pascal\AppData\Local\Programs\Python\Python310\lib\site-packages\binance\api.py", line 117, in send_request self._handle_exception(response) File "C:\Users\Pascal\AppData\Local\Programs\Python\Python310\lib\site-packages\binance\api.py", line 170, in _handle_exception raise ClientError(status_code, err["code"], err["msg"], response.headers) binance.error.ClientError: (409, -2021, 'Order cancel-replace partially failed.', {'Content-Type': 'application/json;charset=UTF-8', 'Content-Length': '562', 'Connection': 'keep-alive', 'Date': 'Tue, 15 Nov 2022 22:52:38 GMT', 'Server': 'nginx', 'x-mbx-uuid': '5abc70b8-6e7b-4008-a43e-760621393663', 'x-mbx-used-weight': '34', 'x-mbx-used-weight-1m': '34', 'x-mbx-order-count-10s': '1', 'x-mbx-order-count-1d': '17', 'Strict-Transport-Security': 'max-age=31536000; includeSubdomains', 'X-Frame-Options': 'SAMEORIGIN', 'X-Xss-Protection': '1; mode=block', 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': "default-src 'self'", 'X-Content-Security-Policy': "default-src 'self'", 'X-WebKit-CSP': "default-src 'self'", 'Cache-Control': 'no-cache, no-store, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'X-Cache': 'Error from cloudfront', 'Via': '1.1 0c792defeeaa18965559ad74895ea56a.cloudfront.net (CloudFront)', 'X-Amz-Cf-Pop': 'FRA60-P3', 'X-Amz-Cf-Id': 'eQX28Fk_LBpAlk017J8xjnPr6roldN-_grxxiuGwtFdcVPyYRkBLGQ=='})

If new order placement fails, the cancellation-data should be returned from the successful cancel.

@aisling-2 already answered:

The problem seems to be ClientError(status_code, err["code"], err["msg"], response.headers), i.e, it provides information on the error code and error message only, although, the response contains data field as well: {
"code": -2021,
"msg": "Order cancel-replace partially failed.",
"data": {}

How can i access the data field as well?

@xInactive
Copy link
Author

xInactive commented Nov 23, 2022

I managed to edit the _handle_exception function (in binance\api.py) which only refers to error code and message but leaves out data. I was not able to just add the data field into ClientError raise because that leads to errors when there is no data response in some request error.
So i just added a global variable that stores the cancel_data returned if Order cancel-replace partially failed:

def _handle_exception(self, response):
        global cancel_data
        status_code = response.status_code
        if status_code < 400:
            return
        if 400 <= status_code < 500:
            try:
                err = json.loads(response.text)
            except JSONDecodeError:
                raise ClientError(status_code, None, response.text, response.headers)
            if err["msg"] == 'Order cancel-replace partially failed.':
                cancel_data = err["data"]
            else:
                raise ClientError(status_code, err["code"], err["msg"], response.headers)
        raise ServerError(status_code, response.text)

Now i can refer to that data in case a exception is raised with api.cancel_data['cancelResponse']

@2pd
Copy link
Contributor

2pd commented Dec 12, 2022

Thanks for the feedback, the data is newly introduced from this endpoint. We will upgrade the connector and fetch it from response.

@2pd 2pd added the enhancement New feature or request label Dec 12, 2022
@2pd 2pd closed this as completed Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants