feat: Alpaca US stock trader integration (Tasks 8-11)

- trader/alpaca/: Full Trader interface implementation for Alpaca
  - Paper/Live trading support (market orders, stop/limit)
  - GetBalance, GetPositions, GetMarketPrice via Alpaca API
  - Fractional share support (4 decimal places)
  - Commission-free trading

- Agent trade routing: stock symbols (AAPL, TSLA) → Alpaca
  - isStockSymbol() heuristic to distinguish crypto vs stock
  - execute_trade tool updated for stock buy/sell semantics
  - get_market_price works for both crypto and stocks

- TraderManager + API: Alpaca registered as exchange type
  - Factory case in auto_trader.go
  - Config mapping in trader_manager.go
  - Temp trader creation in handler_trader.go for balance query

- Frontend: PositionsPanel distinguishes stock vs crypto
  - US flag emoji for stock positions
  - Dollar prefix for stock PnL/prices
  - 'Shares' label instead of 'Qty' for stocks

- System prompts updated for stock trading capabilities
This commit is contained in:
shinchan-zhai
2026-03-23 17:57:14 +08:00
parent 76a7b88ba8
commit d0aebe9f18
9 changed files with 720 additions and 17 deletions

View File

@@ -16,6 +16,7 @@ import (
"nofx/trader/bybit"
"nofx/trader/gate"
"nofx/trader/hyperliquid"
"nofx/trader/alpaca"
"nofx/trader/indodax"
"nofx/trader/kucoin"
"nofx/trader/lighter"
@@ -66,6 +67,11 @@ type AutoTraderConfig struct {
IndodaxAPIKey string
IndodaxSecretKey string
// Alpaca API configuration (US stock trading)
AlpacaAPIKey string
AlpacaAPISecret string
AlpacaPaper bool // true = paper trading, false = live
// Hyperliquid configuration
HyperliquidPrivateKey string
HyperliquidWalletAddr string
@@ -286,6 +292,9 @@ func NewAutoTrader(config AutoTraderConfig, st *store.Store, userID string) (*Au
case "indodax":
logger.Infof("🏦 [%s] Using Indodax Spot trading", config.Name)
trader = indodax.NewIndodaxTrader(config.IndodaxAPIKey, config.IndodaxSecretKey)
case "alpaca":
logger.Infof("🏦 [%s] Using Alpaca US Stock trading (paper=%v)", config.Name, config.AlpacaPaper)
trader = alpaca.NewAlpacaTrader(config.AlpacaAPIKey, config.AlpacaAPISecret, config.AlpacaPaper)
default:
return nil, fmt.Errorf("unsupported trading platform: %s", config.Exchange)
}