StockSwingApp / app.py
netflypsb's picture
Update app.py
02a1377 verified
import streamlit as st
from datetime import datetime, timedelta
import pandas as pd
# Import custom modules
from yfinance_data.fetcher import fetch_stock_data
from indicators.sma import calculate_21_50_sma
from indicators.bollinger_bands import calculate_bollinger_bands
from signals.strategy import generate_signals
from utils.plotting import plot_stock_data_with_signals
from utils.calculator2 import simulate_all_in_trading # Adjusted import for calculator2.py
# Streamlit app title
st.title('StockSwingApp: Stock Trading Signal Generator')
# Sidebar for user inputs
st.sidebar.header('User Input Parameters')
default_symbol = 'AAPL'
symbol = st.sidebar.text_input('Stock Symbol', value=default_symbol, max_chars=10).upper()
start_date = st.sidebar.date_input('Start Date', value=datetime.now() - timedelta(days=90))
end_date = st.sidebar.date_input('End Date', value=datetime.now())
# Data fetching and processing
if st.sidebar.button('Generate Signals'):
st.write(f"Fetching data for {symbol} from {start_date} to {end_date}...")
try:
stock_data = fetch_stock_data(symbol, start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d'), interval='1d')
if not stock_data.empty:
# Calculate indicators and signals
stock_data = calculate_21_50_sma(stock_data)
stock_data = calculate_bollinger_bands(stock_data)
signals_data = generate_signals(stock_data)
# Plotting
fig = plot_stock_data_with_signals(signals_data)
st.pyplot(fig)
# Simulate trading based on generated signals using calculator2 and display results
final_cash, trades = simulate_all_in_trading(signals_data)
st.write(f"Final Cash: ${final_cash:.2f}")
st.write(f"Trades Executed: {trades}")
# Option to display raw data
if st.checkbox('Show Raw Data'):
st.write(signals_data)
else:
st.error("No data available for the given symbol. Please try another one.")
except Exception as e:
st.error(f"An error occurred: {e}")
else:
st.info('Enter parameters and click "Generate Signals" to view analysis.')