netflypsb commited on
Commit
3784578
·
verified ·
1 Parent(s): 29cac4b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from datetime import datetime, timedelta
3
+ import pandas as pd
4
+
5
+ # Import custom modules
6
+ from yfinance_data.fetcher import fetch_stock_data
7
+ from indicators.sma import calculate_21_50_sma
8
+ from indicators.bollinger_bands import calculate_bollinger_bands
9
+ from signals.strategy import generate_signals
10
+ from utils.plotting import plot_stock_data_with_signals
11
+ from utils.calculator import simulate_trading # Import the trading simulation function
12
+
13
+ # Streamlit app title
14
+ st.title('StockSwingApp: Stock Trading Signal Generator')
15
+
16
+ # Sidebar for user inputs
17
+ st.sidebar.header('User Input Parameters')
18
+ default_symbol = 'AAPL'
19
+ symbol = st.sidebar.text_input('Stock Symbol', value=default_symbol, max_chars=5).upper()
20
+ start_date = st.sidebar.date_input('Start Date', value=datetime.now() - timedelta(days=90))
21
+ end_date = st.sidebar.date_input('End Date', value=datetime.now())
22
+
23
+ # Data fetching and processing
24
+ if st.sidebar.button('Generate Signals'):
25
+ st.write(f"Fetching data for {symbol} from {start_date} to {end_date}...")
26
+
27
+ try:
28
+ stock_data = fetch_stock_data(symbol, start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d'), interval='1d')
29
+
30
+ if not stock_data.empty:
31
+ # Calculate indicators and signals
32
+ stock_data = calculate_21_50_sma(stock_data)
33
+ stock_data = calculate_bollinger_bands(stock_data)
34
+ signals_data = generate_signals(stock_data)
35
+
36
+ # Plotting
37
+ fig = plot_stock_data_with_signals(signals_data)
38
+ st.pyplot(fig)
39
+
40
+ # Simulate trading based on generated signals and display results
41
+ final_stock_value, remaining_cash = simulate_trading(signals_data)
42
+ st.write(f"Final Value of Stock Holdings: ${final_stock_value:.2f}")
43
+ st.write(f"Remaining Cash: ${remaining_cash:.2f}")
44
+ total_portfolio_value = final_stock_value + remaining_cash
45
+ st.write(f"Total Portfolio Value: ${total_portfolio_value:.2f}")
46
+
47
+ # Option to display raw data
48
+ if st.checkbox('Show Raw Data'):
49
+ st.write(signals_data)
50
+
51
+ else:
52
+ st.error("No data available for the given symbol. Please try another one.")
53
+
54
+ except Exception as e:
55
+ st.error(f"An error occurred: {e}")
56
+
57
+ else:
58
+ st.info('Enter parameters and click "Generate Signals" to view analysis.')