Member-only story
Automating Profits with Crypto Arbitrage Bot
Print money with a crypto arb bot
Crypto arbitrage bot. Part 2
You can read the first part of the series here.
The function calculate_profit_with_fees_and_slippage calculates the potential profit from buying a cryptocurrency on one exchange and selling it on another, taking into account slippage, withdrawal fees, and trading fees.
def calculate_profit_with_fees_and_slippage(buy_exchange, sell_exchange, buy_price, sell_price, amount, withdrawal_fees, trading_fees, symbol, buy_order_book, sell_order_book):
try:
# Estimate slippage
buy_slippage = estimate_slippage(buy_order_book, amount, 'buy')
sell_slippage = estimate_slippage(sell_order_book, amount, 'sell')
# Adjust prices for slippage
adjusted_buy_price = buy_price * (1 + buy_slippage)
adjusted_sell_price = sell_price * (1 - sell_slippage)
buy_fee_rate = trading_fees.get(buy_exchange, {}).get(symbol, {}).get('taker', 0.001)
if buy_fee_rate is None:
print(f"Warning: Using default taker fee for {buy_exchange} {symbol}")
buy_fee_rate = 0.001
buy_fee = amount * buy_fee_rate
coins_bought = (amount - buy_fee) / adjusted_buy_price
coin = symbol.split('/')[0]
withdrawal_fee = withdrawal_fees.get(buy_exchange, {}).get(coin, 0) or 0
coins_after_withdrawal = coins_bought - withdrawal_fee
sell_fee_rate = trading_fees.get(sell_exchange, {}).get(symbol…