I used your code: while True: lookback = 10080 df = getklines(symbol, interval,…
- User
I used your code: while True: lookback = 10080 df = get_klines(symbol, interval, lookback)
codeif 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() token_price = decimal.Decimal(mark_price) margin = 50 quantity = (usdt_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("Error executing long order:") time.sleep(1) if signals == ['sell']: try: account_info = client.account() token_price = decimal.Decimal(mark_price) margin = 50 quantity = (usdt_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("Error executing short order:") time.sleep(1) time.sleep(1)But I can't understand is it / in your formula: quantity = (usdt_balance * margin) / token_price , is it divide ?
- Assistant
Yes, the "/" symbol in the formula quantity = (usdt_balance * margin) / token_price represents division. In this case, it is dividing the product of usdt_balance and margin by token_price to calculate the quantity value.
- User
But it giving me ERROR
- Assistant
If you are receiving an error, it would be helpful to know the specific error message you are encountering. However, one possible issue could be related to the data types used in the calculation.
Make sure that the data types of
usdt_balance,margin, andtoken_priceare compatible for division. They should all be numeric types such asfloatordecimal.Decimal.If any of these variables have string or other incompatible data types, you may need to convert them to numeric types using functions like
float()ordecimal.Decimal()before performing the division.For example, you could modify the calculation as follows:
pythonquantity = (float(usdt_balance) * float(margin)) / float(token_price)Alternatively, if you are using the
decimalmodule, you can convert the variables todecimal.Decimalobjects for precise decimal division:python