Spaces:
Sleeping
Sleeping
Add estimate_expected_move
Browse files
app.py
CHANGED
|
@@ -47,24 +47,31 @@ def yf_get_ticker_price(ticker: str) -> float:
|
|
| 47 |
Args:
|
| 48 |
ticker: A string representing a stock ticker.
|
| 49 |
"""
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 59 |
-
|
| 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
|
| 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.
|
|
@@ -75,21 +82,26 @@ def yf_get_atm_straddle_price(ticker: str) -> Dict[str, Any]:
|
|
| 75 |
stock = yf.Ticker(ticker)
|
| 76 |
|
| 77 |
# Get current underlying price
|
| 78 |
-
|
| 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 |
-
#
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
calls = opt_chain.calls
|
| 94 |
puts = opt_chain.puts
|
| 95 |
|
|
@@ -123,6 +135,36 @@ def yf_get_atm_straddle_price(ticker: str) -> Dict[str, Any]:
|
|
| 123 |
"straddle_price": straddle_price,
|
| 124 |
}
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
ddgs = DuckDuckGoSearchTool();
|
| 127 |
|
| 128 |
final_answer = FinalAnswerTool()
|
|
@@ -146,7 +188,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 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,
|
|
|
|
| 47 |
Args:
|
| 48 |
ticker: A string representing a stock ticker.
|
| 49 |
"""
|
| 50 |
+
# Get current underlying price
|
| 51 |
+
hist = stock.history(period="1d")
|
| 52 |
+
if hist.empty:
|
| 53 |
+
return {"error": f"No price data found for {ticker}."}
|
| 54 |
+
|
| 55 |
+
return float(hist["Close"].iloc[-1])
|
| 56 |
|
| 57 |
@tool
|
| 58 |
+
def yf_get_atm_straddle_price(ticker: str, dte: int = 0) -> Dict[str, Any]:
|
| 59 |
"""
|
| 60 |
Calculate the price of the at-the-money (ATM) straddle for a given stock ticker,
|
| 61 |
including the implied volatility (IV) of the ATM call and put options. The ATM
|
| 62 |
+
strike is defined as the strike closest to the current underlying price. The
|
| 63 |
+
expiration date is selected based on the specified days-to-expiration (DTE).
|
| 64 |
|
| 65 |
Args:
|
| 66 |
ticker (str): The stock ticker symbol, e.g., AAPL.
|
| 67 |
+
dte (int): Desired days-to-expiration. The tool selects the expiration date
|
| 68 |
+
closest to this value. A value of 0 selects the nearest available
|
| 69 |
+
expiration. Defaults to 0.
|
| 70 |
|
| 71 |
Returns:
|
| 72 |
Dict[str, Any]: A dictionary containing:
|
| 73 |
- 'underlying_price' (float): The current stock price.
|
| 74 |
+
- 'expiration' (str): The selected option expiration date.
|
| 75 |
- 'strike' (float): The ATM strike price selected.
|
| 76 |
- 'call_price' (float): The last traded price of the ATM call option.
|
| 77 |
- 'put_price' (float): The last traded price of the ATM put option.
|
|
|
|
| 82 |
stock = yf.Ticker(ticker)
|
| 83 |
|
| 84 |
# Get current underlying price
|
| 85 |
+
underlying_price = yf_get_ticker_price(ticker)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
# Get option expiration dates
|
| 88 |
expirations = stock.options
|
| 89 |
if not expirations:
|
| 90 |
return {"error": f"No options data available for {ticker}."}
|
| 91 |
|
| 92 |
+
# Convert expiration strings to datetime objects
|
| 93 |
+
exp_dates = [datetime.strptime(exp, "%Y-%m-%d") for exp in expirations]
|
| 94 |
+
today = datetime.now()
|
| 95 |
+
|
| 96 |
+
# Compute DTE for each expiration
|
| 97 |
+
dtes = [(exp - today).days for exp in exp_dates]
|
| 98 |
|
| 99 |
+
# Select expiration closest to requested DTE
|
| 100 |
+
target_idx = min(range(len(dtes)), key=lambda i: abs(dtes[i] - dte))
|
| 101 |
+
expiration = expirations[target_idx]
|
| 102 |
+
|
| 103 |
+
# Load option chain for selected expiration
|
| 104 |
+
opt_chain = stock.option_chain(expiration)
|
| 105 |
calls = opt_chain.calls
|
| 106 |
puts = opt_chain.puts
|
| 107 |
|
|
|
|
| 135 |
"straddle_price": straddle_price,
|
| 136 |
}
|
| 137 |
|
| 138 |
+
|
| 139 |
+
@tool
|
| 140 |
+
def estimate_expected_move(ticker:str, dte:int = 0) -> Dict[str, Any]:
|
| 141 |
+
"""
|
| 142 |
+
Roughly estimates the expected move for a given stock ticker by adding/subtracting the price of the ATM straddle for
|
| 143 |
+
a given expiration to the underlying price.
|
| 144 |
+
|
| 145 |
+
Args:
|
| 146 |
+
ticker (str): The stock ticker to estimate the expected move for.
|
| 147 |
+
dte (int): Days to expiration.
|
| 148 |
+
|
| 149 |
+
Returns:
|
| 150 |
+
Dict[str, Any]: A dictionary containing:
|
| 151 |
+
- 'underlying_price' (float): The current stock price.
|
| 152 |
+
- 'em' (float): The expected move (price of the ATM straddle).
|
| 153 |
+
- 'em_lower' (float): The lower expected move.
|
| 154 |
+
- 'em_upper' (float): The upper expected move.
|
| 155 |
+
"""
|
| 156 |
+
atm_straddle = yf_get_atm_straddle_price(ticker, dte)
|
| 157 |
+
underlying_price = yf_get_ticker_price(ticker)
|
| 158 |
+
em_upper = underlying_price + atm_straddle
|
| 159 |
+
em_lower = underlying_price - atm_straddle
|
| 160 |
+
|
| 161 |
+
return {
|
| 162 |
+
"underlying_price": underlying_price,
|
| 163 |
+
"em": atm_straddle,
|
| 164 |
+
"em_upper": em_upper,
|
| 165 |
+
"em_lower": em_lower,
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
ddgs = DuckDuckGoSearchTool();
|
| 169 |
|
| 170 |
final_answer = FinalAnswerTool()
|
|
|
|
| 188 |
|
| 189 |
agent = CodeAgent(
|
| 190 |
model=model,
|
| 191 |
+
tools=[final_answer, ddgs, yf_get_ticker_price, yf_get_atm_straddle_price, estimate_expected_move], ## add your tools here (don't remove final answer)
|
| 192 |
max_steps=6,
|
| 193 |
verbosity_level=1,
|
| 194 |
grammar=None,
|