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:
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 Pkg
Pkg.add("LongPort")using LongPort
# Load configuration from TOML file
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
quotes = realtime_quote(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 LongPort
# Load configuration from TOML file
cfg = Config.from_toml()
# Create and connect to TradeContext
ctx = TradeContext(cfg)
# Get account balance
resp = account_balance(ctx)
# Get stock positions
resp = stock_positions(ctx, ["700.HK"])
# Get today's orders
resp = today_orders(ctx)
# Get historical orders
resp = history_orders(ctx, "2023-01-01", "2023-02-01")
# Get today's executions
resp = today_executions(ctx)
# Get historical executions
resp = history_executions(ctx, "2023-01-01", "2023-02-01")
# Submit an order
resp = submit_order(ctx, "700.HK", OrderType.LO, Side.Buy, 100, 300.0)
# Modify an order
resp = modify_order(ctx, "order_id", 100, 301.0)
# Cancel an order
resp = cancel_order(ctx, "order_id")
# 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.tomlfileQuoteContext(config): Create and connect toQuoteContextTradeContext(config): Create and connect toTradeContextdisconnect!(ctx): Disconnect from the server
static_info(ctx, symbols): Get basic static information for securitiesrealtime_quote(ctx, symbols): Get real-time stock quotesoption_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 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, symbols): Get stock positionstoday_orders(ctx): Get today's ordershistory_orders(ctx, start_date, end_date): Get historical orderstoday_executions(ctx): Get today's executionshistory_executions(ctx, start_date, end_date): Get historical executionssubmit_order(ctx, symbol, order_type, side, quantity, price): Submit an ordermodify_order(ctx, order_id, quantity, price): Modify an ordercancel_order(ctx, order_id): Cancel an orderset_on_order_changed(ctx, callback): Set the callback function for order status change pushesset_on_trade_changed(ctx, callback): Set the callback function for trade report pushessubscribe_trade(ctx, topics): Subscribe to trade pushesunsubscribe_trade(ctx, topics): Unsubscribe from trade pushes
MIT License