The average price of several purchases is total input spent divided by total output acquired. An ordinary average of individual fill prices gives the wrong result when the fills have different sizes.
Combine unequal fills
Assume a hypothetical acquisition with two fills: 10 tokens at 2 stablecoins each, followed by 90 tokens at 2.20 each. The total spending is 20 + 198 = 218 stablecoins for 100 tokens. Average execution price is 2.18.
The unweighted average of 2 and 2.20 is 2.10. It understates the cost because the larger fill occurred at the higher price. Quantity weights are 10% and 90%, producing 0.10 × 2 + 0.90 × 2.20 = 2.18.
Choose the price direction
If you want output per unit of input, calculate 100 / 218, approximately 0.458716 tokens per stablecoin. Do not average reciprocal prices using the same weights and expect the reciprocal of 2.18.
Uniswap's execution-price definition uses sent and received quantities. Aggregating those quantities first preserves the same meaning across several fills.
Decide where separate costs belong
If the two fills incur another four stablecoins of separate costs, all-in acquisition cost becomes 222 / 100 = 2.22 per token. Keep the token execution price of 2.18 and all-in acquisition price of 2.22 distinct.
Use a consistent unit of account when fills pay fees in other assets. If the fills occur at different times, state the valuation convention for converting those fees. For sales, aggregate sale proceeds and quantities sold; the useful price orientation may be reversed. Also report the period and completed quantity so a favorable partial-fill average is not mistaken for the price of a fully completed intended trade.
Sources & verification (1)
Source-check date is recorded in the article details. URLs are provided for manual verification. Use Copy to keep this page open.
- Pricing with the v2 SDK
Distinction between mid price and average execution price.
https://developers.uniswap.org/docs/sdks/v2/guides/pricing