Repository navigation
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·1191 lines (1074 loc) · 38.9 KB
/
Copy pathrun.sh
File metadata and controls
executable file
·1191 lines (1074 loc) · 38.9 KB
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# run.sh — Start the Schwab trading agent
#
# Usage:
# ./run.sh Dry-run one scan cycle, show signals, exit
# ./run.sh scan Quick scan — show signals without executing
# ./run.sh signals Signaler — alert on trend signal changes (Telegram)
# ./run.sh once Dry-run one full cycle (scan + execute)
# ./run.sh loop Dry-run continuous (scans every 5 min)
# ./run.sh live REAL MONEY — places actual orders on Schwab
# ./run.sh pnl Show P&L summary by strategy
# ./run.sh status Check Schwab connectivity + agent config
# ./run.sh web Start web dashboard (http://localhost:8898)
# ./run.sh harvest Tax-loss harvesting candidates across accounts (advisory)
# ./run.sh rebalance Tax-aware household rebalance plan (advisory; -o max_sharpe)
#
set -e
cd "$(dirname "$0")"
export PYTHONPATH="src${PYTHONPATH:+:$PYTHONPATH}"
VENV=".venv/bin/python"
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
# ---------- helpers ----------
check_ollama() {
LLM_ENABLED=$(grep '^LLM_ENABLED' .env 2>/dev/null | cut -d= -f2)
LLM_PROVIDER=$(grep '^LLM_PROVIDER' .env 2>/dev/null | cut -d= -f2)
if [ "$LLM_ENABLED" != "true" ]; then
echo -e " ${CYAN}·${NC} LLM disabled in .env (optional)"
return 0
fi
if [ "$LLM_PROVIDER" != "ollama" ] && [ -n "$LLM_PROVIDER" ]; then
echo -e " ${GREEN}✓${NC} LLM provider: ${CYAN}${LLM_PROVIDER}${NC}"
return 0
fi
if curl -s http://localhost:11434/api/tags > /dev/null 2>&1; then
MODEL=$(grep OLLAMA_MODEL .env 2>/dev/null | cut -d= -f2)
echo -e " ${GREEN}✓${NC} Ollama running — model: ${CYAN}${MODEL:-qwen2.5:14b-instruct-q5_K_M}${NC}"
return 0
fi
echo -e " ${YELLOW}!${NC} Ollama not running — LLM features disabled"
echo -e " Start it with: ${CYAN}ollama serve${NC} (or set LLM_ENABLED=false)"
return 0
}
check_schwab() {
RESULT=$($VENV -c "
from schwabagent.config import Config
from schwabagent.schwab_client import SchwabClient
c = SchwabClient(Config())
ok = c.authenticate()
acct = '✓' if c._client else '✗'
mkt = '✓' if c._market_client else '✗'
print(f'{acct}|{mkt}')
" 2>/dev/null)
ACCT=$(echo "$RESULT" | cut -d'|' -f1)
MKT=$(echo "$RESULT" | cut -d'|' -f2)
if [ "$ACCT" = "✓" ]; then
echo -e " ${GREEN}✓${NC} Schwab Account API connected"
else
echo -e " ${RED}✗${NC} Schwab Account API not reachable — run ${CYAN}./run.sh enroll${NC}"
fi
if [ "$MKT" = "✓" ]; then
echo -e " ${GREEN}✓${NC} Schwab Market Data API connected"
else
echo -e " ${RED}✗${NC} Schwab Market Data API not reachable — run ${CYAN}./run.sh enroll${NC}"
fi
[ "$ACCT" = "✓" ] || [ "$MKT" = "✓" ]
}
# ---------- commands ----------
cmd_enroll() {
echo ""
echo -e "${CYAN}=== Schwab OAuth Enrollment ===${NC}"
echo ""
echo " Which API to enroll?"
echo ""
echo " 1) Account API — positions, balances, orders"
echo " 2) Market API — quotes, price history, OHLCV"
echo " 3) Both"
echo ""
read -p " Choose [1/2/3]: " choice
case "$choice" in
1) WHICH="account" ;;
2) WHICH="market" ;;
3) WHICH="both" ;;
*) echo " Invalid choice."; return ;;
esac
$VENV -c "
import sys
from schwabagent.config import Config
from schwabagent.schwab_client import SchwabClient
c = SchwabClient(Config())
ok = c.enroll('$WHICH')
if not ok:
print(' \033[0;31m✗\033[0m Enrollment failed')
sys.exit(1)
print()
# Verify by loading from token files
c2 = SchwabClient(Config())
c2.authenticate()
if c2._client:
print(' \033[0;32m✓\033[0m Account API ready')
accounts = c2.get_all_accounts()
if accounts:
for a in accounts:
print(f' {a.account_number} value=\${a.total_value:,.2f} cash=\${a.cash_available:,.2f}')
else:
print(' \033[1;33m!\033[0m Account API not enrolled')
if c2._market_client and c2._market_client is not c2._client:
print(' \033[0;32m✓\033[0m Market Data API ready')
elif not c2._client:
print(' \033[1;33m!\033[0m Market Data API not enrolled')
"
echo ""
}
cmd_status() {
echo ""
echo -e "${CYAN}=== Schwab Agent Status ===${NC}"
echo ""
check_ollama
check_schwab
echo ""
echo -e " Config:"
echo -e " DRY_RUN = $(grep '^DRY_RUN' .env 2>/dev/null | cut -d= -f2)"
echo -e " WATCHLIST = $(grep '^WATCHLIST' .env 2>/dev/null | cut -d= -f2)"
echo -e " STRATEGIES = $(grep '^STRATEGIES' .env 2>/dev/null | cut -d= -f2)"
echo -e " MAX_EXPOSURE = \$$(grep '^MAX_TOTAL_EXPOSURE' .env 2>/dev/null | cut -d= -f2)"
echo -e " LLM = $(grep '^LLM_ENABLED' .env 2>/dev/null | cut -d= -f2)"
echo ""
echo -e " Live trading by strategy:"
for STRAT in ETF_ROTATION MOMENTUM MEAN_REVERSION TREND_FOLLOWING COMPOSITE ETF_SCALP CONVICTION_HOLD BROWN_MOMENTUM TICK_BREADTH AH_SNIPER; do
VAL=$(grep "^LIVE_${STRAT}" .env 2>/dev/null | cut -d= -f2)
if [ "$VAL" = "true" ]; then
echo -e " LIVE_${STRAT} = ${GREEN}true${NC}"
else
echo -e " LIVE_${STRAT} = ${YELLOW}false${NC}"
fi
done
echo ""
# Show account summary if connected
$VENV -c "
import sys
try:
from schwabagent.config import Config
from schwabagent.schwab_client import SchwabClient
c = SchwabClient(Config())
if not c.authenticate():
print(' Accounts: not authenticated')
sys.exit(0)
accounts = c.get_all_accounts()
print(f' Accounts ({len(accounts)}):')
for a in accounts:
print(f' {a.account_number} value=\${a.total_value:,.2f} cash=\${a.cash_available:,.2f} positions={len(a.positions)}')
except Exception as e:
print(f' Account query failed: {e}')
" 2>/dev/null
echo ""
}
cmd_scan() {
echo ""
echo -e "${CYAN}=== Scanning watchlist ===${NC}"
echo ""
$VENV -m schwabagent.cli --scan --dry-run
echo ""
}
cmd_signals() {
echo ""
echo -e "${CYAN}=== Signaler: trend signal changes ===${NC}"
echo ""
$VENV -m schwabagent.cli --signals --dry-run
echo ""
}
cmd_once() {
echo ""
echo -e "${CYAN}=== Dry-run: one scan cycle ===${NC}"
echo ""
$VENV -m schwabagent.cli --dry-run --once
}
cmd_loop() {
echo ""
echo -e "${CYAN}=== Dry-run: continuous mode (Ctrl+C to stop) ===${NC}"
echo ""
INTERVAL=$(grep '^SCAN_INTERVAL_SECONDS' .env 2>/dev/null | cut -d= -f2)
INTERVAL=${INTERVAL:-300}
$VENV -m schwabagent.cli --dry-run --interval="$INTERVAL"
}
cmd_live() {
echo ""
echo -e "${RED}╔═══════════════════════════════════════╗${NC}"
echo -e "${RED}║ WARNING: LIVE TRADING — REAL MONEY ║${NC}"
echo -e "${RED}╚═══════════════════════════════════════╝${NC}"
echo ""
echo -e "This will place real orders on your Charles Schwab account."
echo -e "Max exposure: \$$(grep MAX_TOTAL_EXPOSURE .env 2>/dev/null | cut -d= -f2)"
echo -e "Max per position: \$$(grep MAX_POSITION_VALUE .env 2>/dev/null | cut -d= -f2)"
echo ""
read -p "Type 'yes' to continue: " confirm
if [ "$confirm" != "yes" ]; then
echo "Aborted."
exit 0
fi
echo ""
INTERVAL=$(grep '^SCAN_INTERVAL_SECONDS' .env 2>/dev/null | cut -d= -f2)
INTERVAL=${INTERVAL:-300}
$VENV -m schwabagent.cli --live --interval="$INTERVAL"
}
cmd_pnl() {
echo ""
echo -e "${CYAN}=== Schwab Agent P&L Summary ===${NC}"
echo ""
$VENV -c "
import json, os
from pathlib import Path
state_dir = Path(os.environ.get('STATE_DIR', '~/.schwagent')).expanduser()
# Strategy P&L
pnl_file = state_dir / 'strategy_pnl.json'
if pnl_file.exists():
data = json.loads(pnl_file.read_text())
print(f' {\"Strategy\":<22} {\"Trades\":>8} {\"Realized P&L\":>14} {\"Wins\":>6} {\"Losses\":>7} {\"Win%\":>6}')
print(' ' + '-' * 68)
total_pnl = 0.0
for strat, d in sorted(data.items()):
wr = d['wins'] / d['trades'] * 100 if d['trades'] > 0 else 0
sign = '+' if d['realized_pnl'] >= 0 else ''
print(f' {strat:<22} {d[\"trades\"]:>8} {sign}{d[\"realized_pnl\"]:>13.2f} {d[\"wins\"]:>6} {d[\"losses\"]:>7} {wr:>5.1f}%')
total_pnl += d['realized_pnl']
print(' ' + '-' * 68)
sign = '+' if total_pnl >= 0 else ''
print(f' {\"TOTAL\":<22} {\"\":>8} {sign}{total_pnl:>13.2f}')
else:
print(' No P&L data yet.')
# Recent trades
hist = state_dir / 'trade_history.jsonl'
if hist.exists():
lines = [l for l in hist.read_text().strip().split('\n') if l]
print(f'\n Recent trades ({len(lines)} total):')
print(f' {\"Time\":<20} {\"Strategy\":<18} {\"Symbol\":<8} {\"Side\":<5} {\"Qty\":>5} {\"Price\":>8} {\"Value\":>10}')
print(' ' + '-' * 80)
for line in lines[-20:]:
t = json.loads(line)
ts = t.get('timestamp', '')[:19].replace('T', ' ')
value = t.get('quantity', 0) * t.get('price', 0)
print(f' {ts:<20} {t.get(\"strategy\",\"\"):<18} {t.get(\"symbol\",\"\"):<8} {t.get(\"side\",\"\"):<5} {t.get(\"quantity\",0):>5} {t.get(\"price\",0):>8.2f} {value:>10.2f}')
else:
print('\n No trade history yet.')
print()
" 2>/dev/null
}
cmd_backtest() {
STRATEGY="${2:-all}"
START="${3:-2020-01-01}"
END="${4:-2025-12-31}"
echo ""
echo -e "${CYAN}=== Backtesting: ${STRATEGY} (${START} → ${END}) ===${NC}"
echo ""
$VENV -c "
from schwabagent.backtest import Backtester, BacktestConfig
symbols = ['AAPL','MSFT','GOOGL','AMZN','NVDA','META','TSLA','JPM','V','UNH']
strategies = ['${STRATEGY}'] if '${STRATEGY}' != 'all' else ['momentum','mean_reversion','trend_following','composite']
for strat in strategies:
config = BacktestConfig(
strategy=strat,
symbols=symbols,
start='${START}',
end='${END}',
initial_capital=100000,
data_path='data/sp500_stocks.csv',
)
bt = Backtester(config)
result = bt.run()
result.print_report()
print()
"
}
cmd_ta() {
# Technical indicators via the `ta` library.
# Usage:
# ./run.sh ta → list all available indicators
# ./run.sh ta list → list all indicators (alias)
# ./run.sh ta <indicator> <symbol> → compute for a Schwab symbol
# ./run.sh ta <indicator> <symbol> days=N
SUB="${2:-list}"
SYMBOL="${3:-}"
shift 3 2>/dev/null || true
DAYS="180"
for arg in "$@"; do
KEY="${arg%%=*}"
VAL="${arg#*=}"
case "$KEY" in
days) DAYS="$VAL" ;;
esac
done
if [ "$SUB" = "list" ] || [ -z "$SUB" ]; then
$VENV -c "
from schwabagent.ta_indicators import list_indicators, indicator_names
print()
print(f' {len(indicator_names())} technical indicators available:')
print()
for cat, names in list_indicators().items():
print(f' ${CYAN}{cat}${NC}')
for n in names:
print(f' - {n}')
print()
print(' Usage: ./run.sh ta <indicator> <symbol> [days=N]')
print()
"
return
fi
if [ -z "$SYMBOL" ]; then
echo " Usage: ./run.sh ta <indicator> <symbol> [days=N]"
return 1
fi
echo ""
echo -e "${CYAN}=== ${SUB} on ${SYMBOL} (${DAYS}d) ===${NC}"
echo ""
$VENV -c "
import pandas as pd
from schwabagent.config import Config
from schwabagent.schwab_client import SchwabClient
from schwabagent.ta_indicators import compute
config = Config()
client = SchwabClient(config)
if not client.authenticate():
print(' Schwab API not authenticated — run ./run.sh enroll first.')
import sys; sys.exit(1)
try:
df = client.get_ohlcv('${SYMBOL}', days=int('${DAYS}'))
except Exception as e:
print(f' Error fetching {'\"'}${SYMBOL}{'\"'}: {e}')
import sys; sys.exit(1)
if df is None or df.empty:
print(f' No data for ${SYMBOL}')
import sys; sys.exit(1)
print(f' Fetched {len(df)} bars ({df.index[0].date()} → {df.index[-1].date()})')
print()
try:
result = compute(df, '${SUB}')
except ValueError as e:
print(f' {e}')
import sys; sys.exit(1)
# Show the last 10 values
if isinstance(result, pd.Series):
print(f' Last 10 values:')
for idx, val in result.tail(10).items():
if pd.isna(val):
print(f' {idx.date()} {val}')
else:
print(f' {idx.date()} {val:>12.4f}')
else:
print(f' Last 10 values ({len(result.columns)} columns):')
print(result.tail(10).to_string(float_format=lambda x: f'{x:>10.4f}'))
print()
"
}
cmd_optimize() {
# Portfolio optimization via PyPortfolioOpt.
# Usage:
# ./run.sh optimize <tickers> # max Sharpe, $100k, 1yr data
# ./run.sh optimize SPY,QQQ,TLT,GLD method=min_volatility
# ./run.sh optimize SPY,QQQ,TLT,GLD capital=50000 days=250
# ./run.sh optimize SPY,QQQ,TLT,GLD method=hrp
TICKERS="${2:-}"
shift 2 2>/dev/null || true
if [ -z "$TICKERS" ]; then
echo ""
echo " Portfolio optimization via PyPortfolioOpt"
echo ""
echo -e " ${CYAN}./run.sh optimize <tickers> [method=<m>] [capital=<amount>] [days=<n>] [rate=<r>]${NC}"
echo ""
echo " tickers Comma-separated symbols (e.g. SPY,QQQ,TLT,GLD)"
echo " method max_sharpe (default) | min_volatility | hrp"
echo " capital Total portfolio value in dollars (default: 100000)"
echo " days Calendar days of history to fetch (default: 365)"
echo " rate Risk-free rate as decimal (default: 0.04)"
echo ""
echo " Example:"
echo " ./run.sh optimize SPY,QQQ,IWM,EFA,TLT,GLD,VNQ method=max_sharpe capital=250000"
echo ""
return
fi
METHOD="max_sharpe"
CAPITAL="100000"
DAYS="365"
RATE="0.04"
for arg in "$@"; do
KEY="${arg%%=*}"
VAL="${arg#*=}"
case "$KEY" in
method) METHOD="$VAL" ;;
capital) CAPITAL="$VAL" ;;
days) DAYS="$VAL" ;;
rate) RATE="$VAL" ;;
esac
done
echo ""
echo -e "${CYAN}=== Portfolio optimization: ${TICKERS} (${METHOD}) ===${NC}"
echo ""
$VENV -c "
from schwabagent.config import Config
from schwabagent.schwab_client import SchwabClient
from schwabagent.portfolio_optimizer import optimize_portfolio, format_report
config = Config()
client = SchwabClient(config)
if not client.authenticate():
print(' Schwab API not authenticated — run ./run.sh enroll first.')
import sys; sys.exit(1)
tickers = [t.strip().upper() for t in '${TICKERS}'.split(',') if t.strip()]
days = int('${DAYS}')
print(f' Fetching {days}d of history for {len(tickers)} symbols...')
prices = {}
for t in tickers:
try:
df = client.get_ohlcv(t, days=days)
if df is not None and not df.empty:
prices[t] = df
print(f' {t:<6} ok ({len(df)} bars)')
else:
print(f' {t:<6} ${RED}empty${NC}')
except Exception as e:
print(f' {t:<6} ${RED}error: {e}${NC}')
if len(prices) < 2:
print(' Not enough data to optimize.')
import sys; sys.exit(1)
print()
result = optimize_portfolio(
prices,
method='${METHOD}',
total_value=float('${CAPITAL}'),
risk_free_rate=float('${RATE}'),
)
print(format_report(result))
"
}
cmd_options() {
# Options pricing and strategy analysis.
# Usage:
# ./run.sh options price spot=500 strike=500 dte=30 iv=0.20 type=call
# ./run.sh options iv price=12.5 spot=500 strike=500 dte=30 type=call
# ./run.sh options strategy iron_condor puts=460,470 calls=530,540 premiums=0.8,1.5,1.6,0.75
#
# Strategies supported: long_call, long_put, long_straddle, long_strangle,
# bull_call, bear_put, bull_put, bear_call, iron_condor, long_butterfly,
# covered_call, protective_put
SUB="${2:-help}"
shift 2 2>/dev/null || true
if [ "$SUB" = "help" ] || [ -z "$SUB" ]; then
cat <<EOF
Options pricing and strategy analysis
${CYAN}./run.sh options price spot=<S> strike=<K> dte=<days> iv=<vol> type=<call|put> [rate=<r>]${NC}
Black-Scholes price and Greeks for a single option.
Example: ./run.sh options price spot=500 strike=505 dte=30 iv=0.20 type=call
${CYAN}./run.sh options iv price=<P> spot=<S> strike=<K> dte=<days> type=<call|put> [rate=<r>]${NC}
Implied volatility from an observed market price.
Example: ./run.sh options iv price=3.50 spot=500 strike=505 dte=30 type=call
${CYAN}./run.sh options strategy <name> <args>${NC}
Multi-leg strategy analysis (payoff, max profit/loss, breakevens).
Named strategies (all take --premiums= as a comma list in leg order):
long_call strike=100 premium=5
long_put strike=100 premium=5
long_straddle strike=100 call=5 put=5
long_strangle call_strike=110 put_strike=90 call=2 put=2
bull_call long=100 short=110 long_prem=6 short_prem=2
bear_put long=100 short=90 long_prem=5 short_prem=1
bull_put short=100 long=90 short_prem=4 long_prem=1
bear_call short=100 long=110 short_prem=4 long_prem=1
iron_condor pl=90 ps=95 cs=105 cl=110 pl_prem=0.5 ps_prem=1.5 cs_prem=1.5 cl_prem=0.5
long_butterfly lower=95 mid=100 upper=105 lower_prem=6 mid_prem=3 upper_prem=1
covered_call strike=110 call_prem=2 stock=100
protective_put strike=95 put_prem=3 stock=100
EOF
return
fi
# Build a Python one-liner that reads the positional args into kwargs
ARGS_PY=""
for arg in "$@"; do
KEY="${arg%%=*}"
VAL="${arg#*=}"
ARGS_PY="${ARGS_PY} '${KEY}': '${VAL}',\n"
done
case "$SUB" in
price)
$VENV -c "
from schwabagent.options import bs_price_and_greeks
args = {
$(printf "${ARGS_PY}")
}
spot = float(args['spot'])
strike = float(args['strike'])
dte = float(args['dte'])
iv = float(args['iv'])
opt_type = args['type'].lower()
rate = float(args.get('rate', '0.05'))
T = dte / 365.0
r = bs_price_and_greeks(spot, strike, T, rate, iv, opt_type)
print()
print(f' {opt_type.upper()} spot=\${spot:.2f} strike=\${strike:.2f} dte={int(dte)}d iv={iv:.2%}')
print()
for k, v in r.items():
print(f' {k:6} = {v}')
print()
"
;;
iv)
$VENV -c "
from schwabagent.options import implied_volatility
args = {
$(printf "${ARGS_PY}")
}
price = float(args['price'])
spot = float(args['spot'])
strike = float(args['strike'])
dte = float(args['dte'])
opt_type = args['type'].lower()
rate = float(args.get('rate', '0.05'))
T = dte / 365.0
iv = implied_volatility(price, spot, strike, T, rate, opt_type)
print()
if iv is None:
print(' Error: market price is outside arbitrage bounds')
else:
print(f' Implied volatility: {iv:.4f} ({iv*100:.2f}%)')
print()
"
;;
strategy)
# First positional arg after 'strategy' is the strategy name
STRAT="${1:-}"
shift || true
ARGS_PY=""
for arg in "$@"; do
KEY="${arg%%=*}"
VAL="${arg#*=}"
ARGS_PY="${ARGS_PY} '${KEY}': '${VAL}',\n"
done
$VENV -c "
from schwabagent import options as O
args = {
$(printf "${ARGS_PY}")
}
strat = '${STRAT}'
try:
if strat == 'long_call':
legs = O.long_call(float(args['strike']), float(args['premium']))
elif strat == 'long_put':
legs = O.long_put(float(args['strike']), float(args['premium']))
elif strat == 'long_straddle':
legs = O.long_straddle(float(args['strike']), float(args['call']), float(args['put']))
elif strat == 'long_strangle':
legs = O.long_strangle(float(args['call_strike']), float(args['put_strike']), float(args['call']), float(args['put']))
elif strat == 'bull_call':
legs = O.bull_call_spread(float(args['long']), float(args['short']), float(args['long_prem']), float(args['short_prem']))
elif strat == 'bear_put':
legs = O.bear_put_spread(float(args['long']), float(args['short']), float(args['long_prem']), float(args['short_prem']))
elif strat == 'bull_put':
legs = O.bull_put_spread(float(args['short']), float(args['long']), float(args['short_prem']), float(args['long_prem']))
elif strat == 'bear_call':
legs = O.bear_call_spread(float(args['short']), float(args['long']), float(args['short_prem']), float(args['long_prem']))
elif strat == 'iron_condor':
legs = O.iron_condor(
float(args['pl']), float(args['ps']), float(args['cs']), float(args['cl']),
float(args['pl_prem']), float(args['ps_prem']), float(args['cs_prem']), float(args['cl_prem']),
)
elif strat == 'long_butterfly':
legs = O.long_butterfly(
float(args['lower']), float(args['mid']), float(args['upper']),
float(args['lower_prem']), float(args['mid_prem']), float(args['upper_prem']),
)
elif strat == 'covered_call':
legs = O.covered_call(float(args['strike']), float(args['call_prem']), float(args['stock']))
elif strat == 'protective_put':
legs = O.protective_put(float(args['strike']), float(args['put_prem']), float(args['stock']))
else:
print(f' Unknown strategy: {strat}')
import sys; sys.exit(1)
# Determine spot range from the legs
strikes = [leg.strike for leg in legs if leg.strike > 0]
if strikes:
lo = min(strikes) * 0.85
hi = max(strikes) * 1.15
else:
lo, hi = 80, 120
m = O.strategy_metrics(legs, spot_range=(lo, hi), n_points=5001)
print()
print(f' ${CYAN}{strat}${NC}')
print()
print(f' Legs ({len(legs)}):')
for leg in legs:
sign = '+' if leg.side == 'long' else '-'
print(f' {sign} {leg.quantity}x {leg.option_type:4} strike=\${leg.strike:.2f} @ \${leg.premium:.2f}')
print()
credit_or_debit = 'debit' if m['net_debit'] > 0 else 'credit'
print(f' Net {credit_or_debit:7} \${abs(m[\"net_debit\"]):.2f}')
print(f' Max profit \${m[\"max_profit\"]:.2f}')
print(f' Max loss \${m[\"max_loss\"]:.2f}')
if m['breakevens']:
bes = ', '.join(f'\${b:.2f}' for b in m['breakevens'])
print(f' Breakevens {bes}')
print()
except (KeyError, ValueError) as e:
print(f' Error: {e}')
import sys; sys.exit(1)
"
;;
*)
echo " Unknown subcommand: $SUB"
echo " Try: ./run.sh options help"
return 1
;;
esac
}
cmd_validate() {
# Backtest + statistical validation (Monte Carlo, Bootstrap, Walk-Forward).
# Usage: ./run.sh validate <strategy> [start] [end]
STRATEGY="${2:-momentum}"
START="${3:-2020-01-01}"
END="${4:-2025-12-31}"
echo ""
echo -e "${CYAN}=== Validating: ${STRATEGY} (${START} → ${END}) ===${NC}"
echo ""
$VENV -c "
from schwabagent.backtest import Backtester, BacktestConfig
from schwabagent.backtest_validation import run_validation, format_report
symbols = ['AAPL','MSFT','GOOGL','AMZN','NVDA','META','TSLA','JPM','V','UNH']
config = BacktestConfig(
strategy='${STRATEGY}',
symbols=symbols,
start='${START}',
end='${END}',
initial_capital=100000,
data_path='data/sp500_stocks.csv',
)
bt = Backtester(config)
result = bt.run()
result.print_report()
print()
print('-' * 60)
print()
if len(result.equity_curve) < 10:
print(' Not enough equity observations for validation.')
else:
validation_results = run_validation(
equity_curve=result.equity_curve,
trades=result.trades,
n_simulations=1000,
n_bootstrap=1000,
n_windows=5,
)
print(format_report(validation_results))
"
}
cmd_dream() {
echo ""
echo -e "${CYAN}=== Dreamcycle — autonomous research cycle ===${NC}"
echo ""
$VENV -c "
from schwabagent.config import Config
from schwabagent.runner import AgentRunner
config = Config()
runner = AgentRunner(config)
result = runner.dreamcycle.run_once()
print(f' Duration: {result.duration_seconds:.1f}s')
print(f' Phases OK: {result.phases_completed}')
if result.phases_failed:
print(f' Phases FAILED: {result.phases_failed}')
print(f' Signals recorded: {result.signals_recorded}')
print(f' Drift alerts: {result.drift_alerts}')
print(f' Auto-tune actions: {result.auto_tune_actions}')
print(f' Position mismatches: {result.position_mismatches}')
print(f' Digest sent: {result.digest_sent}')
if result.errors:
print(f' Errors:')
for e in result.errors:
print(f' - {e}')
print()
"
}
cmd_feedback() {
DAYS="${2:-30}"
$VENV -c "
from schwabagent.config import Config
from schwabagent.feedback import FeedbackLoop
fb = FeedbackLoop(Config())
# Strategy summary
summary = fb.get_strategy_summary(days=${DAYS})
if not summary:
print(' No signal data yet. Run scans to populate.')
exit(0)
print()
print(f' Signal Feedback — last ${DAYS} days')
print()
print(f' {\"Strategy\":<22} {\"Signals\":>8} {\"Resolved\":>9} {\"Wins\":>6} {\"Losses\":>7} {\"Win%\":>6} {\"Avg P&L\":>10} {\"Total P&L\":>11}')
print(' ' + '-' * 88)
for strat, d in sorted(summary.items()):
total = d['total_signals']
resolved = d['resolved'] or 0
wins = d['wins'] or 0
losses = d['losses'] or 0
wr = wins / resolved * 100 if resolved > 0 else 0
avg = d['avg_pnl'] or 0
total_pnl = d['total_pnl'] or 0
print(f' {strat:<22} {total:>8} {resolved:>9} {wins:>6} {losses:>7} {wr:>5.1f}% {avg:>+10.2f} {total_pnl:>+11.2f}')
print()
# Drift alerts
alerts = fb.get_drift_alerts(days=7)
if alerts:
print(' Drift Alerts (last 7 days):')
for a in alerts:
print(f' [{a[\"alert_level\"].upper()}] {a[\"strategy\"]}: {a[\"metric\"]} '
f'baseline={a[\"baseline_value\"]:.1f} → current={a[\"current_value\"]:.1f} '
f'({a[\"deviation_pct\"]:+.1f}%)')
print()
# Calibration
print(' Calibration by signal type:')
cal = fb.calibrate_all(days=${DAYS})
for strat, signals in sorted(cal.items()):
if not signals:
continue
print(f' {strat}:')
for sig, c in sorted(signals.items()):
if c['total'] < 3:
continue
pf = f'{c[\"profit_factor\"]:.2f}' if c['profit_factor'] != float('inf') else 'inf'
print(f' {sig:<14} n={c[\"total\"]:>4} wr={c[\"win_rate\"]:>5.1f}% pf={pf:>6} avg=\${c[\"avg_pnl\"]:>+8.2f}')
print()
"
}
cmd_skills() {
$VENV -c "
from schwabagent.skills import SkillsManager
mgr = SkillsManager()
skills = mgr.list_skills()
if not skills:
print(' No skills found in ~/.schwagent/skills/')
exit(0)
# Group by category
cats = {}
for s in skills:
cats.setdefault(s.category or '(uncategorized)', []).append(s)
for cat, items in sorted(cats.items()):
print(f'\n {cat}/')
for s in items:
tags = ', '.join(s.tags[:3]) if s.tags else ''
tag_str = f' [{tags}]' if tags else ''
print(f' {s.name:<24} {s.description[:60]}{tag_str}')
print()
"
}
cmd_sec() {
SYMBOL="${2:-AAPL}"
ACTION="${3:-filings}"
$VENV -c "
import sys
from schwabagent.sec import SECAnalyzer
from schwabagent.config import Config
sec = SECAnalyzer(Config())
symbol = '${SYMBOL}'.upper()
action = '${ACTION}'
if action == 'filings':
for form in ['10-K', '10-Q', '8-K']:
filings = sec.get_filings(symbol, form=form, limit=3)
if filings:
print(f'\n {form}:')
for f in filings:
print(f' {f.filing_date} {f.description[:60] if f.description else f.accession_number}')
elif action == 'analyze':
print(f' Analyzing latest 10-K for {symbol}...')
result = sec.analyze_filing(symbol, form='10-K')
print(f'\n Filing date: {result.filing_date}')
print(f' Text length: {result.raw_text_length:,} chars')
print(f' Sections: {result.sections_extracted}')
print(f'\n Summary: {result.summary}')
print(f'\n Financials: {result.key_financials}')
print(f'\n Risks: {result.risk_assessment}')
print(f'\n Sentiment: {result.sentiment}')
if result.actionable_insights:
print(f'\n Insights:')
for ins in result.actionable_insights:
print(f' - {ins}')
elif action == 'risks':
print(f' Extracting risk factors for {symbol}...')
risks = sec.extract_risk_factors(symbol)
print(f'\n{risks}')
elif action == 'compare':
print(f' Comparing last two 10-Q filings for {symbol}...')
comparison = sec.compare_filings(symbol, form='10-Q')
print(f'\n{comparison}')
elif action == 'scan':
symbols = '${SYMBOL}'.split(',')
print(f' Scanning recent 8-K filings for {symbols}...')
results = sec.quick_scan(symbols, form='8-K')
for r in results[:20]:
print(f' {r[\"date\"]} {r[\"symbol\"]} {r[\"form\"]} {r[\"description\"][:60]}')
else:
print(f' Unknown action: {action}')
print(f' Usage: ./run.sh sec SYMBOL [filings|analyze|risks|compare|scan]')
"
}
cmd_pf() {
SYMBOL="${2:-SPY}"
shift 2 2>/dev/null || shift 1 2>/dev/null || true
# Parse optional flags
BOX="0.01"; REV="3"; DUR="1.0"; PER="2.0"; METHOD="HL"; STYLE="True"; TRENDS="True"; META="False"
while [ $# -gt 0 ]; do
case "$1" in
-b|--box-size) BOX="$2"; shift 2 ;;
-r|--reversal) REV="$2"; shift 2 ;;
-d|--duration) DUR="$2"; shift 2 ;;
-p|--period) PER="$2"; shift 2 ;;
-m|--method) METHOD="$2"; shift 2 ;;
--no-style) STYLE="False"; shift ;;
--no-trends) TRENDS="False"; shift ;;
--meta) META="True"; shift ;;
*) shift ;;
esac
done
$VENV -c "
from schwabagent.config import Config
from schwabagent.schwab_client import SchwabClient
from schwabagent.pf import print_pf_chart
config = Config()
client = SchwabClient(config)
if not client.authenticate():
print('Auth failed')
exit(1)
print_pf_chart(
symbol='${SYMBOL}'.upper(),
client=client,
box_size=${BOX},
reversal=${REV},
duration=${DUR},
period=${PER},
method='${METHOD}',
style=${STYLE},
trend_lines=${TRENDS},
show_meta=${META},
)
"
}
cmd_web() {
echo -e "\n${CYAN}Starting web dashboard on port 8898...${NC}\n"
$VENV -m schwabagent.cli --web
}
cmd_hilo() {
# High/low scanner — find stocks hitting fresh N-day highs and lows
# across the configured universe (watchlist ∪ ah_sniper ∪ momentum).
# Cached to ~/.schwagent/hilo_cache.csv once per ET trading day.
# Usage: ./run.sh hilo [--refresh] [--windows 5,20,60,252]
shift || true
$VENV -m schwabagent.hilo "$@"
}
cmd_earnings() {
# Earnings calendar scraper — Briefing.com, ~5 weeks of US releases.
# Filters to watchlist ∪ ah_sniper ∪ momentum by default.
# Cached to ~/.schwagent/earnings_calendar.json once per ET day.
# Usage: ./run.sh earnings [--refresh] [--all] [--days 7] [--symbol AAPL]
shift || true
$VENV -m schwabagent.scrapers.earnings_calendar "$@"
}
cmd_autoresearch() {
# Full validation pipeline across every strategy: backtest + Monte Carlo +
# bootstrap Sharpe CI + walk-forward + SPY baseline + LLM critique. Writes
# per-strategy markdown reports and a ranked leaderboard under
# ~/.schwagent/research/. Pass --force-fetch to rebuild the data CSV
# even if it's fresh.
FORCE_FLAG=""
if [ "${2:-}" = "--force-fetch" ] || [ "${2:-}" = "-f" ]; then
FORCE_FLAG="--autoresearch-force-fetch"
fi
$VENV -m schwabagent.cli --autoresearch $FORCE_FLAG
}
cmd_ref() {
# Reference skills (bundled with package, ported from vibe-trading).
# Usage: ./run.sh ref → list all skills grouped by category
# ./run.sh ref <name> → show full content for a specific skill
NAME="${1:-}"
$VENV -c "
import sys
from schwabagent.intelligence import SkillsLoader
loader = SkillsLoader()
name = '${NAME}'
if name:
print(loader.get_content(name))
else:
print(f'\n {len(loader.skills)} reference skills loaded\n')
print(loader.get_descriptions())
print()
"
}
cmd_swarm() {
# Multi-agent committee workflows (ported from vibe-trading).
# Usage:
# ./run.sh swarm → list available presets
# ./run.sh swarm <preset> → describe a preset
# ./run.sh swarm <preset> key=val ... → execute a preset
#
# Example: