ahudock commited on
Commit
7b428a4
·
1 Parent(s): f3bf97a

Add yf_get_atm_straddle_price

Browse files
Files changed (2) hide show
  1. app.py +75 -1
  2. requirements.txt +2 -1
app.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  import pytz
5
  import yaml
6
  import yfinance as yf
 
7
  from tools.final_answer import FinalAnswerTool
8
 
9
  from Gradio_UI import GradioUI
@@ -49,6 +50,79 @@ def yf_get_ticker_price(ticker: str) -> float:
49
  data = yf.Ticker(ticker).history(period="1d")
50
  return float(data["Close"].iloc[-1])
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  ddgs = DuckDuckGoSearchTool();
53
 
54
  final_answer = FinalAnswerTool()
@@ -72,7 +146,7 @@ with open("prompts.yaml", 'r') as stream:
72
 
73
  agent = CodeAgent(
74
  model=model,
75
- tools=[final_answer, ddgs, yf_get_ticker_price], ## add your tools here (don't remove final answer)
76
  max_steps=6,
77
  verbosity_level=1,
78
  grammar=None,
 
4
  import pytz
5
  import yaml
6
  import yfinance as yf
7
+ from typing import Dict, Any
8
  from tools.final_answer import FinalAnswerTool
9
 
10
  from Gradio_UI import GradioUI
 
50
  data = yf.Ticker(ticker).history(period="1d")
51
  return float(data["Close"].iloc[-1])
52
 
53
+ @tool
54
+ def yf_get_atm_straddle_price(ticker: str) -> Dict[str, Any]:
55
+ """
56
+ Calculate the price of the at-the-money (ATM) straddle for a given stock ticker,
57
+ including the implied volatility (IV) of the ATM call and put options. The ATM
58
+ strike is defined as the strike closest to the current underlying price, using
59
+ the nearest available expiration date.
60
+
61
+ Args:
62
+ ticker (str): The stock ticker symbol, e.g., AAPL.
63
+
64
+ Returns:
65
+ Dict[str, Any]: A dictionary containing:
66
+ - 'underlying_price' (float): The current stock price.
67
+ - 'expiration' (str): The nearest option expiration date used.
68
+ - 'strike' (float): The ATM strike price selected.
69
+ - 'call_price' (float): The last traded price of the ATM call option.
70
+ - 'put_price' (float): The last traded price of the ATM put option.
71
+ - 'call_iv' (float): The implied volatility of the ATM call option.
72
+ - 'put_iv' (float): The implied volatility of the ATM put option.
73
+ - 'straddle_price' (float): The total ATM straddle price (call + put).
74
+ """
75
+ stock = yf.Ticker(ticker)
76
+
77
+ # Get current underlying price
78
+ hist = stock.history(period="1d")
79
+ if hist.empty:
80
+ return {"error": f"No price data found for {ticker}."}
81
+
82
+ underlying_price = float(hist["Close"].iloc[-1])
83
+
84
+ # Get option expiration dates
85
+ expirations = stock.options
86
+ if not expirations:
87
+ return {"error": f"No options data available for {ticker}."}
88
+
89
+ # Use the nearest expiration
90
+ expiration = expirations[0]
91
+ opt_chain = stock.option_chain(expiration)
92
+
93
+ calls = opt_chain.calls
94
+ puts = opt_chain.puts
95
+
96
+ if calls.empty or puts.empty:
97
+ return {"error": f"Options chain incomplete for {ticker} on {expiration}."}
98
+
99
+ # Find ATM strike
100
+ calls["diff"] = (calls["strike"] - underlying_price).abs()
101
+ atm_strike = float(calls.sort_values("diff").iloc[0]["strike"])
102
+
103
+ # Extract ATM call and put rows
104
+ call_row = calls[calls["strike"] == atm_strike].iloc[0]
105
+ put_row = puts[puts["strike"] == atm_strike].iloc[0]
106
+
107
+ call_price = float(call_row.get("lastPrice", 0.0))
108
+ put_price = float(put_row.get("lastPrice", 0.0))
109
+
110
+ call_iv = float(call_row.get("impliedVolatility", 0.0))
111
+ put_iv = float(put_row.get("impliedVolatility", 0.0))
112
+
113
+ straddle_price = call_price + put_price
114
+
115
+ return {
116
+ "underlying_price": underlying_price,
117
+ "expiration": expiration,
118
+ "strike": atm_strike,
119
+ "call_price": call_price,
120
+ "put_price": put_price,
121
+ "call_iv": call_iv,
122
+ "put_iv": put_iv,
123
+ "straddle_price": straddle_price,
124
+ }
125
+
126
  ddgs = DuckDuckGoSearchTool();
127
 
128
  final_answer = FinalAnswerTool()
 
146
 
147
  agent = CodeAgent(
148
  model=model,
149
+ tools=[final_answer, ddgs, yf_get_ticker_price, yf_get_atm_straddle_price], ## add your tools here (don't remove final answer)
150
  max_steps=6,
151
  verbosity_level=1,
152
  grammar=None,
requirements.txt CHANGED
@@ -3,4 +3,5 @@ smolagents==1.13.0
3
  requests
4
  duckduckgo_search
5
  pandas
6
- yfinance
 
 
3
  requests
4
  duckduckgo_search
5
  pandas
6
+ yfinance
7
+ typing