Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import yfinance as yf
|
3 |
+
import plotly.graph_objects as go
|
4 |
+
|
5 |
+
def fetch_data(ticker, start_date, end_date):
|
6 |
+
data = yf.download(ticker, start=start_date, end=end_date, interval='1m')
|
7 |
+
data['MA Fast'] = data['Close'].rolling(window=5).mean()
|
8 |
+
data['MA Slow'] = data['Close'].rolling(window=10).mean()
|
9 |
+
data['Upper Band'] = data['Close'].rolling(window=20).mean() + 2*data['Close'].rolling(20).std()
|
10 |
+
data['Lower Band'] = data['Close'].rolling(window=20).mean() - 2*data['Close'].rolling(20).std()
|
11 |
+
return data
|
12 |
+
|
13 |
+
def plot_data(data):
|
14 |
+
fig = go.Figure()
|
15 |
+
fig.add_trace(go.Candlestick(x=data.index,
|
16 |
+
open=data['Open'], high=data['High'],
|
17 |
+
low=data['Low'], close=data['Close'],
|
18 |
+
name='Candlesticks'))
|
19 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['MA Fast'], line=dict(color='blue', width=1.5), name='MA Fast'))
|
20 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['MA Slow'], line=dict(color='red', width=1.5), name='MA Slow'))
|
21 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['Upper Band'], line=dict(color='green', width=1), name='Upper Band'))
|
22 |
+
fig.add_trace(go.Scatter(x=data.index, y=data['Lower Band'], line=dict(color='green', width=1), name='Lower Band'))
|
23 |
+
|
24 |
+
# Buy and sell signals based on BBMA logic
|
25 |
+
buys = data[(data['Close'] > data['Lower Band']) & (data['Close'] < data['MA Slow'])]
|
26 |
+
sells = data[(data['Close'] < data['Upper Band']) & (data['Close'] > data['MA Fast'])]
|
27 |
+
|
28 |
+
fig.add_trace(go.Scatter(x=buys.index, y=buys['Close'], mode='markers', marker=dict(color='yellow', size=10), name='Buy Signal'))
|
29 |
+
fig.add_trace(go.Scatter(x=sells.index, y=sells['Close'], mode='markers', marker=dict(color='purple', size=10), name='Sell Signal'))
|
30 |
+
|
31 |
+
return fig
|
32 |
+
|
33 |
+
# Streamlit user interface
|
34 |
+
st.title("BBMA Scalping Strategy Visualizer")
|
35 |
+
st.markdown("""
|
36 |
+
This application visualizes the BBMA Scalping Strategy for selected stocks.
|
37 |
+
Enter the stock ticker, choose a start and end date, and press 'Analyze' to view the strategy's buy and sell signals overlaid on the price chart.
|
38 |
+
""")
|
39 |
+
|
40 |
+
st.sidebar.header('Input Parameters')
|
41 |
+
ticker = st.sidebar.text_input('Enter ticker symbol', value='AAPL')
|
42 |
+
start_date = st.sidebar.date_input('Start Date', value=pd.to_datetime('2021-01-01'))
|
43 |
+
end_date = st.sidebar.date_input('End Date', value=pd.to_datetime('today'))
|
44 |
+
|
45 |
+
button = st.sidebar.button('Analyze')
|
46 |
+
|
47 |
+
if button:
|
48 |
+
data = fetch_data(ticker, start_date, end_date)
|
49 |
+
fig = plot_data(data)
|
50 |
+
st.plotly_chart(fig, use_container_width=True)
|
51 |
+
|