H
commited on
Commit
·
b102c54
1
Parent(s):
61acd39
Add component yahoo finance (#2244)
Browse files### What problem does this PR solve?
### Type of change
- [ ] New Feature (non-breaking change which adds functionality)
- agent/component/__init__.py +2 -0
- agent/component/yahoofinance.py +90 -0
- requirements.txt +2 -1
- requirements_arm.txt +2 -1
agent/component/__init__.py
CHANGED
|
@@ -22,6 +22,8 @@ from .github import GitHub, GitHubParam
|
|
| 22 |
from .baidufanyi import BaiduFanyi, BaiduFanyiParam
|
| 23 |
from .qweather import QWeather, QWeatherParam
|
| 24 |
from .exesql import ExeSQL, ExeSQLParam
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def component_class(class_name):
|
| 27 |
m = importlib.import_module("agent.component")
|
|
|
|
| 22 |
from .baidufanyi import BaiduFanyi, BaiduFanyiParam
|
| 23 |
from .qweather import QWeather, QWeatherParam
|
| 24 |
from .exesql import ExeSQL, ExeSQLParam
|
| 25 |
+
from .yahoofinance import YahooFinance, YahooFinanceParam
|
| 26 |
+
|
| 27 |
|
| 28 |
def component_class(class_name):
|
| 29 |
m = importlib.import_module("agent.component")
|
agent/component/yahoofinance.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 21 |
+
|
| 22 |
+
class YahooFinanceParam(ComponentParamBase):
|
| 23 |
+
"""
|
| 24 |
+
Define the YahooFinance component parameters.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
def __init__(self):
|
| 28 |
+
super().__init__()
|
| 29 |
+
self.info = True
|
| 30 |
+
self.history = False
|
| 31 |
+
self.count = False
|
| 32 |
+
self.financials = False
|
| 33 |
+
self.income_stmt = False
|
| 34 |
+
self.balance_sheet = False
|
| 35 |
+
self.cash_flow_statement = False
|
| 36 |
+
self.news = True
|
| 37 |
+
|
| 38 |
+
def check(self):
|
| 39 |
+
self.check_boolean(self.info, "get all stock info")
|
| 40 |
+
self.check_boolean(self.history, "get historical market data")
|
| 41 |
+
self.check_boolean(self.count, "show share count")
|
| 42 |
+
self.check_boolean(self.financials, "show financials")
|
| 43 |
+
self.check_boolean(self.income_stmt, "income statement")
|
| 44 |
+
self.check_boolean(self.balance_sheet, "balance sheet")
|
| 45 |
+
self.check_boolean(self.cash_flow_statement, "cash flow statement")
|
| 46 |
+
self.check_boolean(self.news, "show news")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
class YahooFinance(ComponentBase, ABC):
|
| 50 |
+
component_name = "YahooFinance"
|
| 51 |
+
|
| 52 |
+
def _run(self, history, **kwargs):
|
| 53 |
+
ans = self.get_input()
|
| 54 |
+
ans = "".join(ans["content"]) if "content" in ans else ""
|
| 55 |
+
if not ans:
|
| 56 |
+
return YahooFinance.be_output("")
|
| 57 |
+
|
| 58 |
+
yohoo_res = []
|
| 59 |
+
try:
|
| 60 |
+
msft = yf.Ticker(ans)
|
| 61 |
+
if self._param.info:
|
| 62 |
+
yohoo_res.append({"content": "info:\n" + pd.Series(msft.info).to_markdown() + "\n"})
|
| 63 |
+
if self._param.history:
|
| 64 |
+
yohoo_res.append({"content": "history:\n" + msft.history().to_markdown() + "\n"})
|
| 65 |
+
if self._param.count:
|
| 66 |
+
yohoo_res.append({"content": "count:\n" + msft.get_shares_full().to_markdown() + "\n"})
|
| 67 |
+
if self._param.financials:
|
| 68 |
+
yohoo_res.append({"content": "calendar:\n" + pd.DataFrame(msft.calendar).to_markdown + "\n"})
|
| 69 |
+
yohoo_res.append({"content": "sec_filings:\n" + pd.DataFrame(msft.sec_filings).to_markdown() + "\n"})
|
| 70 |
+
if self._param.income_stmt:
|
| 71 |
+
yohoo_res.append({"content": "income statement:\n" + msft.income_stmt.to_markdown() + "\n"})
|
| 72 |
+
yohoo_res.append(
|
| 73 |
+
{"content": "quarterly income statement:\n" + msft.quarterly_income_stmt.to_markdown() + "\n"})
|
| 74 |
+
if self._param.balance_sheet:
|
| 75 |
+
yohoo_res.append({"content": "balance sheet:\n" + msft.balance_sheet.to_markdown() + "\n"})
|
| 76 |
+
yohoo_res.append(
|
| 77 |
+
{"content": "quarterly balance sheet:\n" + msft.quarterly_balance_sheet.to_markdown() + "\n"})
|
| 78 |
+
if self._param.cash_flow_statement:
|
| 79 |
+
yohoo_res.append({"content": "cash flow statement:\n" + msft.cashflow.to_markdown() + "\n"})
|
| 80 |
+
yohoo_res.append(
|
| 81 |
+
{"content": "quarterly cash flow statement:\n" + msft.quarterly_cashflow.to_markdown() + "\n"})
|
| 82 |
+
if self._param.news:
|
| 83 |
+
yohoo_res.append({"content": "news:\n" + pd.DataFrame(msft.news).to_markdown() + "\n"})
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print("**ERROR** " + str(e))
|
| 86 |
+
|
| 87 |
+
if not yohoo_res:
|
| 88 |
+
return YahooFinance.be_output("")
|
| 89 |
+
|
| 90 |
+
return pd.DataFrame(yohoo_res)
|
requirements.txt
CHANGED
|
@@ -94,4 +94,5 @@ wikipedia==1.4.0
|
|
| 94 |
word2number==1.1
|
| 95 |
xgboost==2.1.0
|
| 96 |
xpinyin==0.7.6
|
| 97 |
-
|
|
|
|
|
|
| 94 |
word2number==1.1
|
| 95 |
xgboost==2.1.0
|
| 96 |
xpinyin==0.7.6
|
| 97 |
+
yfinance==0.2.43
|
| 98 |
+
zhipuai==2.0.1
|
requirements_arm.txt
CHANGED
|
@@ -167,4 +167,5 @@ scholarly==1.7.11
|
|
| 167 |
deepl==1.18.0
|
| 168 |
psycopg2-binary==2.9.9
|
| 169 |
tabulate==0.9.0
|
| 170 |
-
vertexai==1.64.0
|
|
|
|
|
|
| 167 |
deepl==1.18.0
|
| 168 |
psycopg2-binary==2.9.9
|
| 169 |
tabulate==0.9.0
|
| 170 |
+
vertexai==1.64.0
|
| 171 |
+
yfinance==0.2.43
|