fmilthaler / FinQuant

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

Naive question regarding Allocation #66

Closed gargs2241 closed 1 year ago

gargs2241 commented 3 years ago

Hello Frank,

Awesome job on Finquant. I was trying to use however, I myself am new to finance. I had a question. The allocation column in a portfolio. Are they number of units of stocks? or are they weighted? also. How do I input what is my average price for which i bought these stocks?

Sorry had to ask you via issues since there was not other way to contact. you.

Thanks for your help

Regards

Sriram

fmilthaler commented 3 years ago

Hi @gargs2241,

and thanks for giving FinQuant a try.

About the column Allocation, you have several options:

Set allocation as how much money you wish to invest initially: You can initially set the allocation based on how much money you want to invest in each stock of your portfolio (the values below are just an example). Example:

d = {
    0: {"Name": "WIKI/GOOG", "Allocation": 20},
    1: {"Name": "WIKI/AMZN", "Allocation": 10},
    2: {"Name": "WIKI/MCD", "Allocation": 15},
    3: {"Name": "WIKI/DIS", "Allocation": 18},
}
pf_allocation = pd.DataFrame.from_dict(d, orient="index")
# read in data from repository (assuming you are in the folder `example` of the repository)
df_data_path = pathlib.Path.cwd() / ".." / "data" / "ex1-stockdata.csv"
df_data = pd.read_csv(df_data_path, index_col="Date", parse_dates=True)

# build portfolio
pf = build_portfolio(data=df_data, pf_allocation=pf_allocation)

# printing out properties of initial portfolio
pf.properties()

Set allocation as a weight/percentage Alternatively, you can set the allocation as weights as well (make sure that the sum of all values equals 1):

d = {
    0: {"Name": "WIKI/GOOG", "Allocation": 0.1},
    1: {"Name": "WIKI/AMZN", "Allocation": 0.1},
    2: {"Name": "WIKI/MCD", "Allocation": 0.3},
    3: {"Name": "WIKI/DIS", "Allocation": 0.5},
}
pf_allocation = pd.DataFrame.from_dict(d, orient="index")
# read in data from repository (assuming you are in the folder `example` of the repository)
df_data_path = pathlib.Path.cwd() / ".." / "data" / "ex1-stockdata.csv"
df_data = pd.read_csv(df_data_path, index_col="Date", parse_dates=True)

# build portfolio
pf = build_portfolio(data=df_data, pf_allocation=pf_allocation)

# printing out properties of initial portfolio
pf.properties()

The simple way, not providing any initial allocation By not providing an initial allocation, FinQuant set all allocations to an equal value.

# read in data from repository (assuming you are in the folder `example` of the repository)
df_data_path = pathlib.Path.cwd() / ".." / "data" / "ex1-stockdata.csv"
df_data = pd.read_csv(df_data_path, index_col="Date", parse_dates=True)

# build portfolio
pf = build_portfolio(data=df_data)

# printing out properties of initial portfolio
pf.properties()

When you compute the optimised portfolio later on, the allocation changes of course. The allocation of the optimised portfolio is presented as a weight. Example

# optimisation for maximum Sharpe ratio
pf.ef_maximum_sharpe_ratio(verbose=True)

The output could be something like this:

----------------------------------------------------------------------
Optimised portfolio for Maximum Sharpe Ratio

Time window/frequency: 252
Risk free rate: 0.005
Expected annual Return: 0.345
Annual Volatility: 0.174
Sharpe Ratio: 1.956

Optimal weights:
            WIKI/GOOG  WIKI/AMZN  WIKI/MCD      WIKI/DIS
Allocation        0.0   0.413222  0.586778  2.285851e-17
----------------------------------------------------------------------

Meaning for Maximum Sharpe Ratio, you should invest 0% in the first, 41.3% in the second, 58.7% in the third and again 0% in the fourth stock in this example.

I hope this helps to get you started. Please have a look at the examples in the folder example, those should help as well.