Advertisement
elena1234

Yahoo Finance - code in Python

Jun 29th, 2024 (edited)
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. #1.Importing the Library and Setting the Stock of Interest
  2. import yfinance as yahooFinance
  3. import pandas as pd
  4. pd.set_option('display.max_rows', None)
  5.  
  6. total_energies = yahooFinance.Ticker("TTE.PA")
  7.  
  8.  
  9. #2.Retrieving Stock Information
  10. total_energies.info
  11.  
  12.  
  13. #3.Holders
  14. total_energies.major_holders
  15. total_energies.institutional_holders
  16.  
  17.  
  18. #4.Actions
  19. total_energies.actions
  20.  
  21. total_energies.dividends
  22. df = pd.DataFrame(total_energies.dividends)
  23. df_new = df.groupby(['Date']).agg({'Dividends': 'sum'}).reset_index()
  24. df_new['Date_in_Datetime'] = pd.to_datetime(df_new['Date'])
  25. df_new['Year'] = df_new['Date_in_Datetime'].dt.year
  26. df_converted= df_new.groupby(['Year']).agg({'Dividends': 'sum'}).reset_index()
  27.  
  28. import matplotlib.pyplot as plt #plot the dividends by years
  29. plt.figure(figsize=(10, 6))
  30. plt.bar(df_converted['Year'], df_converted['Dividends'])
  31. plt.xlabel('Year')
  32. plt.ylabel('Total Dividends')
  33. plt.title('Dividends by Year')
  34. plt.show()
  35.  
  36.  
  37. total_energies.splits
  38.  
  39.  
  40. #5.Financial Statements
  41. total_energies.income_stmt
  42. total_energies.quarterly_income_stmt
  43.  
  44. total_energies.balance_sheet
  45. total_energies.quarterly_balance_sheet
  46.  
  47. total_energies.cash_flow
  48. total_energies.quarterly_cashflow
  49.  
  50. _____________________________________________________________________________________________________
  51.  
  52. #QuantStats: Portfolio analytics for quants
  53. %matplotlib inline
  54. import quantstats as qs
  55.  
  56.  
  57. # extend pandas functionality with metrics, etc.
  58. qs.extend_pandas()
  59.  
  60. # fetch the daily returns for a stock
  61. stock = qs.utils.download_returns('TTE.PA')
  62. stock
  63.  
  64.  
  65. # show sharpe ratio
  66. qs.stats.sharpe(stock)
  67.  
  68.  
  69. # or using extend_pandas() :)
  70. stock.sharpe()
  71.  
  72.  
  73. #Visualize stock performance
  74. qs.plots.snapshot(stock, title='TotalEnergies Performance', show=True)
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement