Spaces:
Runtime error
Runtime error
| 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 | |