netflypsb commited on
Commit
cdfa9d4
·
verified ·
1 Parent(s): 20ca0bd

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
8
+ rolling_mean = stock_price.rolling(window=window_size).mean()
9
+ rolling_std = stock_price.rolling(window=window_size).std()
10
+ upper_band = rolling_mean + (rolling_std * num_of_std)
11
+ lower_band = rolling_mean - (rolling_std * num_of_std)
12
+ return rolling_mean, upper_band, lower_band
13
+
14
+ # Streamlit UI
15
+ st.sidebar.header('User Input Parameters')
16
+
17
+ # Getting user input from the sidebar
18
+ ticker_symbol = st.sidebar.text_input('Ticker Symbol', 'AAPL')
19
+ window_size = st.sidebar.slider('Window Size for Moving Average', 5, 100, 20)
20
+ num_of_std = st.sidebar.slider('Number of Standard Deviations', 0.5, 3.0, 2.0)
21
+ start_date = st.sidebar.date_input('Start Date', pd.to_datetime('2020-01-01'))
22
+ end_date = st.sidebar.date_input('End Date', pd.to_datetime('today'))
23
+
24
+ if st.sidebar.button('Analyze'):
25
+ # Fetch data from Yahoo Finance
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
+