Code Reference
OpenApiException
Bases: Exception
OpenAPI exception
code
instance-attribute
code: Optional[int]
Error code
message
instance-attribute
message: str
Error message
__init__
__init__(code: int, message: str) -> None
HttpClient
A HTTP client for longPort open api
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
http_url
|
str
|
HTTP API url |
required |
app_key
|
str
|
App Key |
required |
app_secret
|
str
|
App Secret |
required |
access_token
|
str
|
Access Token |
required |
__init__
__init__(http_url: str, app_key: str, app_secret: str, access_token: str) -> None
from_env
classmethod
from_env() -> HttpClient
Create a new HttpClient from the given environment variables
It first gets the environment variables from the .env file in the current directory.
Variables
LONGPORT_HTTP_URL- HTTP endpoint urlLONGPORT_APP_KEY- App keyLONGPORT_APP_SECRET- App secretLONGPORT_ACCESS_TOKEN- Access token
request
request(method: str, path: str, headers: Optional[dict[str, str]] = None, body: Optional[Any] = None) -> Any
Performs a HTTP reqest
Examples:
::
from longport.openapi import HttpClient
client = HttpClient(http_url, app_key,
app_secret, access_token);
# get
resp = client.request("get", "/foo/bar");
print(resp)
# post
client.request("get", "/foo/bar", { "foo": 1, "bar": 2 });
Config
Configuration options for LongPort sdk
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_key
|
str
|
App Key |
required |
app_secret
|
str
|
App Secret |
required |
access_token
|
str
|
Access Token |
required |
http_url
|
Optional[str]
|
HTTP API url |
None
|
trade_ws_url
|
Optional[str]
|
Websocket url for trade API |
None
|
language
|
Optional[Type[Language]]
|
Language identifier |
None
|
__init__
__init__(app_key: str, app_secret: str, access_token: str, http_url: Optional[str] = None, trade_ws_url: Optional[str] = None, language: Optional[Type[Language]] = None) -> None
from_env
classmethod
from_env() -> Config
Create a new Config from the given environment variables
It first gets the environment variables from the .env file in the current directory.
Variables
LONGPORT_APP_KEY- App keyLONGPORT_APP_SECRET- App secretLONGPORT_ACCESS_TOKEN- Access tokenLONGPORT_HTTP_URL- HTTP endpoint urlLONGPORT_TRADE_WS_URL- Trade websocket endpoint url
Language
TradeStatus
Security Status
Normal
Halted
Delisted
Fuse
PrepareList
CodeMoved
ToBeOpened
SplitStockHalts
Expired
WarrantPrepareList
SuspendTrade
OrderSide
OrderType
Order type
Unknown
LO
ELO
MO
AO
ALO
ODD
LIT
MIT
TSLPAMT
TSLPPCT
TSMAMT
TSMPCT
SLO
OrderStatus
Order status
Unknown
NotReported
ReplacedNotReported
ProtectedNotReported
VarietiesNotReported
Filled
WaitToNew
New
WaitToReplace
PendingReplace
Replaced
PartialFilled
WaitToCancel
PendingCancel
Rejected
Canceled
Expired
PartialWithdrawal
OrderTag
Order tag
Unknown
Normal
LongTerm
Grey
MarginCall
Offline
Creditor
Debtor
NonExercise
AllocatedSub
TriggerStatus
Trigger status
Unknown
Deactive
Active
Released
PushOrderChanged
Order changed message
side
instance-attribute
side: Type[OrderSide]
Order side
stock_name
instance-attribute
stock_name: str
Stock name
submitted_quantity
instance-attribute
submitted_quantity: int
Submitted quantity
symbol
instance-attribute
symbol: str
Order symbol
order_type
instance-attribute
order_type: Type[OrderType]
Order type
submitted_price
instance-attribute
submitted_price: Decimal
Submitted price
executed_quantity
instance-attribute
executed_quantity: int
Executed quantity
executed_price
instance-attribute
executed_price: Optional[Decimal]
Executed price
order_id
instance-attribute
order_id: str
Order ID
currency
instance-attribute
currency: str
Currency
status
instance-attribute
status: Type[OrderStatus]
Order status
submitted_at
instance-attribute
submitted_at: datetime
Submitted time
updated_at
instance-attribute
updated_at: datetime
Last updated time
trigger_price
instance-attribute
trigger_price: Optional[Decimal]
Order trigger price
msg
instance-attribute
msg: str
Rejected message or remark
tag
instance-attribute
tag: Type[OrderTag]
Order tag
trigger_status
instance-attribute
trigger_status: Optional[Type[TriggerStatus]]
Conditional order trigger status
trigger_at
instance-attribute
trigger_at: Optional[datetime]
Conditional order trigger time
trailing_amount
instance-attribute
trailing_amount: Optional[Decimal]
Trailing amount
trailing_percent
instance-attribute
trailing_percent: Optional[Decimal]
Trailing percent
limit_offset
instance-attribute
limit_offset: Optional[Decimal]
Limit offset amount
account_no
instance-attribute
account_no: str
Account no
last_share
instance-attribute
last_share: Optional[Decimal]
Last share
last_price
instance-attribute
last_price: Optional[Decimal]
Last price
remark
instance-attribute
remark: str
Remark message
TopicType
Topic type
Private
TradeContext
Trade context
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Config
|
Configuration object |
required |
__init__
__init__(config: Config) -> None
set_on_order_changed
set_on_order_changed(callback: Callable[[PushOrderChanged], None]) -> None
Set order changed callback, after receiving the order changed event, it will call back to this function.
subscribe
subscribe(topics: List[Type[TopicType]]) -> None
Subscribe
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
topics
|
List[Type[TopicType]]
|
Topic list |
required |
Examples:
::
from time import sleep
from decimal import Decimal
from longport.openapi import TradeContext, Config, OrderSide, OrderType, TimeInForceType, PushOrderChanged, TopicType
def on_order_changed(event: PushOrderChanged):
print(event)
config = Config.from_env()
ctx = TradeContext(config)
ctx.set_on_order_changed(on_order_changed)
ctx.subscribe([TopicType.Private])
sleep(5) # waiting for push event