Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 moving averages
|
| 7 |
+
def calculate_moving_average(data, window_size):
|
| 8 |
+
return data['Close'].rolling(window=window_size).mean()
|
| 9 |
+
|
| 10 |
+
# Function to detect support and resistance levels
|
| 11 |
+
def detect_support_resistance(data):
|
| 12 |
+
# This is a simple placeholder for actual support and resistance logic
|
| 13 |
+
return data['Close'].rolling(window=20).min(), data['Close'].rolling(window=20).max()
|
| 14 |
+
|
| 15 |
+
# VSA signals logic placeholder
|
| 16 |
+
def vsa_signals(data):
|
| 17 |
+
# This should include real VSA calculation logic based on volume and spread
|
| 18 |
+
buy_signals = pd.Series(index=data.index, dtype='float64')
|
| 19 |
+
sell_signals = pd.Series(index=data.index, dtype='float64')
|
| 20 |
+
# Dummy logic for demonstration:
|
| 21 |
+
buy_signals[data['Volume'] > data['Volume'].rolling(20).mean()] = data['Low']
|
| 22 |
+
sell_signals[data['Volume'] < data['Volume'].rolling(20).mean()] = data['High']
|
| 23 |
+
return buy_signals, sell_signals
|
| 24 |
+
|
| 25 |
+
# Streamlit sidebar options
|
| 26 |
+
ticker = st.sidebar.text_input('Ticker Symbol', value='AAPL')
|
| 27 |
+
start_date = st.sidebar.date_input('Start Date', pd.to_datetime('2020-01-01'))
|
| 28 |
+
end_date = st.sidebar.date_input('End Date', pd.to_datetime('2020-12-31'))
|
| 29 |
+
analyze_button = st.sidebar.button('Analyze')
|
| 30 |
+
|
| 31 |
+
if analyze_button:
|
| 32 |
+
data = yf.download(ticker, start=start_date, end=end_date)
|
| 33 |
+
if not data.empty:
|
| 34 |
+
# Calculations
|
| 35 |
+
moving_average = calculate_moving_average(data, window_size=20)
|
| 36 |
+
support, resistance = detect_support_resistance(data)
|
| 37 |
+
buy_signals, sell_signals = vsa_signals(data)
|
| 38 |
+
|
| 39 |
+
# Plotting
|
| 40 |
+
fig = go.Figure()
|
| 41 |
+
|
| 42 |
+
# Add candlestick chart
|
| 43 |
+
fig.add_trace(go.Candlestick(x=data.index,
|
| 44 |
+
open=data['Open'],
|
| 45 |
+
high=data['High'],
|
| 46 |
+
low=data['Low'],
|
| 47 |
+
close=data['Close'], name='Market Data'))
|
| 48 |
+
|
| 49 |
+
# Add Moving Average Line
|
| 50 |
+
fig.add_trace(go.Scatter(x=data.index, y=moving_average, mode='lines', name='20-day MA'))
|
| 51 |
+
|
| 52 |
+
# Add Support and Resistance Lines
|
| 53 |
+
fig.add_trace(go.Scatter(x=data.index, y=support, mode='lines', name='Support', line=dict(color='green')))
|
| 54 |
+
fig.add_trace(go.Scatter(x=data.index, y=resistance, mode='lines', name='Resistance', line=dict(color='red')))
|
| 55 |
+
|
| 56 |
+
# Add Buy and Sell Signals
|
| 57 |
+
fig.add_trace(go.Scatter(x=buy_signals.index, y=buy_signals, mode='markers', marker=dict(color='blue', size=10), name='Buy Signal'))
|
| 58 |
+
fig.add_trace(go.Scatter(x=sell_signals.index, y=sell_signals, mode='markers', marker=dict(color='orange', size=10), name='Sell Signal'))
|
| 59 |
+
|
| 60 |
+
# Layout settings
|
| 61 |
+
fig.update_layout(title='VSA Trading Strategy Analysis', xaxis_title='Date', yaxis_title='Price', template='plotly_dark')
|
| 62 |
+
|
| 63 |
+
# Display the figure
|
| 64 |
+
st.plotly_chart(fig)
|
| 65 |
+
|
| 66 |
+
# App introduction and guide
|
| 67 |
+
st.title('VSA Trading Strategy Visualizer')
|
| 68 |
+
st.markdown('''
|
| 69 |
+
This app provides an interactive way to visualize the Volume Spread Analysis (VSA) trading strategy with buy and sell signals based on the strategy.
|
| 70 |
+
To start, enter the ticker symbol, select the start and end dates, and then click the "Analyze" button.
|
| 71 |
+
The chart below will display the price action with overlays for moving averages, support and resistance levels, and buy/sell signals based on VSA analysis.
|
| 72 |
+
''')
|
| 73 |
+
else:
|
| 74 |
+
st.error('No data found for the selected ticker and date range.')
|
| 75 |
+
|