Longbridge OpenAPI SDK for Python
longbridge
provides an easy-to-use interface for invokes Longbridge OpenAPI
.
Quickstart
Install Longbridge OpenAPI SDK
pip install longbridge
Setting 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"
Setting environment variables(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"
Quote API (Get basic information of securities)
from longbridge.openapi import Config, QuoteContext
config = Config.from_env()
ctx = QuoteContext(config)
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
config = Config.from_env()
def on_quote(symbol: str, event: PushQuote):
print(symbol, event)
ctx = QuoteContext(config)
ctx.set_on_quote(on_quote)
resp = ctx.subscribe(["700.HK"], [SubType.Quote], is_first_push=True)
sleep(30)
Trade API (Submit order)
from decimal import Decimal
from longbridge.openapi import TradeContext, Config, OrderType, OrderSide, TimeInForceType
config = Config.from_env()
ctx = TradeContext(config)
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)
License
Licensed under either of