File size: 4,077 Bytes
9175035 cbabd42 9175035 cbabd42 df42e91 cbabd42 5d5dab0 cbabd42 5d5dab0 df42e91 b0f9ac3 cbabd42 b0f9ac3 cbabd42 b0f9ac3 9175035 df42e91 cbabd42 9175035 cbabd42 9175035 cbabd42 9175035 cbabd42 9175035 |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
import gradio as gr
import yfinance as yf
import numpy as np
import pandas as pd
import plotly.graph_objects as go
from datetime import datetime
def download_stock_data(ticker, start_date, end_date):
stock = yf.Ticker(ticker)
df = stock.history(start=start_date, end=end_date)
return df
def plot_interactive_logarithmic_stock_chart(ticker, start_date, end_date):
try:
stock = yf.Ticker(ticker)
data = stock.history(start=start_date, end=end_date)
if data.empty:
return "No data available for the specified date range."
x = (data.index - data.index[0]).days
y = np.log(data['Close'])
slope, intercept = np.polyfit(x, y, 1)
future_days = 365 * 10
all_days = np.arange(len(x) + future_days)
log_trend = np.exp(intercept + slope * all_days)
inner_upper_band = log_trend * 2
inner_lower_band = log_trend / 2
outer_upper_band = log_trend * 4
outer_lower_band = log_trend / 4
extended_dates = pd.date_range(start=data.index[0], periods=len(all_days), freq='D')
fig = go.Figure()
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price', line=dict(color='#3A9184')))
fig.add_trace(go.Scatter(x=extended_dates, y=log_trend, mode='lines', name='Log Trend', line=dict(color='#FF8C61')))
fig.add_trace(go.Scatter(x=extended_dates, y=inner_upper_band, mode='lines', name='Inner Upper Band', line=dict(color='#6FB1A7')))
fig.add_trace(go.Scatter(x=extended_dates, y=inner_lower_band, mode='lines', name='Inner Lower Band', line=dict(color='#6FB1A7')))
fig.add_trace(go.Scatter(x=extended_dates, y=outer_upper_band, mode='lines', name='Outer Upper Band', line=dict(color='#FFC2A5')))
fig.add_trace(go.Scatter(x=extended_dates, y=outer_lower_band, mode='lines', name='Outer Lower Band', line=dict(color='#FFC2A5')))
fig.update_layout(
title={
'text': 'Stock Log Charts',
'y': 0.95,
'x': 0.5,
'xanchor': 'center',
'yanchor': 'top'
},
xaxis_title='Date',
yaxis_title='Price (Log Scale)',
yaxis_type="log",
height=800,
legend=dict(x=0.01, y=0.99, bgcolor='rgba(255, 255, 255, 0.8)'),
hovermode='x unified',
plot_bgcolor='#F5F9F8',
paper_bgcolor='#F5F9F8',
font=dict(family="Inter, sans-serif", size=12, color="#313D38")
)
fig.update_xaxes(
rangeslider_visible=True,
rangeselector=dict(
buttons=list([
dict(count=1, label="1m", step="month", stepmode="backward"),
dict(count=6, label="6m", step="month", stepmode="backward"),
dict(count=1, label="YTD", step="year", stepmode="todate"),
dict(count=1, label="1y", step="year", stepmode="backward"),
dict(step="all")
])
)
)
return fig
except Exception as e:
return f"An error occurred: {str(e)}"
# Get the current date
current_date = datetime.now().strftime("%Y-%m-%d")
# Create Gradio interface with seafoam theme
with gr.Blocks(theme=gr.themes.Seafoam(), title="Stock Log Charts") as iface:
gr.Markdown("# Stock Log Charts")
gr.Markdown("Enter a stock ticker and date range to generate a logarithmic chart.")
with gr.Row():
ticker = gr.Textbox(label="Stock Ticker", value="MSFT")
start_date = gr.Textbox(label="Start Date", value="2015-01-01")
end_date = gr.Textbox(label="End Date", value=current_date)
submit_button = gr.Button("Generate Chart")
with gr.Row():
log_plot = gr.Plot(label="Logarithmic Stock Chart")
submit_button.click(
plot_interactive_logarithmic_stock_chart,
inputs=[ticker, start_date, end_date],
outputs=[log_plot]
)
# Launch the app
iface.launch() |