import streamlit as st import yfinance as yf import pandas as pd import plotly.graph_objects as go # Function to calculate moving averages def calculate_moving_average(data, window_size): return data['Close'].rolling(window=window_size).mean() # Function to detect support and resistance levels def detect_support_resistance(data): # This is a simple placeholder for actual support and resistance logic return data['Close'].rolling(window=20).min(), data['Close'].rolling(window=20).max() # VSA signals logic placeholder def vsa_signals(data): # This should include real VSA calculation logic based on volume and spread buy_signals = pd.Series(index=data.index, dtype='float64') sell_signals = pd.Series(index=data.index, dtype='float64') # Dummy logic for demonstration: 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 # Streamlit sidebar options 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: # Calculations moving_average = calculate_moving_average(data, window_size=20) support, resistance = detect_support_resistance(data) buy_signals, sell_signals = vsa_signals(data) # Plotting fig = go.Figure() # Add candlestick chart fig.add_trace(go.Candlestick(x=data.index, open=data['Open'], high=data['High'], low=data['Low'], close=data['Close'], name='Market Data')) # Add Moving Average Line fig.add_trace(go.Scatter(x=data.index, y=moving_average, mode='lines', name='20-day MA')) # Add Support and Resistance Lines 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'))) # Add Buy and Sell Signals 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')) # Layout settings fig.update_layout(title='VSA Trading Strategy Analysis', xaxis_title='Date', yaxis_title='Price', template='plotly_dark') # Display the figure st.plotly_chart(fig) # App introduction and guide 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.')