Skip to content
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

Fix position card avg entry preview calculation so it matches position chart #1670

Merged
merged 3 commits into from
Dec 1, 2022

Conversation

0xwalde
Copy link
Contributor

@0xwalde 0xwalde commented Nov 23, 2022

The modifiedAverage functions in PositionCard.tsx and PositionChart.tsx are supposed to be calculating the same thing, but they get different results. It looks like the calculation in PositionCard.tsx is incorrect because it is adding the size property of the potential trade to get the new total size when it should be using sizeDelta instead.

In addition, the data being fed into the PositionCard version of the calculation can be positive or negative depending on the side of the trade, which isn't being handled quite right either.

Related issue

https://github.com/Kwenta/kwenta-private/issues/261

How Has This Been Tested?

Tested on Goerli Optimism in the 4 following scenarios:

  • Adding to an existing long position
  • Subtracting from an existing long position
  • Adding to an existing short position
  • Subtracting from an existing short position

Video:

Kwenta-avg-entry-short-fixed.mov
Kwenta-avg-entry-long-fixed.mov

@vercel
Copy link

vercel bot commented Nov 23, 2022

@0xwalde is attempting to deploy a commit to the Kwenta Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Nov 28, 2022

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated
kwenta ✅ Ready (Inspect) Visit Preview Dec 1, 2022 at 5:28PM (UTC)

@vercel vercel bot temporarily deployed to Preview November 28, 2022 13:05 Inactive
Copy link
Contributor

@Tburm Tburm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think this presents an incorrect average price. I tested reducing a long position, and it decreases my average price. This is incorrect, if I buy 1 unit at $1000 then sell .5 units my average price is still $1000/unit.

The expected behavior to match the subgraph calculation would be:

  • If increasing position size calculate a new average
  • If decreasing position size the average stays the same
  • If switching sides from long -> short or short -> long, the new average price is the current oracle price

If you want to take another shot at fixing this let me know, otherwise I can pick up where you left off. Thanks for pointing this out!

@0xwalde
Copy link
Contributor Author

0xwalde commented Nov 29, 2022

Ah yeah, thanks for the feedback Troy! I was mainly concerned with making the card preview match the chart preview, but you're right that they're both wrong in several of those scenarios.

I pushed up another commit with some new calculation logic that should handle all of the cases you pointed out-- let me know if that seems to behave better. Thanks again!

@0xwalde 0xwalde requested review from Tburm and removed request for platschi November 29, 2022 20:10
@vercel vercel bot temporarily deployed to Preview December 1, 2022 17:28 Inactive
@platschi platschi merged commit 2f70df2 into Kwenta:dev Dec 1, 2022
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

Successfully merging this pull request may close these issues.

3 participants