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

Remove cap on bet amount for microchain agent #239

Merged
merged 9 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
TokenAmount,
)
from prediction_market_agent_tooling.markets.markets import MarketType
from prediction_market_agent_tooling.tools.betting_strategies.kelly_criterion import (
KellyBet,
get_kelly_bet,
)
from prediction_market_agent_tooling.tools.utils import utcnow
from prediction_prophet.benchmark.agents import (
_make_prediction as prophet_make_prediction,
Expand Down Expand Up @@ -191,26 +195,19 @@ def __init__(self, market_type: MarketType, outcome: str, keys: APIKeys):
)
self.user_address = self.keys.bet_from_address

# Prevent the agent from spending recklessly!
self.MAX_AMOUNT = 0.1 if market_type == MarketType.OMEN else 1.0

@property
def description(self) -> str:
return (
f"Use this function to buy {self.outcome} outcome tokens of a "
f"prediction market. The first parameter is the market id. The "
f"second parameter specifies how much {self.currency} you spend."
f"This is capped at {self.MAX_AMOUNT}{self.currency}."
)

@property
def example_args(self) -> list[t.Union[str, float]]:
return [get_example_market_id(self.market_type), 2.3]

def __call__(self, market_id: str, amount: float) -> str:
if amount > self.MAX_AMOUNT:
return f"Failed. Bet amount {amount} cannot exceed {self.MAX_AMOUNT} {self.currency}."

account_balance = float(
get_balance(self.keys, market_type=self.market_type).amount
)
Expand Down Expand Up @@ -382,6 +379,38 @@ def __call__(self, n_days: int = 7) -> list[ResolvedBet]:
)


class GetKellyBet(MarketFunction):
@property
def description(self) -> str:
return (
"Use the Kelly Criterion to calculate the optimal bet size and "
"direction for a binary market. Pass in the market p_yes and your "
"estimated p_yes."
)

@property
def example_args(self) -> list[float]:
return [0.6, 0.5]

def __call__(
self,
market_p_yes: float,
estimated_p_yes: float,
) -> str:
confidence = 0.5 # Until confidence score is available, be conservative
max_bet = float(get_balance(self.keys, market_type=self.market_type).amount)
kelly_bet: KellyBet = get_kelly_bet(
market_p_yes=market_p_yes,
estimated_p_yes=estimated_p_yes,
max_bet=max_bet,
confidence=confidence,
)
return (
f"Bet size: {kelly_bet.size:.2f}{self.currency}, "
f"Bet direction: {kelly_bet.direction}"
)


# Functions that interact with the prediction markets
MARKET_FUNCTIONS: list[type[MarketFunction]] = [
GetMarkets,
Expand All @@ -394,4 +423,5 @@ def __call__(self, n_days: int = 7) -> list[ResolvedBet]:
SellNo,
GetLiquidPositions,
GetResolvedBetsWithOutcomes,
GetKellyBet,
]
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ poetry = "^1.7.1"
poetry-plugin-export = "^1.6.0"
functions-framework = "^3.5.0"
cron-validator = "^1.0.8"
prediction-market-agent-tooling = { version = "^0.43.4", extras = ["langchain", "google"] }
prediction-market-agent-tooling = { version = "^0.45.0", extras = ["langchain", "google"] }
pydantic-settings = "^2.1.0"
autoflake = "^2.2.1"
isort = "^5.13.2"
Expand Down
8 changes: 8 additions & 0 deletions tests/agents/microchain/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
BuyNo,
BuyYes,
GetBalance,
GetKellyBet,
GetMarketProbability,
GetMarkets,
PredictProbabilityForQuestion,
Expand Down Expand Up @@ -188,3 +189,10 @@ def test_remember_past_learnings(long_term_memory: LongTermMemoryTableHandler) -
model="gpt-4o-2024-05-13",
)
print(remember_past_learnings())


@pytest.mark.parametrize("market_type", [MarketType.OMEN])
def test_kelly_bet(market_type: MarketType) -> None:
get_kelly_bet = GetKellyBet(market_type=market_type, keys=APIKeys())
bet = get_kelly_bet(market_p_yes=0.1, estimated_p_yes=0.1)
assert "Bet size: 0.0" in bet # No 'edge', so no bet size
Loading