# Using Calculus to optimise Arbitrage Bots

# What is Arbitrage?

Arbitrage is the force which drives efficient markets. What it basically means, is an instantaneous profit without risk.

one easy way to explain a practical way of implementing it is with BSC Dexes.

For example, if BNB costs $400 on Pancake Swap, and it costs $380 on BakerySwap. Clearly there is an opportunity to leverage the difference in price.

So you might pay $380 to buy BNB on Bakery Swap, and then sell that for $400 on pancakeswap. Making a $20 profit, without risk. you might say there is risk that price moves between buying and selling, but actually the arbitrage bots utilise smarts contracts, which places both transactions in the one transaction, so if it fails the transaction reverts.

there are two important things to consider in arbitrage trading

- transaction fees
- the impact on the price your transaction will have

# How to work out your return

using the example above, we need to work out an equation to work out profits

Profits = Amount_purchased * ( price1 - price2) - Gas fees - Amount_purchased * price_impact - Amount purchased * trading_fees

but the price impact depends on the amount you purchase, so we need to look into the mechanics of automated market makers (i.e. pancakeswap) a little to explore this further

# Price impact of AMM's

An automatic market Maker (AMM) is very simple in operation. It essentially holds reserve of two coins in reserve

Lets denote these as reserve0 and reserve1 so the price of coin0 = reserve1/reserve0 the price of coin1 = reserve0/reserve1

if someone wishes to trade, they will add to the reserves of the coin they are selling and reduce the reserves of the coin they are buying. However the AMM is structured so that reserve0 * reserve1 = k (and k is fixed until further liquidity is added or removed).

In order to maintain k, the amount exchanged needs to differ from the ideal price, and here in lies the market impact.

There is a complicated formula for doing this, but for our purposes, a good approximation for market impact is the amount you want to trade divided by the reserves.

Therefore price impact = trade_amount / reserves (in that coin)

# Return allowing for price impact

This is where we need to start using variables to make the formulas easier Let y = profit x = amount traded f = fees (as a percentage) g = gas (absolute value) p0 = price AMM 0 p1 = price AMM 1 r0 = reserves AMM 0 r1 = reserves AMM 1

so the profit would be

y = x * ((p0/p1-1) - fees - x/r0 -x/r1) - g

so another way of expressing this is

y = -(r0+r1)/(r0r1)*x^2 + (p0/p1-1-fees)*x - g

whoah, this is starting to look very mathematical, but necessary.

# How do we work out how much to buy?

This is where we need to get those high school text books out again, and remember how to maximize a function. The answer is, we take the derivative, and set that to 0, and then solve to X. the reason this works is that when the derivative is at 0, the curve is changing direction, meaning that is the peak.

so in our example

dy/dx = -2(r0+r1)/(r0r1)*x + (p0/p1-1-fees)

now solving for dy/dx = 0 0 = -2(r0+r1)/(r0r1)*x + (p0/p1-1-fees)

therefore:

x = (p0/p1-1-fees) * r0r1/(2(r0+r1))

# An Example

Lets say, the price of BNB is $380 on Pancake swap, and $400 on bakerswap

p0/p1 -1 = 400/380-1 = 5.2% raw return

Lets say the fees are 0.25% on PCS and baker swap

So the return after fees = 5.2% - 0.25%*2 = 4.7%

Now the reserves are say $100,000 on PCS and $50,000 on Bakeryswap (I know this is unrealistic, but small reserves make the example work better)

so the optimal volume is x = 4.7% * 5,000,000,000/(300,000) = $783

Lets find out the profit (assuming no gas)

y = x * ((p0/p1-1) - fees - x/r0 -x/r1) - g

= 783*(5.2% - 0.5% - 783/100000 - 783/50000) =783* (5.2% - 0.5% - 2.35% ) = $18.41

so the profit on this trade is $18.41

now lets try trading $1000, and see if we make more or less

= 1000 *(5.2% - 0.5% - 1000/100000 - 1000/50000) = 1000 * (5.2% - 0.5% -3%) = $17

So we made less money despite trading more, this shows the optimization using calculus really works. The graph at the top of this article is this example, the blue line is the profits, the orange is the derivate at the maximum profit level.

# So why talk about this rather than just implement it and make money

so I tried writing a program that finds opportunities, and it was successful on finding opportunities, and then I tried adding a transaction in the bot after I find them, and almost 100% of the time, another bot jumps in front and trades before me eliminating the opportunity, they usually only last less than 5 seconds. So to be a winner on this, you need to be a great blockchain programmer, that can optimize your program. I also suspect they might run nodes themselves so they can get a view of the transactions before they are processed.

I might be great at financially optimizing things, but programming is not my strong skill, be interested if anyone else has tried this and had success.

Posted Using LeoFinance Beta