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 manifold bet profit calculation #19

Merged
merged 6 commits into from
Feb 20, 2024

Conversation

evangriffiths
Copy link
Contributor

@evangriffiths evangriffiths commented Feb 19, 2024

Fixes a bug in manifold monitoring code where per-bet profit was calculated based on a user's market 'position'. However, this was incorrect, as a user can have multiple bets on a given market, but only one position. This meant:

  1. Profit/loss was being double-counted in the case where a user has >1 bet on a given market
  2. If an agent has start_time, s and has placed at least one bet on a given market after s, its profit for that market took into account bets also made before s

Result, before:
Screenshot 2024-02-19 at 12 44 37

After:
Screenshot 2024-02-19 at 12 47 02

Summary by CodeRabbit

  • New Features

    • Enhanced functionality to calculate total fees and profits for bets in the prediction market tool.
  • Refactor

    • Improved profit calculation logic for more accurate outcomes.
    • Streamlined the process of assigning market outcomes to bets.

Copy link

coderabbitai bot commented Feb 19, 2024

Warning

Rate Limit Exceeded

@evangriffiths has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 30 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.
Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.
Please see our FAQ for further information.

Commits Files that changed from the base of the PR and between 63c3c91 and 0c924d9.

Walkthrough

The recent updates aim to streamline bet management and calculation processes in a prediction market tool. Changes include introducing methods for fee calculation and profit determination directly within the respective classes. The update also simplifies profit calculation by removing the ProfitAmount entity and enhancing the method for computing profits based on market outcomes.

Changes

File Path Changes
prediction_market_agent_tooling/markets/data_models.py - Added get_total method to ManifoldBetFees class for calculating total fees.
- Added get_resolved_boolean_outcome method to ManifoldBet class for handling outcomes.
- Added get_profit method to ManifoldBet class for computing profit based on market outcome and fees.
prediction_market_agent_tooling/markets/manifold.py - Removed ProfitAmount entity.
- Changed calculation of profit for a bet to call bet.get_profit() with market_outcome as an argument.
- Extracted assignment of market_outcome to a separate variable before creating ResolvedBet.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b04007c and af9fca8.
Files selected for processing (2)
  • prediction_market_agent_tooling/markets/data_models.py (2 hunks)
  • prediction_market_agent_tooling/markets/manifold.py (2 hunks)
Additional comments: 2
prediction_market_agent_tooling/markets/manifold.py (1)
  • 126-134: The refactoring to use bet.get_profit(market_outcome=market_outcome) for profit calculation in manifold_to_generic_resolved_bet function is a significant improvement. It aligns with the objective of accurately calculating profit on a per-bet basis rather than aggregating positions. However, ensure that the ManifoldBet.get_profit method correctly handles all edge cases, especially considering the removal of the ProfitAmount entity. This change should also be reflected in any documentation or examples that previously used ProfitAmount.
prediction_market_agent_tooling/markets/data_models.py (1)
  • 238-240: The get_total method in the ManifoldBetFees class is a straightforward and efficient way to calculate the total fees. This method enhances the class's functionality by providing a clear and concise way to get the sum of all fees. It's a good practice to encapsulate such logic within the class itself, promoting encapsulation and maintainability.

Comment on lines 263 to 275
def get_profit(self, market_outcome: bool) -> ProfitAmount:
outcome_bool = self.outcome == "YES"
profit = (
self.shares - self.amount
if outcome_bool == market_outcome
else -self.amount
)
profit -= self.fees.get_total()
return ProfitAmount(
amount=profit,
currency=Currency.Mana,
)

Copy link

Choose a reason for hiding this comment

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

The get_profit method in the ManifoldBet class introduces a more nuanced approach to calculating profit, taking into account the market outcome and fees. This method is crucial for addressing the identified issues with profit calculation. However, ensure that the logic correctly handles all scenarios, especially the subtraction of total fees from the profit and the handling of outcomes. It might be beneficial to add unit tests specifically for this method to cover various edge cases and ensure its reliability.

Would you like me to help with creating unit tests for the get_profit method to ensure its correctness across different scenarios?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between af9fca8 and e53a84c.
Files selected for processing (1)
  • prediction_market_agent_tooling/markets/data_models.py (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • prediction_market_agent_tooling/markets/data_models.py

@@ -257,6 +260,19 @@ class ManifoldBet(BaseModel):
createdTime: datetime
outcome: str

def get_profit(self, market_outcome: bool) -> ProfitAmount:
outcome_bool = self.outcome == "YES"
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be True if self.outcome == "YES" else False if self.outcome == "NO" else should_not_happen()? Just to catch potential errors if anything changes in their APIs.

bet_position = next(p for p in positions if p.contractId == bet.contractId)
profit = bet_position.profit

market_outcome = market.resolution == "YES"
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly here?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e53a84c and 9337d1f.
Files selected for processing (2)
  • prediction_market_agent_tooling/markets/data_models.py (6 hunks)
  • prediction_market_agent_tooling/markets/manifold.py (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • prediction_market_agent_tooling/markets/manifold.py
Additional comments: 1
prediction_market_agent_tooling/markets/data_models.py (1)
  • 289-299: The get_profit method in the ManifoldBet class correctly calculates profit based on the market outcome and fees. However, the calculation of profit directly subtracts self.amount from self.shares, which might not be correct without considering the price per share or the outcome. Ensure the logic aligns with the expected financial model, especially in terms of how shares and amounts are meant to interact.

Please verify the profit calculation logic with the financial model to ensure it aligns with expected outcomes, particularly how shares and amounts interact in profit calculations.

Comment on lines +255 to +256
def get_total(self) -> Decimal:
return Decimal(sum([self.platformFee, self.liquidityFee, self.creatorFee]))
Copy link

Choose a reason for hiding this comment

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

The implementation of get_total in the ManifoldBetFees class correctly sums up the fees. However, consider using Decimal directly on the list comprehension for precision in financial calculations.

- return Decimal(sum([self.platformFee, self.liquidityFee, self.creatorFee]))
+ return sum([self.platformFee, self.liquidityFee, self.creatorFee], start=Decimal(0))

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
def get_total(self) -> Decimal:
return Decimal(sum([self.platformFee, self.liquidityFee, self.creatorFee]))
def get_total(self) -> Decimal:
return sum([self.platformFee, self.liquidityFee, self.creatorFee], start=Decimal(0))

prediction_market_agent_tooling/markets/data_models.py Outdated Show resolved Hide resolved
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9337d1f and 63c3c91.
Files selected for processing (2)
  • prediction_market_agent_tooling/markets/data_models.py (6 hunks)
  • prediction_market_agent_tooling/markets/manifold.py (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • prediction_market_agent_tooling/markets/data_models.py
  • prediction_market_agent_tooling/markets/manifold.py

@evangriffiths evangriffiths merged commit 4908f23 into main Feb 20, 2024
6 checks passed
@evangriffiths evangriffiths deleted the evan/manifold-monitoring-profit-fix branch February 20, 2024 09:47
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.

2 participants