-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
143 lines (112 loc) · 5.05 KB
/
main.py
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# main.py
from dotenv import load_dotenv
load_dotenv() # Load environment variables
import streamlit as st
import os
import logging
from ai_response import configure_ai, get_gemini_response
from data_acquisition import fetch_stock_data
from pattern_recognition import calculate_indicators
from trading_recommendations import generate_signals
from stock_list import get_stock_list
from sentiment_analysis import get_news_sentiment
from market_data import fetch_index_data, get_index_summary
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Load API keys from environment variables
alpha_vantage_api_key = os.getenv("ALPHA_VANTAGE_API_KEY")
# Configure AI
configure_ai(os.getenv("GOOGLE_API_KEY"))
# Initialize Streamlit app
st.set_page_config(page_title="Dispersal AI")
st.header("WWW.Dispersal.net AI Preview")
# Initialize session state for chat history if it doesn't exist
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = []
def get_help_message():
help_message = """
### Help Guide
**Stock Analysis**:
- "Analyze stock SYMBOL" - Get a recommendation for a specific stock. Replace SYMBOL with the stock ticker (e.g., AAPL, MSFT).
**Stock Recommendations for Monday**:
- "Recommend stocks for Monday" - Get recommendations for all stocks listed in `stock_list.py` for the upcoming Monday.
**Market Summary**:
- "Market summary" - Get a summary of the market performance for major indices.
**Other Commands**:
- "Help me" - Display this help guide.
"""
return help_message
def process_input(user_input):
if user_input.lower().startswith("analyze stock "):
symbol = user_input.split(" ")[-1].upper()
return get_stock_recommendation(symbol)
elif user_input.lower() == "recommend stocks for monday":
return recommend_stocks()
elif user_input.lower() == "market summary":
return get_market_summary()
elif user_input.lower() == "help me":
return get_help_message()
else:
return "I'm sorry, I don't understand that command. Type 'help me' for a list of commands."
def recommend_stocks():
stock_list = get_stock_list()
recommendations = []
for symbol in stock_list:
try:
data = fetch_stock_data(symbol, alpha_vantage_api_key)
if data is None or data.empty:
logger.error(f"No data fetched for {symbol}")
recommendations.append(f"No data fetched for {symbol}")
continue
data = calculate_indicators(data)
data = generate_signals(data)
sentiments = get_news_sentiment(symbol)
sentiment_score = sum(sentiment['compound'] for sentiment in sentiments) / len(sentiments) if sentiments else 0
logger.info(f"Symbol: {symbol}, Last Signal: {data['Signal'].iloc[-1]}, Sentiment Score: {sentiment_score}")
if data['Signal'].iloc[-1] == 1 and sentiment_score > 0:
recommendations.append(f"Buy Call on {symbol}")
elif data['Signal'].iloc[-1] == -1 and sentiment_score < 0:
recommendations.append(f"Buy Put on {symbol}")
else:
recommendations.append(f"Hold on {symbol}")
except Exception as e:
logger.error(f"Error processing {symbol}: {e}")
recommendations.append(f"Error processing {symbol}")
return "\n".join(recommendations)
def get_stock_recommendation(symbol):
data = fetch_stock_data(symbol, alpha_vantage_api_key)
if data is None or data.empty:
logger.error(f"No data fetched for {symbol}")
return f"No data fetched for {symbol}"
data = calculate_indicators(data)
logger.info(f"Data with indicators for {symbol}: {data.tail()}")
data = generate_signals(data)
logger.info(f"Data with signals for {symbol}: {data.tail()}")
sentiment_score = 0 # Default sentiment score for debugging
sentiments = get_news_sentiment(symbol)
if sentiments:
sentiment_score = sum(sentiment['compound'] for sentiment in sentiments) / len(sentiments)
logger.info(f"Symbol: {symbol}, Last Signal: {data['Signal'].iloc[-1]}, Sentiment Score: {sentiment_score}")
if data['Signal'].iloc[-1] == 1:
return f"Recommendation: Buy Call on {symbol}"
elif data['Signal'].iloc[-1] == -1:
return f"Recommendation: Buy Put on {symbol}"
else:
return f"Recommendation: Hold on {symbol}"
def get_market_summary():
summary = get_index_summary()
if summary:
return "\n".join(summary)
else:
return "No market summary data available."
input = st.text_input("Input: ", key="input")
submit = st.button("Ask the question")
if submit and input:
bot_response = process_input(input)
st.write(bot_response)
st.session_state['chat_history'].append(("Bot", bot_response))
st.session_state['chat_history'].append(("You", input))
st.subheader("Chat History")
for role, text in st.session_state['chat_history']:
st.write(f"{role}: {text}")