curveresearch / curvesim

Simulates Curve Finance pools
MIT License
156 stars 32 forks source link

Exclude trading pair from multipair_optimal_arbitrage if volume limit is 0 #223

Closed nagakingg closed 1 year ago

nagakingg commented 1 year ago

Version Information

All curvesim versions for all Python versions and OSs

What's your issue about?

In the volume limited arbitrage pipeline, when a trading pair is limited to 0 volume, the trading size for that pair is still included as a free parameter to estimate in multipair_optimal_arbitrage. This can cause scipy.optimize.least_squares to hang or raise a ValueError "'x' is not within the trust region".

How can it be fixed?

Any trade with a size limit less than a pool's minimum trade size will never be executed during optimization, so they can be excluded from the least_squares optimization. When there are no eligible trades, optimization can/should be skipped.

The most simple adjustment is this, but:

def multipair_optimal_arbitrage(pool, prices, limits):
    init_trades = get_arb_trades(pool, prices)

    limited_init_trades = []
    for t in init_trades:
        size, pair, price_target = t
        limit = int(limits[pair] * 10**18)
        t = min(size, limit), pair, price_target, 0, limit + 1
        if t[0] > pool.get_min_trade_size(pair[0]):  # Only include trade if it is possible for it to be executed
            limited_init_trades.append(t)

    if not limited_init_trades: # Skip optimization if no eligible trades
        return [], [], []

[...]

To do this more cleanly, I propose: 1) Reorganize the above proposed code:

2) Introduce a get_price_error() function that combines the price errors returned by least_squares with price errors for any excluded pairs

chanhosuh commented 1 year ago

re: Trade objects, maybe we can extend to ArbTrade which has a price_target attribute.

I would agree we should cleanup the function. It is rather lengthy, which is why there are explanatory comments. These shouldn't really be necessary if there are smaller functions properly named with helpful signatures.