Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

How to make a trading bot use 10X Cross and not only available balance

$
0
0

I've begun coding a beginner trading bot in Python and am having some issues with getting the bot to use 100% of the available balance.

I have the following code:

        try:            balance = exchange.fetch_balance()['total']['USDT']            position_size = balance            precision = exchange.markets[symbol]['precision']['amount']            position_size = math.floor(position_size / entry_price / precision) * precision  # Round position size to the nearest precision            order = exchange.create_order(symbol, order_type, 'buy', position_size, entry_price, {'leverage': leverage})            if order:                logging.info(f'Buy order placed: {order}')                exchange.create_order(symbol, order_type, 'sell', position_size, take_profit_price, {'leverage': leverage})                logging.info('Take profit order placed.')            else:                logging.error('Failed to place buy order.')        except ccxt.InsufficientFunds as e:            logging.error(f'Insufficient funds error: {e}')            logging.info('Retrying in 10 seconds...')            time.sleep(10)            place_order()        except ccxt.BaseError as e:            logging.error(f'An error occurred: {e}')            logging.info('Retrying in 10 seconds...')            time.sleep(10)            place_order()    else:        logging.warning('No suitable entry price found.')

That's the extent of the bot for now, and I understand that when I fetch_balance, it fetches the actual balance on my account rather than the amount that I am able to trade with if I include the 10X Leverage; I tried a simple trick like:

leverage = 10position_size = balance * leverage

However, this didn't work and kept giving me an insufficient funds error. I tried doing it with a value of 9 for leverage to account for price difference, but still to no avail. Is there perhaps an easier way of utilizing 100% of the available balance to trade and not just account balance? Like forcing it to choose 100% on the QTY scroll bar in the trading interface?


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>