Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Real-time and historical stock and forex market data via Twelve Data API: quotes, OHLCV time series, and symbol search.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
tools/reference_data.py
1"""2Twelve Data Reference Data Tools — Search and discover stocks and forex pairs.34Provides tools for searching symbols, listing available stocks, forex pairs, and exchanges.5"""67import logging89from core.tool import BaseTool, ToolContext, ToolResult10from .client import get_client, handle_api_error1112logger = logging.getLogger(__name__)131415class TwelveDataSearchTool(BaseTool):16"""Search for stocks or forex pairs by name or symbol."""1718@property19def name(self) -> str:20return "twelvedata_search"2122@property23def description(self) -> str:24return """Search for stocks or forex pairs by company name, symbol, or currency.2526Use this to find the correct symbol before fetching quotes or time series data.2728Examples:29- Search for "Apple" to find AAPL30- Search for "EUR" to find EUR/USD and other EUR pairs31- Search for "MSFT" to get Microsoft details3233Parameters:34- query: Search query (company name, stock symbol, or currency pair)3536Returns: Array of matching symbols with name, exchange, type, and currency info"""3738@property39def parameters(self) -> dict:40return {41"type": "object",42"properties": {43"query": {44"type": "string",45"description": "Search query (company name, stock symbol, or currency)",46},47},48"required": ["query"],49}5051async def execute(self, ctx: ToolContext, query: str = "", **kwargs) -> ToolResult:52if not query:53return ToolResult(success=False, error="'query' is required")54try:55data = await get_client().search_symbol(query=query)56if data.get("status") == "error":57return ToolResult(success=False, error=f"API Error: {data.get('message', 'Unknown error')}")58return ToolResult(success=True, output=data)59except Exception as e:60return handle_api_error(e)616263class TwelveDataStocksTool(BaseTool):64"""Get list of available stocks, optionally filtered by exchange or country."""6566@property67def name(self) -> str:68return "twelvedata_stocks"6970@property71def description(self) -> str:72return """Get list of available stocks on Twelve Data.7374Can filter by exchange (NASDAQ, NYSE, etc.) or country (US, GB, etc.) to narrow results.7576Parameters:77- exchange: (optional) Filter by exchange code (e.g., NASDAQ, NYSE, LSE)78- country: (optional) Filter by country code (e.g., US, GB, JP)7980Returns: Array of stocks with symbol, name, currency, exchange, and type"""8182@property83def parameters(self) -> dict:84return {85"type": "object",86"properties": {87"exchange": {88"type": "string",89"description": "Filter by exchange code (NASDAQ, NYSE, etc.) - optional",90},91"country": {92"type": "string",93"description": "Filter by country code (US, GB, JP, etc.) - optional",94},95},96}9798async def execute(self, ctx: ToolContext, exchange: str = "", country: str = "", **kwargs) -> ToolResult:99try:100data = await get_client().get_stocks(101exchange=exchange if exchange else None,102country=country if country else None,103)104if data.get("status") == "error":105return ToolResult(success=False, error=f"API Error: {data.get('message', 'Unknown error')}")106return ToolResult(success=True, output=data)107except Exception as e:108return handle_api_error(e)109110111class TwelveDataForexPairsTool(BaseTool):112"""Get list of available forex pairs."""113114@property115def name(self) -> str:116return "twelvedata_forex_pairs"117118@property119def description(self) -> str:120return """Get list of all available forex (currency) pairs on Twelve Data.121122Returns major, minor, and exotic forex pairs including:123- Major pairs: EUR/USD, GBP/USD, USD/JPY, etc.124- Minor pairs: EUR/GBP, GBP/JPY, etc.125- Exotic pairs: USD/TRY, EUR/HUF, etc.126127No parameters required.128129Returns: Array of forex pairs with symbol, currency base, currency quote"""130131@property132def parameters(self) -> dict:133return {"type": "object", "properties": {}}134135async def execute(self, ctx: ToolContext, **kwargs) -> ToolResult:136try:137data = await get_client().get_forex_pairs()138if data.get("status") == "error":139return ToolResult(success=False, error=f"API Error: {data.get('message', 'Unknown error')}")140return ToolResult(success=True, output=data)141except Exception as e:142return handle_api_error(e)143144145class TwelveDataExchangesTool(BaseTool):146"""Get list of supported stock exchanges."""147148@property149def name(self) -> str:150return "twelvedata_exchanges"151152@property153def description(self) -> str:154return """Get list of all supported stock exchanges on Twelve Data.155156Returns global exchanges including NASDAQ, NYSE, LSE, TSE, and many more.157158No parameters required.159160Returns: Array of exchanges with name, code, country, and timezone"""161162@property163def parameters(self) -> dict:164return {"type": "object", "properties": {}}165166async def execute(self, ctx: ToolContext, **kwargs) -> ToolResult:167try:168data = await get_client().get_exchanges()169if data.get("status") == "error":170return ToolResult(success=False, error=f"API Error: {data.get('message', 'Unknown error')}")171return ToolResult(success=True, output=data)172except Exception as e:173return handle_api_error(e)174