I used this code: while True: lookback = 10080 df = getklines(symbol, interval,…
- User
I used this code: while True: lookback = 10080 df = get_klines(symbol, interval, lookback)
if df is not None: signals = signal_generator(df) print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Signals: {signals}") mark_price = client.ticker_price(symbol=symbol) if signals == ['buy']: try: mark_price = client.ticker_price(symbol=symbol) balance_data = client.balance() usdt_balance = [balance['balance'] for balance in balance_data if balance['asset'] == 'USDT'] quantity = float(usdt_balance[0]) / float(mark_price) client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=str(quantity)) print("Long order executed!") except binance.error.ClientError as e: print("Error executing long order: ", e) time.sleep(1) if signals == ['sell']: try: mark_price = client.ticker_price(symbol=symbol) balance_data = client.balance() usdt_balance = [balance['balance'] for balance in balance_data if balance['asset'] == 'USDT'] quantity = float(usdt_balance[0]) / float(mark_price) client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=str(quantity)) print("Short order executed!") except binance.error.ClientError as e: print("Error executing short order: ", e) time.sleep(1) time.sleep(1) time.sleep(1)But I getting ERROR:Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 177, in quantity = float(usdt_balance[0]) / float(mark_price) ^^^^^^^^^^^^^^^^^ TypeError: float() argument must be a string or a real number, not 'dict'
- Assistant
The error message indicates that the variable
mark_priceis a dictionary instead of a string or a real number. The error occurs when you try to convertmark_priceto a float using thefloat()function.To resolve this issue, you need to check the structure of the
mark_pricedictionary and access the specific value that represents the price. For example, if the price value is stored in the key'price', you can modify the code as follows:mark_price = client.ticker_price(symbol=symbol)['price']This will retrieve the value from the
'price'key and assign it to themark_pricevariable. Make sure to update this change in both the 'buy' and 'sell' sections of your code. - User
But I getting ERROR: The signal time is: 2023-08-18 17:24:00 - Signals: ['buy'] Error executing long order: (400, -1111, 'Precision is over the maximum defined for this asset.', {'Date': 'Fri, 18 Aug 2023 14:24:01 GMT', 'Content-Type': 'application/json', 'Content-Length': '76', 'Connection': 'keep-alive', 'Server': 'Tengine', 'x-mbx-used-weight-1m': '-1', 'x-mbx-order-count-10s': '1', 'x-mbx-order-count-1m': '1', 'x-response-time': '1ms', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS'})
- Assistant
The error message indicates that there is an issue with the precision of the quantity you are trying to buy. The asset you are trading has a maximum precision limit, and you are exceeding that limit.
To resolve this issue, you need to round the
quantityvalue to match the precision limit of the asset. You can do this by using thedecimalmodule in Python. Here's an example of how to round the quantity to 8 decimal places:from decimal import Decimal, ROUND_DOWN # Calculate the quantity with sufficient decimal places quantity = Decimal(usdt_balance[0]) / Decimal(mark_price) quantity = quantity.quantize(Decimal('0.00000000'), rounding=ROUND_DOWN) # Convert the rounded quantity to string quantity_str = str(quantity) # Place the rounded quantity in the new order client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity_str)Make sure to update this change in both the 'buy' and 'sell' sections of your code.
- User
Give me full code please
- Assistant
Certainly! Here's the updated code with the necessary changes to handle precision errors:
from decimal import Decimal, ROUND_DOWN while True: lookback = 10080 df = get_klines(symbol, interval, lookback) if df is not None: signals = signal_generator(df) print(f"The signal time is: {dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Signals: {signals}") mark_price = client.ticker_price(symbol=symbol)['price'] if signals == ['buy']: try: mark_price = client.ticker_price(symbol=symbol)['price'] balance_data = client.balance() usdt_balance = [balance['balance'] for balance in balance_data if balance['asset'] == 'USDT'] quantity = Decimal(usdt_balance[0]) / Decimal(mark_price) quantity = quantity.quantize(Decimal('0.00000000'), rounding=ROUND_DOWN) quantity_str = str(quantity) client.new_order(symbol=symbol, side='BUY', type='MARKET', quantity=quantity_str) print("Long order executed!") except binance.error.ClientError as e: print("Error executing long order: ", e) time.sleep(1) if signals == ['sell']: try: mark_price = client.ticker_price(symbol=symbol)['price'] balance_data = client.balance() usdt_balance = [balance['balance'] for balance in balance_data if balance['asset'] == 'USDT'] quantity = Decimal(usdt_balance[0]) / Decimal(mark_price) quantity = quantity.quantize(Decimal('0.00000000'), rounding=ROUND_DOWN) quantity_str = str(quantity) client.new_order(symbol=symbol, side='SELL', type='MARKET', quantity=quantity_str) print("Short order executed!") except binance.error.ClientError as e: print("Error executing short order: ", e) time.sleep(1) time.sleep(1) - User
But I getting same ERROR
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.