Anupam251272 commited on
Commit
7c6dde2
β€’
1 Parent(s): f18dd22

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import yfinance as yf
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import mplfinance as mpf
5
+
6
+ def create_beginner_friendly_candlestick_chart(ticker, period="6mo", interval="1d"):
7
+ try:
8
+ # Fetch stock data
9
+ stock = yf.Ticker(ticker)
10
+ df = stock.history(period=period, interval=interval)
11
+
12
+ if df.empty:
13
+ return "No data found for the stock", None
14
+
15
+ # Ensure the index is a DatetimeIndex
16
+ df.index = pd.to_datetime(df.index)
17
+
18
+ # Prepare data for mplfinance
19
+ df_mpl = df[['Open', 'High', 'Low', 'Close', 'Volume']]
20
+
21
+ # Create a more straightforward mplfinance plot
22
+ style = mpf.make_mpf_style(base_mpf_style='yahoo', rc={'font.size':10})
23
+
24
+ # Save plot directly using mplfinance
25
+ plot_path = f'{ticker}_candlestick_chart.png'
26
+ mpf.plot(
27
+ df_mpl,
28
+ type='candle',
29
+ volume=True,
30
+ title=f'{ticker} Stock Price - Candlestick Chart',
31
+ style=style,
32
+ savefig=plot_path
33
+ )
34
+
35
+ # Generate a beginner-friendly explanation
36
+ explanation = f"""
37
+ πŸ•―οΈ Candlestick Chart Explained for {ticker}:
38
+
39
+ What is a Candlestick?
40
+ - Each 'candle' represents one day of trading
41
+ - Shows four key price points: Open, Close, High, Low
42
+
43
+ How to Read the Candles:
44
+ 🟒 Green Candle = Price Went Up
45
+ - Bottom of candle = Opening Price
46
+ - Top of candle = Closing Price
47
+
48
+ πŸ”΄ Red Candle = Price Went Down
49
+ - Top of candle = Opening Price
50
+ - Bottom of candle = Closing Price
51
+
52
+ Additional Insights:
53
+ - Candle 'Wicks' show the highest and lowest prices of the day
54
+ - Volume chart below shows how many shares were traded
55
+
56
+ Recent Performance:
57
+ - Latest Close Price: ${df['Close'][-1]:.2f}
58
+ - Price Change: ${df['Close'][-1] - df['Close'][-2]:.2f}
59
+ - Percentage Change: {((df['Close'][-1] / df['Close'][-2] - 1) * 100):.2f}%
60
+ """
61
+
62
+ return explanation, plot_path
63
+
64
+ except Exception as e:
65
+ return f"Error creating chart: {e}", None
66
+
67
+ # Gradio Interface for Candlestick Chart
68
+ import gradio as gr
69
+
70
+ def candlestick_interface(ticker, period, interval):
71
+ explanation, plot_path = create_beginner_friendly_candlestick_chart(ticker, period, interval)
72
+ return explanation, plot_path
73
+
74
+ # Create Gradio Interface
75
+ iface = gr.Interface(
76
+ fn=candlestick_interface,
77
+ inputs=[
78
+ gr.Textbox(label="Stock Ticker (e.g., AAPL, MSFT)", value="AAPL"),
79
+ gr.Textbox(label="Time Period (e.g., 6mo, 1y)", value="6mo"),
80
+ gr.Textbox(label="Data Interval (e.g., 1d, 1h)", value="1d")
81
+ ],
82
+ outputs=[
83
+ gr.Textbox(label="Candlestick Chart Explanation"),
84
+ gr.Image(label="Candlestick Chart")
85
+ ],
86
+ title="Candlestick Chart Explained",
87
+ description="Understand stock price movements with an easy-to-read candlestick chart"
88
+ )
89
+
90
+ # Launch the interface
91
+ iface.launch(share=True)