Money Management

Photo by AbsolutVision on Unsplash

Highlights

  • Difficulty:★★☆☆☆
  • Reminder: We use PYTHON to implement the Kelly formula. If you are interested in the derivation of the Kelly formula, you can further watch the video introduced by Mr. Li Yongle.

Preface

We are strange and familiar with the winning rates, odds, and betting ratios. We often depend on our sentiment to multiply the winning percentage and odds in our hearts and determine the betting ratio of the investment. Therefore, it’s difficult to explain our subjective betting ratio to someone.

It is inevitable in investment that the winning rate and the odds have extremely high subjective judgments. Still, we can use the Kelly formula to determine the betting ratio after determining the winning rate and the odds. Under the condition that the principal will never return to zero and maximize long-term gains.

Next, we use the Kelly formula to determine the betting ratio and reinvest the 0050 ETF at the beginning of each month. Meanwhile, we compare investment groups with the same buy & hold principal.

Editing Environment and Modules Required

Windows OS and Jupyter Notebook

import matplotlib.pyplot as plt
import numpy as npimport tejapi
tejapi.ApiConfig.api_key = "your key"

Database

Data Processing

We gather the 0050 ETF monthly return from the TEJ database.

data_return = tejapi.get('TWN/AAPRCM1',
coid = '0050',
mdate= {'gte': '2010-01-01','lte':'2015-12-31'},
opts={'columns':['coid','mdate','roi']},
chinese_column_name=True,paginate=True)

Kelly’s formula

We base on the previous 12-month monthly rate of return. If the monthly rate of return is greater than 0, it is considered a success; otherwise, it fails. This way, we can obtain the winning rate and odds when investing in the 0050 ETF at the beginning of each month.

  • P: Winning rate is the fraction of games or matches a team or individual has won.
  • b: Odds ratio measures the association between an exposure and an outcome.
  • f: The ratio of invested capital to principal
data_return['勝率'] = np.where(data_return['報酬率%_月'] >= 0 ,1,0)
data_return['勝率'] = data_return.rolling(12)['勝率'].sum() / 12data_return['獲利'] = np.where(data_return['報酬率%_月'] >= 0 ,data_return['報酬率%_月'],np.nan)
data_return['虧損'] = np.where(data_return['報酬率%_月'] < 0 ,data_return['報酬率%_月'],np.nan)data_return['賠率'] = data_return.rolling(12, min_periods=1)['獲利'].mean() \
/ data_return.rolling(12, min_periods=1)['虧損'].mean().abs()

We then use the Kelly formula to get the betting ratio and multiply the principal by the betting ratio, the current bet amount. If the numerator in Kelly’s formula is negative. It means the investment is not worth betting on, and we set the betting ratio for this period to 0.

Kelly formula
data_return['下注比例'] = \
np.where(data_return['勝率'] * data_return['賠率'] - (1 - data_return['勝率']) < 0,0,
(data_return['勝率'] * data_return['賠率'] - (1 - data_return['勝率'])) / data_return['賠率']
)
# 前一個月的下注比例,避免前視偏誤
data_return['下注比例'] = data_return['下注比例'].shift()

We systematically adjust betting ratios based on win rates and odds.

for i in range(0,len(data_return)):
if i == 0:
data_return['buy & hold本金'] = 1000000 # 起始本金
else:
data_return.loc[i,'buy & hold本金'] = 1000000 * data_return.loc[i-1,'累積報酬率%_月']for i in range(0,len(data_return)):
if i == 0:
data_return['凱利本金'] = 1000000 # 起始本金
else:
if data_return.loc[i,'下注比例'] > 0:
data_return.loc[i,'凱利本金'] = data_return.loc[i-1,'凱利本金'] * (1 - data_return.loc[i,'下注比例']) \
+ data_return.loc[i-1,'凱利本金'] * data_return.loc[i,'下注比例'] * (1 + data_return.loc[i,'報酬率%_月'])
# - data_return.loc[i-1,'凱利本金'] * data_return.loc[i,'下注比例'] * 0.002 # 手續費
else:
data_return.loc[i,'凱利本金'] = data_return.loc[i-1,'凱利本金']

Conclusion

We observe the accumulation trend chart of principal from 2011 to 2015 compared the investment groups under Buy & Hold and Kelly’s formula. We find that the accumulated principal amount of the latter doesn’t win the former. The possible reason is the Kelly formula applies to all known possibilities or investments where possibilities can be estimated. The uncertain odds in the stock market make it difficult for Kelly’s formula to work. However, the thinking of Kelly’s formula is still worth learning. The betting ratio controls risk. Even with a high winning rate and high odds, do not All in to avoid permanent losses.

In the future, readers can try to study how to improve the winning rate and odds of the strategy to make the betting ratio more accurate. When we enlarge our position with the thinking of risk control, which will help us survive in the market a long time. Finally, readers are welcome to visit our official website, which provides more financial, trading, and other financial information to help readers make better stock selection strategies!

The content of this webpage is not an investment device. It does not constitute an offer or solicitation to offer or recommendation of any investment product. It is for learning purposes only and does not consider your individual needs, investment objectives, and specific financial circumstances. Investment involves risk. Past performance is not indicative of future performance. Readers are requested to use their independent thinking skills to make investment decisions independently. The author will not be involved if losses are incurred due to relevant suggestions.

Source Code

Extended Reading

Related Link

Back
Procesing