# How can I calculate the amount of volume to use to ensure highest profit on an arbitrage trade of two Constant Product Market Making exchanges? [closed]

If there exists an arbitrage opportunity between two Constant Product Market Making exchanges, how can you confidently determine the maximum volume to use in order to ensure highest profit?

I can imagine incrementing and testing various values, but it seems extremely inneficient.

As an example:

These exchanges do not have an orderbook, but rather provide a varying rate that depends upon the volume of the input. A rough example would be the following:

If I were to buy 1 share of AAPL, I would get it for $174.13. If I were to buy 2 shares of AAPL, I would get it for less, maybe$174.10.

The rates are derived from the amount of liquidity in the market (imagine a pool of AAPL stock). If there are 1000 AAPL in the pool, and you tried to buy 999 of them, the price would be in the millions of dollars, because you will have effectively depleted the pool.