在运行我的代码时,我一直得到相同的错误,qty无效,但我给它的最低要求值,并获得了足够的帐户余额,正如你所看到的。
已经检查了很多问题在谷歌上,但无法找到这个具体的错误。Starting USDT balance: 99.97 No BTC in the account. Buying 0.001 BTC... Placing market buy order for 0.00100000 BTC at 28254.80 USDT/BTC Order response: {'ret_code': 10001, 'ret_msg': 'params error: qty invalid', 'result': {}, 'ext_code': '', 'ext_info': '', 'time_now': '1680641484.301085', 'rate_limit_status': 99, 'rate_limit': 100, 'rate_limit_reset_ms': 1680641484300}
Python代码:
import requests
import time
import hmac
import hashlib
from urllib.parse import urlencode
# API keys
API_KEY = 'API KEY'
API_SECRET = 'API SECRET'
# Base URLs
BYBIT_BASE_URL = 'https://api.bybit.com'
# Constants
SYMBOL = 'BTCUSDT'
ORDER_TYPE = 'Market'
FIXED_ORDER_QTY_BTC = 0.001
QUANTITY_STEP_SIZE = 0.001
def generate_signature(secret, params):
query_string = urlencode(sorted(params.items()))
hmac_sha256 = hmac.new(secret.encode('utf-8'), query_string.encode('utf-8'), hashlib.sha256)
return hmac_sha256.hexdigest()
def get_balance(coin):
timestamp = int(time.time() * 1000)
params = {
'api_key': API_KEY,
'timestamp': timestamp,
'coin': coin
}
params['sign'] = generate_signature(API_SECRET, params)
url = f"{BYBIT_BASE_URL}/v2/private/wallet/balance"
response = requests.get(url, params=params)
return response.json()
def place_order(side, qty, order_type):
timestamp = int(time.time() * 1000)
params = {
'api_key': API_KEY,
'timestamp': timestamp,
'symbol': SYMBOL,
'side': side,
'order_type': order_type,
'qty': qty,
'time_in_force': 'GoodTillCancel',
'reduce_only': False
}
params['sign'] = generate_signature(API_SECRET, params)
url = f"{BYBIT_BASE_URL}/v2/private/order/create"
response = requests.post(url, data=params)
return response.json()
def get_ticker_price(symbol):
url = f"{BYBIT_BASE_URL}/v2/public/tickers?symbol={symbol}"
response = requests.get(url)
return response.json()
def main():
# Get current USDT balance
usdt_balance = float(get_balance('USDT')['result']['USDT']['available_balance'])
print(f"Starting USDT balance: {usdt_balance:.2f}")
# Check if BTC balance is zero
btc_balance = float(get_balance('BTC')['result']['BTC']['available_balance'])
if btc_balance == 0:
print(f"No BTC in the account. Buying {FIXED_ORDER_QTY_BTC} BTC...")
ticker_price = float(get_ticker_price(SYMBOL)['result'][0]['last_price'])
order_qty = round(FIXED_ORDER_QTY_BTC / QUANTITY_STEP_SIZE) * QUANTITY_STEP_SIZE
print(f"Placing market buy order for {order_qty:.8f} BTC at {ticker_price:.2f} USDT/BTC")
response = place_order('Buy', order_qty, ORDER_TYPE)
print(f"Order response: {response}")
while True:
# Get current BTC balance
btc_balance = float(get_balance('BTC')['result']['BTC']['available_balance'])
if btc_balance >= order_qty:
print(f"Bought {order_qty:.8f} BTC")
break
else:
print("Waiting for BTC to be bought...")
time.sleep(10)
# Get updated USDT balance
usdt_balance = float(get_balance('USDT')['result']['USDT']['available_balance'])
print(f"Updated USDT balance: {usdt_balance:.2f}")
if __name__ == '__main__':
main()
尝试通过API购买最小数量的BTC。
1条答案
按热度按时间5kgi1eie1#
在这里你可以使用官方的API测试代码,只需要从终端删除测试,并把你的api信息