-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathbacktester.py
723 lines (616 loc) · 29.5 KB
/
backtester.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
import sys
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
import questionary
import matplotlib.pyplot as plt
import pandas as pd
from colorama import Fore, Style, init
import numpy as np
import itertools
from llm.models import LLM_ORDER, get_model_info
from utils.analysts import ANALYST_ORDER
from main import run_hedge_fund
from tools.api import (
get_company_news,
get_price_data,
get_prices,
get_financial_metrics,
get_insider_trades,
)
from utils.display import print_backtest_results, format_backtest_row
from typing_extensions import Callable
init(autoreset=True)
class Backtester:
def __init__(
self,
agent: Callable,
tickers: list[str],
start_date: str,
end_date: str,
initial_capital: float,
model_name: str = "gpt-4o",
model_provider: str = "OpenAI",
selected_analysts: list[str] = [],
initial_margin_requirement: float = 0.0,
):
"""
:param agent: The trading agent (Callable).
:param tickers: List of tickers to backtest.
:param start_date: Start date string (YYYY-MM-DD).
:param end_date: End date string (YYYY-MM-DD).
:param initial_capital: Starting portfolio cash.
:param model_name: Which LLM model name to use (gpt-4, etc).
:param model_provider: Which LLM provider (OpenAI, etc).
:param selected_analysts: List of analyst names or IDs to incorporate.
:param initial_margin_requirement: The margin ratio (e.g. 0.5 = 50%).
"""
self.agent = agent
self.tickers = tickers
self.start_date = start_date
self.end_date = end_date
self.initial_capital = initial_capital
self.model_name = model_name
self.model_provider = model_provider
self.selected_analysts = selected_analysts
# Store the margin ratio (e.g. 0.5 means 50% margin required).
self.margin_ratio = initial_margin_requirement
# Initialize portfolio with support for long/short positions
self.portfolio_values = []
self.portfolio = {
"cash": initial_capital,
"margin_used": 0.0, # total margin usage across all short positions
"positions": {
ticker: {
"long": 0, # Number of shares held long
"short": 0, # Number of shares held short
"long_cost_basis": 0.0, # Average cost basis per share (long)
"short_cost_basis": 0.0, # Average cost basis per share (short)
"short_margin_used": 0.0 # Dollars of margin used for this ticker's 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
}
}
def execute_trade(self, ticker: str, action: str, quantity: float, current_price: float):
"""
Execute trades with support for both long and short positions.
`quantity` is the number of shares the agent wants to buy/sell/short/cover.
We will only trade integer shares to keep it simple.
"""
if quantity <= 0:
return 0
quantity = int(quantity) # force integer shares
position = self.portfolio["positions"][ticker]
if action == "buy":
cost = quantity * current_price
if cost <= self.portfolio["cash"]:
# Weighted average cost basis for the new total
old_shares = position["long"]
old_cost_basis = position["long_cost_basis"]
new_shares = quantity
total_shares = old_shares + new_shares
if total_shares > 0:
total_old_cost = old_cost_basis * old_shares
total_new_cost = cost
position["long_cost_basis"] = (total_old_cost + total_new_cost) / total_shares
position["long"] += quantity
self.portfolio["cash"] -= cost
return quantity
else:
# Calculate maximum affordable quantity
max_quantity = int(self.portfolio["cash"] / current_price)
if max_quantity > 0:
cost = max_quantity * current_price
old_shares = position["long"]
old_cost_basis = position["long_cost_basis"]
total_shares = old_shares + max_quantity
if total_shares > 0:
total_old_cost = old_cost_basis * old_shares
total_new_cost = cost
position["long_cost_basis"] = (total_old_cost + total_new_cost) / total_shares
position["long"] += max_quantity
self.portfolio["cash"] -= cost
return max_quantity
return 0
elif action == "sell":
# You can only sell as many as you own
quantity = min(quantity, position["long"])
if quantity > 0:
# Realized gain/loss using average cost basis
avg_cost_per_share = position["long_cost_basis"] if position["long"] > 0 else 0
realized_gain = (current_price - avg_cost_per_share) * quantity
self.portfolio["realized_gains"][ticker]["long"] += realized_gain
position["long"] -= quantity
self.portfolio["cash"] += quantity * current_price
if position["long"] == 0:
position["long_cost_basis"] = 0.0
return quantity
elif action == "short":
"""
Typical short sale flow:
1) Receive proceeds = current_price * quantity
2) Post margin_required = proceeds * margin_ratio
3) Net effect on cash = +proceeds - margin_required
"""
proceeds = current_price * quantity
margin_required = proceeds * self.margin_ratio
if margin_required <= self.portfolio["cash"]:
# Weighted average short cost basis
old_short_shares = position["short"]
old_cost_basis = position["short_cost_basis"]
new_shares = quantity
total_shares = old_short_shares + new_shares
if total_shares > 0:
total_old_cost = old_cost_basis * old_short_shares
total_new_cost = current_price * new_shares
position["short_cost_basis"] = (total_old_cost + total_new_cost) / total_shares
position["short"] += quantity
# Update margin usage
position["short_margin_used"] += margin_required
self.portfolio["margin_used"] += margin_required
# Increase cash by proceeds, then subtract the required margin
self.portfolio["cash"] += proceeds
self.portfolio["cash"] -= margin_required
return quantity
else:
# Calculate maximum shortable quantity
if self.margin_ratio > 0:
max_quantity = int(self.portfolio["cash"] / (current_price * self.margin_ratio))
else:
max_quantity = 0
if max_quantity > 0:
proceeds = current_price * max_quantity
margin_required = proceeds * self.margin_ratio
old_short_shares = position["short"]
old_cost_basis = position["short_cost_basis"]
total_shares = old_short_shares + max_quantity
if total_shares > 0:
total_old_cost = old_cost_basis * old_short_shares
total_new_cost = current_price * max_quantity
position["short_cost_basis"] = (total_old_cost + total_new_cost) / total_shares
position["short"] += max_quantity
position["short_margin_used"] += margin_required
self.portfolio["margin_used"] += margin_required
self.portfolio["cash"] += proceeds
self.portfolio["cash"] -= margin_required
return max_quantity
return 0
elif action == "cover":
"""
When covering shares:
1) Pay cover cost = current_price * quantity
2) Release a proportional share of the margin
3) Net effect on cash = -cover_cost + released_margin
"""
quantity = min(quantity, position["short"])
if quantity > 0:
cover_cost = quantity * current_price
avg_short_price = position["short_cost_basis"] if position["short"] > 0 else 0
realized_gain = (avg_short_price - current_price) * quantity
if position["short"] > 0:
portion = quantity / position["short"]
else:
portion = 1.0
margin_to_release = portion * position["short_margin_used"]
position["short"] -= quantity
position["short_margin_used"] -= margin_to_release
self.portfolio["margin_used"] -= margin_to_release
# Pay the cost to cover, but get back the released margin
self.portfolio["cash"] += margin_to_release
self.portfolio["cash"] -= cover_cost
self.portfolio["realized_gains"][ticker]["short"] += realized_gain
if position["short"] == 0:
position["short_cost_basis"] = 0.0
position["short_margin_used"] = 0.0
return quantity
return 0
def calculate_portfolio_value(self, current_prices):
"""
Calculate total portfolio value, including:
- cash
- market value of long positions
- unrealized gains/losses for short positions
"""
total_value = self.portfolio["cash"]
for ticker in self.tickers:
position = self.portfolio["positions"][ticker]
price = current_prices[ticker]
# Long position value
long_value = position["long"] * price
total_value += long_value
# Short position unrealized PnL = short_shares * (short_cost_basis - current_price)
if position["short"] > 0:
total_value += position["short"] * (position["short_cost_basis"] - price)
return total_value
def prefetch_data(self):
"""Pre-fetch all data needed for the backtest period."""
print("\nPre-fetching data for the entire backtest period...")
# Convert end_date string to datetime, fetch up to 1 year before
end_date_dt = datetime.strptime(self.end_date, "%Y-%m-%d")
start_date_dt = end_date_dt - relativedelta(years=1)
start_date_str = start_date_dt.strftime("%Y-%m-%d")
for ticker in self.tickers:
# Fetch price data for the entire period, plus 1 year
get_prices(ticker, start_date_str, self.end_date)
# Fetch financial metrics
get_financial_metrics(ticker, self.end_date, limit=10)
# Fetch insider trades
get_insider_trades(ticker, self.end_date, start_date=self.start_date, limit=1000)
# Fetch company news
get_company_news(ticker, self.end_date, start_date=self.start_date, limit=1000)
print("Data pre-fetch complete.")
def parse_agent_response(self, agent_output):
"""Parse JSON output from the agent (fallback to 'hold' if invalid)."""
import json
try:
decision = json.loads(agent_output)
return decision
except Exception:
print(f"Error parsing action: {agent_output}")
return {"action": "hold", "quantity": 0}
def run_backtest(self):
# Pre-fetch all data at the start
self.prefetch_data()
dates = pd.date_range(self.start_date, self.end_date, freq="B")
table_rows = []
performance_metrics = {
'sharpe_ratio': None,
'sortino_ratio': None,
'max_drawdown': None,
'long_short_ratio': None,
'gross_exposure': None,
'net_exposure': None
}
print("\nStarting backtest...")
# Initialize portfolio values list with initial capital
if len(dates) > 0:
self.portfolio_values = [{"Date": dates[0], "Portfolio Value": self.initial_capital}]
else:
self.portfolio_values = []
for current_date in dates:
lookback_start = (current_date - timedelta(days=30)).strftime("%Y-%m-%d")
current_date_str = current_date.strftime("%Y-%m-%d")
previous_date_str = (current_date - timedelta(days=1)).strftime("%Y-%m-%d")
# Skip if there's no prior day to look back (i.e., first date in the range)
if lookback_start == current_date_str:
continue
# Get current prices for all tickers
try:
current_prices = {
ticker: get_price_data(ticker, previous_date_str, current_date_str).iloc[-1]["close"]
for ticker in self.tickers
}
except Exception:
# If data is missing or there's an API error, skip this day
print(f"Error fetching prices between {previous_date_str} and {current_date_str}")
continue
# ---------------------------------------------------------------
# 1) Execute the agent's trades
# ---------------------------------------------------------------
output = self.agent(
tickers=self.tickers,
start_date=lookback_start,
end_date=current_date_str,
portfolio=self.portfolio,
model_name=self.model_name,
model_provider=self.model_provider,
selected_analysts=self.selected_analysts,
)
decisions = output["decisions"]
analyst_signals = output["analyst_signals"]
# Execute trades for each ticker
executed_trades = {}
for ticker in self.tickers:
decision = decisions.get(ticker, {"action": "hold", "quantity": 0})
action, quantity = decision.get("action", "hold"), decision.get("quantity", 0)
executed_quantity = self.execute_trade(ticker, action, quantity, current_prices[ticker])
executed_trades[ticker] = executed_quantity
# ---------------------------------------------------------------
# 2) Now that trades have executed trades, recalculate the final
# portfolio value for this day.
# ---------------------------------------------------------------
total_value = self.calculate_portfolio_value(current_prices)
# Also compute long/short exposures for final post‐trade state
long_exposure = sum(
self.portfolio["positions"][t]["long"] * current_prices[t]
for t in self.tickers
)
short_exposure = sum(
self.portfolio["positions"][t]["short"] * current_prices[t]
for t in self.tickers
)
# Calculate gross and net exposures
gross_exposure = long_exposure + short_exposure
net_exposure = long_exposure - short_exposure
long_short_ratio = (
long_exposure / short_exposure if short_exposure > 1e-9 else float('inf')
)
# Track each day's portfolio value in self.portfolio_values
self.portfolio_values.append({
"Date": current_date,
"Portfolio Value": total_value,
"Long Exposure": long_exposure,
"Short Exposure": short_exposure,
"Gross Exposure": gross_exposure,
"Net Exposure": net_exposure,
"Long/Short Ratio": long_short_ratio
})
# ---------------------------------------------------------------
# 3) Build the table rows to display
# ---------------------------------------------------------------
date_rows = []
# For each ticker, record signals/trades
for ticker in self.tickers:
ticker_signals = {}
for agent_name, signals in analyst_signals.items():
if ticker in signals:
ticker_signals[agent_name] = signals[ticker]
bullish_count = len([s for s in ticker_signals.values() if s.get("signal", "").lower() == "bullish"])
bearish_count = len([s for s in ticker_signals.values() if s.get("signal", "").lower() == "bearish"])
neutral_count = len([s for s in ticker_signals.values() if s.get("signal", "").lower() == "neutral"])
# Calculate net position value
pos = self.portfolio["positions"][ticker]
long_val = pos["long"] * current_prices[ticker]
short_val = pos["short"] * current_prices[ticker]
net_position_value = long_val - short_val
# Get the action and quantity from the decisions
action = decisions.get(ticker, {}).get("action", "hold")
quantity = executed_trades.get(ticker, 0)
# Append the agent action to the table rows
date_rows.append(
format_backtest_row(
date=current_date_str,
ticker=ticker,
action=action,
quantity=quantity,
price=current_prices[ticker],
shares_owned=pos["long"] - pos["short"], # net shares
position_value=net_position_value,
bullish_count=bullish_count,
bearish_count=bearish_count,
neutral_count=neutral_count,
)
)
# ---------------------------------------------------------------
# 4) Calculate performance summary metrics
# ---------------------------------------------------------------
total_realized_gains = sum(
self.portfolio["realized_gains"][t]["long"] +
self.portfolio["realized_gains"][t]["short"]
for t in self.tickers
)
# Calculate cumulative return vs. initial capital
portfolio_return = ((total_value + total_realized_gains) / self.initial_capital - 1) * 100
# Add summary row for this day
date_rows.append(
format_backtest_row(
date=current_date_str,
ticker="",
action="",
quantity=0,
price=0,
shares_owned=0,
position_value=0,
bullish_count=0,
bearish_count=0,
neutral_count=0,
is_summary=True,
total_value=total_value,
return_pct=portfolio_return,
cash_balance=self.portfolio["cash"],
total_position_value=total_value - self.portfolio["cash"],
sharpe_ratio=performance_metrics["sharpe_ratio"],
sortino_ratio=performance_metrics["sortino_ratio"],
max_drawdown=performance_metrics["max_drawdown"],
),
)
table_rows.extend(date_rows)
print_backtest_results(table_rows)
# Update performance metrics if we have enough data
if len(self.portfolio_values) > 3:
self._update_performance_metrics(performance_metrics)
return performance_metrics
def _update_performance_metrics(self, performance_metrics):
"""Helper method to update performance metrics using daily returns."""
values_df = pd.DataFrame(self.portfolio_values).set_index("Date")
values_df["Daily Return"] = values_df["Portfolio Value"].pct_change()
clean_returns = values_df["Daily Return"].dropna()
if len(clean_returns) < 2:
return # not enough data points
# Assumes 252 trading days/year
daily_risk_free_rate = 0.0434 / 252
excess_returns = clean_returns - daily_risk_free_rate
mean_excess_return = excess_returns.mean()
std_excess_return = excess_returns.std()
# Sharpe ratio
if std_excess_return > 1e-12:
performance_metrics["sharpe_ratio"] = np.sqrt(252) * (mean_excess_return / std_excess_return)
else:
performance_metrics["sharpe_ratio"] = 0.0
# Sortino ratio
negative_returns = excess_returns[excess_returns < 0]
if len(negative_returns) > 0:
downside_std = negative_returns.std()
if downside_std > 1e-12:
performance_metrics["sortino_ratio"] = np.sqrt(252) * (mean_excess_return / downside_std)
else:
performance_metrics["sortino_ratio"] = float('inf') if mean_excess_return > 0 else 0
else:
performance_metrics["sortino_ratio"] = float('inf') if mean_excess_return > 0 else 0
# Maximum drawdown
rolling_max = values_df["Portfolio Value"].cummax()
drawdown = (values_df["Portfolio Value"] - rolling_max) / rolling_max
performance_metrics["max_drawdown"] = drawdown.min() * 100
def analyze_performance(self):
"""Creates a performance DataFrame, prints summary stats, and plots equity curve."""
if not self.portfolio_values:
print("No portfolio data found. Please run the backtest first.")
return pd.DataFrame()
performance_df = pd.DataFrame(self.portfolio_values).set_index("Date")
if performance_df.empty:
print("No valid performance data to analyze.")
return performance_df
final_portfolio_value = performance_df["Portfolio Value"].iloc[-1]
total_realized_gains = sum(
self.portfolio["realized_gains"][ticker]["long"] for ticker in self.tickers
)
total_return = ((final_portfolio_value - self.initial_capital) / self.initial_capital) * 100
print(f"\n{Fore.WHITE}{Style.BRIGHT}PORTFOLIO PERFORMANCE SUMMARY:{Style.RESET_ALL}")
print(f"Total Return: {Fore.GREEN if total_return >= 0 else Fore.RED}{total_return:.2f}%{Style.RESET_ALL}")
print(f"Total Realized Gains/Losses: {Fore.GREEN if total_realized_gains >= 0 else Fore.RED}${total_realized_gains:,.2f}{Style.RESET_ALL}")
# Plot the portfolio value over time
plt.figure(figsize=(12, 6))
plt.plot(performance_df.index, performance_df["Portfolio Value"], color="blue")
plt.title("Portfolio Value Over Time")
plt.ylabel("Portfolio Value ($)")
plt.xlabel("Date")
plt.grid(True)
plt.show()
# Compute daily returns
performance_df["Daily Return"] = performance_df["Portfolio Value"].pct_change().fillna(0)
daily_rf = 0.0434 / 252 # daily risk-free rate
mean_daily_return = performance_df["Daily Return"].mean()
std_daily_return = performance_df["Daily Return"].std()
# Annualized Sharpe Ratio
if std_daily_return != 0:
annualized_sharpe = np.sqrt(252) * ((mean_daily_return - daily_rf) / std_daily_return)
else:
annualized_sharpe = 0
print(f"\nSharpe Ratio: {Fore.YELLOW}{annualized_sharpe:.2f}{Style.RESET_ALL}")
# Max Drawdown
rolling_max = performance_df["Portfolio Value"].cummax()
drawdown = (performance_df["Portfolio Value"] - rolling_max) / rolling_max
max_drawdown = drawdown.min()
max_drawdown_date = drawdown.idxmin()
if pd.notnull(max_drawdown_date):
print(f"Maximum Drawdown: {Fore.RED}{max_drawdown * 100:.2f}%{Style.RESET_ALL} (on {max_drawdown_date.strftime('%Y-%m-%d')})")
else:
print(f"Maximum Drawdown: {Fore.RED}0.00%{Style.RESET_ALL}")
# Win Rate
winning_days = len(performance_df[performance_df["Daily Return"] > 0])
total_days = max(len(performance_df) - 1, 1)
win_rate = (winning_days / total_days) * 100
print(f"Win Rate: {Fore.GREEN}{win_rate:.2f}%{Style.RESET_ALL}")
# Average Win/Loss Ratio
positive_returns = performance_df[performance_df["Daily Return"] > 0]["Daily Return"]
negative_returns = performance_df[performance_df["Daily Return"] < 0]["Daily Return"]
avg_win = positive_returns.mean() if not positive_returns.empty else 0
avg_loss = abs(negative_returns.mean()) if not negative_returns.empty else 0
if avg_loss != 0:
win_loss_ratio = avg_win / avg_loss
else:
win_loss_ratio = float('inf') if avg_win > 0 else 0
print(f"Win/Loss Ratio: {Fore.GREEN}{win_loss_ratio:.2f}{Style.RESET_ALL}")
# Maximum Consecutive Wins / Losses
returns_binary = (performance_df["Daily Return"] > 0).astype(int)
if len(returns_binary) > 0:
max_consecutive_wins = max((len(list(g)) for k, g in itertools.groupby(returns_binary) if k == 1), default=0)
max_consecutive_losses = max((len(list(g)) for k, g in itertools.groupby(returns_binary) if k == 0), default=0)
else:
max_consecutive_wins = 0
max_consecutive_losses = 0
print(f"Max Consecutive Wins: {Fore.GREEN}{max_consecutive_wins}{Style.RESET_ALL}")
print(f"Max Consecutive Losses: {Fore.RED}{max_consecutive_losses}{Style.RESET_ALL}")
return performance_df
### 4. Run the Backtest #####
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Run backtesting simulation")
parser.add_argument(
"--tickers",
type=str,
required=False,
help="Comma-separated list of stock ticker symbols (e.g., AAPL,MSFT,GOOGL)",
)
parser.add_argument(
"--end-date",
type=str,
default=datetime.now().strftime("%Y-%m-%d"),
help="End date in YYYY-MM-DD format",
)
parser.add_argument(
"--start-date",
type=str,
default=(datetime.now() - relativedelta(months=1)).strftime("%Y-%m-%d"),
help="Start date in YYYY-MM-DD format",
)
parser.add_argument(
"--initial-capital",
type=float,
default=100000,
help="Initial capital amount (default: 100000)",
)
parser.add_argument(
"--margin-requirement",
type=float,
default=0.0,
help="Margin ratio for short positions, e.g. 0.5 for 50% (default: 0.0)",
)
args = parser.parse_args()
# Parse tickers from comma-separated string
tickers = [ticker.strip() for ticker in args.tickers.split(",")] if args.tickers else []
# Choose analysts
selected_analysts = None
choices = questionary.checkbox(
"Use the Space bar to select/unselect analysts.",
choices=[questionary.Choice(display, value=value) for display, value in ANALYST_ORDER],
instruction="\n\nPress 'a' to toggle all.\n\nPress Enter when done to run the hedge fund.",
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: "
f"{', '.join(Fore.GREEN + choice.title().replace('_', ' ') + Style.RESET_ALL for choice in choices)}"
)
# 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:
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 and run the backtester
backtester = Backtester(
agent=run_hedge_fund,
tickers=tickers,
start_date=args.start_date,
end_date=args.end_date,
initial_capital=args.initial_capital,
model_name=model_choice,
model_provider=model_provider,
selected_analysts=selected_analysts,
initial_margin_requirement=args.margin_requirement,
)
performance_metrics = backtester.run_backtest()
performance_df = backtester.analyze_performance()