Python Forum
Stock Return calculation problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stock Return calculation problem
#6
If you want the percent change for each day I would do this:
import pandas_datareader as web
import pandas as pd

#Enter the stocks you want to use as variables
 
portfolio = {
    "BRK-B":0.2,
    "AMZN":0.3,
    "AAPL":0.1,
    "NFLX":0.2,
    "GOOG":0.2,
}
 
#select start date for correlation window as well as list of tickers
def percent_change(portfolio, start, end):
    pct_change = {}
    for ticker in portfolio:
        stock = web.get_data_yahoo(ticker, start=start, end=end)
        pct_change[ticker] = stock['Adj Close'].pct_change()[1:]
    return pd.DataFrame(pct_change)

pct_change = percent_change(portfolio, "2013-01-01", "2013-01-10")

weighted_change = pct_change * portfolio.values()
weighted_change['Total'] = weighted_change.sum(axis = 1)

countsum = []
cs = 100
for total in weighted_change["Total"]:
    cs *= (1 + total)
    countsum.append(cs)
weighted_change["Countsum"] = countsum

print(weighted_change)
Output:
BRK-B AMZN AAPL NFLX GOOG Total Countsum Date 2013-01-03 0.000901 0.001364 -0.001262 0.009955 0.000116 0.011075 101.107477 2013-01-04 0.000491 0.000778 -0.002785 -0.001263 0.003952 0.001173 101.226027 2013-01-07 -0.000852 0.010778 -0.000588 0.006710 -0.000873 0.015174 102.762025 2013-01-08 0.000770 -0.002324 0.000269 -0.004113 -0.000395 -0.005792 102.166791 2013-01-09 -0.001045 -0.000034 -0.001563 -0.002573 0.001315 -0.003900 101.768360 2013-01-10 0.002850 -0.001138 0.001240 0.004358 0.000910 0.008221 102.605005
Reply


Messages In This Thread
Stock Return calculation problem - by LFin - Sep-22-2022, 02:13 PM
RE: Stock Return calculation problem - by ibreeden - Sep-23-2022, 08:57 AM
RE: Stock Return calculation problem - by perfringo - Sep-23-2022, 11:23 AM
RE: Stock Return calculation problem - by LFin - Sep-23-2022, 06:46 PM
RE: Stock Return calculation problem - by perfringo - Sep-24-2022, 08:14 AM
RE: Stock Return calculation problem - by LFin - Sep-26-2022, 01:52 PM
RE: Stock Return calculation problem - by deanhystad - Sep-24-2022, 11:11 AM
RE: Stock Return calculation problem - by LFin - Sep-26-2022, 01:59 PM
RE: Stock Return calculation problem - by LFin - Sep-26-2022, 03:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,214 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Any suggestions to improve BuySell stock problem efficiency? mrapple2020 0 1,401 May-13-2020, 06:19 PM
Last Post: mrapple2020
  Problem with simple 3D Vektor calculation Pythocodras 0 1,742 Dec-11-2019, 07:18 PM
Last Post: Pythocodras
  Matrix Calculation Problem arshad 4 2,735 Nov-04-2019, 03:48 PM
Last Post: baquerik
  Pass variable script return twice output problem Faruk 8 4,520 Dec-26-2018, 11:57 AM
Last Post: Faruk
  problem with function return value ujjwalrathod007 9 23,802 Sep-23-2016, 03:02 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020