Spaces:
Runtime error
Runtime error
File size: 2,209 Bytes
3784578 0ff7d89 3784578 02a1377 3784578 0ff7d89 3784578 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
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.')
|