0

I have the following code which calculates the maximum order quantity permissible given a price impact of i, which I wrote based on this explanation of price impact:

https://dailydefi.org/articles/price-impact-and-how-to-calculate/

and this answer:

Understand price impact and liquidity in pancakeswap

Now I would like to modify this to instead determine the order size necessary to bring the spread between the two pools to 1.0 (same price on both).

def calculate_swap(self, qty=0, max_impact=0.01, token=0):
    """
    Calculate amount received and execution price for a swap
    with price impact.

    Pool info
    USDC = 2,000,000
    ETH = 1,000
    Constant Product = 2,000,000,000
    Market Price = 2,000
    First example, 10,000 USDC for ETH
    After swap
    USDC = 2,010,000 (because we added 10,000 to the pool)
    Constant Product = 2,000,000,000 (stays the same)
    ETH = 995.024 (constant product / new usdc amount)
    ETH recieved = 4.976 (old eth amount - new eth amount)
    Price paid per ETH = 2009.64 USDC
    ETH recieved = 4.976 (old eth amount - new eth amount)
    Price impact = 0.48%

    :param qty: of sell token
    :param token: index of sell token
    :return: amount received, price paid, price impact, qty
    """



    fee = 0.003
    r0, r1 = self.reserves()
    r0 = (r0 / (10 ** self.pair.base_decimals))
    r1 = (r1 / (10 ** self.pair.quote_decimals))
    constant_product = r0 * r1

    token0_price = r1 / r0
    token1_price = r0 / r1
    if qty == 0 and token == 0:
        qty = r0 * max_impact / ((1 - max_impact) * (1 - fee))
    if qty == 0 and token == 1:
        qty = r1 * max_impact / ((1 - max_impact) * (1 - fee))
    if token == 0:
        old_price = token1_price
        new_r0 = r0 + (qty * (1 - fee))
        new_constant_product = constant_product + (1 - (qty * (1 - fee)))
        new_r1 = new_constant_product / new_r0
        new_price = new_r0 / new_r1
        price_impact = ((new_price / old_price) - 1) * 100
        received = r1 - new_r1
        return received, new_price, price_impact, qty
    else:
        old_price = token0_price
        new_r1 = r1 + (qty * (1 - fee))
        new_constant_product = constant_product + (1 - (qty * (1 - fee)))
        new_r0 = new_constant_product / new_r1
        new_price = new_r1 / new_r0
        price_impact = ((new_price / old_price) - 1) * 100
        received = r0 - new_r0
        return received, new_price, price_impact, qty

Currently, what I am doing is first determining the quantity based on the supplied max_price_impact parameter, and then feeding that quantity into my smart contract's quote function. But surely this is not ideal. Math was never my best subject and I don't know how to modify this formula to determine the quantity needed to converge the price on both exchanges.

Here is some example output from running my program:

[+] Calculating. Spread: 1.0007158869188668 .. 
{'Qty': 0.17514863141096126, 'Received': '289.3469742446323/288.46620750176953', 'Price': '0.000603971757515697/0.0006071421600407992', 'Impact': '0.15633445380558442/0.6026959610538452', 'SellAsset': 'weth', 'BuyAsset': 'AAVE', 'SellEx': 'sushiswap', 'BuyEx': 'quickswap', 'Spread': 1.0030532752882584}

I'm also not entirely sure that I am calculating the prices correctly here. I know that they are very, very close, but I think that they are not 100% accurate.

Any insight would be appreciated!

1
  • have a good time with this python code. but your code is not practical. because the time this code uses to calculate optimal input is equal to the time other arbitrageurs use to calculate the input and place an order. Nov 14, 2023 at 15:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.