BitFinTrainer / README.md
luohoa97's picture
Deploy BitNet-Transformer Trainer
d5b7ee9 verified
---
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