This is an unofficial SDK, currently for personal use only. Some functions in the Trade module have not been tested yet. Issues are welcome.
See NEWS.md for detailed release notes.
References:
using Pkg
Pkg.add(url="https://github.com/rzhli/LongBridge.jl")LongBridge supports two authentication methods:
OAuth 2.0 uses Bearer tokens without requiring HMAC signatures. Tokens are persisted locally and refreshed automatically.
Step 1: Register an OAuth client
curl -X POST https://openapi.longbridgeapp.com/oauth2/register \
-H "Content-Type: application/json" \
-d '{
"client_name": "My Application",
"redirect_uris": ["http://localhost:60355/callback"],
"grant_types": ["authorization_code", "refresh_token"]
}'Step 2: Build OAuth handle and create config
using LongBridge
oauth = OAuthBuilder("your-client-id") |> build(url -> run(`xdg-open $url`))
cfg = Config.from_oauth(oauth)On first run, this opens the browser for authorization. Subsequent runs use the cached token automatically.
Create a config.toml file:
# Required
app_key = "your_app_key"
app_secret = "your_app_secret"
access_token = "your_access_token"
token_expire_time = "2025-07-22T00:00:00" # ISO8601 format, UTC time
# Optional (uses China endpoints by default)
# http_url = "https://openapi.longportapp.com"
# quote_ws_url = "wss://openapi-quote.longportapp.com"
# trade_ws_url = "wss://openapi-trade.longportapp.com"using LongBridge
cfg = Config.from_toml()using LongBridge
cfg = Config.from_toml()
# Create and connect to QuoteContext
ctx = QuoteContext(cfg)
# Get basic static information for securities
resp = static_info(ctx, ["700.HK", "AAPL.US", "TSLA.US"])
# Get real-time quotes for securities (one-shot server snapshot)
quotes = quote_snapshot(ctx, ["GOOGL.US", "AAPL.US", "TSLA.US"])
# Get real-time option quotes
resp = option_quote(ctx, ["AAPL230317P160000.US"])
# Get real-time warrant quotes
resp = warrant_quote(ctx, ["14993.HK", "66642.HK"])
# Get market depth for a security
resp = depth(ctx, "700.HK")
# Get candlestick data
candlesticks_data = candlesticks(ctx, "GOOGL.US", CandlePeriod.SIXTY_MINUTE, 365)
# Get trade details for a security
trades_data = trades(ctx, "AAPL.US", 10)
# Get intraday data for a security
intraday_data = intraday(ctx, "700.HK")
# Get historical K-line data
using Dates
history_data = history_candlesticks_by_date(
ctx, "700.HK", CandlePeriod.DAY, AdjustType.NO_ADJUST; start_date=Date(2023, 1, 1), end_date=Date(2023, 2, 1)
)
# Get the list of expiry dates for an option chain
expiry_dates = option_chain_expiry_date_list(ctx, "AAPL.US")
# Get trading days for a market
trading_days_df = trading_days(ctx, Market.HK, Date(2025, 8, 1), Date(2025, 8, 30))
# Get capital flow for a security
capital_flow_data = capital_flow(ctx, "700.HK")
# Get market temperature
temp = market_temperature(ctx, Market.US)
# Get historical market temperature
history_temp = history_market_temperature(ctx, Market.US, Date(2025, 7, 1), Date(2025, 7, 31))
# Disconnect
disconnect!(ctx)using LongBridge, Dates
cfg = Config.from_toml()
# Create and connect to TradeContext
ctx = TradeContext(cfg)
# Get account balance
resp = account_balance(ctx)
# Get stock positions (optionally filter by symbol)
resp = stock_positions(ctx)
resp = stock_positions(ctx; symbol = "700.HK")
# Get today's orders
resp = today_orders(ctx)
# Get historical orders (date range via keyword args)
resp = history_orders(ctx; start_at = Date(2024, 1, 1), end_at = Date(2024, 2, 1))
# Get today's executions
resp = today_executions(ctx)
# Get historical executions
resp = history_executions(ctx; start_at = Date(2024, 1, 1), end_at = Date(2024, 2, 1))
# Submit an order via SubmitOrderOptions
resp = submit_order(
ctx, SubmitOrderOptions(
symbol = "700.HK",
order_type = OrderType.LO,
side = OrderSide.Buy,
submitted_quantity = 100,
time_in_force = TimeInForceType.Day,
submitted_price = 300.0,
)
)
# Modify an order
resp = replace_order(
ctx, ReplaceOrderOptions(
order_id = "709043056541253632",
submitted_quantity = 100,
submitted_price = 301.0,
)
)
# Cancel an order
resp = cancel_order(ctx, "709043056541253632")
# Disconnect
disconnect!(ctx)# 1. Define a callback function
function on_quote_callback(symbol::String, event::PushQuote)
println(symbol, event)
end
# 2. Set the callback
set_on_quote(ctx, on_quote_callback)
# 3. Subscribe to quotes (can choose different types: QUOTE, DEPTH, BROKERS, TRADE)
Quote.subscribe(ctx, ["GOOGL.US"], [SubType.DEPTH]; is_first_push=true)
# 4. Unsubscribe from quotes
Quote.unsubscribe(ctx, ["GOOGL.US"], [SubType.QUOTE, SubType.DEPTH])Config.from_toml(): Load configuration fromconfig.tomlfileConfig.from_oauth(oauth_handle): Create configuration from an OAuth handleOAuthBuilder(client_id) |> build(open_url_fn): Build an OAuth handle with browser-based authorizationQuoteContext(config): Create and connect toQuoteContextTradeContext(config): Create and connect toTradeContextdisconnect!(ctx): Disconnect from the server
static_info(ctx, symbols): Get basic static information for securitiesquote_snapshot(ctx, symbols): Get a one-shot snapshot of real-time stock quotes (server query)realtime_quote(ctx, symbol_or_symbols): Read the latest pushed quote from the local cache (requires priorsubscribe)option_quote(ctx, symbols): Get real-time option quoteswarrant_quote(ctx, symbols): Get real-time warrant quotesdepth(ctx, symbol): Get market depth data for a securitybrokers(ctx, symbol): Get broker queue for a securityparticipants(ctx): Get a list of broker seat IDstrades(ctx, symbol, count): Get trade details for a securityintraday(ctx, symbol): Get intraday data for a securityhistory_candlesticks_by_date(ctx, ...): Get historical K-line data by dateoption_chain_expiry_date_list(ctx, symbol): Get a list of expiry dates for an option chainwarrant_issuers(ctx): Get a list of warrant issuer IDswarrant_list(ctx, ...): Get a filtered list of warrantstrading_session(ctx): Get the trading session for each market for the current daytrading_days(ctx, market, start_date, end_date): Get trading days for a marketcapital_flow(ctx, symbol): Get capital flow for a security for the current daycapital_distribution(ctx, symbol): Get capital distribution for a security for the current daycandlesticks(ctx, symbol, period, count): Get candlestick datahistory_candlesticks_by_offset(ctx, ...): Get historical K-line data by offsetoption_chain_info_by_date(ctx, symbol, expiry_date): Get option chain information for a specific expiry datesubscriptions(ctx): Query currently subscribed securitiescalc_indexes(ctx, symbols): Get calculated indexesmarket_temperature(ctx, market): Get market temperaturehistory_market_temperature(ctx, market, start_date, end_date): Get historical market temperaturesecurity_list(ctx, market, category): Get a list of securities
set_on_quote(ctx, callback): Set the callback function for quote pushesset_on_depth(ctx, callback): Set the callback function for market depth pushesset_on_brokers(ctx, callback): Set the callback function for broker queue pushesset_on_trades(ctx, callback): Set the callback function for trade detail pushesset_on_candlestick(ctx, callback): Set the callback function for candlestick pushessubscribe(ctx, symbols, sub_types): Subscribe to quotesunsubscribe(ctx, symbols, sub_types): Unsubscribe from quotes
realtime_depth(ctx, symbol): Get cached depth data for subscribed symbolrealtime_brokers(ctx, symbol): Get cached broker queue for subscribed symbolrealtime_trades(ctx, symbol; count): Get cached trades for subscribed symbolrealtime_candlesticks(ctx, symbol, period; count): Get cached K-line data
subscribe_candlesticks(ctx, symbol, period; count): Subscribe and get initial K-line dataunsubscribe_candlesticks(ctx, symbol, period): Unsubscribe and clear cached data
create_watchlist_group(ctx, name; securities): Create a watchlist groupwatchlist(ctx): View watchlist groupsdelete_watchlist_group(ctx, group_id, with_securities): Delete a watchlistupdate_watchlist_group(ctx, group_id; name, securities, mode): Update a watchlist group
account_balance(ctx): Get account balancestock_positions(ctx; symbol): Get stock positions (optional symbol filter)fund_positions(ctx): Get fund positionstoday_orders(ctx; symbol, status, side): Get today's ordershistory_orders(ctx; symbol, status, side, start_at, end_at): Get historical orderstoday_executions(ctx; symbol): Get today's executionshistory_executions(ctx; symbol, start_at, end_at): Get historical executionsorder_detail(ctx, order_id): Get order detailsubmit_order(ctx, ::SubmitOrderOptions): Submit an orderreplace_order(ctx, ::ReplaceOrderOptions): Modify an ordercancel_order(ctx, order_id): Cancel an orderestimate_max_purchase_quantity(ctx, ::EstimateMaxPurchaseQuantityOptions): Estimate max purchase quantitycash_flow(ctx; start_at, end_at): Get cash flow recordsmargin_ratio(ctx, symbol): Get margin ratio for a securityset_on_order_changed(ctx, callback): Set the callback function for order status change pushesTrade.subscribe(ctx, topics::Vector{TopicType.T}): Subscribe to trade pushes (e.g.[TopicType.Private])Trade.unsubscribe(ctx, topics::Vector{TopicType.T}): Unsubscribe from trade pushes
FundamentalContext(config): Create context (HTTP-only, no disconnect needed)financial_report(ctx, symbol; kind, period): Full financial statements (IS/BS/CF/All)institution_rating(ctx, symbol): Analyst ratings (latest + summary, parallel fan-out)institution_rating_detail(ctx, symbol): Historical rating distributiondividend(ctx, symbol)/dividend_detail(ctx, symbol): Dividend history / detailsforecast_eps(ctx, symbol): Analyst EPS forecastsconsensus(ctx, symbol): Revenue / profit / EPS consensus vs actualvaluation(ctx, symbol)/valuation_history(ctx, symbol): PE/PB/PS/dividend yieldindustry_valuation(ctx, symbol)/industry_valuation_dist(ctx, symbol): Peer comparisoncompany(ctx, symbol)/executive(ctx, symbol)/shareholder(ctx, symbol)/fund_holder(ctx, symbol)corp_action(ctx, symbol): Corporate actions (dividends, splits, buybacks)invest_relation(ctx, symbol),operating(ctx, symbol),buyback(ctx, symbol),ratings(ctx, symbol)
market_status(ctx): Market open/close status across regionsbroker_holding(ctx, symbol, period)/broker_holding_detail(ctx, symbol)/broker_holding_daily(ctx, symbol, broker_id)ah_premium(ctx, symbol, period, count)/ah_premium_intraday(ctx, symbol): A/H premium klines & intradaytrade_stats(ctx, symbol): Buy/sell/neutral trade statisticsanomaly(ctx, market): Market anomaly eventsconstituent(ctx, symbol): Index constituents (e.g."HSI.HK")
finance_calendar(ctx, category, start, end_; market): Financial calendar eventsCalendarEventsResponse.next_date: pass as the nextstartwith the sameend_to fetch the next pageCalendarCategory:Report/Dividend/Split/Ipo/MacroData/Closed/Meeting/Merge
exchange_rate(ctx): Current rates for all supported currenciesprofit_analysis(ctx; start, end_): Account P&L (summary + sublist parallel fan-out)profit_analysis_by_market(ctx; page, size, market, ...): Paginated by marketprofit_analysis_detail(ctx, symbol; ...): Per-security P&L breakdownprofit_analysis_flows(ctx, symbol; page, size, derivative, ...): Per-security flows
list_alerts(ctx): List all alerts grouped by securityadd_alert(ctx, symbol, condition, trigger_value, frequency): Add alertAlertCondition:PriceRise/PriceFall/PercentRise/PercentFallAlertFrequency:Daily/EveryTime/Once
update_alert(ctx, item::AlertItem): Update alert (toggle enabled etc.)delete_alerts(ctx, ids::Vector{String}): Delete alerts
list_sharelists(ctx; count)/popular_sharelists(ctx; count)/sharelist_detail(ctx, id)create_sharelist(ctx, name; description)/delete_sharelist(ctx, id)add_sharelist_securities(ctx, id, symbols)/remove_sharelist_securities(ctx, id, symbols)/sort_sharelist_securities(ctx, id, symbols)
list_dca(ctx; status, symbol)/dca_stats(ctx; symbol)create_dca(ctx, symbol, amount, frequency; day_of_week, day_of_month, allow_margin)DCAFrequency:Daily/Weekly/Fortnightly/MonthlyDCAStatus:Active/Suspended/Finished
update_dca(ctx, plan_id; amount, frequency, ...)pause_dca(ctx, plan_id)/resume_dca(ctx, plan_id)/stop_dca(ctx, plan_id)dca_history(ctx, plan_id; page, limit)/dca_check_support(ctx, symbols)/dca_calc_date(ctx, symbol, frequency; ...)/dca_set_reminder(ctx, hours)
news(ctx, symbol): News for a securitytopics_by_symbol(ctx, symbol)/topic_detail(ctx, id)/topic_replies(ctx, topic_id; page, size)my_topics(ctx; page, size, topic_type): List topics you've publishedcreate_topic(ctx, title, body; topic_type, tickers, hashtags): Publish a topic, returns IDcreate_topic_reply(ctx, topic_id, body; reply_to_id): Post a reply (plain text)
short_positions(ctx, symbol): US short interest (FINRA bi-monthly)option_volume(ctx, symbol)/option_volume_daily(ctx, symbol, timestamp, count): Option volume statsupdate_pinned(ctx, mode::PinnedMode.T, symbols): Pin/unpin watchlist securities
quote_snapshot(ctx, symbols): One-shot quote snapshot via WebSocket (was previouslyrealtime_quote)realtime_quote(ctx, symbol_or_symbols): Read latest pushed quote from local cache (subscribe first); aligned withrealtime_depth/brokers/tradesfilings(ctx, symbol): Company filings list (REST/v1/quote/filings), returnsVector{FilingItem}member_id(ctx)/quote_level(ctx)/quote_package_details(ctx): Now backed by a realQueryUserQuoteProfilecall on connect (previously returned zero-value stubs)
AssetContext(config): Create context (HTTP-only, no disconnect needed)statements(ctx, type::StatementType.T; page, page_size): Paginated list of statement records (type=Daily/Monthly)statement_download_url(ctx, file_key): Resolve a statement record's download URL
ScreenerContext(config): Create context (HTTP-only, no disconnect needed)screener_recommend_strategies(ctx, market): Preset built-in screener strategies for a market (e.g."US"/"HK")screener_user_strategies(ctx, market): Current user's saved screener strategies for a marketscreener_strategy(ctx, id): Detail of one strategy (path-param id);filter_prefix is stripped from filter keysscreener_search(ctx, market; strategy_id=nothing, conditions=ScreenerCondition[], show=String[], page=0, size=20): Mode A (whenstrategy_idis given — fetches strategy and forwards filters) or Mode B (typedScreenerConditionobjects).DEFAULT_RETURNSalways included;filter_prefix is stripped from response indicator keys.screener_indicators(ctx): Available indicator definitions;filter_prefix stripped +tech_valuesrebuilt fromtech_indicators
business_segments(ctx, symbol): Latest business segment revenue breakdownbusiness_segments_history(ctx, symbol; report, cate): Historical business + regional segment snapshotsinstitution_rating_views(ctx, symbol): Historical analyst rating distribution time seriesindustry_rank(ctx, market, indicator, sort_type, limit): Industry rank in a marketindustry_peers(ctx, counter_or_symbol, market; industry_id): Recursive industry peer chainfinancial_report_snapshot(ctx, symbol; report, fiscal_year, fiscal_period): Earnings snapshot (actual vs estimate, key ratios)shareholder_top(ctx, symbol): Top-shareholder ranking (raw JSON)shareholder_detail(ctx, symbol, object_id): Holding history for one shareholder object (raw JSON)valuation_comparison(ctx, symbol, currency; comparison_symbols): Valuation comparison (PE/PB/PS) with historical curve
top_movers(ctx, markets, sort, limit; date): Top movers across one or more markets (renamed from upstreamstock_events)rank_categories(ctx): Available rank category keys/labels (raw JSON)rank_list(ctx, key; need_article): Ranked securities for one category
short_positions(ctx, symbol; count=20): Breaking — unified HK + US short-position endpoint (was US-only with fixedlast_timestamp=0, page_size=100). The item struct is nowShortPositionsItemand the response dropssymbol/sources. Timestamps areDateTime(UTC).short_trades(ctx, symbol; count=20): HK/US short-trade records, endpoint auto-selected by.HKsuffix.
MIT License