Skip to content

extend flux aggregator with exponential moving average #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
defactojob opened this issue Jan 8, 2021 · 0 comments
Open

extend flux aggregator with exponential moving average #9

defactojob opened this issue Jan 8, 2021 · 0 comments

Comments

@defactojob
Copy link
Contributor

This is how Uniswap's price oracle works:

They provides exponential price average, which allows users to calculate an average price for any arbitrary elapsed time (1h, 1 day, 1week) to track the exponential moving average, uniswap records price * seconds:

price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed; 

and to calculate moving average for a period, a smart contract can record the difference between the price cumulatives, and divide by the time period, like this:

price0Average = FixedPoint.uq112x112(
            uint224((price0Cumulative - price0CumulativeLast) / timeElapsed)
        );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant