From 0bf3520502faddc732db5125d9702caa54023e2e Mon Sep 17 00:00:00 2001 From: Adam Cohen Hillel Date: Wed, 16 Apr 2025 18:09:05 +0100 Subject: [PATCH] Yahoo finance --- .../tools/api_services/YahooFinanceService.py | 190 ++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 backend/agent/tools/api_services/YahooFinanceService.py diff --git a/backend/agent/tools/api_services/YahooFinanceService.py b/backend/agent/tools/api_services/YahooFinanceService.py new file mode 100644 index 00000000..dbba83ac --- /dev/null +++ b/backend/agent/tools/api_services/YahooFinanceService.py @@ -0,0 +1,190 @@ +from typing import Dict, Optional + +from agent.tools.api_services.APIServicesBase import APIServicesBase, EndpointSchema + + +class YahooFinanceService(APIServicesBase): + def __init__(self): + endpoints: Dict[str, EndpointSchema] = { + "get_tickers": { + "route": "/v2/markets/tickers", + "method": "GET", + "name": "Yahoo Finance Tickers", + "description": "Get financial tickers from Yahoo Finance with various filters and parameters.", + "payload": { + "page": "Page number for pagination (optional, default: 1)", + "type": "Asset class type (required): STOCKS, ETF, MUTUALFUNDS, or FUTURES", + } + }, + "search": { + "route": "/v1/markets/search", + "method": "GET", + "name": "Yahoo Finance Search", + "description": "Search for financial instruments on Yahoo Finance", + "payload": { + "search": "Search term (required)", + } + }, + "get_news": { + "route": "/v2/markets/news", + "method": "GET", + "name": "Yahoo Finance News", + "description": "Get news related to specific tickers from Yahoo Finance", + "payload": { + "tickers": "Stock symbol (optional, e.g., AAPL)", + "type": "News type (optional): ALL, VIDEO, or PRESS_RELEASE", + } + }, + "get_stock_module": { + "route": "/v1/markets/stock/modules", + "method": "GET", + "name": "Yahoo Finance Stock Module", + "description": "Get detailed information about a specific stock module", + "payload": { + "ticker": "Company ticker symbol (required, e.g., AAPL)", + "module": "Module to retrieve (required): asset-profile, financial-data, earnings, etc.", + } + }, + "get_sma": { + "route": "/v1/markets/indicators/sma", + "method": "GET", + "name": "Yahoo Finance SMA Indicator", + "description": "Get Simple Moving Average (SMA) indicator data for a stock", + "payload": { + "symbol": "Stock symbol (required, e.g., AAPL)", + "interval": "Time interval (required): 5m, 15m, 30m, 1h, 1d, 1wk, 1mo, 3mo", + "series_type": "Series type (required): open, close, high, low", + "time_period": "Number of data points used for calculation (required)", + "limit": "Limit the number of results (optional, default: 50)", + } + }, + "get_rsi": { + "route": "/v1/markets/indicators/rsi", + "method": "GET", + "name": "Yahoo Finance RSI Indicator", + "description": "Get Relative Strength Index (RSI) indicator data for a stock", + "payload": { + "symbol": "Stock symbol (required, e.g., AAPL)", + "interval": "Time interval (required): 5m, 15m, 30m, 1h, 1d, 1wk, 1mo, 3mo", + "series_type": "Series type (required): open, close, high, low", + "time_period": "Number of data points used for calculation (required)", + "limit": "Limit the number of results (optional, default: 50)", + } + }, + "get_earnings_calendar": { + "route": "/v1/markets/calendar/earnings", + "method": "GET", + "name": "Yahoo Finance Earnings Calendar", + "description": "Get earnings calendar data for a specific date", + "payload": { + "date": "Calendar date in yyyy-mm-dd format (optional, e.g., 2023-11-30)", + } + }, + "get_insider_trades": { + "route": "/v1/markets/insider-trades", + "method": "GET", + "name": "Yahoo Finance Insider Trades", + "description": "Get recent insider trading activity", + "payload": {} + }, + } + base_url = "https://yahoo-finance15.p.rapidapi.com/api" + super().__init__(base_url, endpoints) + + +if __name__ == "__main__": + import os + os.environ["RAPID_API_KEY"] = "b5258e1150msh2e4f098fdeeda7fp1ac998jsncbc24fc76b95" + tool = YahooFinanceService() + + # Example for getting stock tickers + tickers_result = tool.call_endpoint( + route="get_tickers", + payload={ + "page": 1, + "type": "STOCKS" + } + ) + print("Tickers Result:", tickers_result) + + # Example for searching financial instruments + search_result = tool.call_endpoint( + route="search", + payload={ + "search": "AA" + } + ) + print("Search Result:", search_result) + + # Example for getting financial news + news_result = tool.call_endpoint( + route="get_news", + payload={ + "tickers": "AAPL", + "type": "ALL" + } + ) + print("News Result:", news_result) + + # Example for getting stock asset profile module + stock_module_result = tool.call_endpoint( + route="get_stock_module", + payload={ + "ticker": "AAPL", + "module": "asset-profile" + } + ) + print("Asset Profile Result:", stock_module_result) + + # Example for getting financial data module + financial_data_result = tool.call_endpoint( + route="get_stock_module", + payload={ + "ticker": "AAPL", + "module": "financial-data" + } + ) + print("Financial Data Result:", financial_data_result) + + # Example for getting SMA indicator data + sma_result = tool.call_endpoint( + route="get_sma", + payload={ + "symbol": "AAPL", + "interval": "5m", + "series_type": "close", + "time_period": "50", + "limit": "50" + } + ) + print("SMA Result:", sma_result) + + # Example for getting RSI indicator data + rsi_result = tool.call_endpoint( + route="get_rsi", + payload={ + "symbol": "AAPL", + "interval": "5m", + "series_type": "close", + "time_period": "50", + "limit": "50" + } + ) + print("RSI Result:", rsi_result) + + # Example for getting earnings calendar data + earnings_calendar_result = tool.call_endpoint( + route="get_earnings_calendar", + payload={ + "date": "2023-11-30" + } + ) + print("Earnings Calendar Result:", earnings_calendar_result) + + # Example for getting insider trades + insider_trades_result = tool.call_endpoint( + route="get_insider_trades", + payload={} + ) + print("Insider Trades Result:", insider_trades_result) +