Spaces:
Build error
Build error
File size: 1,535 Bytes
dffd7bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import gradio as gr
import yfinance as yf
# --- Define your logic ---
def analyze_stock(ticker):
"""
Fetch current and previous data for the given stock symbol,
check price change, and give BUY or SELL recommendation.
"""
try:
stock = yf.Ticker(ticker)
data = stock.history(period="5d")
if data.empty:
return f"β οΈ No data found for '{ticker}'"
# Get current and previous close
current_price = data['Close'][-1]
previous_price = data['Close'][-2]
# Calculate % change
change_percent = ((current_price - previous_price) / previous_price) * 100
# --- Decision logic ---
recommendation = "π HOLD"
if 10 <= change_percent <= 11:
recommendation = "π’ BUY SIGNAL"
elif change_percent <= -14:
recommendation = "π΄ SELL SIGNAL"
return f"""
π **{ticker.upper()} Stock Analysis**
- Current Price: {current_price:.2f} USD
- Previous Close: {previous_price:.2f} USD
- Change: {change_percent:.2f}%
**AI Suggestion:** {recommendation}
"""
except Exception as e:
return f"β Error: {str(e)}"
# --- Gradio UI ---
iface = gr.Interface(
fn=analyze_stock,
inputs=gr.Textbox(label="Enter Stock Symbol (e.g., AAPL, TSLA, INFY.NS)"),
outputs=gr.Markdown(label="Result"),
title="π€ Smart AI Bull",
description="An AI-powered stock signal tracker that shows when to BUY or SELL based on % gain/loss.",
)
iface.launch()
|