File size: 3,085 Bytes
cdfa9d4
 
 
8aa27c2
cdfa9d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8aa27c2
 
cdfa9d4
8aa27c2
 
 
 
 
 
cdfa9d4
8aa27c2
 
 
 
 
 
 
 
 
 
 
 
cdfa9d4
 
 
 
8aa27c2
cdfa9d4
8aa27c2
cdfa9d4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import streamlit as st
import yfinance as yf
import pandas as pd
import plotly.graph_objects as go

# Function to calculate Bollinger Bands
def bollinger_bands(stock_price, window_size, num_of_std):
    rolling_mean = stock_price.rolling(window=window_size).mean()
    rolling_std = stock_price.rolling(window=window_size).std()
    upper_band = rolling_mean + (rolling_std * num_of_std)
    lower_band = rolling_mean - (rolling_std * num_of_std)
    return rolling_mean, upper_band, lower_band

# Streamlit UI
st.sidebar.header('User Input Parameters')

# Getting user input from the sidebar
ticker_symbol = st.sidebar.text_input('Ticker Symbol', 'AAPL')
window_size = st.sidebar.slider('Window Size for Moving Average', 5, 100, 20)
num_of_std = st.sidebar.slider('Number of Standard Deviations', 0.5, 3.0, 2.0)
start_date = st.sidebar.date_input('Start Date', pd.to_datetime('2020-01-01'))
end_date = st.sidebar.date_input('End Date', pd.to_datetime('today'))

if st.sidebar.button('Analyze'):
    # Fetch data from Yahoo Finance
    data = yf.download(ticker_symbol, start=start_date, end=end_date)
    data['Middle Band'], data['Upper Band'], data['Lower Band'] = bollinger_bands(data['Adj Close'], window_size, num_of_std)

    # Plotting with Plotly
    fig = go.Figure()
    
    # Add traces for stock price and Bollinger Bands
    fig.add_trace(go.Scatter(x=data.index, y=data['Adj Close'], mode='lines', name='Adjusted Close'))
    fig.add_trace(go.Scatter(x=data.index, y=data['Upper Band'], fill=None, mode='lines', name='Upper Band', line=dict(color='green')))
    fig.add_trace(go.Scatter(x=data.index, y=data['Middle Band'], fill='tonexty', mode='lines', name='Middle Band', line=dict(color='red')))
    fig.add_trace(go.Scatter(x=data.index, y=data['Lower Band'], fill='tonexty', mode='lines', name='Lower Band', line=dict(color='green')))

    # Buy and sell signals
    buys = data[data['Adj Close'] < data['Lower Band']]
    sells = data[data['Adj Close'] > data['Upper Band']]
    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')))
    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')))

    # Update layout for a more interactive look
    fig.update_layout(title=f'Bollinger Bands for {ticker_symbol}',
                      xaxis_title='Date',
                      yaxis_title='Price',
                      hovermode='x')

    st.plotly_chart(fig)
else:
    st.title("Bollinger Bands Trading Strategy App")
    st.markdown("""
    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.
    * **Python libraries:** yfinance, pandas, plotly
    * **Data source:** Yahoo Finance
    * **Tip:** Hover over the plot to see interactive data points.
    """)