Method314 commited on
Commit
9175035
·
verified ·
1 Parent(s): 6af68d9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import yfinance as yf
3
+ import numpy as np
4
+ import pandas as pd
5
+ import plotly.graph_objects as go
6
+ from datetime import datetime
7
+
8
+ def download_stock_data(ticker, start_date, end_date):
9
+ stock = yf.Ticker(ticker)
10
+ df = stock.history(start=start_date, end=end_date)
11
+ return df
12
+
13
+ def plot_interactive_logarithmic_stock_chart(ticker, start_date, end_date):
14
+ try:
15
+ stock = yf.Ticker(ticker)
16
+ data = stock.history(start=start_date, end=end_date)
17
+
18
+ if data.empty:
19
+ return "No data available for the specified date range."
20
+
21
+ x = (data.index - data.index[0]).days
22
+ y = np.log(data['Close'])
23
+ slope, intercept = np.polyfit(x, y, 1)
24
+
25
+ future_days = 365 * 10
26
+ all_days = np.arange(len(x) + future_days)
27
+ log_trend = np.exp(intercept + slope * all_days)
28
+
29
+ inner_upper_band = log_trend * 2
30
+ inner_lower_band = log_trend / 2
31
+ outer_upper_band = log_trend * 4
32
+ outer_lower_band = log_trend / 4
33
+
34
+ extended_dates = pd.date_range(start=data.index[0], periods=len(all_days), freq='D')
35
+
36
+ fig = go.Figure()
37
+
38
+ fig.add_trace(go.Scatter(x=data.index, y=data['Close'], mode='lines', name='Close Price', line=dict(color='blue')))
39
+ fig.add_trace(go.Scatter(x=extended_dates, y=log_trend, mode='lines', name='Log Trend', line=dict(color='red')))
40
+ fig.add_trace(go.Scatter(x=extended_dates, y=inner_upper_band, mode='lines', name='Inner Upper Band', line=dict(color='green')))
41
+ fig.add_trace(go.Scatter(x=extended_dates, y=inner_lower_band, mode='lines', name='Inner Lower Band', line=dict(color='green')))
42
+ fig.add_trace(go.Scatter(x=extended_dates, y=outer_upper_band, mode='lines', name='Outer Upper Band', line=dict(color='orange')))
43
+ fig.add_trace(go.Scatter(x=extended_dates, y=outer_lower_band, mode='lines', name='Outer Lower Band', line=dict(color='orange')))
44
+
45
+ fig.update_layout(
46
+ title=f'{ticker} Stock Price (Logarithmic Scale) with Extended Trend Lines and Outer Bands',
47
+ xaxis_title='Date',
48
+ yaxis_title='Price (Log Scale)',
49
+ yaxis_type="log",
50
+ height=800,
51
+ legend=dict(x=0.01, y=0.99, bgcolor='rgba(255, 255, 255, 0.8)'),
52
+ hovermode='x unified'
53
+ )
54
+
55
+ fig.update_xaxes(
56
+ rangeslider_visible=True,
57
+ rangeselector=dict(
58
+ buttons=list([
59
+ dict(count=1, label="1m", step="month", stepmode="backward"),
60
+ dict(count=6, label="6m", step="month", stepmode="backward"),
61
+ dict(count=1, label="YTD", step="year", stepmode="todate"),
62
+ dict(count=1, label="1y", step="year", stepmode="backward"),
63
+ dict(step="all")
64
+ ])
65
+ )
66
+ )
67
+
68
+ return fig
69
+ except Exception as e:
70
+ return f"An error occurred: {str(e)}"
71
+
72
+ # Get the current date
73
+ current_date = datetime.now().strftime("%Y-%m-%d")
74
+
75
+ # Custom CSS to make charts full-width and larger
76
+ custom_css = """
77
+ .container {max-width: 100% !important; padding: 0 !important;}
78
+ .plot-container {height: 800px !important; width: 100% !important;}
79
+ .react-plotly-container {height: 100% !important; width: 100% !important;}
80
+ """
81
+
82
+ # Create Gradio interface
83
+ with gr.Blocks(css=custom_css, title="Alan's Logarithmic Charting Tool") as iface:
84
+ gr.Markdown("# Alan's Logarithmic Charting Tool")
85
+ gr.Markdown("Enter a stock ticker and date range to generate a logarithmic chart.")
86
+
87
+ with gr.Row():
88
+ ticker = gr.Textbox(label="Stock Ticker", value="MSFT")
89
+ start_date = gr.Textbox(label="Start Date", value="2015-01-01")
90
+ end_date = gr.Textbox(label="End Date", value=current_date)
91
+
92
+ submit_button = gr.Button("Generate Chart")
93
+
94
+ with gr.Row():
95
+ log_plot = gr.Plot(label="Logarithmic Stock Chart")
96
+
97
+ submit_button.click(
98
+ plot_interactive_logarithmic_stock_chart,
99
+ inputs=[ticker, start_date, end_date],
100
+ outputs=[log_plot]
101
+ )
102
+
103
+ # Launch the app
104
+ iface.launch()