Update app.py
Browse files
app.py
CHANGED
|
@@ -71,6 +71,9 @@ def plot_interactive_logarithmic_stock_chart(ticker, start_date, end_date):
|
|
| 71 |
if data.empty:
|
| 72 |
return "No data available for the specified date range."
|
| 73 |
|
|
|
|
|
|
|
|
|
|
| 74 |
x = (data.index - data.index[0]).days
|
| 75 |
y = np.log(data['Close'])
|
| 76 |
slope, intercept = np.polyfit(x, y, 1)
|
|
@@ -79,10 +82,10 @@ def plot_interactive_logarithmic_stock_chart(ticker, start_date, end_date):
|
|
| 79 |
all_days = np.arange(len(x) + future_days)
|
| 80 |
log_trend = np.exp(intercept + slope * all_days)
|
| 81 |
|
| 82 |
-
inner_upper_band = log_trend *
|
| 83 |
-
inner_lower_band = log_trend
|
| 84 |
-
outer_upper_band = log_trend *
|
| 85 |
-
outer_lower_band = log_trend
|
| 86 |
|
| 87 |
extended_dates = pd.date_range(start=data.index[0], periods=len(all_days), freq='D')
|
| 88 |
|
|
@@ -90,10 +93,10 @@ def plot_interactive_logarithmic_stock_chart(ticker, start_date, end_date):
|
|
| 90 |
|
| 91 |
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price', line=dict(color='blue')))
|
| 92 |
fig.add_trace(go.Scatter(x=extended_dates, y=log_trend, mode='lines', name='Log Trend', line=dict(color='red')))
|
| 93 |
-
fig.add_trace(go.Scatter(x=extended_dates, y=inner_upper_band, mode='lines', name='
|
| 94 |
-
fig.add_trace(go.Scatter(x=extended_dates, y=inner_lower_band, mode='lines', name='
|
| 95 |
-
fig.add_trace(go.Scatter(x=extended_dates, y=outer_upper_band, mode='lines', name='
|
| 96 |
-
fig.add_trace(go.Scatter(x=extended_dates, y=outer_lower_band, mode='lines', name='
|
| 97 |
|
| 98 |
fig.update_layout(
|
| 99 |
title={
|
|
@@ -134,9 +137,9 @@ def plot_interactive_logarithmic_stock_chart(ticker, start_date, end_date):
|
|
| 134 |
# Get the current date
|
| 135 |
current_date = datetime.now().strftime("%Y-%m-%d")
|
| 136 |
|
| 137 |
-
# Custom CSS for button hover
|
| 138 |
custom_css = """
|
| 139 |
-
#generate-button:hover {
|
| 140 |
background-color: #FFB3BA !important; /* Pastel red */
|
| 141 |
}
|
| 142 |
"""
|
|
@@ -147,17 +150,15 @@ with gr.Blocks(theme=seafoam, title="Stock Log Charts", css=custom_css) as iface
|
|
| 147 |
gr.Markdown("Enter a stock ticker and date range to generate a logarithmic chart.")
|
| 148 |
|
| 149 |
with gr.Row():
|
| 150 |
-
ticker = gr.Textbox(label="Stock Ticker", value="MSFT")
|
| 151 |
-
start_date = gr.Textbox(label="Start Date", value="2015-01-01")
|
| 152 |
-
end_date = gr.Textbox(label="End Date", value=current_date)
|
| 153 |
|
| 154 |
submit_button = gr.Button("Generate Chart", elem_id="generate-button")
|
| 155 |
|
| 156 |
with gr.Row():
|
| 157 |
log_plot = gr.Plot(label="Logarithmic Stock Chart")
|
| 158 |
|
| 159 |
-
share_button = gr.Button("Share Chart")
|
| 160 |
-
|
| 161 |
submit_button.click(
|
| 162 |
plot_interactive_logarithmic_stock_chart,
|
| 163 |
inputs=[ticker, start_date, end_date],
|
|
|
|
| 71 |
if data.empty:
|
| 72 |
return "No data available for the specified date range."
|
| 73 |
|
| 74 |
+
log_returns = np.log(data['Close'] / data['Close'].shift(1))
|
| 75 |
+
log_returns_std = log_returns.std()
|
| 76 |
+
|
| 77 |
x = (data.index - data.index[0]).days
|
| 78 |
y = np.log(data['Close'])
|
| 79 |
slope, intercept = np.polyfit(x, y, 1)
|
|
|
|
| 82 |
all_days = np.arange(len(x) + future_days)
|
| 83 |
log_trend = np.exp(intercept + slope * all_days)
|
| 84 |
|
| 85 |
+
inner_upper_band = log_trend * np.exp(log_returns_std)
|
| 86 |
+
inner_lower_band = log_trend * np.exp(-log_returns_std)
|
| 87 |
+
outer_upper_band = log_trend * np.exp(2 * log_returns_std)
|
| 88 |
+
outer_lower_band = log_trend * np.exp(-2 * log_returns_std)
|
| 89 |
|
| 90 |
extended_dates = pd.date_range(start=data.index[0], periods=len(all_days), freq='D')
|
| 91 |
|
|
|
|
| 93 |
|
| 94 |
fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price', line=dict(color='blue')))
|
| 95 |
fig.add_trace(go.Scatter(x=extended_dates, y=log_trend, mode='lines', name='Log Trend', line=dict(color='red')))
|
| 96 |
+
fig.add_trace(go.Scatter(x=extended_dates, y=inner_upper_band, mode='lines', name='1 SD Upper Band', line=dict(color='#6FB1A7')))
|
| 97 |
+
fig.add_trace(go.Scatter(x=extended_dates, y=inner_lower_band, mode='lines', name='1 SD Lower Band', line=dict(color='#6FB1A7')))
|
| 98 |
+
fig.add_trace(go.Scatter(x=extended_dates, y=outer_upper_band, mode='lines', name='2 SD Upper Band', line=dict(color='#FFC2A5')))
|
| 99 |
+
fig.add_trace(go.Scatter(x=extended_dates, y=outer_lower_band, mode='lines', name='2 SD Lower Band', line=dict(color='#FFC2A5')))
|
| 100 |
|
| 101 |
fig.update_layout(
|
| 102 |
title={
|
|
|
|
| 137 |
# Get the current date
|
| 138 |
current_date = datetime.now().strftime("%Y-%m-%d")
|
| 139 |
|
| 140 |
+
# Custom CSS for button and input hover effects
|
| 141 |
custom_css = """
|
| 142 |
+
#generate-button:hover, #ticker-input:hover, #start-date-input:hover, #end-date-input:hover {
|
| 143 |
background-color: #FFB3BA !important; /* Pastel red */
|
| 144 |
}
|
| 145 |
"""
|
|
|
|
| 150 |
gr.Markdown("Enter a stock ticker and date range to generate a logarithmic chart.")
|
| 151 |
|
| 152 |
with gr.Row():
|
| 153 |
+
ticker = gr.Textbox(label="Stock Ticker", value="MSFT", elem_id="ticker-input")
|
| 154 |
+
start_date = gr.Textbox(label="Start Date", value="2015-01-01", elem_id="start-date-input")
|
| 155 |
+
end_date = gr.Textbox(label="End Date", value=current_date, elem_id="end-date-input")
|
| 156 |
|
| 157 |
submit_button = gr.Button("Generate Chart", elem_id="generate-button")
|
| 158 |
|
| 159 |
with gr.Row():
|
| 160 |
log_plot = gr.Plot(label="Logarithmic Stock Chart")
|
| 161 |
|
|
|
|
|
|
|
| 162 |
submit_button.click(
|
| 163 |
plot_interactive_logarithmic_stock_chart,
|
| 164 |
inputs=[ticker, start_date, end_date],
|