Spaces:
Sleeping
Sleeping
import gradio as gr | |
import yfinance as yf | |
import pandas as pd | |
import plotly.graph_objects as go | |
import warnings | |
warnings.filterwarnings('ignore') | |
# Function to download stock data | |
def download_stock_data(ticker): | |
stock_data = yf.Ticker(ticker) | |
df = stock_data.history(period="1y")[['Close']] | |
return df | |
# Function to calculate Bollinger Bands | |
def calculate_bollinger_bands(df, window, no_of_stds): | |
rolling_mean = df['Close'].rolling(window=window).mean() | |
rolling_std = df['Close'].rolling(window=window).std() | |
df['Bollinger High'] = rolling_mean + (rolling_std * no_of_stds) | |
df['Bollinger Low'] = rolling_mean - (rolling_std * no_of_stds) | |
return df | |
# Function for anomaly detection using Bollinger Bands | |
def detect_anomalies(df): | |
anomalies = (df['Close'] > df['Bollinger High']) | (df['Close'] < df['Bollinger Low']) | |
return anomalies | |
# Function to create the plot | |
def plot_stock(ticker, window, no_of_stds): | |
df = download_stock_data(ticker) | |
df_with_bands = calculate_bollinger_bands(df, window, no_of_stds) | |
anomalies = detect_anomalies(df_with_bands) | |
df_with_bands['Anomaly'] = anomalies | |
fig = go.Figure() | |
fig.add_trace(go.Scatter(x=df_with_bands.index, y=df_with_bands['Close'], name='Close Price', mode='lines')) | |
fig.add_trace(go.Scatter(x=df_with_bands.index, y=df_with_bands['Bollinger High'], name='Bollinger High', line=dict(width=1))) | |
fig.add_trace(go.Scatter(x=df_with_bands.index, y=df_with_bands['Bollinger Low'], name='Bollinger Low', line=dict(width=1))) | |
fig.add_trace(go.Scatter(x=df_with_bands.index, y=df_with_bands['Close'].where(df_with_bands['Anomaly']), mode='markers', name='Anomaly', marker=dict(color='red', size=10))) | |
fig.update_layout(title=f'Bollinger Bands and Anomalies for {ticker}', xaxis_title='Date', yaxis_title='Price', height=600) | |
return fig | |
"""# Gradio interface for version 4.5.0 | |
iface = gr.Interface( | |
fn=plot_stock, | |
inputs=[ | |
gr.Dropdown(['RELIANCE.NS', 'TCS.NS', 'HDFCBANK.NS', 'INFY.NS', 'MARUTI.NS'], label="Select Stock Ticker"), | |
gr.Slider(minimum=5, maximum=100, value=20, label="Window Size for SMA"), | |
gr.Slider(minimum=1, maximum=3, value=2, label="Number of Standard Deviations") | |
], | |
outputs="plot", | |
title="Stock Anomaly Detection with Bollinger Bands", | |
description="Select a stock ticker and adjust the window size and standard deviations to view Bollinger Bands and anomalies." | |
) | |
if __name__ == "__main__": | |
iface.launch()""" | |
# Correct the indentation of line 18 | |
iface = gr.Interface( | |
fn=plot_stock, | |
inputs=[ | |
gr.Dropdown(['RELIANCE.NS', 'TCS.NS', 'HDFCBANK.NS', 'INFY.NS', 'MARUTI.NS'], label="Select Stock Ticker"), | |
gr.Slider(minimum=5, maximum=100, value=20, label="Window Size for SMA"), | |
gr.Slider(minimum=1, maximum=3, value=2, label="Number of Standard Deviations") | |
], | |
outputs="plot", | |
title="Stock Anomaly Detection with Bollinger Bands", | |
description="Select a stock ticker and adjust the window size and standard deviations to view Bollinger Bands and anomalies.", | |
theme=gr.themes.Soft() # Apply the Soft theme | |
) | |
if __name__ == "__main__": | |
iface.launch() | |