from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool from datetime import datetime import requests import pytz import yaml import yfinance as yf from typing import Tuple, Dict, Any from tools.final_answer import FinalAnswerTool from Gradio_UI import GradioUI def _get_atm_option_data(ticker: str, dte: int): """ Internal helper to fetch underlying price, selected expiration, ATM strike, ATM call row, and ATM put row. """ stock = yf.Ticker(ticker) hist = stock.history(period="1d") if hist.empty: raise ValueError(f"No price data found for {ticker}.") underlying_price = float(hist["Close"].iloc[-1]) expirations = stock.options if not expirations: raise ValueError(f"No options data available for {ticker}.") exp_dates = [datetime.strptime(exp, "%Y-%m-%d") for exp in expirations] today = datetime.now() dtes = [(exp - today).days for exp in exp_dates] target_idx = min(range(len(dtes)), key=lambda i: abs(dtes[i] - dte)) expiration = expirations[target_idx] opt_chain = stock.option_chain(expiration) calls = opt_chain.calls puts = opt_chain.puts if calls.empty or puts.empty: raise ValueError(f"Options chain incomplete for {ticker} on {expiration}.") calls["diff"] = (calls["strike"] - underlying_price).abs() atm_strike = float(calls.sort_values("diff").iloc[0]["strike"]) call_row = calls[calls["strike"] == atm_strike].iloc[0] put_row = puts[puts["strike"] == atm_strike].iloc[0] return underlying_price, expiration, atm_strike, call_row, put_row @tool def yf_underlying_price(ticker: str, dte: int = 0) -> float: """ Get the current underlying stock price for the given ticker. Args: ticker (str): The stock ticker symbol, e.g., AAPL. dte (int): Days-to-expiration used to select the option chain. Defaults to 0 (nearest expiration). Returns: float: The current underlying stock price. """ underlying_price, _, _, _, _ = _get_atm_option_data(ticker, dte) return underlying_price @tool def yf_selected_expiration(ticker: str, dte: int = 0) -> str: """ Return the option expiration date selected for the given stock ticker and desired days-to-expiration (DTE). The expiration chosen is the one whose days-to-expiration is closest to the requested DTE. Args: ticker (str): The stock ticker symbol, e.g., AAPL. dte (int): Desired days-to-expiration. A value of 0 selects the nearest available expiration. Defaults to 0. Returns: str: The selected expiration date in YYYY-MM-DD format. """ _, expiration, _, _, _ = _get_atm_option_data(ticker, dte) return expiration @tool def yf_atm_strike(ticker: str, dte: int = 0) -> float: """ Get the at-the-money (ATM) strike price for the given ticker. Args: ticker (str): The stock ticker symbol. dte (int): Desired days-to-expiration. Defaults to 0. Returns: float: The ATM strike price. """ _, _, atm_strike, _, _ = _get_atm_option_data(ticker, dte) return atm_strike @tool def yf_atm_call_price(ticker: str, dte: int = 0) -> float: """ Get the last traded price of the ATM call option. Args: ticker (str): The stock ticker symbol. dte (int): Desired days-to-expiration. Defaults to 0. Returns: float: The ATM call option price. """ _, _, _, call_row, _ = _get_atm_option_data(ticker, dte) return float(call_row.get("ask", 0.0)) @tool def yf_atm_put_price(ticker: str, dte: int = 0) -> float: """ Get the last traded price of the ATM put option. Args: ticker (str): The stock ticker symbol. dte (int): Desired days-to-expiration. Defaults to 0. Returns: float: The ATM put option price. """ _, _, _, _, put_row = _get_atm_option_data(ticker, dte) return float(put_row.get("ask", 0.0)) @tool def yf_atm_call_iv(ticker: str, dte: int = 0) -> float: """ Get the implied volatility (IV) of the ATM call option. Args: ticker (str): The stock ticker symbol. dte (int): Desired days-to-expiration. Defaults to 0. Returns: float: The implied volatility of the ATM call option. """ _, _, _, call_row, _ = _get_atm_option_data(ticker, dte) return float(call_row.get("impliedVolatility", 0.0)) @tool def yf_atm_put_iv(ticker: str, dte: int = 0) -> float: """ Get the implied volatility (IV) of the ATM put option. Args: ticker (str): The stock ticker symbol. dte (int): Desired days-to-expiration. Defaults to 0. Returns: float: The implied volatility of the ATM put option. """ _, _, _, _, put_row = _get_atm_option_data(ticker, dte) return float(put_row.get("impliedVolatility", 0.0)) @tool def yf_atm_straddle_price(ticker: str, dte: int = 0) -> float: """ Calculate the price of the ATM straddle (call + put). Args: ticker (str): The stock ticker symbol. dte (int): Desired days-to-expiration. Defaults to 0. Returns: float: The total ATM straddle price. """ _, _, _, call_row, put_row = _get_atm_option_data(ticker, dte) call_price = float(call_row.get("ask", 0.0)) put_price = float(put_row.get("ask", 0.0)) return call_price + put_price ddgs = DuckDuckGoSearchTool(); final_answer = FinalAnswerTool() # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder: # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud' model = HfApiModel( max_tokens=2096, temperature=0.5, model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded custom_role_conversions=None, ) # Import tool from Hub image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True) with open("prompts.yaml", 'r') as stream: prompt_templates = yaml.safe_load(stream) agent = CodeAgent( model=model, tools=[final_answer, ddgs, yf_underlying_price, yf_selected_expiration, yf_atm_strike, yf_atm_call_price, yf_atm_put_price, yf_atm_call_iv, yf_atm_put_iv, yf_atm_straddle_price], ## add your tools here (don't remove final answer) max_steps=6, verbosity_level=1, grammar=None, planning_interval=None, name=None, description=None, prompt_templates=prompt_templates ) GradioUI(agent).launch()