You gave me this algorithm: balance = float(usdtbalance) # Convert balance to a float…
- User
You gave me this algorithm: balance = float(usdt_balance) # Convert balance to a float token_price = mark_price leverage = 50 quantity = (balance * leverage) / token_price
Can you add it in my code: while True: lookback = 10080 df = get_klines(symbol, interval, lookback)
if df is not None: signals = signal_generator(df) 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: account_info = client.account() balance = float(usdt_balance) token_price = mark_price margin = 50 quantity = (balance * margin) / token_price order = client.new_order( symbol=symbol,side='BUY',type='MARKET',quantity=quantity) print("Long order executed!") except BinanceAPIException as e: print(f"Error executing long order: ") time.sleep(1) if signals == ['sell']: try: account_info = client.account() balance = float(usdt_balance) token_price = mark_price margin = 50 quantity = (balance * margin) / token_price order = client.new_order(symbol=symbol,side='SELL',type='MARKET',quantity=quantity) print("Short order executed!") except BinanceAPIException as e: print(f"Error executing short order: ") time.sleep(1) time.sleep(1) - Assistant
Here's your updated code with the algorithm added:
while True: lookback = 10080 df = get_klines(symbol, interval, lookback) if df is not None: signals = signal_generator(df) 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: account_info = client.account() usdt_balance = account_info['balances'][0]['free'] balance = float(usdt_balance) token_price = mark_price leverage = 50 quantity = (balance * leverage) / token_price order = client.new_order( symbol=symbol,side='BUY',type='MARKET',quantity=quantity) print("Long order executed!") except BinanceAPIException as e: print(f"Error executing long order: ") time.sleep(1) if signals == ['sell']: try: account_info = client.account() usdt_balance = account_info['balances'][0]['free'] balance = float(usdt_balance) token_price = mark_price leverage = 50 quantity = (balance * leverage) / token_price order = client.new_order(symbol=symbol,side='SELL',type='MARKET',quantity=quantity) print("Short order executed!") except BinanceAPIException as e: print(f"Error executing short order: ") time.sleep(1) time.sleep(1) - User
Code to get balance :usdt_balance = None
for balance in balance_info: if balance['asset'] == 'USDT': usdt_balance = balance['balance']
if usdt_balance is not None: print("USDT Balance:", usdt_balance) else: print("USDT balance not found.")
- Assistant
Here's the updated code snippet to retrieve the USDT balance:
usdt_balance = None for balance in balance_info: if balance['asset'] == 'USDT': usdt_balance = balance['balance'] if usdt_balance is not None: print("USDT Balance:", usdt_balance) else: print("USDT balance not found.")Please replace
balance_infowith the appropriate variable or function that contains the balance information. This code will assign the USDT balance to theusdt_balancevariable if it exists, and print the balance if it's found. Otherwise, it will print a message indicating that the USDT balance was not found. - User
Gvie me code with this code : usdt_balance = None
for balance in balance_info: if balance['asset'] == 'USDT': usdt_balance = balance['balance']
if usdt_balance is not None: print("USDT Balance:", usdt_balance) else: print("USDT balance not found.")
- Assistant
Certainly! Here's the updated code with
usdt_balanceincluded:while True: lookback = 10080 df = get_klines(symbol, interval, lookback) if df is not None: signals = signal_generator(df) 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}") usdt_balance = None for balance in balance_info: if balance['asset'] == 'USDT': usdt_balance = balance['balance'] if usdt_balance is not None: print("USDT Balance:", usdt_balance) else: print("USDT balance not found.") if signals == ['buy']: try: account_info = client.account() token_price = mark_price leverage = 50 quantity = (float(usdt_balance) * leverage) / token_price order = client.new_order( symbol=symbol,side='BUY',type='MARKET',quantity=quantity) print("Long order executed!") except BinanceAPIException as e: print(f"Error executing long order: ") time.sleep(1) if signals == ['sell']: try: account_info = client.account() token_price = mark_price leverage = 50 quantity = (float(usdt_balance) * leverage) / token_price order = client.new_order(symbol=symbol,side='SELL',type='MARKET',quantity=quantity) print("Short order executed!") except BinanceAPIException as e: print(f"Error executing short order: ") time.sleep(1) time.sleep(1) - User
But I getting ERROR: The signal time is: 2023-08-22 17:10:55 Price: [185.94] - Signals: ['buy'] Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 188, in order = client.new_order( ^^^^^^^^^^^^^^^^^ File "C:\Users\Alan\AppData\Roaming\Python\Python311\site-packages\binance\um_futures\account.py", line 113, in new_order return self.sign_request("POST", url_path, params) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Alan\AppData\Roaming\Python\Python311\site-packages\binance\api.py", line 83, in sign_request return self.send_request(http_method, url_path, payload, special) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Alan\AppData\Roaming\Python\Python311\site-packages\binance\api.py", line 118, in send_request self._handle_exception(response) File "C:\Users\Alan\AppData\Roaming\Python\Python311\site-packages\binance\api.py", line 172, in _handle_exception raise ClientError(status_code, err["code"], err["msg"], response.headers) binance.error.ClientError: (400, -1111, 'Precision is over the maximum defined for this asset.', {'Date': 'Tue, 22 Aug 2023 14:10:55 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': '4ms', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS'})
- Assistant
- User
ERROR: Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 207, in quantity = (usdt_balance * margin) / token_price ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~ TypeError: unsupported operand type(s) for /: 'str' and 'float'
- Assistant
Apologies for the error. It seems that the value is still being treated as a string instead of a float. To resolve this, we can update the code to convert to a float using the function.
- User
But this is same code
- Assistant
Apologies for the confusion caused. It seems that the issue still persists. Upon reviewing the code, I noticed an error in the placement of the variable assignment for .
- User
ERROR: The signal time is: 2023-08-22 17:24:24 Price: [185.95] - Signals: ['sell'] Traceback (most recent call last): File "c:\Users\Alan.vscode\jew_bot\jew_bot\jew_bot.py", line 207, in quantity = (usdt_balance * margin) / token_price ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~ TypeError: unsupported operand type(s) for /: 'str' and 'decimal.Decimal'
- Assistant
Apologies for the error. It seems there is an issue with the data type used for the
usdt_balancevariable. Let's update the code to convert it to adecimal.Decimalobject as well.Here's the updated code:
import decimal decimal.getcontext().prec = 8 while True: lookback = 10080 df = get_klines(symbol, interval, lookback) if df is not None: signals = signal_generator(df) mark_price_data = client.ticker_price(symbol=symbol) mark_price = float(mark_price_data['price']) if 'price' in mark_price_data else
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.