blog.dopana

Back

Do you want to trade stocks from your terminal without opening a browser? Longbridge Terminal is a CLI tool built in Rust that lets you access market data, manage your portfolio, and execute trades directly from the command line. It’s specifically designed to work with AI agents, making automated trading easier than ever.

Why Longbridge Terminal?#

Traditional stock trading typically requires:

  • Web browser: Open app, login, navigate through multiple screens
  • Mobile app: Slow, not convenient for complex tasks
  • Spreadsheet: Manual updates, not real-time
  • Custom API: Write your own code, manage authentication

Longbridge Terminal solves these problems by providing a comprehensive CLI:

  • Real-time data: Quotes, depth, K-lines, options, warrants
  • Full trading: Buy, sell, cancel, modify orders
  • Portfolio management: Assets, positions, P/L tracking
  • AI-friendly: JSON output, tool-calling ready
  • Interactive TUI: Full-screen interface for monitoring

What is Longbridge Terminal?#

Think of Longbridge Terminal as a “swiss army knife” for stock trading. It’s a CLI that covers every Longbridge OpenAPI endpoint:

  • Market Data: Real-time quotes, depth, K-lines, options, warrants
  • Portfolio: Account balances, stock positions, fund positions
  • Trading: Order submission, modification, cancellation, execution history
  • Fundamentals: Financial reports, analyst ratings, valuation
  • News & Signals: News articles, regulatory filings, strategy signals

Built with Rust using:

  • Ratatui: TUI rendering framework
  • Tokio: Async runtime
  • Bevy ECS: Entity-Component-System architecture
  • Longbridge SDK: Official OpenAPI SDK

Installation#

Installation is simple - just one command:

macOS/Linux
brew install --cask longbridge/tap/longbridge-terminal
bash

Or use the install script:

macOS/Linux
curl -sSL https://github.com/longbridge/longbridge-terminal/raw/main/install | sh
bash

Windows:

iwr https://github.com/longbridge/longbridge-terminal/raw/main/install.ps1 | iex
powershell

Authentication#

Uses OAuth 2.0 - no manual token management required:

longbridge auth login # Opens browser for OAuth and saves token
longbridge auth logout # Clear saved token
longbridge check # Verify token, region, and API connectivity
bash

The CLI automatically detects your region (China Mainland uses .cn endpoints, others use global).

CLI Usage#

Command structure:

longbridge <command> [options]
bash

All commands support --format json for machine-readable output:

longbridge quote TSLA.US --format json
longbridge positions --format json | jq '.[] | {symbol, quantity}'
bash

Quotes - Market Data#

# Real-time quotes
longbridge quote TSLA.US 700.HK

# Level 2 order book depth
longbridge depth TSLA.US

# K-line (candlestick) data
longbridge kline TSLA.US --period day

# Intraday price and volume
longbridge intraday TSLA.US

# Static reference info
longbridge static TSLA.US NVDA.US
bash

Output example:

$ longbridge static TSLA.US NVDA.US
| Symbol  | Last    | Prev Close | Open    | High    | Low     | Volume    | Turnover        | Status |
|---------|---------|------------|---------|---------|---------|-----------|-----------------|--------|
| TSLA.US | 395.560 | 391.200    | 396.220 | 403.730 | 394.420 | 58068343  | 23138752546.000 | Normal |
| NVDA.US | 183.220 | 180.250    | 182.970 | 188.880 | 181.410 | 217307380 | 40023702698.000 | Normal |
bash

Trading - Execute Trades#

[!WARNING] Order commands are always dry-run on first run. They only preview, don’t actually place orders. You need to run again with --execute <CODE> to actually execute.

Portfolio - Asset Management#

# Asset overview
longbridge assets

# Portfolio overview
longbridge portfolio

# Current positions
longbridge positions

# Fund positions
longbridge fund-positions

# Profit analysis
longbridge profit-analysis
bash

Fundamentals - Financial Analysis#

# Financial reports
longbridge financial-report AAPL.US --kind IS

# Analyst ratings
longbridge institution-rating AAPL.US

# Valuation
longbridge valuation AAPL.US --indicator pe

# Dividend history
longbridge dividend AAPL.US
bash

TUI - Interactive Interface#

For real-time monitoring with a beautiful interface:

longbridge tui
bash

Features:

  • Real-time watchlist
  • Candlestick charts
  • Portfolio view
  • Stock search
  • Vim-like keybindings
  • Mouse support

AI Chat - LongbridgeAI#

Chat with AI for market analysis:

longbridge ai
bash

Features:

  • Streaming answers with Markdown
  • Charts and tables
  • Live quote cards
  • Command palette
  • Multi-line input
  • Session persistence

Architecture#

Longbridge TerminalCLI CommandsTUI InterfaceAI ChatJSON-RPC ServerQuote APITrade APIFundamentals APIRatatui TUIReal-time WebSocketLongbridgeAIMarkdown RenderingExternal AppsJSON-RPC 2.0Longbridge SDK

Embedding in Other Apps#

To integrate Longbridge Terminal into other apps (desktop widgets, dashboards):

longbridge serve
bash

This opens a JSON-RPC 2.0 server on stdin/stdout:

→ {"jsonrpc":"2.0","id":1,"method":"quote.quote","params":{"symbols":["700.HK"]}}
← {"jsonrpc":"2.0","id":1","result":[{"symbol":"700.HK","last_done":"445.600", ...}]}
→ {"jsonrpc":"2.0","id":2,"method":"quote.subscribe","params":{"symbols":["700.HK"]}}
← {"jsonrpc":"2.0","id":2","result":{"subscribed":[{"symbol":"700.HK"}]}}
← {"jsonrpc":"2.0","method":"quote.updated","params":{"symbol":"700.HK","last_done":"446.000"}}
json

AI Agent Integration#

Longbridge Terminal is specifically designed for AI agents. Install the skill:

npx skills add longbridge/developers
bash

Claude can query market data, run technical analysis, and manage trades:

claude> Show me TSLA and NVDA performance over the last 5 days
text

Claude will automatically run the necessary CLI commands and return results.

Shell Completion#

Enable tab-completion:

Bash
source <(longbridge completion bash)
bash
Zsh
source <(longbridge completion zsh)
bash
Fish
longbridge completion fish | source
bash

Advanced Features#

Grid Trading#

Automated trading strategy:

# Preview grid strategy
longbridge grid submit 700.HK --base-price 300 --upper-price 360 --lower-price 240 \
  --trigger-type percent --trigger-up 2 --trigger-down 2 --quantity 100

# Actually submit
longbridge grid submit 700.HK --base-price 300 ... --execute 123
bash

Recurring Investment (DCA)#

Auto-invest regularly:

# Create weekly DCA plan
longbridge dca create TSLA.US --amount 500 --frequency weekly --day-of-week mon

# View all plans
longbridge dca

# Pause plan
longbridge dca pause <PLAN_ID>
bash

Stock Screener#

Find stocks matching criteria:

# List strategies
longbridge screener strategies

# Run custom filter
longbridge screener search --market HK --filter filter_marketcap:100:1000 --filter filter_divyld:3
bash

Comparison with Other Solutions#

FeatureLongbridge TerminalWeb AppMobile AppCustom API
LanguageRust CLIWebMobileCustom
Real-time data✓✓✓✓
Scripting✓✗✗✓
AI integration✓✗✗Optional
TUI interface✓✗✗✗
Offline capabilityLimited✗Limited✓
Learning curveMediumLowLowHigh

When to Use Longbridge Terminal?#

Use when:

  • You prefer working in the terminal
  • Need scripting for automated trading
  • Want to integrate with AI agents
  • Need real-time monitoring with TUI
  • Are a developer or power user

Don’t use when:

  • You’re new to trading
  • Need a simple GUI
  • Not familiar with command line
  • Only need basic trading features

Conclusion#

Longbridge Terminal is a powerful tool for traders and developers who want to manage trading from the terminal. With the combination of CLI commands, TUI interface, and AI integration, it provides flexibility not available in web apps or mobile apps. If you’re a terminal user and want to trade stocks efficiently, Longbridge Terminal is worth considering.

References#