You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

yahoofinance.py 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. from abc import ABC
  17. import pandas as pd
  18. from agent.component.base import ComponentBase, ComponentParamBase
  19. import yfinance as yf
  20. class YahooFinanceParam(ComponentParamBase):
  21. """
  22. Define the YahooFinance component parameters.
  23. """
  24. def __init__(self):
  25. super().__init__()
  26. self.info = True
  27. self.history = False
  28. self.count = False
  29. self.financials = False
  30. self.income_stmt = False
  31. self.balance_sheet = False
  32. self.cash_flow_statement = False
  33. self.news = True
  34. def check(self):
  35. self.check_boolean(self.info, "get all stock info")
  36. self.check_boolean(self.history, "get historical market data")
  37. self.check_boolean(self.count, "show share count")
  38. self.check_boolean(self.financials, "show financials")
  39. self.check_boolean(self.income_stmt, "income statement")
  40. self.check_boolean(self.balance_sheet, "balance sheet")
  41. self.check_boolean(self.cash_flow_statement, "cash flow statement")
  42. self.check_boolean(self.news, "show news")
  43. class YahooFinance(ComponentBase, ABC):
  44. component_name = "YahooFinance"
  45. def _run(self, history, **kwargs):
  46. ans = self.get_input()
  47. ans = "".join(ans["content"]) if "content" in ans else ""
  48. if not ans:
  49. return YahooFinance.be_output("")
  50. yohoo_res = []
  51. try:
  52. msft = yf.Ticker(ans)
  53. if self._param.info:
  54. yohoo_res.append({"content": "info:\n" + pd.Series(msft.info).to_markdown() + "\n"})
  55. if self._param.history:
  56. yohoo_res.append({"content": "history:\n" + msft.history().to_markdown() + "\n"})
  57. if self._param.financials:
  58. yohoo_res.append({"content": "calendar:\n" + pd.DataFrame(msft.calendar).to_markdown() + "\n"})
  59. if self._param.balance_sheet:
  60. yohoo_res.append({"content": "balance sheet:\n" + msft.balance_sheet.to_markdown() + "\n"})
  61. yohoo_res.append(
  62. {"content": "quarterly balance sheet:\n" + msft.quarterly_balance_sheet.to_markdown() + "\n"})
  63. if self._param.cash_flow_statement:
  64. yohoo_res.append({"content": "cash flow statement:\n" + msft.cashflow.to_markdown() + "\n"})
  65. yohoo_res.append(
  66. {"content": "quarterly cash flow statement:\n" + msft.quarterly_cashflow.to_markdown() + "\n"})
  67. if self._param.news:
  68. yohoo_res.append({"content": "news:\n" + pd.DataFrame(msft.news).to_markdown() + "\n"})
  69. except Exception as e:
  70. print("**ERROR** " + str(e))
  71. if not yohoo_res:
  72. return YahooFinance.be_output("")
  73. return pd.DataFrame(yohoo_res)