I used your code: balanceinfo = client.balance() usdtbalance = None for balance in…
- User
I used your code: balance_info = client.balance()
usdt_balance = None
for balance in balance_info: if balance['asset'] == 'USDT': usdt_balance = round(float(balance['balance']), 2)
def get_quantity(symbol, usdt_balance): try: mark_price_data = client.ticker_price(symbol=symbol) mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0
token_price = mark_price margin = 50 quantity = round(float(usdt_balance) * margin / token_price, 2) return quantity except BinanceAPIException as e: print("Error executing long order:") return 0print(get_quantity(symbol, usdt_balance))
while True: try: lookback = 10080 interval = '1m' df = get_klines(symbol, interval, lookback)
if df is not None: signals = signal_generator(df) order_quantity = get_quantity(symbol, usdt_balance) mark_price_data = client.ticker_price(symbol=symbol) mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else 0.0 print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} Price: [{mark_price}] - Signals: {signals}") if signals == ['buy']: try: client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=order_quantity) print("Long order executed!") except BinanceAPIException as e: print("Error executing long order:") time.sleep(1) if signals == ['sell']: try: client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=order_quantity) print("Short order executed!") except BinanceAPIException as e: print("Error executing short order:") time.sleep(1) except BinanceAPIException as e: print("Error executing short order:") time.sleep(1)But it doesn't executing buy order, please solve this problem
- Assistant
There are a few potential reasons why your code may not be executing the buy order. Here are some possible solutions:
-
Double-check the symbol variable: Make sure the "symbol" variable is initialized correctly and matches the trading pair you want to use for buying. For example, if you want to trade BTC/USDT, the symbol variable should be set as 'BTCUSDT'.
-
Check for available USDT balance: Ensure that you have sufficient USDT balance in your Binance account to execute the buy order. If your USDT balance is too low, the order may not be executed.
-
Verify the calculation of "order_quantity": Review the calculation in the "get_quantity" function to ensure it accurately calculates the order quantity based on the available USDT balance and token price. You can print out the calculated "quantity" value before executing the buy order to verify its accuracy.
-
Handle exceptions properly: Update the exception handling in your code to print the specific error message received from the Binance API. This can help identify any errors occurring during the order execution process.
-
- User
Give me code which will execute buy orders too
- Assistant
Certainly! Here's an updated version of your code that includes the buy order execution:
- User
But it wrong code , I don't need order_quantity = get_quantity(symbol, usdt_balance) execute_long_order(symbol, order_quantity) , I need client.new_order()
- Assistant
- User
Where is sell order executing system and for get usdt_balance you need this code:for balance in balance_info: if balance['asset'] == 'USDT': usdt_balance = round(float(balance['balance']), 2)
- Assistant