Spaces:
Running
Running
File size: 3,212 Bytes
c619ce9 f4a6f78 |
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
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()
|