-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathportfolio.py
More file actions
197 lines (182 loc) · 5.11 KB
/
portfolio.py
File metadata and controls
197 lines (182 loc) · 5.11 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Copyright 2021 D-Wave Systems Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import click
from demo_configs import DEFAULT_STOCKS
from src.demo_enums import SolverType
from src.multi_period import MultiPeriod
from src.single_period import SinglePeriod
@click.command()
@click.option(
"-s",
"--stocks",
multiple=True,
type=str,
default=DEFAULT_STOCKS,
show_default=True,
help="Stock name to be included."
"When a file is provided, stock name must be included in the file ",
)
@click.option("-b", "--budget", default=1000, show_default=True, help="Portfolio budget")
@click.option(
"-n",
"--bin-size",
type=int,
help="Maximum number of intervals for each stock. This a DQM-only option.",
)
@click.option(
"-g",
"--gamma",
multiple=True,
type=float,
help="Penalty coefficient for budget constraint. This is a DQM-only option.",
)
@click.option(
"-a",
"--alpha",
multiple=True,
type=float,
default=[0.005],
show_default=True,
help="Risk aversion coefficient",
)
@click.option(
"-f",
"--file-path",
default="data/basic_data.csv",
show_default=True,
type=str,
help="Full path of csv file containing input stock data",
)
@click.option(
"-z",
"--baseline",
default="^GSPC",
show_default=True,
help="Baseline stock for comparison in multi-period run",
)
@click.option(
"-u", "--max-risk", default=0.0, help="Upper bound on risk/variance. This only works for CQM."
)
@click.option(
"-l", "--min-return", default=0.0, help="Lower bound on the returns. This only works for CQM."
)
@click.option(
"-d",
"--dates",
nargs=2,
type=str,
help="Start and end date to query stock data",
)
@click.option(
"-m",
"--model-type",
default="CQM",
multiple=False,
type=click.Choice(["CQM", "DQM"], case_sensitive=False),
show_default=True,
help="Model type, CQM or DQM",
)
@click.option(
"-r",
"--rebalance",
is_flag=True,
default=False,
help="Make a multi-period rebalancing portfolio optimization run; "
"otherwise, make a single-period portfolio optimization run",
)
@click.option(
"-p",
"--params",
default="{}",
help="Pass sampler arguments such as profile and solver; " 'usage: -p \'{"profile": "test"}\'',
)
@click.option("-v", "--verbose", is_flag=True, help="Enable additional program console output")
@click.option(
"-k",
"--num",
default=0,
type=click.IntRange(
0,
),
help="Number of stocks to be randomnly generated."
"When this option is selected, dates need to be provided.",
)
@click.option(
"-t",
"--t-cost",
default=0.00,
type=click.FloatRange(0, 1),
help="Transaction cost: percentage of transaction dollar value.",
)
def main(
stocks: list,
budget: int,
bin_size: int,
gamma: tuple,
params: str,
file_path: str,
max_risk: float,
num: int,
min_return: float,
baseline: str,
dates: str,
model_type: str,
rebalance: bool,
alpha: tuple,
verbose: bool,
t_cost: float,
):
solver_type = SolverType.CQM if model_type == "CQM" else SolverType.DQM
if (max_risk or min_return) and solver_type is SolverType.DQM:
raise Exception("The bound options require a CQM.")
if (gamma or bin_size) and solver_type is SolverType.CQM:
raise Exception("The option gamma or bin-size requires a DQM.")
if num and not dates:
raise Exception("User must provide dates with option 'num'.")
if t_cost and solver_type is SolverType.DQM:
raise Exception("The transaction cost option requires a CQM. Set t_cost=0 for DQM.")
if rebalance:
print(f"\nRebalancing portfolio optimization run...")
my_portfolio = MultiPeriod(
stocks=stocks,
budget=budget,
sampler_args=params,
bin_size=bin_size,
dates=dates,
file_path=file_path,
gamma=gamma,
model_type=solver_type,
alpha=alpha,
verbose=verbose,
baseline=baseline,
t_cost=t_cost,
)
else:
print(f"\nSingle period portfolio optimization run...")
my_portfolio = SinglePeriod(
stocks=stocks,
budget=budget,
bin_size=bin_size,
gamma=gamma,
file_path=file_path,
dates=dates,
model_type=solver_type,
alpha=alpha,
verbose=verbose,
sampler_args=params,
t_cost=t_cost,
)
my_portfolio.run(min_return=min_return, max_risk=max_risk, num=num)
if __name__ == "__main__":
main()