[text] FIn

Viewer

  1. import pandas as pd
  2. import yfinance as yf
  3. import matplotlib.pyplot as plt
  4.  
  5. # Downloading the data
  6. start_date = '2015-07-01'
  7. end_date = '2023-08-01'
  8. brk_a = yf.download('BRK-A', start=start_date, end=end_date)['Adj Close']
  9. sp500 = yf.download('^GSPC', start=start_date, end=end_date)['Adj Close']
  10.  
  11. # Normalizing the data
  12. brk_a_norm = brk_a / brk_a.iloc[0] * 100
  13. sp500_norm = sp500 / sp500.iloc[0] * 100
  14.  
  15. # Plotting the normalized prices
  16. plt.figure(figsize=(14, 7))
  17. plt.plot(brk_a_norm, label='Berkshire Hathaway (BRK-A)')
  18. plt.plot(sp500_norm, label='S&P 500 Index')
  19. plt.title('Normalized Prices of BRK-A and S&P 500 Index (2015-2023)')
  20. plt.xlabel('Date')
  21. plt.ylabel('Normalized Price (Base 100 on Jul 1, 2015)')
  22. plt.legend()
  23. plt.grid(True)
  24. plt.show()
  25.  

Editor

You can edit this paste and save as new:


File Description
  • FIn
  • Paste Code
  • 23 Apr-2024
  • 774 Bytes
You can Share it: