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

Fix/kelly #147

Merged
merged 9 commits into from
Nov 19, 2023
Merged

Fix/kelly #147

merged 9 commits into from
Nov 19, 2023

Conversation

DavidMinarsch
Copy link
Contributor

No description provided.

@@ -72,6 +72,56 @@ 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 perturb_y_if_x_equals_y(x: int, y: int) -> float:
"""Introduce perturbation to the y value if x equals y."""
epsilon = (x + y) / 10000
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still looking at best strategy...

@@ -216,45 +266,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(
Copy link
Contributor Author

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

return 0
if x == y:
# o/w kelly traders will never be the first to bet in a new market
y = perturb_y_if_x_equals_y(x, y)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If x==y both denominator AND numerator are zero! So the previous implementation avoided division by zero but didn't solve the issue of 0 bet amount.

@@ -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 (
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

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":
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be checked once at construction, not here.

@@ -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."
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted

@DavidMinarsch DavidMinarsch merged commit f3e8775 into main Nov 19, 2023
6 checks passed
@DavidMinarsch DavidMinarsch deleted the fix/kelly branch November 19, 2023 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant