From d2b76e2cd76fa4bb4d861b90bdf5bc0b35f56f0f Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Thu, 21 Nov 2024 12:30:46 +0800 Subject: [PATCH] Create kelly_formula.py --- 2024.11.21_kelly_formula/kelly_formula.py | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 2024.11.21_kelly_formula/kelly_formula.py diff --git a/2024.11.21_kelly_formula/kelly_formula.py b/2024.11.21_kelly_formula/kelly_formula.py new file mode 100644 index 0000000..5e78c0e --- /dev/null +++ b/2024.11.21_kelly_formula/kelly_formula.py @@ -0,0 +1,49 @@ +""" +This code is supported by the website: https://www.guanjihuan.com +The newest version of this code is on the web page: https://www.guanjihuan.com/archives/43508 +""" + +import numpy as np +import matplotlib.pyplot as plt + +investment_ratio_array = np.arange(0.1, 1.1, 0.1) +investment_times = 1000 +test_times = 100 + +# 几个例子:https://www.guanjihuan.com/archives/43412 + +# 例子(2)的参数 +p = 0.6 # 胜率 +b = 1 # 收益 +a = 1 # 损失 + +# # 例子(3)的参数 +# p = 0.5 +# b = 1 +# a = 0.5 + +win_array = [] # 胜出的仓位 +for i0 in range(test_times): + # print(i0) + capital_array = [] + for f in investment_ratio_array: + capital = 1 + for _ in range(investment_times): + investment = capital*f + if investment>0: + random_value = np.random.uniform(0, 1) + if random_value