Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New portfolio optimization menu #1642

Merged
merged 91 commits into from
May 3, 2022

Conversation

dcajasn
Copy link
Contributor

@dcajasn dcajasn commented Apr 5, 2022

Description

This PR updates the Portfolio Optimization menu. It adds several new portfolio optimization models that are available in Riskfolio-Lib and allows the user plots new charts for optimal portfolios: histogram, drawdown, risk contribution and correlation heatmap. Also add the option to build the efficient frontier for additional risk measures.

Some examples:

2022 Apr 05, 18:44 (🦋) /portfolio/po/ $ add MSFT,AMZN,TSLA,BA,T,FB,AAPL

2022 Apr 05, 18:51 (🦋) /portfolio/po/ $ hrp -p 5y --heat --pie --rc-chart --his
 --dd

 [5 Years] Hierarchical risk parity portfolio using pearson codependence,
single linkage and volatility as risk measure

     Weights      
┏━━━━━━┳━━━━━━━━━┓
┃      ┃ Value   ┃
┡━━━━━━╇━━━━━━━━━┩
│ AAPL │ 14.37 % │
├──────┼─────────┤
│ AMZN │ 14.80 % │
├──────┼─────────┤
│ BA   │  7.5 %  │
├──────┼─────────┤
│ FB   │ 10.87 % │
├──────┼─────────┤
│ MSFT │ 17.19 % │
├──────┼─────────┤
│ T    │ 28.97 % │
├──────┼─────────┤
│ TSLA │  6.28 % │
└──────┴─────────┘

Annual (by 252) expected return: 23.61%
Annual (by √252) volatility: 22.85%
Sharpe ratio: 1.0249

Figure_1

Figure_2

Figure_3

Figure_4

Figure_5

2022 Apr 05, 18:51 (🦋) /portfolio/po/ $ ef -p 5y -rm CVaR

frontier

How has this been tested?

I'm still writing tests but I would like your opinion about new features.

Checklist:

Others

  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • My code passes all the checks pylint, flake8, black, ... To speed up development you should run pre-commit install.
  • New and existing unit tests pass locally with my changes. You can test this locally using pytest tests/....

@dcajasn dcajasn added the feat L Large T-Shirt size Feature label Apr 6, 2022
@JerBouma JerBouma self-requested a review April 6, 2022 04:59
@JerBouma
Copy link
Contributor

JerBouma commented Apr 7, 2022

Hey @dcajasn,

First of all, great work. The functionalities you have added will improve the Portfolio menu by a lot. My first question would be when are you going to make an Excel sheet that people can use to set their preferences? As there are so many options, it is very relevant that this can be directly done via an Excel. This is absolutely crucial for this menu to work.

A few comments:
I want you to think carefully about each parameter and if it makes sense that they are all actually there. For example, when it comes to data collection from Yahoo Finance. Is it absolutely crucial that this is determined for each argument or could this be inside the menu as done in the "SIA" menu? Parameters that are probably constant between all commands should also be set accordingly within the menu.

image

I think that all of these could be set similarly to how you see it on the SIA menu:

  -p PERIOD, --period PERIOD
                        Period to get yfinance data from (default: 3y)
  -s START, --start START
                        Start date to get yfinance data from (default: )
  -e END, --end END     End date to get yfinance data from (default: )
  -lr, --log-returns    If use logarithmic or arithmetic returns to calculate returns (default: False)
  -f {d,w,m}, --freq {d,w,m}
                        Frequency used to calculate returns (default: d)
  -mn MAXNAN, --maxnan MAXNAN
                        Max percentage of nan values accepted per asset to be considered in the optimization process (default: 0.05)
  -th THRESHOLD, --threshold THRESHOLD
                        Value used to replace outliers that are higher to threshold in absolute value (default: 0.3)
  -mt METHOD, --method METHOD
                        Method used to fill nan values (default: time)

You have added a lot of parameters to each command. E.g. maxnan, threshold, method, rm, risk_free, alpha, target_return and more. I would like to invite you to include all of these parameters in the documentation and explain what each represents. E.g. what is FLPM or what is EWMA1 and EWMA2 or what is the difference between relaxed risk parity model A, B and C. It needs to abundantly clear what settings I am using and what they represent. You don't have to explain the entire mathematics as long as it makes sense for the Financial professional.

To reduce the amount of arguments, one suggestion be to create a different command that does the plotting. To achieve this, you can "store" your calculations. E.g. if I run maxsharpe, it will store as "maxsharpe1" (and the next will be "maxsharpe2" if settings change. Then I can plot pie charts of maxsharpe1 and maxsharpe2 to graphically understand the difference X parameter makes. This command could also allow for combining certain plots for example.

Could you look at the "Other optimization techniques" and see if they can fit in a category and if it is old commands, if they can be removed? We don't want too many functionalities if they don't even make too much sense.

I still need to look into the theory of all the options but these are some general pointers at least.

@dcajasn
Copy link
Contributor Author

dcajasn commented Apr 12, 2022

Hi @JerBouma, following your comments I split plot functions from portfolio optimization models. Also, I added the option to save portfolios and then make plots to compare them. I'm starting to work with the excel template to create most complex portfolios and finishing to update documentation.

@JerBouma
Copy link
Contributor

Hi @dcajasn, that is really great. Well done. Could you focus on integrating Riskfolio-lib functions within the terminal directly first? I will work on the spreadsheet.

@dcajasn
Copy link
Contributor Author

dcajasn commented Apr 12, 2022

Sure, no problem.

@dcajasn
Copy link
Contributor Author

dcajasn commented Apr 13, 2022

Hi @JerBouma, I added riskfolio-lib modules. It is working without problems but I need to make some changes to pass flake8 pre-commit-hooks.

@dcajasn
Copy link
Contributor Author

dcajasn commented Apr 28, 2022

Hi @JerBouma, I'm going to fix the bugs asap. In the case of the Excel, I was thinking that you will design all Excels for the portfolio optimization menu like input parameters and Black Litterman, but no problem I will create and Excel for p_views and q_views.

@dcajasn
Copy link
Contributor Author

dcajasn commented Apr 29, 2022

Hi @JerBouma,

I changed the colors of all charts to openBB style, I added the option to download and upload a template to build views for black litterman model. I couldn't find errors with -dd cmd, I tried with several portfolios and works well, could you send me the exact portfolio to reproduce the error? because in the image there isn't a cmd to create a portfolio.

@JerBouma
Copy link
Contributor

JerBouma commented Apr 29, 2022

@dcajasn For -dd if I try the following:

2022 Apr 29, 04:40 (🦋) /portfolio/po/ $ load 50:30:10:10 Portfolio.xlsx
2022 Apr 29, 04:40 (🦋) /portfolio/po/ $ file OpenBB_Parameters_Template v1.0.0.xlsx
Parameters:
    historic_period         : 3y
    log_returns             : 0
    return_frequency        : d
    max_nan                 : 0.05
    threshold_value         : 0.3
    nan_fill_method         : time
    risk_free               : 0.003
    significance_level      : 0.05
    risk_measure            : MV
    target_return           : -1
    target_risk             : -1
    expected_return         : hist
    covariance              : hist
    smoothing_factor_ewma   : 0.94
    long_allocation         : 1
    short_allocation        : 0

2022 Apr 29, 04:41 (🦋) /portfolio/po/ $ riskparity

 [3 Years] Risk parity portfolio based on risk budgeting approach
using volatility as risk measure

      Weights       
┏━━━━━━━┳━━━━━━━━━━┓
┃       ┃ Value    ┃
┡━━━━━━━╇━━━━━━━━━━┩
│ AAPL  │   3.24 % │
├───────┼──────────┤
│ AMZN  │   4.03 % │
├───────┼──────────┤
│ APTV  │   2.15 % │
├───────┼──────────┤
│ ASML  │   2.45 % │
├───────┼──────────┤
│ BABA  │   3.40 % │
├───────┼──────────┤
│ GOOGL │   3.49 % │
├───────┼──────────┤
│ HYG   │   9.30 % │
├───────┼──────────┤
│ IYR   │   3.95 % │
├───────┼──────────┤
│ NKE   │   3.69 % │
├───────┼──────────┤
│ PEX   │   3.84 % │
├───────┼──────────┤
│ PSP   │   3.46 % │
├───────┼──────────┤
│ REZ   │   4.04 % │
├───────┼──────────┤
│ TIP   │  26.93 % │
├───────┼──────────┤
│ TLT   │  22.75 % │
├───────┼──────────┤
│ TSM   │   3.28 % │
└───────┴──────────┘
Annual (by 252) expected return: 10.10%
Annual (by √252) volatility: 11.21%
Sharpe ratio: 0.8741

2022 Apr 29, 04:41 (🦋) /portfolio/po/ $ plot -pf RP_0 -dd
Error: 'AxesSubplot' object is not iterable

@dcajasn
Copy link
Contributor Author

dcajasn commented Apr 29, 2022

@JerBouma here is working well.

2022 Apr 29, 10:45 (🦋) /portfolio/po/ $ load 50:30:10:10 Portfolio.xlsx
2022 Apr 29, 10:45 (🦋) /portfolio/po/ $ file OpenBB_Parameters_Template v1.0.0.
xlsx
Parameters:
    historic_period         : 3y
    log_returns             : 0
    return_frequency        : d
    max_nan                 : 0.05
    threshold_value         : 0.3
    nan_fill_method         : time
    risk_free               : 0.003
    significance_level      : 0.05
    risk_measure            : MV
    target_return           : -1
    target_risk             : -1
    expected_return         : hist
    covariance              : hist
    smoothing_factor_ewma   : 0.94
    long_allocation         : 1
    short_allocation        : 0

2022 Apr 29, 10:46 (🦋) /portfolio/po/ $ riskparity

 [3 Years] Risk parity portfolio based on risk budgeting approach
using volatility as risk measure

      Weights       
┏━━━━━━━┳━━━━━━━━━━┓
┃       ┃ Value    ┃
┡━━━━━━━╇━━━━━━━━━━┩
│ AAPL  │   3.24 % │
├───────┼──────────┤
│ AMZN  │   4.03 % │
├───────┼──────────┤
│ APTV  │   2.15 % │
├───────┼──────────┤
│ ASML  │   2.45 % │
├───────┼──────────┤
│ BABA  │   3.40 % │
├───────┼──────────┤
│ GOOGL │   3.49 % │
├───────┼──────────┤
│ HYG   │   9.30 % │
├───────┼──────────┤
│ IYR   │   3.95 % │
├───────┼──────────┤
│ NKE   │   3.69 % │
├───────┼──────────┤
│ PEX   │   3.84 % │
├───────┼──────────┤
│ PSP   │   3.46 % │
├───────┼──────────┤
│ REZ   │   4.04 % │
├───────┼──────────┤
│ TIP   │  26.93 % │
├───────┼──────────┤
│ TLT   │  22.75 % │
├───────┼──────────┤
│ TSM   │   3.28 % │
└───────┴──────────┘
Annual (by 252) expected return: 10.10%
Annual (by √252) volatility: 11.21%
Sharpe ratio: 0.8741

2022 Apr 29, 10:46 (🦋) /portfolio/po/ $ plot -pf RP_0 -dd

Figure_1

@JerBouma JerBouma marked this pull request as ready for review May 2, 2022 13:34
@JerBouma JerBouma merged commit 34bc290 into OpenBB-finance:main May 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat XL Extra Large feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants