Risk Free Line (#785)

* began rf_line changes

* Continued risk-free

* Added rf optimal line

* Fixing commit issue

* Attempting to fix black

* Original James updates

* Added line to legend

* Fixed label and charting

Co-authored-by: Colin Delahunty <colindelahunty@Colins-MacBook-Air.local>
Co-authored-by: Colin Delahunty <colindelahunty@Colins-Air.indiana.edu>
This commit is contained in:
Colin Delahunty
2021-10-05 16:15:17 -04:00
committed by GitHub
parent ba98078366
commit 3e53bd908c
3 changed files with 30 additions and 2 deletions

View File

@@ -5,6 +5,8 @@ import math
from typing import List
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
import numpy as np
import pandas as pd
from pypfopt import plotting
@@ -321,7 +323,12 @@ def display_efficient_return(
print("")
def display_ef(stocks: List[str], period: str = "3mo", n_portfolios: int = 300):
def display_ef(
stocks: List[str],
period: str = "3mo",
n_portfolios: int = 300,
risk_free: bool = False,
):
"""Display efficient frontier
Parameters
@@ -345,6 +352,17 @@ def display_ef(stocks: List[str], period: str = "3mo", n_portfolios: int = 300):
verbose=True, risk_free_rate=rfrate
)
ax.scatter(std_sharpe, ret_sharpe, marker="*", s=100, c="r", label="Max Sharpe")
# Add risk free line
if risk_free:
y = ret_sharpe * 1.2
b = get_rf()
m = (ret_sharpe - b) / std_sharpe
x2 = (y - b) / m
x = [0, x2]
y = [b, y]
line = Line2D(x, y, color="#FF0000", label="Capital Allocation Line")
ax.set_xlim(xmin=min(stds) * 0.8)
ax.add_line(line)
ax.set_title(f"Efficient Frontier simulating {n_portfolios} portfolios")
ax.legend()
fig.tight_layout()

View File

@@ -711,6 +711,14 @@ Mean Variance Optimization:
dest="n_port",
help="number of portfolios to simulate",
)
parser.add_argument(
"-r",
"--risk-free",
action="store_true",
dest="risk_free",
default=False,
help="Adds the optimal line with the risk-free asset",
)
try:
ns_parser = parse_known_args_and_warn(parser, other_args)
@@ -725,6 +733,7 @@ Mean Variance Optimization:
stocks=self.tickers,
period=ns_parser.period,
n_portfolios=ns_parser.n_port,
risk_free=ns_parser.risk_free,
)
except Exception as e:

View File

@@ -1,5 +1,5 @@
```
usage: ef [-p {1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max}] [-n N_PORT] [-h]
usage: ef [-p {1d,5d,1mo,3mo,6mo,1y,2y,5y,10y,ytd,max}] [-n N_PORT] [-h] [-r]
```
This function plots random portfolios based on their risk and returns and shows the efficient frontier.
@@ -11,4 +11,5 @@ optional arguments:
-n N_PORT, --number-portfolios N_PORT
number of portfolios to simulate (default: 300)
-h, --help show this help message (default: False)
-r, --risk-free shows the risk free optimal line
```