riteshcp commited on
Commit
3374424
·
verified ·
1 Parent(s): 9d41fbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -35
app.py CHANGED
@@ -1,14 +1,15 @@
1
  import yfinance as yf
2
  import pandas as pd
3
  import numpy as np
4
- import matplotlib.pyplot as plt
5
- import seaborn as sns
6
  import streamlit as st
 
7
 
8
- # Step 1: Define a function to fetch real-time market data
9
  def fetch_data(ticker_symbol):
10
  ticker = yf.Ticker(ticker_symbol)
11
- data = ticker.history(period="1y") # Fetches 1 year of historical data
 
12
  return data
13
 
14
  # Step 2: Define a function to calculate indicators
@@ -63,49 +64,70 @@ nifty_data = calculate_indicators(nifty_data)
63
  st.subheader(f"Data Overview for {ticker_symbol}")
64
  st.write(nifty_data.head())
65
 
66
- # Step 6: Visualizations
67
- st.subheader(f"Close Price for {ticker_symbol}")
68
- fig, ax = plt.subplots(figsize=(12, 6))
69
- sns.lineplot(x=nifty_data.index, y=nifty_data['Close'])
70
- st.pyplot(fig)
71
-
72
- st.subheader(f"Moving Averages, EMA, and RSI for {ticker_symbol}")
73
- fig, ax = plt.subplots(figsize=(12, 6))
74
- sns.lineplot(x=nifty_data.index, y=nifty_data['MA20'], label='MA20')
75
- sns.lineplot(x=nifty_data.index, y=nifty_data['MA50'], label='MA50')
76
- sns.lineplot(x=nifty_data.index, y=nifty_data['RSI'], label='RSI')
77
- plt.axhline(y=70, color='red', linestyle='--', label='Overbought (70)')
78
- plt.axhline(y=30, color='green', linestyle='--', label='Oversold (30)')
79
- plt.legend()
80
- plt.title("Moving Averages and RSI")
81
- st.pyplot(fig)
82
-
83
- # Step 7: Buy and Sell Signals Visualization
84
  st.subheader(f"Buy and Sell Signals for {ticker_symbol}")
85
- fig, ax = plt.subplots(figsize=(12, 6))
86
- sns.lineplot(x=nifty_data.index, y=nifty_data['Close'], label='Close Price', color='blue')
87
- sns.lineplot(x=nifty_data.index, y=nifty_data['TrendSMA'], label='Trend SMA', color='gray')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
89
  # Plot Buy Signals
90
  buy_signals = nifty_data[nifty_data['BuySignal']]
91
- plt.scatter(buy_signals.index, buy_signals['Close'], marker='^', color='green', label='Buy Signal', s=100)
 
 
 
 
 
 
92
 
93
  # Plot Sell Signals
94
  sell_signals = nifty_data[nifty_data['SellSignal']]
95
- plt.scatter(sell_signals.index, sell_signals['Close'], marker='v', color='red', label='Sell Signal', s=100)
96
-
97
- plt.legend()
98
- plt.title("Buy and Sell Signals with Trend Filter")
99
- st.pyplot(fig)
100
-
101
- # Step 8: Interactive Timeframe Selection and Alerts
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  st.sidebar.subheader("Select Timeframe:")
103
- timeframes = ['15m', '60m', '1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd', 'max']
104
  selected_timeframe = st.sidebar.selectbox('Timeframe', timeframes)
105
 
106
  st.sidebar.subheader("Set Alerts:")
107
  alert_type = st.sidebar.selectbox('Alert Type', ['Price', 'RSI'])
108
  alert_value = st.sidebar.number_input('Enter Alert Value')
109
 
110
- # Step 9: Run the Streamlit App (Note: Do not use st.run())
111
  # Save as `app.py` and run it using `streamlit run app.py`
 
1
  import yfinance as yf
2
  import pandas as pd
3
  import numpy as np
4
+ import plotly.graph_objects as go
 
5
  import streamlit as st
6
+ from datetime import datetime
7
 
8
+ # Step 1: Define a function to fetch real-time market data up to the current date
9
  def fetch_data(ticker_symbol):
10
  ticker = yf.Ticker(ticker_symbol)
11
+ end_date = datetime.now().strftime('%Y-%m-%d') # Get the current date
12
+ data = ticker.history(start="2023-01-01", end=end_date) # Fetches data from the start of this year to the current date
13
  return data
14
 
15
  # Step 2: Define a function to calculate indicators
 
64
  st.subheader(f"Data Overview for {ticker_symbol}")
65
  st.write(nifty_data.head())
66
 
67
+ # Step 6: Visualizations using Plotly for Interactivity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  st.subheader(f"Buy and Sell Signals for {ticker_symbol}")
69
+
70
+ # Create a Plotly figure
71
+ fig = go.Figure()
72
+
73
+ # Add Close Price line
74
+ fig.add_trace(go.Scatter(
75
+ x=nifty_data.index,
76
+ y=nifty_data['Close'],
77
+ mode='lines',
78
+ name='Close Price',
79
+ line=dict(color='blue')
80
+ ))
81
+
82
+ # Add Trend SMA line
83
+ fig.add_trace(go.Scatter(
84
+ x=nifty_data.index,
85
+ y=nifty_data['TrendSMA'],
86
+ mode='lines',
87
+ name='Trend SMA',
88
+ line=dict(color='gray')
89
+ ))
90
 
91
  # Plot Buy Signals
92
  buy_signals = nifty_data[nifty_data['BuySignal']]
93
+ fig.add_trace(go.Scatter(
94
+ x=buy_signals.index,
95
+ y=buy_signals['Close'],
96
+ mode='markers',
97
+ name='Buy Signal',
98
+ marker=dict(symbol='triangle-up', color='green', size=10)
99
+ ))
100
 
101
  # Plot Sell Signals
102
  sell_signals = nifty_data[nifty_data['SellSignal']]
103
+ fig.add_trace(go.Scatter(
104
+ x=sell_signals.index,
105
+ y=sell_signals['Close'],
106
+ mode='markers',
107
+ name='Sell Signal',
108
+ marker=dict(symbol='triangle-down', color='red', size=10)
109
+ ))
110
+
111
+ # Update layout for better readability
112
+ fig.update_layout(
113
+ title=f"Buy and Sell Signals with Trend Filter for {ticker_symbol}",
114
+ xaxis_title="Date",
115
+ yaxis_title="Close Price",
116
+ legend_title="Legend",
117
+ template="plotly_dark"
118
+ )
119
+
120
+ # Show Plotly figure in Streamlit
121
+ st.plotly_chart(fig)
122
+
123
+ # Step 7: Interactive Timeframe Selection and Alerts
124
  st.sidebar.subheader("Select Timeframe:")
125
+ timeframes = ['1d', '5d', '1mo', '3mo', '6mo', '1y', '2y', '5y', '10y', 'ytd', 'max']
126
  selected_timeframe = st.sidebar.selectbox('Timeframe', timeframes)
127
 
128
  st.sidebar.subheader("Set Alerts:")
129
  alert_type = st.sidebar.selectbox('Alert Type', ['Price', 'RSI'])
130
  alert_value = st.sidebar.number_input('Enter Alert Value')
131
 
132
+ # Step 8: Run the Streamlit App (Note: Do not use st.run())
133
  # Save as `app.py` and run it using `streamlit run app.py`