netflypsb commited on
Commit
8aa27c2
·
verified ·
1 Parent(s): cdfa9d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -18
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import streamlit as st
2
  import yfinance as yf
3
  import pandas as pd
4
- import matplotlib.pyplot as plt
5
 
6
  # Function to calculate Bollinger Bands
7
  def bollinger_bands(stock_price, window_size, num_of_std):
@@ -26,29 +26,34 @@ if st.sidebar.button('Analyze'):
26
  data = yf.download(ticker_symbol, start=start_date, end=end_date)
27
  data['Middle Band'], data['Upper Band'], data['Lower Band'] = bollinger_bands(data['Adj Close'], window_size, num_of_std)
28
 
29
- # Plotting
30
- fig, ax = plt.subplots()
31
- ax.plot(data.index, data['Adj Close'], label='Adjusted Close', color='blue')
32
- ax.plot(data.index, data['Middle Band'], label='Middle Band', linestyle='--', color='red')
33
- ax.plot(data.index, data['Upper Band'], label='Upper Band', linestyle='--', color='green')
34
- ax.plot(data.index, data['Lower Band'], label='Lower Band', linestyle='--', color='green')
35
 
 
 
 
 
 
 
36
  # Buy and sell signals
37
- buy_signal = (data['Adj Close'] < data['Lower Band'])
38
- sell_signal = (data['Adj Close'] > data['Upper Band'])
39
- ax.scatter(data.index[buy_signal], data['Adj Close'][buy_signal], color='gold', label='Buy Signal', marker='^', alpha=1)
40
- ax.scatter(data.index[sell_signal], data['Adj Close'][sell_signal], color='purple', label='Sell Signal', marker='v', alpha=1)
41
-
42
- plt.title('Bollinger Bands for {}'.format(ticker_symbol))
43
- plt.xlabel('Date')
44
- plt.ylabel('Price')
45
- plt.legend()
46
- st.pyplot(fig)
 
 
47
  else:
48
  st.title("Bollinger Bands Trading Strategy App")
49
  st.markdown("""
50
  This app retrieves stock data using the ticker symbol entered by the user and applies the Bollinger Bands trading strategy to visualize potential buy and sell points. Adjust the parameters using the sidebar and click "Analyze" to see the results.
51
- * **Python libraries:** yfinance, pandas, matplotlib
52
  * **Data source:** Yahoo Finance
 
53
  """)
54
 
 
1
  import streamlit as st
2
  import yfinance as yf
3
  import pandas as pd
4
+ import plotly.graph_objects as go
5
 
6
  # Function to calculate Bollinger Bands
7
  def bollinger_bands(stock_price, window_size, num_of_std):
 
26
  data = yf.download(ticker_symbol, start=start_date, end=end_date)
27
  data['Middle Band'], data['Upper Band'], data['Lower Band'] = bollinger_bands(data['Adj Close'], window_size, num_of_std)
28
 
29
+ # Plotting with Plotly
30
+ fig = go.Figure()
 
 
 
 
31
 
32
+ # Add traces for stock price and Bollinger Bands
33
+ fig.add_trace(go.Scatter(x=data.index, y=data['Adj Close'], mode='lines', name='Adjusted Close'))
34
+ fig.add_trace(go.Scatter(x=data.index, y=data['Upper Band'], fill=None, mode='lines', name='Upper Band', line=dict(color='green')))
35
+ fig.add_trace(go.Scatter(x=data.index, y=data['Middle Band'], fill='tonexty', mode='lines', name='Middle Band', line=dict(color='red')))
36
+ fig.add_trace(go.Scatter(x=data.index, y=data['Lower Band'], fill='tonexty', mode='lines', name='Lower Band', line=dict(color='green')))
37
+
38
  # Buy and sell signals
39
+ buys = data[data['Adj Close'] < data['Lower Band']]
40
+ sells = data[data['Adj Close'] > data['Upper Band']]
41
+ fig.add_trace(go.Scatter(x=buys.index, y=buys['Adj Close'], mode='markers', name='Buy Signal', marker=dict(color='gold', size=10, symbol='triangle-up')))
42
+ fig.add_trace(go.Scatter(x=sells.index, y=sells['Adj Close'], mode='markers', name='Sell Signal', marker=dict(color='purple', size=10, symbol='triangle-down')))
43
+
44
+ # Update layout for a more interactive look
45
+ fig.update_layout(title=f'Bollinger Bands for {ticker_symbol}',
46
+ xaxis_title='Date',
47
+ yaxis_title='Price',
48
+ hovermode='x')
49
+
50
+ st.plotly_chart(fig)
51
  else:
52
  st.title("Bollinger Bands Trading Strategy App")
53
  st.markdown("""
54
  This app retrieves stock data using the ticker symbol entered by the user and applies the Bollinger Bands trading strategy to visualize potential buy and sell points. Adjust the parameters using the sidebar and click "Analyze" to see the results.
55
+ * **Python libraries:** yfinance, pandas, plotly
56
  * **Data source:** Yahoo Finance
57
+ * **Tip:** Hover over the plot to see interactive data points.
58
  """)
59