-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathkelly_criterion.py
executable file
·163 lines (131 loc) · 5.53 KB
/
kelly_criterion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# Copyright (c) 2014-2019, Tibor Kiss <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
"""Kelly Criterion - (c) 2014-2019, Tibor Kiss <[email protected]>
Usage:
kelly_criterion [options] <start-date> <end-date> <security>...
Options:
--risk-free-rate=<pct> Annualized percentage of the Risk Free Rate.
[default: 0.04]
"""
import sys
from datetime import datetime, date
from typing import Set, Dict
import logging
from docopt import docopt
from pandas import DataFrame
from numpy.linalg import inv
from iexfinance.stocks import get_historical_data
log = logging.getLogger(__name__)
def calc_kelly_leverages(securities: Set[str],
start_date: date,
end_date: date,
risk_free_rate: float = 0.04) -> Dict[str, float]:
"""Calculates the optimal leverages for the given securities and
time frame. Returns a list of (security, leverage) tuple with the
calculate optimal leverages.
Note: risk_free_rate is annualized
"""
f = {}
ret = {}
excess_return = {}
# Download the historical prices from Yahoo Finance and calculate the
# excess return (return of security - risk free rate) for each security.
for symbol in securities:
try:
hist_prices = get_historical_data(
symbol, start=start_date, end=end_date,
output_format='pandas')
except IOError as e:
raise ValueError(f'Unable to download data for {symbol}. '
f'Reason: {str(e)}')
f[symbol] = hist_prices
ret[symbol] = hist_prices['close'].pct_change()
# risk_free_rate is annualized
excess_return[symbol] = (ret[symbol] - (risk_free_rate / 252))
# Create a new DataFrame based on the Excess Returns.
df = DataFrame(excess_return).dropna()
# Calculate the CoVariance and Mean of the DataFrame
C = 252 * df.cov()
M = 252 * df.mean()
# Calculate the Kelly-Optimal Leverages using Matrix Multiplication
F = inv(C).dot(M)
# Return a list of (security, leverage) tuple
return {security: leverage
for security, leverage in zip(df.columns.values.tolist(), F)}
def main():
"""Entry point of Kelly Criterion calculation."""
logging.basicConfig(level=logging.INFO)
log.info("Kelly Criterion calculation")
args = docopt(__doc__, sys.argv[1:])
# Parse risk-free-rate
try:
risk_free_rate = float(args['--risk-free-rate'])
except ValueError:
log.error(f"Error converting risk-free-rate to float: "
f"{args['--risk-free-rate']}")
sys.exit(-1)
# Verify risk-free-rate
if not 0 <= risk_free_rate <= 1.0:
log.error(f"risk-free-rate is not in between 0 and 1: "
f"{risk_free_rate:%.2f}")
sys.exit(-1)
# Parse start and end dates
try:
start_date = datetime.strptime(args['<start-date>'], "%Y-%m-%d").date()
except ValueError:
log.error(f"Error parsing start-date: {args['<start-date>']}")
sys.exit(-1)
try:
end_date = datetime.strptime(args['<end-date>'], "%Y-%m-%d").date()
except ValueError:
log.error(f"Error parsing end-date: {args['<start-date>']}")
sys.exit(-1)
log.info(
f"Arguments: "
f"risk-free-rate={args['--risk-free-rate']} "
f"start-date={start_date} "
f"end-date={end_date} "
f"securities={args['<security>']}")
# Calculate the Kelly Optimal leverages
try:
leverages = calc_kelly_leverages(
args['<security>'], start_date, end_date, risk_free_rate)
except ValueError as e:
log.error(f"Error during Kelly calculation: {str(e)}")
sys.exit(-1)
# Print the results if calculation was successful
if leverages:
log.info("Leverages per security:")
sum_leverage = 0
for symbol, leverage in leverages.items():
sum_leverage += leverage
log.info(f" {symbol}: {leverage:.2f}")
log.info(f"Sum leverage: {sum_leverage}")
if __name__ == '__main__':
main()