diff --git a/cvxopt b/cvxopt new file mode 160000 index 0000000..f236615 --- /dev/null +++ b/cvxopt @@ -0,0 +1 @@ +Subproject commit f236615e5bd326daeb6cda511c31d86bcc354747 diff --git a/main.py b/main.py index 91d4c72..0d59bbb 100644 --- a/main.py +++ b/main.py @@ -20,25 +20,25 @@ pd.options.plotting.backend = "plotly" # PARAMETERS CONFIGS = { - "ENV": "development", - "DEBUG": True, + # "ENV": "development", + # "DEBUG": True, "SECRET_KEY": os.urandom(30), # Set the secret key for session authentication "PERMANENT_SESSION_LIFETIME": timedelta(minutes=60) } -# SQL_CONFIG = dict( -# database= os.getenv("PGDATABASE"), -# user=os.getenv("PGUSER"), -# host=os.getenv("PGHOST"), -# port=os.getenv("PGPORT"), -# password=os.getenv("PGPASSWORD") -# ) SQL_CONFIG = dict( - database="railway", - user="postgres", - host="containers-us-west-103.railway.app", - port="5913", - password="gv5Mh7cPjCm9YTjAmsYD" + database= os.getenv("PGDATABASE"), + user=os.getenv("PGUSER"), + host=os.getenv("PGHOST"), + port=os.getenv("PGPORT"), + password=os.getenv("PGPASSWORD") ) +# SQL_CONFIG = dict( +# database="railway", +# user="postgres", +# host="containers-us-west-103.railway.app", +# port="5913", +# password="gv5Mh7cPjCm9YTjAmsYD" +# ) # SQL_CONFIG = { # 'database': "tpm", # 'user': "hsienchen", diff --git a/portfolio_builder.py b/portfolio_builder.py index df6144f..e2ca465 100644 --- a/portfolio_builder.py +++ b/portfolio_builder.py @@ -101,8 +101,17 @@ class MVO(object): retPort = ret@w # T-dimensional array stdPort = np.std(retPort) return cov@w/stdPort + @staticmethod + def quadratic_utility(w, ret, gamma): + retPort = ret@w # T-dimensional array + varPort = np.var(retPort) + return np.mean(retPort) - 0.5*gamma*varPort + @staticmethod + def quadratic_utility_grad(w, ret, cov, gamma): + manual_ret = np.mean(ret, axis=0) + return manual_ret - gamma*cov@w @classmethod - def opt(cls, ret, role="max_sharpe"): + def opt(cls, ret, gamma=0, role="max_sharpe"): n = ret.shape[1] init=np.ones(n)/n if role=="max_sharpe": @@ -117,6 +126,10 @@ class MVO(object): cov=np.cov(ret.T) loss = lambda w: cls.volatility(w, ret) grad = lambda w: cls.volatility_grad(w, ret, cov) + elif role=="quadratic_utility": + cov=np.cov(ret.T) + loss = lambda w: -cls.quadratic_utility(w, ret, gamma) + grad = lambda w: -cls.quadratic_utility_grad(w, ret, cov, gamma) else: return init bnds = [[0, 0.6] for i in range(n)] diff --git a/templates/base.html b/templates/base.html index 6a2376f..be35d30 100644 --- a/templates/base.html +++ b/templates/base.html @@ -62,6 +62,7 @@ ('/', 'index', '首頁', 'bi bi-house-fill'), ('/strategy', 'strategy', '建立策略', 'fa-solid fa-chart-pie'), ('/strategy_tw', 'strategy_tw', '台股建立策略', 'fa-solid fa-chart-pie'), + ('/strategy_bl', 'strategy_bl', 'Black-Litterman配置', 'fa-solid fa-chess-knight'), ('/custom', 'custom', '自訂數據建立策略', 'bi bi-database-fill-add'), ('/result', 'result', '分析結果排行', 'fa-solid fa-chart-simple'), ('mailto:r10246002@ntu.edu.tw', 'error', '錯誤回報', 'bi bi-bug-fill')