I used this code: client = UMFutures(key=APIKEYBINANCE, secret=APISECRETBINANCE) symbol…
- User
I used this code: client = UMFutures(key=API_KEY_BINANCE, secret=API_SECRET_BINANCE) symbol = 'BCHUSDT' print(client.get_account_trades(symbol=symbol))
url = 'https://api.binance.com/api/v3/account' # Replace with the appropriate Binance API endpoint
headers = { 'X-MBX-APIKEY': API_KEY_BINANCE }
timestamp = int(dt.datetime.now().timestamp() * 1000) # Generate the current timestamp in milliseconds
Create signature
params = urlencode({'timestamp': timestamp}) signature = hmac.new(API_SECRET_BINANCE.encode('utf-8'), params.encode('utf-8'), hashlib.sha256).hexdigest()
Add signature and API key to request parameters
params += f'&signature={signature}'
Make API request to check connection
response = requests.get(url, headers=headers, params=params) But I getting ERROR: Failed to connect to the Binance API. Response: {"code":-1021,"msg":"Timestamp for this request was 1000ms ahead of the server's time."}
- Assistant
The error message indicates that the timestamp used in your request is ahead of the server's time by 1000 milliseconds. This can happen if your local system's time is not synchronized with the server's time.
To fix this issue, you can try the following steps:
-
Make sure your system's time is accurate and synchronized with a reliable time server.
-
If you are using a virtual machine or Docker container, check if the system time inside the container is properly syncronized with the host system.
-
Ensure that the timezone is correctly set on your system.
-
If you have verified that your system's time is correct, you can try increasing the timestamp correction by a few milliseconds (e.g., timestamp += 2000) to account for any slight variations between your system and the server's time.
-
If none of the above steps resolve the issue, you can consider contacting Binance support for further assistance.
-
- User
Okay , how my code need to seem with your advices ?
- Assistant
Here's an updated version of your code with the suggested changes:
import datetime as dt import requests import hmac import hashlib from urllib.parse import urlencode # Replace with your Binance API key and secret API_KEY_BINANCE = 'your_api_key' API_SECRET_BINANCE = 'your_api_secret' symbol = 'BCHUSDT' timestamp = int(dt.datetime.now().timestamp() * 1000) # Create signature params = urlencode({'timestamp': timestamp}) signature = hmac.new(API_SECRET_BINANCE.encode('utf-8'), params.encode('utf-8'), hashlib.sha256).hexdigest() # Add signature and API key to request parameters params += f'&signature={signature}' url = 'https://api.binance.com/api/v3/account' headers = { 'X-MBX-APIKEY': API_KEY_BINANCE } # Make API request to check connection response = requests.get(url, headers=headers, params=params) print(response.json())Make sure to replace
'your_api_key'and'your_api_secret'with your actual Binance API key and secret. Also, ensure that your system's time is properly synchronized with a reliable time source. - User
But wait !!! I am UTC=3
- Assistant
If you are in a UTC+3 timezone, you can adjust the timestamp calculation in the code to be compatible with Binance's server time. Here's an updated version of the code considering your UTC+3 timezone:
import datetime as dt import requests import hmac import hashlib from urllib.parse import urlencode # Replace with your Binance API key and secret API_KEY_BINANCE = 'your_api_key' API_SECRET_BINANCE = 'your_api_secret' symbol = 'BCHUSDT' timestamp = int((dt.datetime.utcnow() + dt.timedelta(hours=3)).timestamp() * 1000) # Create signature params = urlencode({'timestamp': timestamp}) signature = hmac.new(API_SECRET_BINANCE.encode('utf-8'), params.encode('utf-8'), hashlib.sha256).hexdigest() # Add signature and API key to request parameters params += f'&signature={signature}' url = 'https://api.binance.com/api/v3/account' headers = { 'X-MBX-APIKEY': API_KEY_BINANCE } # Make API request to check connection response = requests.get(url, headers=headers, params=params) print(response.json())In this code,
dt.datetime.utcnow()is used instead ofdt.datetime.now()to get the current UTC time. Additionally,dt.timedelta(hours=3)is added to offset the time by 3 hours to match your UTC+3 timezone. Adjust the offset value as necessary based on your specific timezone. - User
I getting ERROR after order is executed : The signal time is: 2023-07-24 18:27:00 - Signals: buy Traceback (most recent call last): File "c:\Users\service.vscode\jew_bot.py", line 190, in client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=0.1) File "C:\Users\service\AppData\Local\Programs\Python\Python311\Lib\site-packages\binance\um_futures\account.py", line 113, in new_order return self.sign_request("POST", url_path, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\service\AppData\Local\Programs\Python\Python311\Lib\site-packages\binance\api.py", line 83, in sign_request return self.send_request(http_method, url_path, payload, special) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\service\AppData\Local\Programs\Python\Python311\Lib\site-packages\binance\api.py", line 118, in send_request self._handle_exception(response) File "C:\Users\service\AppData\Local\Programs\Python\Python311\Lib\site-packages\binance\api.py", line 172, in _handle_exception raise ClientError(status_code, err["code"], err["msg"], response.headers)
- Assistant
The error message suggests that there is a
ClientErrorbeing raised when executing thenew_order()method from theUMFuturesclient. The specific error details are not shown in the provided traceback.To troubleshoot the issue, you can try the following steps:
-
Check the status code, error code, and error message returned by the API call. You can print the values from the
ClientErrorexception to get more specific error information.except ClientError as e: print(f"Status Code: {e.status_code}") print(f"Error Code: {e.error_code}") print(f"Error Message: {e.error_message}") -
Verify that you have sufficient account balance and permissions for executing the order. Make sure you have enough funds in your account and the correct trading permissions set.
-
Double-check the symbol you are using for the order. Ensure that it is a valid trading pair on the Binance Futures platform.
-
Check if there are any specific order parameters that need to be provided according to the Binance API documentation. For example, some orders may require additional parameters like stop price, quantity, or leverage.
-
Ensure that the API key and secret you are using are correct and have the necessary permissions for trading.
By following these steps and inspecting the specific error details, you should be able to identify and resolve the issue causing the
ClientErrorwhen executing the order. -
- User
Okay, how my code need to seem qith your advices ?
- Assistant
Here's an updated version of your code with the suggested changes to handle the
ClientErrorand print the specific error details:from binance.um_futures import UMFutures from binance.exceptions import ClientError # Replace with your Binance API key and secret API_KEY_BINANCE = 'your_api_key' API_SECRET_BINANCE = 'your_api_secret' symbol = 'BCHUSDT' client = UMFutures(key=API_KEY_BINANCE, secret=API_SECRET_BINANCE) try: response = client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=0.1) print(response) except ClientError as e: print(f"Status Code: {e.status_code}") print(f"Error Code: {e.error_code}") print(f"Error Message: {e.error_message}")Make sure to replace
'your_api_key'and'your_api_secret'with your actual Binance API key and secret.By enclosing the
client.new_order()call in atryblock and handling theClientError, you can print out the specific error details if the order execution encounters an issue. This will help identify the cause of the error and provide more information for troubleshooting.