Longbridge OpenAPI SDK for Python
longbridge provides an easy-to-use interface for invokes Longbridge OpenAPI.
References
The configuration of the SDK.
The Quote API part of the SDK, e.g.: get basic information of securities, subscribe quotes...
The Trade API part of the SDK, e.g.: submit order, get order status...
Async quote API for use with asyncio; create via AsyncQuoteContext.create(config) and await in asyncio.
Async trade API for use with asyncio; create via AsyncTradeContext.create(config) and await in asyncio.
Quickstart
Install Longbridge OpenAPI SDK
pip install longbridge
Authentication
OAuth 2.0 (Recommended)
from longbridge.openapi import OAuthBuilder, Config
oauth = OAuthBuilder("your-client-id").build(
lambda url: print(f"Open this URL to authorize: {url}")
)
config = Config.from_oauth(oauth)
Legacy API Key (Environment Variables)
macOS/Linux
export LONGBRIDGE_APP_KEY="App Key get from user center"
export LONGBRIDGE_APP_SECRET="App Secret get from user center"
export LONGBRIDGE_ACCESS_TOKEN="Access Token get from user center"
Windows
setx LONGBRIDGE_APP_KEY "App Key get from user center"
setx LONGBRIDGE_APP_SECRET "App Secret get from user center"
setx LONGBRIDGE_ACCESS_TOKEN "Access Token get from user center"
from longbridge.openapi import Config
config = Config.from_apikey_env()
Quote API (Get basic information of securities)
from longbridge.openapi import Config, QuoteContext, OAuthBuilder
oauth = OAuthBuilder("your-client-id").build(
lambda url: print(f"Open this URL to authorize: {url}")
)
config = Config.from_oauth(oauth)
# Create a context for quote APIs
ctx = QuoteContext(config)
# Get basic information of securities
resp = ctx.quote(["700.HK", "AAPL.US", "TSLA.US", "NFLX.US"])
print(resp)
Quote API (Subscribe quotes)
from time import sleep
from longbridge.openapi import Config, QuoteContext, SubType, PushQuote, OAuthBuilder
oauth = OAuthBuilder("your-client-id").build(
lambda url: print(f"Open this URL to authorize: {url}")
)
config = Config.from_oauth(oauth)
# A callback to receive quote data
def on_quote(symbol: str, event: PushQuote):
print(symbol, event)
# Create a context for quote APIs
ctx = QuoteContext(config)
ctx.set_on_quote(on_quote)
# Subscribe
ctx.subscribe(["700.HK"], [SubType.Quote])
# Receive push for 30 seconds
sleep(30)
Trade API (Submit order)
from decimal import Decimal
from longbridge.openapi import TradeContext, Config, OrderType, OrderSide, TimeInForceType, OAuthBuilder
oauth = OAuthBuilder("your-client-id").build(
lambda url: print(f"Open this URL to authorize: {url}")
)
config = Config.from_oauth(oauth)
# Create a context for trade APIs
ctx = TradeContext(config)
# Submit order
resp = ctx.submit_order(
"700.HK", OrderType.LO, OrderSide.Buy,
Decimal("500"), TimeInForceType.Day,
submitted_price=Decimal("50"),
remark="Hello from Python SDK",
)
print(resp)
Asynchronous API
The SDK provides async contexts and an async HTTP client for use with Python's asyncio. All I/O methods return awaitables; callbacks (e.g. for push events) are set the same way as in the sync API.
import asyncio
from longbridge.openapi import Config, AsyncQuoteContext, SubType, PushQuote, OAuthBuilder
def on_quote(symbol: str, event: PushQuote):
print(symbol, event)
async def main():
oauth = await OAuthBuilder("your-client-id").build_async(
lambda url: print(f"Open this URL to authorize: {url}")
)
config = Config.from_oauth(oauth)
ctx = await AsyncQuoteContext.create(config)
ctx.set_on_quote(on_quote)
await ctx.subscribe(["700.HK", "AAPL.US"], [SubType.Quote])
quotes = await ctx.quote(["700.HK"])
print(quotes)
await asyncio.sleep(10)
asyncio.run(main())
See the *_async.py examples in the repo and the reference for AsyncQuoteContext, AsyncTradeContext, and HttpClient.request_async.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license LICENSE-MIT at your option.