|
import streamlit as st |
|
import yfinance as yf |
|
import pandas as pd |
|
import plotly.graph_objects as go |
|
|
|
|
|
def calculate_moving_average(data, window_size): |
|
return data['Close'].rolling(window=window_size).mean() |
|
|
|
|
|
def detect_support_resistance(data): |
|
|
|
return data['Close'].rolling(window=20).min(), data['Close'].rolling(window=20).max() |
|
|
|
|
|
def vsa_signals(data): |
|
|
|
buy_signals = pd.Series(index=data.index, dtype='float64') |
|
sell_signals = pd.Series(index=data.index, dtype='float64') |
|
|
|
buy_signals[data['Volume'] > data['Volume'].rolling(20).mean()] = data['Low'] |
|
sell_signals[data['Volume'] < data['Volume'].rolling(20).mean()] = data['High'] |
|
return buy_signals, sell_signals |
|
|
|
|
|
ticker = st.sidebar.text_input('Ticker Symbol', value='AAPL') |
|
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('2020-12-31')) |
|
analyze_button = st.sidebar.button('Analyze') |
|
|
|
if analyze_button: |
|
data = yf.download(ticker, start=start_date, end=end_date) |
|
if not data.empty: |
|
|
|
moving_average = calculate_moving_average(data, window_size=20) |
|
support, resistance = detect_support_resistance(data) |
|
buy_signals, sell_signals = vsa_signals(data) |
|
|
|
|
|
fig = go.Figure() |
|
|
|
|
|
fig.add_trace(go.Candlestick(x=data.index, |
|
open=data['Open'], |
|
high=data['High'], |
|
low=data['Low'], |
|
close=data['Close'], name='Market Data')) |
|
|
|
|
|
fig.add_trace(go.Scatter(x=data.index, y=moving_average, mode='lines', name='20-day MA')) |
|
|
|
|
|
fig.add_trace(go.Scatter(x=data.index, y=support, mode='lines', name='Support', line=dict(color='green'))) |
|
fig.add_trace(go.Scatter(x=data.index, y=resistance, mode='lines', name='Resistance', line=dict(color='red'))) |
|
|
|
|
|
fig.add_trace(go.Scatter(x=buy_signals.index, y=buy_signals, mode='markers', marker=dict(color='blue', size=10), name='Buy Signal')) |
|
fig.add_trace(go.Scatter(x=sell_signals.index, y=sell_signals, mode='markers', marker=dict(color='orange', size=10), name='Sell Signal')) |
|
|
|
|
|
fig.update_layout(title='VSA Trading Strategy Analysis', xaxis_title='Date', yaxis_title='Price', template='plotly_dark') |
|
|
|
|
|
st.plotly_chart(fig) |
|
|
|
|
|
st.title('VSA Trading Strategy Visualizer') |
|
st.markdown(''' |
|
This app provides an interactive way to visualize the Volume Spread Analysis (VSA) trading strategy with buy and sell signals based on the strategy. |
|
To start, enter the ticker symbol, select the start and end dates, and then click the "Analyze" button. |
|
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. |
|
''') |
|
else: |
|
st.error('No data found for the selected ticker and date range.') |
|
|