Skip to content

Commit

Permalink
Merge pull request #130 from valory-xyz/fix/kelly
Browse files Browse the repository at this point in the history
Update Kelly formula
  • Loading branch information
Adamantios authored Nov 13, 2023
2 parents 282cca8 + a4132c7 commit aef7dff
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 28 deletions.
10 changes: 5 additions & 5 deletions packages/packages.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"dev": {
"skill/valory/market_manager_abci/0.1.0": "bafybeibrjre5yka3sbvb6lsefgc5yutovb6h4ffzrpecplfkjwtl5i36te",
"skill/valory/decision_maker_abci/0.1.0": "bafybeiggsi3hyu2lqqfxh5fonkwh7rdmounrpbkaxtt6aeqm4cr362cwuy",
"skill/valory/trader_abci/0.1.0": "bafybeihqqcq63pijsep4aqyz6criwncga6dgoinkmb6ptbsrcxekw6f4xe",
"skill/valory/decision_maker_abci/0.1.0": "bafybeic6xegb3zbo46uv2q3l63itanw7m26r55ifqo4j4iophos46mppcu",
"skill/valory/trader_abci/0.1.0": "bafybeiaew6nufibg53yuzyrumd2wlqfmiyg3brcwetymvtlnehi6fwlskm",
"contract/valory/market_maker/0.1.0": "bafybeif4mm2s3gxtvp227yypkcnna5ftec7vajcftvtbdmqddh7nprah5m",
"agent/valory/trader/0.1.0": "bafybeien2oswgsiklat7snobp6tt6kwoatbhm23qdcwou4nxzkzvmybefu",
"service/valory/trader/0.1.0": "bafybeidru4qhpp7wgx2qqkg74rvp7fytvl45bj2iosalwava4j3hhsedjy",
"agent/valory/trader/0.1.0": "bafybeigdtxgx2dwsw77j6cmp3ld42cin335q5m4n34uwkwtbnageiefate",
"service/valory/trader/0.1.0": "bafybeibluamqcl2brsm2rirkudtv22gsglohwuruhhlzm6viqjcisc5y5i",
"contract/valory/erc20/0.1.0": "bafybeieqj7dea4tcv6z2yqkgtmhd23vbiycsr5trhwxdvlbwulpl6vhmam",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeibda3fyr6hbbnlz5unncuhaidznconrwqa7tqbsuxqpal2vdlalra",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeiep7dgp7hpamdw6avwku5f5wpqw6zbg4evaux6s4yna6el3jay2x4",
"contract/valory/mech/0.1.0": "bafybeihvc6btuk3nvernzcx4qpezvuhiw2wwnagqj5nkeljvszghv7mq64",
"contract/valory/realitio/0.1.0": "bafybeiamgkwwqhray4fs2hlipwxkq7mosmi7ev7jut4vneetaaycc4ruji",
"contract/valory/realitio_proxy/0.1.0": "bafybeidx37xzjjmapwacedgzhum6grfzhp5vhouz4zu3pvpgdy5pgb2fr4",
Expand Down
6 changes: 3 additions & 3 deletions packages/valory/agents/trader/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ skills:
- valory/reset_pause_abci:0.1.0:bafybeicpxn2khtaesuf4cq6ypwdmdmonlqroj2q2i6cxvpizc2y4cw66pe
- valory/termination_abci:0.1.0:bafybeieqm46zuccaagnko3qlw6p3nvoohdrfgvpmw467r5lyil2dqrzjsy
- valory/transaction_settlement_abci:0.1.0:bafybeia6cdxdlqrcwk2maw25fo7dafzd2p3rs7syropvufophk2pitzbwy
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeibda3fyr6hbbnlz5unncuhaidznconrwqa7tqbsuxqpal2vdlalra
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeiep7dgp7hpamdw6avwku5f5wpqw6zbg4evaux6s4yna6el3jay2x4
- valory/market_manager_abci:0.1.0:bafybeibrjre5yka3sbvb6lsefgc5yutovb6h4ffzrpecplfkjwtl5i36te
- valory/decision_maker_abci:0.1.0:bafybeiggsi3hyu2lqqfxh5fonkwh7rdmounrpbkaxtt6aeqm4cr362cwuy
- valory/trader_abci:0.1.0:bafybeihqqcq63pijsep4aqyz6criwncga6dgoinkmb6ptbsrcxekw6f4xe
- valory/decision_maker_abci:0.1.0:bafybeic6xegb3zbo46uv2q3l63itanw7m26r55ifqo4j4iophos46mppcu
- valory/trader_abci:0.1.0:bafybeiaew6nufibg53yuzyrumd2wlqfmiyg3brcwetymvtlnehi6fwlskm
default_ledger: ethereum
required_ledgers:
- ethereum
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/services/trader/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license: Apache-2.0
fingerprint:
README.md: bafybeigtuothskwyvrhfosps2bu6suauycolj67dpuxqvnicdrdu7yhtvq
fingerprint_ignore_patterns: []
agent: valory/trader:0.1.0:bafybeien2oswgsiklat7snobp6tt6kwoatbhm23qdcwou4nxzkzvmybefu
agent: valory/trader:0.1.0:bafybeigdtxgx2dwsw77j6cmp3ld42cin335q5m4n34uwkwtbnageiefate
number_of_agents: 4
deployment: {}
---
Expand Down
27 changes: 12 additions & 15 deletions packages/valory/skills/decision_maker_abci/behaviours/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
SAFE_GAS = 0
CID_PREFIX = "f01701220"
WXDAI = "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d"
EPSILON = 1e-7


def remove_fraction_wei(amount: int, fraction: float) -> int:
Expand Down Expand Up @@ -219,14 +220,9 @@ def _calculate_kelly_bet_amount(
self, x: int, y: int, p: float, c: float, b: int, f: float
) -> int:
"""Calculate the Kelly bet amount."""
if b == 0 or x**2 * f == y**2 * f:
self.context.logger.error(
"Could not calculate Kelly bet amount. "
"Either bankroll is 0 or pool token amount is distributed as x^2*f - y^2*f = 0:\n"
f"Bankroll: {b}\n"
f"Pool token amounts: {x}, {y}"
f"Fee, fee fraction f: {1-f}, {f}"
)
if b == 0:
error = "Cannot calculate Kelly bet amount with no bankroll."
self.context.logger.error(error)
return 0
kelly_bet_amount = (
-4 * x**2 * y
Expand Down Expand Up @@ -256,7 +252,7 @@ def _calculate_kelly_bet_amount(
)
)
** (1 / 2)
) / (2 * (x**2 * f - y**2 * f))
) / (2 * (x**2 * f - y**2 * f)) + EPSILON
return int(kelly_bet_amount)

def get_max_bet_amount(self, a: int, x: int, y: int, f: float) -> int:
Expand Down Expand Up @@ -362,20 +358,21 @@ def default_error(
f"using {contract_callable!r}: {response_msg}"
)

def _propagate_contract_messages(self, response_msg: ContractApiMessage) -> None:
def _propagate_contract_messages(self, response_msg: ContractApiMessage) -> bool:
"""Propagate the contract's message to the logger, if exists.
Contracts can only return one message at a time.
:param response_msg: the response message from the contract method.
:return: None
:return: whether a message has been propagated.
"""
for level in ("info", "warning", "error"):
msg = response_msg.raw_transaction.body.get(level, None)
if msg is not None:
logger = getattr(self.context.logger, level)
logger(msg)
return
return True
return False

def contract_interact(
self,
Expand All @@ -400,11 +397,11 @@ def contract_interact(
self.default_error(contract_id, contract_callable, response_msg)
return False

self._propagate_contract_messages(response_msg)

propagated = self._propagate_contract_messages(response_msg)
data = response_msg.raw_transaction.body.get(data_key, None)
if data is None:
self.default_error(contract_id, contract_callable, response_msg)
if not propagated:
self.default_error(contract_id, contract_callable, response_msg)
return False

setattr(self, placeholder, data)
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/skills/decision_maker_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fingerprint:
README.md: bafybeia367zzdwndvlhw27rvnwodytjo3ms7gbc3q7mhrrjqjgfasnk47i
__init__.py: bafybeih563ujnigeci2ldzh7hakbau6a222vsed7leg3b7lq32vcn3nm4a
behaviours/__init__.py: bafybeih6ddz2ocvm6x6ytvlbcz6oi4snb5ee5xh5h65nq4w2qf7fd7zfky
behaviours/base.py: bafybeidlbdyrcd2sifjmbxql4mdki3mpzxbu6bug2e2ljmd7lnliycc7hm
behaviours/base.py: bafybeig4apfcv34bdne5tyrdg7balg3lva6c672dt5um5ay45m5pnyjuqm
behaviours/bet_placement.py: bafybeigtz4uimsqjjwq4r5p3a5v6niqdtqezbuf2ghy6o7syhsh4k5gjfa
behaviours/blacklisting.py: bafybeicl6b4hcmqmekta2mcuhkkydnzk7jmic6k44e6ns3u2ibad3awzvu
behaviours/decision_receive.py: bafybeifacce2ke7oltnwnpdjdqfd74eaaw5wxnjfzk6c5tqdsxsmbzjj3m
Expand Down
4 changes: 2 additions & 2 deletions packages/valory/skills/trader_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ skills:
- valory/transaction_settlement_abci:0.1.0:bafybeia6cdxdlqrcwk2maw25fo7dafzd2p3rs7syropvufophk2pitzbwy
- valory/termination_abci:0.1.0:bafybeieqm46zuccaagnko3qlw6p3nvoohdrfgvpmw467r5lyil2dqrzjsy
- valory/market_manager_abci:0.1.0:bafybeibrjre5yka3sbvb6lsefgc5yutovb6h4ffzrpecplfkjwtl5i36te
- valory/decision_maker_abci:0.1.0:bafybeiggsi3hyu2lqqfxh5fonkwh7rdmounrpbkaxtt6aeqm4cr362cwuy
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeibda3fyr6hbbnlz5unncuhaidznconrwqa7tqbsuxqpal2vdlalra
- valory/decision_maker_abci:0.1.0:bafybeic6xegb3zbo46uv2q3l63itanw7m26r55ifqo4j4iophos46mppcu
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeiep7dgp7hpamdw6avwku5f5wpqw6zbg4evaux6s4yna6el3jay2x4
behaviours:
main:
args: {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contracts: []
protocols: []
skills:
- valory/abstract_round_abci:0.1.0:bafybeid7neqpxxe4ualp23gxgrtpf6r5u3myr2mmc4uvwameyqshfdoydq
- valory/decision_maker_abci:0.1.0:bafybeiggsi3hyu2lqqfxh5fonkwh7rdmounrpbkaxtt6aeqm4cr362cwuy
- valory/decision_maker_abci:0.1.0:bafybeic6xegb3zbo46uv2q3l63itanw7m26r55ifqo4j4iophos46mppcu
behaviours:
main:
args: {}
Expand Down

0 comments on commit aef7dff

Please sign in to comment.