0% found this document useful (0 votes)
33 views22 pages

LongPort OpenAPI SDK

The LongPort OpenAPI SDK provides a comprehensive set of functions for accessing financial data, including security quotes, candlestick data, trading sessions, and watchlist management. Users can subscribe to real-time updates, retrieve historical data, and manage watchlists through various API endpoints. The SDK is designed to facilitate seamless integration with financial applications, allowing users to interact with market data efficiently.

Uploaded by

-nobody-
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views22 pages

LongPort OpenAPI SDK

The LongPort OpenAPI SDK provides a comprehensive set of functions for accessing financial data, including security quotes, candlestick data, trading sessions, and watchlist management. Users can subscribe to real-time updates, retrieve historical data, and manage watchlists through various API endpoints. The SDK is designed to facilitate seamless integration with financial applications, allowing users to interact with market data efficiently.

Uploaded by

-nobody-
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

LongPort OpenAPI SDK

QuoteContext
Quote context

Parameters:

Name Type Description Default

config Config Configuration object required

brokers(symbol)
Get security brokers

Parameters:

Name Type Description Default

symbol str Security code required

Returns:

Type Description

SecurityBrokers Security brokers

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.brokers("700.HK")
print(resp)

calc_indexes(symbols, indexes)
Get calc indexes

Parameters:

Name Type Description Default

symbols List[str] Security codes required


Name Type Description Default

indexes List[Type[CalcIndex]] Calc indexes required

Returns:

Type Description

List[SecurityCalcIndex] Calc indexes of the symbols

Examples:

::

from longport.openapi import QuoteContext, Config, CalcIndex

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.calc_indexes(["700.HK", "APPL.US"], [CalcIndex.LastDone, CalcIndex


print(resp)

candlesticks(symbol, period, count,


adjust_type)
Get security candlesticks

Parameters:

Name Type Description Default

symbol str Security code required

period Type[Period] Candlestick period required

count int Count of required


cancdlestick
(Maximum is 1000 )

adjust_type Type[AdjustType] Adjustment type required

Returns:

Type Description

List[Candlestick] Candlesticks

Examples:
::

from longport.openapi import QuoteContext, Config, Period, AdjustType

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.candlesticks(
"700.HK", Period.Day, 10, AdjustType.NoAdjust)
print(resp)

capital_distribution(symbol)
Get capital distribution

Parameters:

Name Type Description Default

symbol str Security code required

Returns:

Type Description

CapitalDistributionResponse Capital distribution

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.capital_distribution("700.HK")
print(resp)

capital_flow(symbol)
Get capital flow intraday

Parameters:

Name Type Description Default

symbol str Security code required

Returns:
Type Description

List[CapitalFlowLine] Capital flow list

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.capital_flow("700.HK")
print(resp)

create_watchlist_group(name,
securities=None)
Create watchlist group

Parameters:

Name Type Description Default

name str Group name required

securities Optional[List[str]] Securities None

Returns:

Type Description

int Group ID

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)
group_id = ctx.create_watchlist_group(name = "Watchlist1", securities = ["700
print(group_id)

delete_watchlist_group(id,
purge=False)
Delete watchlist group
Parameters:

Name Type Description Default

id int Group ID required

purge bool Move securities in this group to the False


default group

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)
ctx.delete_watchlist_group(10086)

depth(symbol)
Get security depth

Parameters:

Name Type Description Default

symbol str Security code required

Returns:

Type Description

SecurityDepth Security depth

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.depth("700.HK")
print(resp)

history_candlesticks_by_date(symbol,
period, adjust_type, start, end)
Get security history candlesticks by date
Parameters:

Name Type Description Default

symbol str Security code required

period Type[Period] Period type required

adjust_type Type[AdjustType] Adjust type required

start Optional[date] Start date required

end Optional[date] End date required

history_candlesticks_by_offset(symbol
, period, adjust_type, forward, time,
count)
Get security history candlesticks by offset

Parameters:

Name Type Description Default

symbol str Security code required

period Type[Period] Period type required

adjust_type Type[AdjustType] Adjust type required

forward bool If True , query the required


latest from the
specified time

time datetime Datetime required

count int Count of required


candlesticks

intraday(symbol)
Get security intraday lines

Parameters:
Name Type Description Default

symbol str Security code required

Returns:

Type Description

List[IntradayLine] Intraday lines

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.intraday("700.HK")
print(resp)

member_id()
Returns the member ID

option_chain_expiry_date_list(symbol
)
Get option chain expiry date list

Parameters:

Name Type Description Default

symbol str Security code required

Returns:

Type Description

List[date] Option chain expiry date list

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)
resp = ctx.option_chain_expiry_date_list("AAPL.US")
print(resp)

option_chain_info_by_date(symbol,
expiry_date)
Get option chain info by date

Parameters:

Name Type Description Default

symbol str Security code required

expiry_date date Expiry date required

Returns:

Type Description

List[StrikePriceInfo] Option chain info

Examples:

::

from datetime import date


from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.option_chain_info_by_date(
"AAPL.US", date(2023, 1, 20))
print(resp)

option_quote(symbols)
Get quote of option securities

Parameters:

Name Type Description Default

symbols List[str] Security codes required

Returns:

Type Description

List[OptionQuote] Option quote list


Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.option_quote(["AAPL230317P160000.US"])
print(resp)

participants()
Get participants

Returns:

Type Description

List[ParticipantInfo] Participants

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.participants()
print(resp)

quote(symbols)
Get quote of securities

Parameters:

Name Type Description Default

symbols List[str] Security codes required

Returns:

Type Description

List[SecurityQuote] Security quote list

Examples:

::
from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.quote(["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"])


print(resp)

quote_level()
Returns the quote level

quote_package_details()
Returns the quote package details

realtime_brokers(symbol)
Get real-time brokers

Get real-time brokers of the subscribed symbols, it always returns


the data in the local storage.

Parameters:

Name Type Description Default

symbol str Security code required

Returns:

Type Description

SecurityBrokers Security brokers

Examples:

::

from time import sleep


from longport.openapi import QuoteContext, Config, SubType

config = Config.from_env()
ctx = QuoteContext(config)

ctx.subscribe(["700.HK", "AAPL.US"], [
SubType.Brokers], is_first_push = True)
sleep(5)
resp = ctx.realtime_brokers("700.HK")
print(resp)

realtime_candlesticks(symbol, period,
count)
Get real-time candlesticks
Get Get real-time candlesticks of the subscribed symbols, it always
returns the data in the local storage.

Parameters:

Name Type Description Default

symbol str Security code required

period Type[Period] Period type required

count int Count of candlesticks required

Returns:

Type Description

List[Candlestick] Security candlesticks

Examples:

::

from time import sleep


from longport.openapi import QuoteContext, Config, Period

config = Config.from_env()
ctx = QuoteContext(config)

ctx.subscribe_candlesticks("AAPL.US", Period.Min_1)
sleep(5)
resp = ctx.realtime_candlesticks("AAPL.US", Period.Min_1, 10)
print(resp)

realtime_depth(symbol)
Get real-time depth

Get real-time depth of the subscribed symbols, it always returns


the data in the local storage.

Parameters:

Name Type Description Default

symbol str Security code required

Returns:
Type Description

SecurityDepth Security depth

Examples:

::

from time import sleep


from longport.openapi import QuoteContext, Config, SubType

config = Config.from_env()
ctx = QuoteContext(config)

ctx.subscribe(["700.HK", "AAPL.US"], [
SubType.Depth], is_first_push = True)
sleep(5)
resp = ctx.realtime_depth("700.HK")
print(resp)

realtime_quote(symbols)
Get real-time quote

Get real-time quotes of the subscribed symbols, it always returns


the data in the local storage.

Parameters:

Name Type Description Default

symbols List[str] Security codes required

Returns:

Type Description

List[RealtimeQuote] Quote list

Examples:

::

from time import sleep


from longport.openapi import QuoteContext, Config, SubType

config = Config.from_env()
ctx = QuoteContext(config)

ctx.subscribe(["700.HK", "AAPL.US"], [
SubType.Quote], is_first_push = True)
sleep(5)
resp = ctx.realtime_quote(["700.HK", "AAPL.US"])
print(resp)
realtime_trades(symbol, count)
Get real-time trades

Get real-time trades of the subscribed symbols, it always returns


the data in the local storage.

Parameters:

Name Type Description Default

symbol str Security code required

count int Count of trades required

Returns:

Type Description

List[Trade] Security trades

Examples:

::

from time import sleep


from longport.openapi import QuoteContext, Config, SubType

config = Config.from_env()
ctx = QuoteContext(config)

ctx.subscribe(["700.HK", "AAPL.US"], [
SubType.Trade], is_first_push = False)
sleep(5)
resp = ctx.realtime_trades("700.HK", 10)
print(resp)

security_list(market, category)
Get security list

Parameters:

Name Type Description Default

market Type[Market] Market required

category Type[SecurityListCategory] Security list required


category

Returns:
Type Description

List[Security] Security list

Examples:

::

from longport.openapi import QuoteContext, Config, Market, SecurityListCatego

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.security_list(Market.HK, SecurityListCategory.Overnight)


print(resp)

set_on_brokers(callback)
Set brokers callback, after receiving the brokers data push, it will
call back to this function.

set_on_candlestick(callback)
Set candlestick callback, after receiving the candlestick updated
event, it will call back to this function.

set_on_depth(callback)
Set depth callback, after receiving the depth data push, it will call
back to this function.

set_on_quote(callback)
Set quote callback, after receiving the quote data push, it will call
back to this function.

set_on_trades(callback)
Set trades callback, after receiving the trades data push, it will call
back to this function.

static_info(symbols)
Get basic information of securities

Parameters:

Name Type Description Default

symbols List[str] Security codes required

Returns:
Type Description

List[SecurityStaticInfo] Security info list

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.static_info(
["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"])
print(resp)

subscribe(symbols, sub_types,
is_first_push=False)
Subscribe

Parameters:

Name Type Description Default

symbols List[str] Security codes required

sub_types List[Type[SubType]] Subscribe required


types

is_first_push bool Whether to False


perform a data
push
immediately
after
subscribing.
(trade not
supported)

Examples:

::

from time import sleep


from longport.openapi import QuoteContext, Config, SubType, PushQuote

def on_quote(symbol: str, event: PushQuote):


print(symbol, event)

config = Config.from_env()
ctx = QuoteContext(config)
ctx.set_on_quote(on_quote)
ctx.subscribe(["700.HK", "AAPL.US"], [
SubType.Quote], is_first_push = True)
sleep(30)

subscribe_candlesticks(symbol,
period)
Subscribe security candlesticks

Parameters:

Name Type Description Default

symbol str Security code required

period Type[Period] Period type required

Examples:

::

from longport.openapi import QuoteContext, Config, PushCandlestick


config = Config.from_env()
ctx = QuoteContext(config)

def on_candlestick(symbol: str, event: PushCandlestick):


print(symbol, event)

ctx.set_on_candlestick(on_candlestick)
ctx.subscribe_candlesticks("700.HK", Period.Min_1)
sleep(30)

subscriptions()
Get subscription information

Examples:

::

from longport.openapi import QuoteContext, Config, SubType


config = Config.from_env()
ctx = QuoteContext(config)

ctx.subscribe(["700.HK", "AAPL.US"], [SubType.Quote])


resp = ctx.subscriptions()
print(resp)

trades(symbol, count)
Get security trades

Parameters:
Name Type Description Default

symbol str Security code required

count int Count of trades (Maximum is 1000 ) required

Returns:

Type Description

List[Trade] Trades

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.trades("700.HK", 10)


print(resp)

trading_days(market, begin, end)


Get trading session of the day

The interval must be less than one month, and only the most
recent year is supported.

Parameters:

Name Type Description Default

market Type[Market] Market required

begin date Begin date required

end date End date required

Returns:

Type Description

MarketTradingDays Trading days

Examples:

::
from datetime import date
from longport.openapi import QuoteContext, Config, Market

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.trading_days(
Market.HK, date(2022, 1, 1), date(2022, 2, 1))
print(resp)

trading_session()
Get trading session of the day

Returns:

Type Description

List[MarketTradingSession] Trading session of the day

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.trading_session()
print(resp)

unsubscribe(symbols, sub_types)
Unsubscribe

Parameters:

Name Type Description Default

symbols List[str] Security codes required

sub_types List[Type[SubType]] Subscribe types required

Examples:

::

from longport.openapi import QuoteContext, Config, SubType


config = Config.from_env()
ctx = QuoteContext(config)

ctx.subscribe(["700.HK", "AAPL.US"], [SubType.Quote])


ctx.unsubscribe(["AAPL.US"], [SubType.Quote])
unsubscribe_candlesticks(symbol,
period)
Subscribe security candlesticks

Parameters:

Name Type Description Default

symbol str Security code required

period Type[Period] Period type required

update_watchlist_group(id, name=None,
securities=None, mode=None)
Update watchlist group

Parameters:

Name Type Description Default

id int Group ID required

name Optional[str] Group name None

securities Optional[List[str]] Securities None

Examples:

::

from longport.openapi import QuoteContext, Config, SecuritiesUpdateMode

config = Config.from_env()
ctx = QuoteContext(config)
ctx.update_watchlist_group(10086, name = "Watchlist2", securities = ["700.HK

warrant_issuers()
Get warrant issuers

Returns:

Type Description

List[IssuerInfo] Warrant issuers


Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.warrant_issuers()
print(resp)

warrant_list(symbol, sort_by,
sort_order, warrant_type=None,
issuer=None, expiry_date=None,
price_type=None, status=None)
Get warrant list

Parameters:

Name Type Description De

symbol str Security req


code

sort_by Type[WarrantSortBy] Sort by req


field

sort_order Type[SortOrderType] Sort order req

warrant_type Optional[List[Type[WarrantType]]] Filter by No


warrant
type

issuer Optional[List[int]] Filter by No


issuer

expiry_date Optional[List[Type[FilterWarrantExpiryDate]]] Filter by No


expiry date

price_type Optional[List[Type[FilterWarrantInOutBoundsType]]] Filter by No


price type

status Optional[List[Type[WarrantStatus]]] Filter by No


status

Returns:
Type Description

List[WarrantInfo] Warrant list

Examples:

::

from longport.openapi import QuoteContext, Config, WarrantSortBy, SortOrderTy

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.warrant_list("700.HK", WarrantSortBy.LastDone, SortOrderType.Ascen


print(resp)

warrant_quote(symbols)
Get quote of warrant securities

Parameters:

Name Type Description Default

symbols List[str] Security codes required

Returns:

Type Description

List[WarrantQuote] Warrant quote list

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.warrant_quote(["21125.HK"])
print(resp)

watchlist()
Get watch list

Returns:
Type Description

List[WatchlistGroup] Watch list groups

Examples:

::

from longport.openapi import QuoteContext, Config

config = Config.from_env()
ctx = QuoteContext(config)

resp = ctx.watchlist()
print(resp)

Documentation built with MkDocs.

You might also like