Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import logging
  17. from abc import ABC
  18. import pandas as pd
  19. from agent.component.base import ComponentBase, ComponentParamBase
  20. import yfinance as yf
  21. class YahooFinanceParam(ComponentParamBase):
  22. """
  23. Define the YahooFinance component parameters.
  24. """
  25. def __init__(self):
  26. super().__init__()
  27. self.info = True
  28. self.history = False
  29. self.count = False
  30. self.financials = False
  31. self.income_stmt = False
  32. self.balance_sheet = False
  33. self.cash_flow_statement = False
  34. self.news = True
  35. def check(self):
  36. self.check_boolean(self.info, "get all stock info")
  37. self.check_boolean(self.history, "get historical market data")
  38. self.check_boolean(self.count, "show share count")
  39. self.check_boolean(self.financials, "show financials")
  40. self.check_boolean(self.income_stmt, "income statement")
  41. self.check_boolean(self.balance_sheet, "balance sheet")
  42. self.check_boolean(self.cash_flow_statement, "cash flow statement")
  43. self.check_boolean(self.news, "show news")
  44. class YahooFinance(ComponentBase, ABC):
  45. component_name = "YahooFinance"
  46. def _run(self, history, **kwargs):
  47. ans = self.get_input()
  48. ans = "".join(ans["content"]) if "content" in ans else ""
  49. if not ans:
  50. return YahooFinance.be_output("")
  51. yohoo_res = []
  52. try:
  53. msft = yf.Ticker(ans)
  54. if self._param.info:
  55. yohoo_res.append({"content": "info:\n" + pd.Series(msft.info).to_markdown() + "\n"})
  56. if self._param.history:
  57. yohoo_res.append({"content": "history:\n" + msft.history().to_markdown() + "\n"})
  58. if self._param.financials:
  59. yohoo_res.append({"content": "calendar:\n" + pd.DataFrame(msft.calendar).to_markdown() + "\n"})
  60. if self._param.balance_sheet:
  61. yohoo_res.append({"content": "balance sheet:\n" + msft.balance_sheet.to_markdown() + "\n"})
  62. yohoo_res.append(
  63. {"content": "quarterly balance sheet:\n" + msft.quarterly_balance_sheet.to_markdown() + "\n"})
  64. if self._param.cash_flow_statement:
  65. yohoo_res.append({"content": "cash flow statement:\n" + msft.cashflow.to_markdown() + "\n"})
  66. yohoo_res.append(
  67. {"content": "quarterly cash flow statement:\n" + msft.quarterly_cashflow.to_markdown() + "\n"})
  68. if self._param.news:
  69. yohoo_res.append({"content": "news:\n" + pd.DataFrame(msft.news).to_markdown() + "\n"})
  70. except Exception:
  71. logging.exception("YahooFinance got exception")
  72. if not yohoo_res:
  73. return YahooFinance.be_output("")
  74. return pd.DataFrame(yohoo_res)