class Binance_API():
api_url = 'https://fapi.binance.com'
api_key = None
secret_key = None
def __init__(self, api_key, secret_key):
self.api_key = api_key
self.secret_key = secret_key
def getSignature(self, params):
param_str = '&'.join([f'{k}={v}' for k,v in params.items()])
hash = hmac.new(bytes(self.secret_key, 'utf-8'), param_str.encode('utf-8'), hashlib.sha256)
return hash.hexdigest()
def HTTP_Request(self, endpoint, method, params):
header = {
'X-MBX-APIKEY': self.api_key
}
params['timestamp'] = int(time.time() * 1000)
params['signature'] = self.getSignature(params)
try:
if method == 'GET':
response = requests.get(url=self.api_url + endpoint, params=params, headers=header)
elif method == 'POST':
response = requests.post(url=self.api_url + endpoint, params=params, headers=header)
response.raise_for_status()
return response.json()
except RequestException as e:
print(f"Request failed: {e}")
print("Retrying in 60 seconds...")
time.sleep(60) # Ждем 60 секунд перед повторной попыткой запроса
return None
def create_market_order(self, symbol, side, qnt):
endPoint = '/fapi/v1/order'
method = 'POST'
params = {
"symbol": symbol,
"side": side,
"quantity": qnt,
"type": "MARKET",
'recvWindow': 5000
}
try:
response = self.HTTP_Request(endpoint=endPoint, method=method, params=params)
return response
except (RequestException, HTTPError) as e:
print(f"Error creating market order: {e}")
return None
def create_take_profit(self, symbol, side, qnt, price):
endPoint = '/fapi/v1/order'
method = 'POST'
params = {
"symbol": symbol,
"side": side,
"quantity": qnt,
"type": "TAKE_PROFIT",
"price": price
}
print(f"Take Request: {params}")
response = self.HTTP_Request(endpoint=endPoint, method=method, params=params)
print(f"Take Response: {response}")
if response is not None and 'code' in response and response['code'] != 200:
print(f"Error creating take-profit order. Server response: {response}")
else:
print("Take-profit order created successfully")
return response
def create_stop(self, symbol, side, stopPrice):
endPoint = '/fapi/v1/order'
method = 'POST'
params = {
"symbol": symbol,
"side": side,
"type": "STOP",
"price": stopPrice
}
print(f"Stop Request: {params}")
response = self.HTTP_Request(endpoint=endPoint, method=method, params=params)
print(f"Stop Response: {response}")
if response is not None and 'code' in response and response['code'] != 200:
print(f"Error creating stop order. Server response: {response}")
else:
print("Stop order created successfully")
return response
Please provide more details, e.g. what’s the error message do you see? what is the log do you get?
thanks