fmilthaler / FinQuant

A program for financial portfolio management, analysis and optimisation.
MIT License
1.4k stars 199 forks source link

How to use with lib plotly #112

Closed homerokzam closed 1 year ago

homerokzam commented 1 year ago

Congratulations for the work. Excellent library!

How to use the efficient frontier data to plot in the plotly library the EF Min Volatility and EF Max Sharpe Ratio?

Thanks

fmilthaler commented 1 year ago

Hi @homerokzam. Thank you, glad you like the project. :)

Currently FinQuant has some methods defined in the class EfficientFrontier that let you automatically plot using matplotlib. I guess you have seen that, that is why you are asking for plotly.

When you follow the example Example-Optimisation, you end up with an object for your portfolio. In the example it is called pf. Through that, you have access to the data you want to plot. pf.ef holds all of the portfolio's efficient frontier properties.

To plot the values you asked for (in plotly), you first need to get the values out of the object. Here is an example on how to do that:

The following code snippet should get you all the data you need to plot it in plotly:

from finquant.quants import annualised_portfolio_quantities

# efficient frontier data:
ef_data = pf.ef_efficient_frontier()

# minimum volatility:
min_vol_weights = pf.ef_minimum_volatility()
min_vol_vals = list(
  annualised_portfolio_quantities(
    min_vol_weights , pf.ef.mean_returns, pf.ef.cov_matrix, freq= pf.ef.freq
  )
)[0:2]
min_vol_vals.reverse()

# maximum sharpe ratio:
max_sharpe_weights = pf.ef_maximum_sharpe_ratio()

max_sharpe_vals = list(
  annualised_portfolio_quantities(
    max_sharpe_weights, self.mean_returns, self.cov_matrix, freq=self.freq
  )
)[0:2]
max_sharpe_vals.reverse()

In the above code snippet:

I have not used plotly in a long time, but I guess you know how to use that data to plot it in plotly yourself.

homerokzam commented 1 year ago

That's just what I needed! Thank you very much.