create account

Using Calculus to optimise Arbitrage Bots by notak

View this thread on: hive.blogpeakd.comecency.com
· @notak ·
$63.04
Using Calculus to optimise Arbitrage Bots
![image.png](https://i.imgur.com/BWn1lU5.png)
# 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
1. transaction fees 
2. 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 <sup>Beta</sup>](https://leofinance.io/@notak/using-calculus-to-optimise-arbitrage-bots)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 193 others
properties (23)
authornotak
permlinkusing-calculus-to-optimise-arbitrage-bots
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["leo","defi","arbitrage","stem","math","leofinance"],"canonical_url":"https://leofinance.io/@notak/using-calculus-to-optimise-arbitrage-bots","image":["https://i.imgur.com/BWn1lU5.png"]}
created2022-01-14 23:29:33
last_update2022-01-14 23:29:33
depth0
children7
last_payout2022-01-21 23:29:33
cashout_time1969-12-31 23:59:59
total_payout_value31.577 HBD
curator_payout_value31.459 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,162
author_reputation138,836,657,829,495
root_title"Using Calculus to optimise Arbitrage Bots"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id109,494,643
net_rshares41,820,686,005,267
author_curate_reward""
vote details (257)
@ecency ·
**Yay!** 🤗<br>Your content has been **boosted with Ecency Points**, by @notak. <br>Use Ecency daily to boost your growth on platform! <br><br><b>Support Ecency</b><br>[Vote for new Proposal](https://hivesigner.com/sign/update-proposal-votes?proposal_ids=%5B197%5D&approve=true)<br>[Delegate HP and earn more](https://ecency.com/hive-125125/@ecency/daily-100-curation-rewards)
properties (22)
authorecency
permlinkre-2022115t75921511z
categoryhive-167922
json_metadata{"tags":["ecency"],"app":"ecency/3.0.16-welcome","format":"markdown+html"}
created2022-01-15 07:59:21
last_update2022-01-15 07:59:21
depth1
children0
last_payout2022-01-22 07:59:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length375
author_reputation618,450,550,057,772
root_title"Using Calculus to optimise Arbitrage Bots"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id109,502,667
net_rshares0
@josediccus ·
$1.08
Well, I know about abitration, happens sometimes on hive-engine. Like you pointed out at the end, it's all about the speed, I didn't even know it could be run by bots, until you actually mentioned it. 

Posted Using [LeoFinance <sup>Beta</sup>](https://leofinance.io/@josediccus/re-notak-eresc)
👍  ,
properties (23)
authorjosediccus
permlinkre-notak-eresc
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hive-167922","leofinance"],"canonical_url":"https://leofinance.io/@josediccus/re-notak-eresc"}
created2022-01-15 01:19:57
last_update2022-01-15 01:19:57
depth1
children1
last_payout2022-01-22 01:19:57
cashout_time1969-12-31 23:59:59
total_payout_value0.542 HBD
curator_payout_value0.542 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length294
author_reputation2,583,710,421,042,936
root_title"Using Calculus to optimise Arbitrage Bots"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id109,496,631
net_rshares721,560,835,997
author_curate_reward""
vote details (2)
@vimukthi ·
Arbitrages between HIVE-Engine and STEEM-Engine have had some incredibly profitable trades in my experience. 
!PIZZA 

Posted Using [LeoFinance <sup>Beta</sup>](https://leofinance.io/@vimukthi/re-josediccus-6hiprt)
properties (22)
authorvimukthi
permlinkre-josediccus-6hiprt
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hive-167922","leofinance"],"canonical_url":"https://leofinance.io/@vimukthi/re-josediccus-6hiprt"}
created2022-01-15 09:19:00
last_update2022-01-15 09:19:00
depth2
children0
last_payout2022-01-22 09:19:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length214
author_reputation495,470,059,142,241
root_title"Using Calculus to optimise Arbitrage Bots"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id109,504,022
net_rshares0
@pizzabot · (edited)
RE: Using Calculus to optimise Arbitrage Bots
<center>PIZZA! PIZZA! 


PIZZA Holders sent <strong>$PIZZA</strong> tips in this post's comments:
vimukthi tipped josediccus (x1)
@vimukthi<sub>(2/5)</sub> tipped @notak (x1)


<sub>Join us in <a href="https://discord.gg/hivepizza">Discord</a>!</sub></center>
properties (22)
authorpizzabot
permlinkre-using-calculus-to-optimise-arbitrage-bots-20220115t091956z
categoryhive-167922
json_metadata"{"app": "beem/0.24.26"}"
created2022-01-15 09:19:57
last_update2022-01-15 09:21:45
depth1
children0
last_payout2022-01-22 09:19:57
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length259
author_reputation7,503,882,671,105
root_title"Using Calculus to optimise Arbitrage Bots"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id109,504,038
net_rshares0
@tokenizedsociety ·
$1.03
Great guide.

I was familiar with the concept but this is the first time I saw a scientific breakdown of it. Thanks for sharing

Posted Using [LeoFinance <sup>Beta</sup>](https://leofinance.io/@tokenizedsociety/re-notak-6ew3mn)
👍  
properties (23)
authortokenizedsociety
permlinkre-notak-6ew3mn
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hive-167922","leofinance"],"canonical_url":"https://leofinance.io/@tokenizedsociety/re-notak-6ew3mn"}
created2022-01-15 13:08:42
last_update2022-01-15 13:08:42
depth1
children0
last_payout2022-01-22 13:08:42
cashout_time1969-12-31 23:59:59
total_payout_value0.517 HBD
curator_payout_value0.517 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length227
author_reputation129,401,698,517,947
root_title"Using Calculus to optimise Arbitrage Bots"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id109,508,077
net_rshares691,483,395,831
author_curate_reward""
vote details (1)
@vimukthi ·
Excellent piece of writing that explain the basics. Too bad I don't know enough about programming to create the bots needed to get the best out of these arbitrage opportunities.
!PIZZA
!LUV 

Posted Using [LeoFinance <sup>Beta</sup>](https://leofinance.io/@vimukthi/re-notak-3fv7wn)
properties (22)
authorvimukthi
permlinkre-notak-3fv7wn
categoryhive-167922
json_metadata{"app":"leofinance/0.2","format":"markdown","tags":["hive-167922","leofinance"],"canonical_url":"https://leofinance.io/@vimukthi/re-notak-3fv7wn"}
created2022-01-15 09:20:33
last_update2022-01-15 09:20:33
depth1
children1
last_payout2022-01-22 09:20:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length282
author_reputation495,470,059,142,241
root_title"Using Calculus to optimise Arbitrage Bots"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id109,504,049
net_rshares0
@luvshares ·
<center>
    @vimukthi<sub>(1/1)</sub> gave you LUV. <a href="https://peakd.com/@luvshares/about" style="text-decoration:none"><img src="https://files.peakd.com/file/peakd-hive/crrdlx/AKU7oyCXxGwYyudB42kJ7JtoZ63bdeHvm4icoT9xdGNxA4i4BwudGyPvTQrEwPd.gif"></a> <a
    href="https://crrdlx.websavvy.work/" style="text-decoration:none">H-E tools</a> | <a 
    href="https://discord.gg/K5GvNhcPqR" style="text-decoration:none">connect</a> | <a href="https://ichthys.netlify.app" style="text-decoration:none"><><</a>
</center>
properties (22)
authorluvshares
permlinkre-re-notak-3fv7wn-20220115t092138z
categoryhive-167922
json_metadata"{"app": "beem/0.24.26"}"
created2022-01-15 09:21:39
last_update2022-01-15 09:21:39
depth2
children0
last_payout2022-01-22 09:21:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length519
author_reputation5,651,102,754,153
root_title"Using Calculus to optimise Arbitrage Bots"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id109,504,072
net_rshares0