File size: 6,743 Bytes
6977c60
d5b7ee9
 
 
 
6977c60
 
 
 
d5b7ee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: AI Trading Fusion - BitNet Transformer
emoji: πŸ“ˆ
colorFrom: blue
colorTo: green
sdk: docker
pinned: false
---

# trading-cli
 
A full-screen TUI AI trading application powered by **FinBERT** sentiment analysis and **Alpaca** paper trading.
 
```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ TRADING CLI - Paper Trading Mode    Cash: $98,234.50    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ [1] Dashboard  [2] Watchlist  [3] Portfolio             β”‚
β”‚ [4] Trades     [5] Sentiment  [6] Config  [q] Quit      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ MARKET STATUS: ● OPEN   Last Updated: 14:23:45 EST      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```
 
---
 
## Features
 
| Feature | Details |
|---|---|
| Full-screen TUI | Textual-based, single command launch |
| FinBERT sentiment | Local inference, ProsusAI/finbert |
| Paper trading | Alpaca paper API (or built-in demo mode) |
| Live prices | Alpaca market data + yfinance fallback |
| Hybrid signals | 0.6 Γ— technical + 0.4 Γ— sentiment |
| Persistent state | SQLite (trades, watchlist, sentiment cache) |
| Demo mode | Works without any API keys |
 
---
 
## Quick Start
 
### 1. Install uv (if not already installed)
```bash
curl -LsSf https://astral.sh/uv/install.sh | sh
```
 
### 2. Clone and install
```bash
git clone https://github.com/luohoa97/ai-trading.git
cd ai-trading
uv sync
```
 
### 3. Run
```bash
uv run trading-cli
```
 
On first launch, FinBERT (~500 MB) downloads from HuggingFace and is cached locally.
The app starts in **Demo Mode** automatically if no Alpaca keys are configured.
 
---
 
## Alpaca Paper Trading Setup (optional)
 
1. Sign up at [alpaca.markets](https://alpaca.markets) β€” free, no credit card needed
2. Generate paper trading API keys in the Alpaca dashboard
3. Open Config in the app (`6`), enter your keys, press `Ctrl+S`
 
The app always uses paper trading endpoints β€” no real money is ever at risk.
 
---
 
## Configuration
 
Config file: `~/.config/trading-cli/config.toml`
 
```toml
alpaca_api_key    = "PKxxxxxxxxxxxx"
alpaca_api_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
alpaca_paper      = true
 
# Risk management
risk_pct        = 0.02    # 2% of portfolio per trade
max_drawdown    = 0.15    # halt trading at 15% drawdown
stop_loss_pct   = 0.05    # 5% stop-loss per position
max_positions   = 10
 
# Signal thresholds (hybrid score: -1 to +1)
signal_buy_threshold  =  0.5
signal_sell_threshold = -0.3

# Poll intervals (seconds)
poll_interval_prices    = 30
poll_interval_news      = 900
poll_interval_signals   = 300
poll_interval_positions = 60
```
 
---
 
## Keyboard Shortcuts
 
| Key | Action |
|---|---|
| `1`–`6` | Switch screens |
| `q` / `Ctrl+C` | Quit |
| `r` | Refresh current screen |
| `a` | Add symbol (Watchlist) |
| `d` | Delete selected symbol (Watchlist) |
| `x` | Close position (Portfolio) |
| `e` | Export trades to CSV (Trades) |
| `f` | Focus filter (Trades) |
| `Enter` | Submit symbol / confirm action |
| `Ctrl+S` | Save config (Config screen) |
 
---
 
## Screens
 
**1 β€” Dashboard**: Account balance, market status, live positions, real-time signal log.
 
**2 β€” Watchlist**: Add/remove symbols. See live prices, sentiment score, and BUY/SELL/HOLD signal per symbol.
 
**3 β€” Portfolio**: Full position detail from Alpaca. Press `x` to close a position via market order.
 
**4 β€” Trades**: Scrollable history with Alpaca `order_id`. Press `e` to export CSV.
 
**5 β€” Sentiment**: Type any symbol, press Enter β€” see FinBERT scores per headline and an aggregated gauge.
 
**6 β€” Config**: Edit API keys, thresholds, risk limits, toggle auto-trading.
 
---
 
## Trading Strategy
 
**Signal = 0.6 Γ— technical + 0.4 Γ— sentiment**
 
| Component | Calculation |
|---|---|
| `technical_score` | 0.5 Γ— SMA crossover (20/50) + 0.5 Γ— RSI(14) |
| `sentiment_score` | FinBERT weighted average on latest news |
| BUY | hybrid > +0.50 |
| SELL | hybrid < βˆ’0.30 |
 
In **manual mode** (default), signals appear in the log for review.
In **auto-trading mode** (Config β†’ toggle), market orders are submitted automatically.
 
---
 
## Project Structure
 
```
trading_cli/
β”œβ”€β”€ __main__.py           # Entry point: uv run trading-cli
β”œβ”€β”€ app.py                # Textual App, workers, screen routing
β”œβ”€β”€ config.py             # Load/save ~/.config/trading-cli/config.toml
β”œβ”€β”€ screens/
β”‚   β”œβ”€β”€ dashboard.py      # Screen 1 β€” main dashboard
β”‚   β”œβ”€β”€ watchlist.py      # Screen 2 β€” symbol watchlist
β”‚   β”œβ”€β”€ portfolio.py      # Screen 3 β€” positions & P&L
β”‚   β”œβ”€β”€ trades.py         # Screen 4 β€” trade history
β”‚   β”œβ”€β”€ sentiment.py      # Screen 5 β€” FinBERT analysis
β”‚   └── config_screen.py  # Screen 6 β€” settings editor
β”œβ”€β”€ widgets/
β”‚   β”œβ”€β”€ positions_table.py  # Reusable P&L table
β”‚   β”œβ”€β”€ signal_log.py       # Scrolling signal feed
β”‚   └── sentiment_gauge.py  # Visual [-1, +1] gauge
β”œβ”€β”€ sentiment/
β”‚   β”œβ”€β”€ finbert.py          # Singleton model, batch inference, cache
β”‚   └── aggregator.py       # Score aggregation + gauge renderer
β”œβ”€β”€ strategy/
β”‚   β”œβ”€β”€ signals.py          # SMA + RSI + sentiment hybrid signal
β”‚   └── risk.py             # Position sizing, stop-loss, drawdown
β”œβ”€β”€ execution/
β”‚   └── alpaca_client.py    # Real AlpacaClient + MockAlpacaClient
└── data/
    β”œβ”€β”€ market.py           # OHLCV via Alpaca / yfinance
    β”œβ”€β”€ news.py             # Headlines via Alpaca News / yfinance
    └── db.py               # SQLite schema + all queries
```
 
---
 
## Database
 
Location: `~/.config/trading-cli/trades.db`
 
| Table | Contents |
|---|---|
| `trades` | Every executed order with Alpaca `order_id` |
| `signals` | Every generated signal (executed or not) |
| `watchlist` | Monitored symbols |
| `sentiment_cache` | MD5(headline) β†’ label + score |
| `price_history` | OHLCV bars per symbol |
 
---
 
## Development
 
```bash
# Run app
uv run trading-cli
 
# Live logs
tail -f ~/.config/trading-cli/app.log
 
# Reset state
rm ~/.config/trading-cli/trades.db
rm ~/.config/trading-cli/config.toml
```# ai-trading