-
Notifications
You must be signed in to change notification settings - Fork 19
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/kelly #147
Fix/kelly #147
Changes from all commits
7e221be
c8141cc
e109827
1303887
c7b2468
72f556c
4450235
484414b
95fad2f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ | |
from packages.valory.skills.decision_maker_abci.models import ( | ||
DecisionMakerParams, | ||
MultisendBatch, | ||
STRATEGY_BET_AMOUNT_PER_CONF_THRESHOLD, | ||
SharedState, | ||
) | ||
from packages.valory.skills.decision_maker_abci.policy import EGreedyPolicy | ||
|
@@ -61,7 +62,6 @@ | |
SAFE_GAS = 0 | ||
CID_PREFIX = "f01701220" | ||
WXDAI = "0xe91D153E0b41518A2Ce8Dd3D7944Fa863463a97d" | ||
EPSILON = 1e-7 | ||
|
||
|
||
def remove_fraction_wei(amount: int, fraction: float) -> int: | ||
|
@@ -72,6 +72,46 @@ def remove_fraction_wei(amount: int, fraction: float) -> int: | |
raise ValueError(f"The given fraction {fraction!r} is not in the range [0, 1].") | ||
|
||
|
||
def calculate_kelly_bet_amount( | ||
x: int, y: int, p: float, c: float, b: int, f: float | ||
) -> int: | ||
"""Calculate the Kelly bet amount.""" | ||
if b == 0: | ||
return 0 | ||
numerator = ( | ||
-4 * x**2 * y | ||
+ b * y**2 * p * c * f | ||
+ 2 * b * x * y * p * c * f | ||
+ b * x**2 * p * c * f | ||
- 2 * b * y**2 * f | ||
- 2 * b * x * y * f | ||
+ ( | ||
( | ||
4 * x**2 * y | ||
- b * y**2 * p * c * f | ||
- 2 * b * x * y * p * c * f | ||
- b * x**2 * p * c * f | ||
+ 2 * b * y**2 * f | ||
+ 2 * b * x * y * f | ||
) | ||
** 2 | ||
- ( | ||
4 | ||
* (x**2 * f - y**2 * f) | ||
* ( | ||
-4 * b * x * y**2 * p * c | ||
- 4 * b * x**2 * y * p * c | ||
+ 4 * b * x * y**2 | ||
) | ||
) | ||
) | ||
** (1 / 2) | ||
) | ||
denominator = 2 * (x**2 * f - y**2 * f) | ||
kelly_bet_amount = numerator / denominator | ||
return int(kelly_bet_amount) | ||
|
||
|
||
class DecisionMakerBaseBehaviour(BaseBehaviour, ABC): | ||
"""Represents the base class for the decision-making FSM behaviour.""" | ||
|
||
|
@@ -216,45 +256,6 @@ def check_balance(self) -> WaitableConditionType: | |
self.context.logger.info(f"The safe has {native} xDAI and {collateral}.") | ||
return True | ||
|
||
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: | ||
error = "Cannot calculate Kelly bet amount with no bankroll." | ||
self.context.logger.error(error) | ||
return 0 | ||
kelly_bet_amount = ( | ||
-4 * x**2 * y | ||
+ b * y**2 * p * c * f | ||
+ 2 * b * x * y * p * c * f | ||
+ b * x**2 * p * c * f | ||
- 2 * b * y**2 * f | ||
- 2 * b * x * y * f | ||
+ ( | ||
( | ||
4 * x**2 * y | ||
- b * y**2 * p * c * f | ||
- 2 * b * x * y * p * c * f | ||
- b * x**2 * p * c * f | ||
+ 2 * b * y**2 * f | ||
+ 2 * b * x * y * f | ||
) | ||
** 2 | ||
- ( | ||
4 | ||
* (x**2 * f - y**2 * f) | ||
* ( | ||
-4 * b * x * y**2 * p * c | ||
- 4 * b * x**2 * y * p * c | ||
+ 4 * b * x * y**2 | ||
) | ||
) | ||
) | ||
** (1 / 2) | ||
) / (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: | ||
"""Get max bet amount based on available shares.""" | ||
if x**2 * f**2 + 2 * x * y * f**2 + y**2 * f**2 == 0: | ||
|
@@ -293,17 +294,18 @@ def get_bet_amount( | |
) -> Generator[None, None, int]: | ||
"""Get the bet amount given a specified trading strategy.""" | ||
|
||
if strategy == "bet_amount_per_conf_threshold": | ||
# Kelly Criterion does not trade for equally weighted pools. | ||
if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than using some hack, let's just revert to this strategy when Kelly Criterion would suggest not to trade. |
||
strategy == STRATEGY_BET_AMOUNT_PER_CONF_THRESHOLD | ||
or selected_type_tokens_in_pool == other_tokens_in_pool | ||
): | ||
self.context.logger.info( | ||
"Used trading strategy: Bet amount per confidence threshold" | ||
) | ||
threshold = round(confidence, 1) | ||
bet_amount = self.params.bet_amount_per_threshold[threshold] | ||
return bet_amount | ||
|
||
if strategy != "kelly_criterion": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be checked once at construction, not here. |
||
raise ValueError(f"Invalid trading strategy: {strategy}") | ||
|
||
self.context.logger.info("Used trading strategy: Kelly Criterion") | ||
# bankroll: the max amount of DAI available to trade | ||
yield from self.wait_for_condition_with_sleep(self.check_balance) | ||
|
@@ -323,7 +325,10 @@ def get_bet_amount( | |
|
||
fee_fraction = 1 - self.wei_to_native(bet_fee) | ||
self.context.logger.info(f"Fee fraction: {fee_fraction}") | ||
kelly_bet_amount = self._calculate_kelly_bet_amount( | ||
if bankroll_adj == 0: | ||
error = "Cannot calculate Kelly bet amount with no bankroll." | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extracted |
||
self.context.logger.error(error) | ||
kelly_bet_amount = calculate_kelly_bet_amount( | ||
selected_type_tokens_in_pool, | ||
other_tokens_in_pool, | ||
win_probability, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2021-2023 Valory AG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""This module contains the tests for valory/decision_maker_abci.""" | ||
|
||
from aea.configurations.base import PublicId | ||
|
||
|
||
PUBLIC_ID = PublicId.from_str("valory/decision_maker_abci:0.1.0") |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2021-2023 Valory AG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
|
||
"""Conftest module for io tests.""" | ||
|
||
import os | ||
from pathlib import Path | ||
|
||
from hypothesis import settings | ||
|
||
|
||
# pylint: skip-file | ||
|
||
|
||
CI = "CI" | ||
PACKAGE_DIR = Path(__file__).parent.parent | ||
settings.register_profile(CI, deadline=5000) | ||
profile_name = ("default", "CI")[bool(os.getenv("CI"))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Antipattern to have this as an instance method. It's much harder to test this way