-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathmain.py
281 lines (244 loc) · 9.62 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import sys
from dotenv import load_dotenv
from langchain_core.messages import HumanMessage
from langgraph.graph import END, StateGraph
from colorama import Fore, Back, Style, init
import questionary
from agents.ben_graham import ben_graham_agent
from agents.bill_ackman import bill_ackman_agent
from agents.fundamentals import fundamentals_agent
from agents.portfolio_manager import portfolio_management_agent
from agents.technicals import technical_analyst_agent
from agents.risk_manager import risk_management_agent
from agents.sentiment import sentiment_agent
from agents.warren_buffett import warren_buffett_agent
from graph.state import AgentState
from agents.valuation import valuation_agent
from utils.display import print_trading_output
from utils.analysts import ANALYST_ORDER, get_analyst_nodes
from utils.progress import progress
from llm.models import LLM_ORDER, get_model_info
import argparse
from datetime import datetime
from dateutil.relativedelta import relativedelta
from tabulate import tabulate
from utils.visualize import save_graph_as_png
# Load environment variables from .env file
load_dotenv()
init(autoreset=True)
def parse_hedge_fund_response(response):
import json
try:
return json.loads(response)
except:
print(f"Error parsing response: {response}")
return None
##### Run the Hedge Fund #####
def run_hedge_fund(
tickers: list[str],
start_date: str,
end_date: str,
portfolio: dict,
show_reasoning: bool = False,
selected_analysts: list[str] = [],
model_name: str = "gpt-4o",
model_provider: str = "OpenAI",
):
# Start progress tracking
progress.start()
try:
# Create a new workflow if analysts are customized
if selected_analysts:
workflow = create_workflow(selected_analysts)
agent = workflow.compile()
else:
agent = app
final_state = agent.invoke(
{
"messages": [
HumanMessage(
content="Make trading decisions based on the provided data.",
)
],
"data": {
"tickers": tickers,
"portfolio": portfolio,
"start_date": start_date,
"end_date": end_date,
"analyst_signals": {},
},
"metadata": {
"show_reasoning": show_reasoning,
"model_name": model_name,
"model_provider": model_provider,
},
},
)
return {
"decisions": parse_hedge_fund_response(final_state["messages"][-1].content),
"analyst_signals": final_state["data"]["analyst_signals"],
}
finally:
# Stop progress tracking
progress.stop()
def start(state: AgentState):
"""Initialize the workflow with the input message."""
return state
def create_workflow(selected_analysts=None):
"""Create the workflow with selected analysts."""
workflow = StateGraph(AgentState)
workflow.add_node("start_node", start)
# Get analyst nodes from the configuration
analyst_nodes = get_analyst_nodes()
# Default to all analysts if none selected
if selected_analysts is None:
selected_analysts = list(analyst_nodes.keys())
# Add selected analyst nodes
for analyst_key in selected_analysts:
node_name, node_func = analyst_nodes[analyst_key]
workflow.add_node(node_name, node_func)
workflow.add_edge("start_node", node_name)
# Always add risk and portfolio management
workflow.add_node("risk_management_agent", risk_management_agent)
workflow.add_node("portfolio_management_agent", portfolio_management_agent)
# Connect selected analysts to risk management
for analyst_key in selected_analysts:
node_name = analyst_nodes[analyst_key][0]
workflow.add_edge(node_name, "risk_management_agent")
workflow.add_edge("risk_management_agent", "portfolio_management_agent")
workflow.add_edge("portfolio_management_agent", END)
workflow.set_entry_point("start_node")
return workflow
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run the hedge fund trading system")
parser.add_argument(
"--initial-cash",
type=float,
default=100000.0,
help="Initial cash position. Defaults to 100000.0)"
)
parser.add_argument(
"--margin-requirement",
type=float,
default=0.0,
help="Initial margin requirement. Defaults to 0.0"
)
parser.add_argument("--tickers", type=str, required=True, help="Comma-separated list of stock ticker symbols")
parser.add_argument(
"--start-date",
type=str,
help="Start date (YYYY-MM-DD). Defaults to 3 months before end date",
)
parser.add_argument("--end-date", type=str, help="End date (YYYY-MM-DD). Defaults to today")
parser.add_argument("--show-reasoning", action="store_true", help="Show reasoning from each agent")
parser.add_argument(
"--show-agent-graph", action="store_true", help="Show the agent graph"
)
args = parser.parse_args()
# Parse tickers from comma-separated string
tickers = [ticker.strip() for ticker in args.tickers.split(",")]
# Select analysts
selected_analysts = None
choices = questionary.checkbox(
"Select your AI analysts.",
choices=[questionary.Choice(display, value=value) for display, value in ANALYST_ORDER],
instruction="\n\nInstructions: \n1. Press Space to select/unselect analysts.\n2. Press 'a' to select/unselect all.\n3. Press Enter when done to run the hedge fund.\n",
validate=lambda x: len(x) > 0 or "You must select at least one analyst.",
style=questionary.Style(
[
("checkbox-selected", "fg:green"),
("selected", "fg:green noinherit"),
("highlighted", "noinherit"),
("pointer", "noinherit"),
]
),
).ask()
if not choices:
print("\n\nInterrupt received. Exiting...")
sys.exit(0)
else:
selected_analysts = choices
print(f"\nSelected analysts: {', '.join(Fore.GREEN + choice.title().replace('_', ' ') + Style.RESET_ALL for choice in choices)}\n")
# Select LLM model
model_choice = questionary.select(
"Select your LLM model:",
choices=[questionary.Choice(display, value=value) for display, value, _ in LLM_ORDER],
style=questionary.Style([
("selected", "fg:green bold"),
("pointer", "fg:green bold"),
("highlighted", "fg:green"),
("answer", "fg:green bold"),
])
).ask()
if not model_choice:
print("\n\nInterrupt received. Exiting...")
sys.exit(0)
else:
# Get model info using the helper function
model_info = get_model_info(model_choice)
if model_info:
model_provider = model_info.provider.value
print(f"\nSelected {Fore.CYAN}{model_provider}{Style.RESET_ALL} model: {Fore.GREEN + Style.BRIGHT}{model_choice}{Style.RESET_ALL}\n")
else:
model_provider = "Unknown"
print(f"\nSelected model: {Fore.GREEN + Style.BRIGHT}{model_choice}{Style.RESET_ALL}\n")
# Create the workflow with selected analysts
workflow = create_workflow(selected_analysts)
app = workflow.compile()
if args.show_agent_graph:
file_path = ""
if selected_analysts is not None:
for selected_analyst in selected_analysts:
file_path += selected_analyst + "_"
file_path += "graph.png"
save_graph_as_png(app, file_path)
# Validate dates if provided
if args.start_date:
try:
datetime.strptime(args.start_date, "%Y-%m-%d")
except ValueError:
raise ValueError("Start date must be in YYYY-MM-DD format")
if args.end_date:
try:
datetime.strptime(args.end_date, "%Y-%m-%d")
except ValueError:
raise ValueError("End date must be in YYYY-MM-DD format")
# Set the start and end dates
end_date = args.end_date or datetime.now().strftime("%Y-%m-%d")
if not args.start_date:
# Calculate 3 months before end_date
end_date_obj = datetime.strptime(end_date, "%Y-%m-%d")
start_date = (end_date_obj - relativedelta(months=3)).strftime("%Y-%m-%d")
else:
start_date = args.start_date
# Initialize portfolio with cash amount and stock positions
portfolio = {
"cash": args.initial_cash, # Initial cash amount
"margin_requirement": args.margin_requirement, # Initial margin requirement
"positions": {
ticker: {
"long": 0, # Number of shares held long
"short": 0, # Number of shares held short
"long_cost_basis": 0.0, # Average cost basis for long positions
"short_cost_basis": 0.0, # Average price at which shares were sold short
} for ticker in tickers
},
"realized_gains": {
ticker: {
"long": 0.0, # Realized gains from long positions
"short": 0.0, # Realized gains from short positions
} for ticker in tickers
}
}
# Run the hedge fund
result = run_hedge_fund(
tickers=tickers,
start_date=start_date,
end_date=end_date,
portfolio=portfolio,
show_reasoning=args.show_reasoning,
selected_analysts=selected_analysts,
model_name=model_choice,
model_provider=model_provider,
)
print_trading_output(result)