Skip to content

Commit

Permalink
Dynamic max bet amount (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Oct 25, 2024
1 parent 6312211 commit 4aea81b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 12 deletions.
9 changes: 8 additions & 1 deletion prediction_market_agent/agents/invalid_agent/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
from prediction_market_agent_tooling.markets.markets import MarketType
from prediction_market_agent_tooling.tools.is_invalid import is_invalid

from prediction_market_agent.agents.utils import get_maximum_possible_bet_amount
from prediction_market_agent.utils import APIKeys


class InvalidAgent(DeployableTraderAgent):
"""This agent works only on Omen.
Expand All @@ -32,7 +35,11 @@ def verify_market(self, market_type: MarketType, market: AgentMarket) -> bool:

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
# Keep Kelly here! See `answer_binary_market`.
return KellyBettingStrategy(max_bet_amount=5)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=1, max_=5, trading_balance=market.get_trade_balance(APIKeys())
)
)

def answer_binary_market(self, market: AgentMarket) -> ProbabilisticAnswer | None:
return ProbabilisticAnswer(
Expand Down
13 changes: 11 additions & 2 deletions prediction_market_agent/agents/known_outcome_agent/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
Result,
get_known_outcome,
)
from prediction_market_agent.agents.utils import market_is_saturated
from prediction_market_agent.agents.utils import (
get_maximum_possible_bet_amount,
market_is_saturated,
)
from prediction_market_agent.utils import APIKeys


class DeployableKnownOutcomeAgent(DeployableTraderAgent):
Expand All @@ -23,7 +27,12 @@ class DeployableKnownOutcomeAgent(DeployableTraderAgent):
supported_markets = [MarketType.OMEN]

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
return KellyBettingStrategy(max_bet_amount=2, max_price_impact=0.6)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=1, max_=2, trading_balance=market.get_trade_balance(APIKeys())
),
max_price_impact=0.6,
)

def load(self) -> None:
self.markets_with_known_outcomes: dict[str, Result] = {}
Expand Down
45 changes: 38 additions & 7 deletions prediction_market_agent/agents/prophet_agent/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
PredictionProphetAgent,
)

from prediction_market_agent.utils import DEFAULT_OPENAI_MODEL
from prediction_market_agent.agents.utils import get_maximum_possible_bet_amount
from prediction_market_agent.utils import DEFAULT_OPENAI_MODEL, APIKeys


class DeployableTraderAgentER(DeployableTraderAgent):
Expand All @@ -40,7 +41,12 @@ class DeployablePredictionProphetGPT4oAgent(DeployableTraderAgentER):
agent: PredictionProphetAgent

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
return KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.7)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=1, max_=5, trading_balance=market.get_trade_balance(APIKeys())
),
max_price_impact=0.7,
)

def load(self) -> None:
super().load()
Expand All @@ -56,7 +62,12 @@ class DeployablePredictionProphetGPT4TurboPreviewAgent(DeployableTraderAgentER):
agent: PredictionProphetAgent

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
return KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.5)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=1, max_=5, trading_balance=market.get_trade_balance(APIKeys())
),
max_price_impact=0.5,
)

def load(self) -> None:
super().load()
Expand All @@ -72,7 +83,12 @@ class DeployablePredictionProphetGPT4TurboFinalAgent(DeployableTraderAgentER):
agent: PredictionProphetAgent

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
return KellyBettingStrategy(max_bet_amount=5, max_price_impact=None)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=1, max_=5, trading_balance=market.get_trade_balance(APIKeys())
),
max_price_impact=None,
)

def load(self) -> None:
super().load()
Expand All @@ -88,7 +104,12 @@ class DeployableOlasEmbeddingOAAgent(DeployableTraderAgentER):
agent: OlasAgent

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
return KellyBettingStrategy(max_bet_amount=25, max_price_impact=0.5)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=5, max_=25, trading_balance=market.get_trade_balance(APIKeys())
),
max_price_impact=0.5,
)

def load(self) -> None:
super().load()
Expand All @@ -101,7 +122,12 @@ class DeployablePredictionProphetGPTo1PreviewAgent(DeployableTraderAgentER):
agent: PredictionProphetAgent

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
return KellyBettingStrategy(max_bet_amount=25, max_price_impact=0.7)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=5, max_=25, trading_balance=market.get_trade_balance(APIKeys())
),
max_price_impact=0.7,
)

def load(self) -> None:
super().load()
Expand All @@ -120,7 +146,12 @@ class DeployablePredictionProphetGPTo1MiniAgent(DeployableTraderAgentER):
agent: PredictionProphetAgent

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
return KellyBettingStrategy(max_bet_amount=5, max_price_impact=None)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=1, max_=5, trading_balance=market.get_trade_balance(APIKeys())
),
max_price_impact=None,
)

def load(self) -> None:
super().load()
Expand Down
16 changes: 14 additions & 2 deletions prediction_market_agent/agents/think_thoroughly_agent/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
ThinkThoroughlyWithItsOwnResearch,
ThinkThoroughlyWithPredictionProphetResearch,
)
from prediction_market_agent.agents.utils import get_maximum_possible_bet_amount
from prediction_market_agent.utils import APIKeys


class DeployableThinkThoroughlyAgentBase(DeployableTraderAgent):
Expand Down Expand Up @@ -39,15 +41,25 @@ class DeployableThinkThoroughlyAgent(DeployableThinkThoroughlyAgentBase):
model: str = "gpt-4-turbo-2024-04-09"

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
return KellyBettingStrategy(max_bet_amount=5, max_price_impact=None)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=1, max_=5, trading_balance=market.get_trade_balance(APIKeys())
),
max_price_impact=None,
)


class DeployableThinkThoroughlyProphetResearchAgent(DeployableThinkThoroughlyAgentBase):
agent_class = ThinkThoroughlyWithPredictionProphetResearch
model: str = "gpt-4-turbo-2024-04-09"

def get_betting_strategy(self, market: AgentMarket) -> BettingStrategy:
return KellyBettingStrategy(max_bet_amount=5, max_price_impact=0.4)
return KellyBettingStrategy(
max_bet_amount=get_maximum_possible_bet_amount(
min_=1, max_=5, trading_balance=market.get_trade_balance(APIKeys())
),
max_price_impact=0.4,
)


if __name__ == "__main__":
Expand Down
8 changes: 8 additions & 0 deletions prediction_market_agent/agents/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,11 @@ def get_event_date_from_question(question: str) -> DatetimeUTC | None:
return None

return event_date


def get_maximum_possible_bet_amount(
min_: float, max_: float, trading_balance: float
) -> float:
trading_balance *= 0.95 # Allow to use only most of the trading balance, to keep something to pay for fees on markets where it's necessary.
# Require bet size of at least `min_` and maximum `max_`, use available trading balance if its between.
return min(max(min_, trading_balance), max_)
16 changes: 16 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import pytest
from crewai import Task

from prediction_market_agent.agents.utils import get_maximum_possible_bet_amount
from prediction_market_agent.utils import disable_crewai_telemetry


Expand All @@ -10,3 +12,17 @@ def test_disable_crewai_telemetry() -> None:
expected_output="bar",
)
assert not t._telemetry.task_started(task=t)


@pytest.mark.parametrize(
"min_, max_, trading_balance, expected",
[
(1, 5, 3, 3 * 0.95),
(1, 5, 100, 5),
(1, 5, 0.1, 1),
],
)
def test_get_maximum_possible_bet_amount(
min_: float, max_: float, trading_balance: float, expected: float
) -> None:
assert get_maximum_possible_bet_amount(min_, max_, trading_balance) == expected

0 comments on commit 4aea81b

Please sign in to comment.