Skip to content

Commit

Permalink
hotfix - trade amounts now correctly calculated (#396)
Browse files Browse the repository at this point in the history
* Added dummy log

* 1 changes in PMAT logic

* Added test

* bumped version PMAT and updated lock

* Small tweaks

* Small tweaks 2

* Added lock

* Trying other configs for ape

* Trying other configs for ape 2

* Trying other configs for ape 3

* Reverting local chain run

* Removing unnecessary changes

* Updated lock

* Update tests/test_betting_strategy.py

Co-authored-by: Peter Jung <peter@jung.ninja>

* Update tests/test_betting_strategy.py

Co-authored-by: Peter Jung <peter@jung.ninja>

* Implemented PR comments

---------

Co-authored-by: Peter Jung <peter@jung.ninja>
  • Loading branch information
gabrielfior and kongzii authored Sep 11, 2024
1 parent fffbc83 commit d755388
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 87 deletions.
170 changes: 88 additions & 82 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion prediction_market_agent_tooling/deploy/betting_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _build_rebalance_trades_from_positions(

if prev_amount.currency != new_amount.currency:
raise ValueError("Cannot handle positions with different currencies")
diff_amount = prev_amount.amount - new_amount.amount
diff_amount = new_amount.amount - prev_amount.amount
if diff_amount == 0:
continue
trade_type = TradeType.SELL if diff_amount < 0 else TradeType.BUY
Expand Down
3 changes: 2 additions & 1 deletion prediction_market_agent_tooling/markets/agent_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ def has_successful_resolution(self) -> bool:
def has_unsuccessful_resolution(self) -> bool:
return self.resolution in [Resolution.CANCEL, Resolution.MKT]

def get_outcome_str_from_bool(self, outcome: bool) -> OutcomeStr:
@staticmethod
def get_outcome_str_from_bool(outcome: bool) -> OutcomeStr:
raise NotImplementedError("Subclasses must implement this method")

def get_outcome_str(self, outcome_index: int) -> str:
Expand Down
3 changes: 2 additions & 1 deletion prediction_market_agent_tooling/markets/omen/omen.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,8 @@ def index_set_to_outcome_str(cls, index_set: int) -> OutcomeStr:
cls.get_outcome_str(cls.index_set_to_outcome_index(index_set))
)

def get_outcome_str_from_bool(self, outcome: bool) -> OutcomeStr:
@staticmethod
def get_outcome_str_from_bool(outcome: bool) -> OutcomeStr:
return (
OutcomeStr(OMEN_TRUE_OUTCOME) if outcome else OutcomeStr(OMEN_FALSE_OUTCOME)
)
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "prediction-market-agent-tooling"
version = "0.48.12"
version = "0.48.13"
description = "Tools to benchmark, deploy and monitor prediction market agents."
authors = ["Gnosis"]
readme = "README.md"
Expand All @@ -20,7 +20,7 @@ cron-validator = "^1.0.8"
pydantic = "^2.6.1"
web3 = "^6.15.1"
eth-typing = "^3.0.0"
pydantic-settings = "^2.1.0"
pydantic-settings = "^2.4.0" #eth-ape limit
numpy = "^1.26.4"
autoflake = "^2.2.1"
isort = "^5.13.2"
Expand Down
42 changes: 42 additions & 0 deletions tests/test_betting_strategy.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
from unittest.mock import Mock

import pytest

from prediction_market_agent_tooling.deploy.betting_strategy import (
MaxAccuracyBettingStrategy,
)
from prediction_market_agent_tooling.gtypes import Probability
from prediction_market_agent_tooling.markets.data_models import (
Currency,
Position,
ProbabilisticAnswer,
TokenAmount,
TradeType,
)
from prediction_market_agent_tooling.markets.omen.omen import OmenAgentMarket


@pytest.mark.parametrize(
Expand All @@ -18,3 +29,34 @@ def test_answer_decision(
betting_strategy = MaxAccuracyBettingStrategy()
direction: bool = betting_strategy.calculate_direction(market_p_yes, estimate_p_yes)
assert direction == expected_direction


def test_rebalance() -> None:
strategy = MaxAccuracyBettingStrategy()

mock_amount = TokenAmount(amount=5, currency=Currency.xDai)
tiny_amount = TokenAmount(amount=0.0001, currency=Currency.xDai)
mock_existing_position = Position(
market_id="0x123",
amounts={
OmenAgentMarket.get_outcome_str_from_bool(True): mock_amount,
OmenAgentMarket.get_outcome_str_from_bool(False): mock_amount,
},
)
mock_answer = ProbabilisticAnswer(p_yes=Probability(0.9), confidence=0.5)
mock_market = Mock(OmenAgentMarket, wraps=OmenAgentMarket)
mock_market.get_tiny_bet_amount.return_value = tiny_amount
mock_market.current_p_yes = 0.5
mock_market.currency = Currency.xDai
mock_market.id = "0x123"

trades = strategy.calculate_trades(mock_existing_position, mock_answer, mock_market)
# assert 1 buy trade and 1 sell trade
assert len(trades) == 2
# buy trades should come first, sell trades last
buy_trade = trades[0]
assert buy_trade.trade_type == TradeType.BUY
assert buy_trade.amount.amount == (mock_amount.amount + tiny_amount.amount)
sell_trade = trades[1]
assert sell_trade.trade_type == TradeType.SELL
assert sell_trade.amount.amount == mock_amount.amount

0 comments on commit d755388

Please sign in to comment.