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

Kelly criterion calculated wrong. #407

Merged
merged 2 commits into from
Sep 15, 2023

Conversation

yakir4123
Copy link
Contributor

I wanted to implement kelly criterion in my strategy after seeing this video:
https://www.youtube.com/watch?v=_FuuYSM7yOo&ab_channel=MarcinAnforowicz

Happy to know that jesse support that and also has an example in the docs.
https://docs.jesse.trade/docs/strategies/example-strategies.html#getting-the-size-right

Unfortunate after several experiments and investigatation I found that something is off. The problem was in the method of utils.kelly_criterion

The results are completly different between these formulas to utils.kelly_criterion formula.

@saleh-mir saleh-mir merged commit 8ece966 into jesse-ai:master Sep 15, 2023
3 checks passed
@saleh-mir
Copy link
Member

I cannot make the unit tests for this to pass. I tried all sorts of help from Chad GPT and similar tools two. I don't really know much about Kelly criterion. Can you help with this please?

def kelly_criterion(win_rate: float, ratio_avg_win: float, ratio_avg_loss: float) -> float:
    return win_rate - ((1 - win_rate) * ratio_avg_loss / ratio_avg_win)

def test_kelly_criterion():
    # Scenario 1: win_rate is 60%, average win is twice the bet amount, and average loss is the bet amount
    assert abs(kelly_criterion(0.6, 2, 1) - (0.6 - 0.4/2)) < 1e-9

    # Scenario 2: Even win rate, average win and average loss are the same
    assert abs(kelly_criterion(0.5, 1, 1)) < 1e-9  # Expected result is 0

    # Scenario 3: win_rate is 70%, average win is three times the bet amount, average loss is twice the bet amount
    assert abs(kelly_criterion(0.7, 3, 2) - (0.7 - 0.6/3)) < 1e-9

    # Scenario 4: win_rate is 40%, average win is half the bet amount, average loss is four times the bet amount
    assert abs(kelly_criterion(0.4, 0.5, 4) - (0.4 - 2.4)) < 1e-9

    # Scenario 5: win_rate is 80%, average win is 1.5 times the bet amount, average loss is 0.5 times the bet amount
    assert abs(kelly_criterion(0.8, 1.5, 0.5) - (0.8 - 0.1/1.5)) < 1e-9

saleh-mir added a commit that referenced this pull request Sep 27, 2023
This pull request was closed.
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.

2 participants