Skip to main content

A Python library for LongPort Open API

Project description

LongPort OpenAPI SDK for Python

longport provides an easy-to-use interface for invokes LongPort OpenAPI.

References

  • Config

    The configuration of the SDK.

  • QuoteContext

    The Quote API part of the SDK, e.g.: get basic information of securities, subscribe quotes...

  • TradeContext

    The Trade API part of the SDK, e.g.: submit order, get order status...

Quickstart

Install LongPort OpenAPI SDK

pip install longport

Setting environment variables(MacOS/Linux)

export LONGPORT_APP_KEY="App Key get from user center"
export LONGPORT_APP_SECRET="App Secret get from user center"
export LONGPORT_ACCESS_TOKEN="Access Token get from user center"

Setting environment variables(Windows)

setx LONGPORT_APP_KEY "App Key get from user center"
setx LONGPORT_APP_SECRET "App Secret get from user center"
setx LONGPORT_ACCESS_TOKEN "Access Token get from user center"

Quote API (Get basic information of securities)

from longpport.openapi import Config, QuoteContext

# Load configuration from environment variables
config = Config.from_env()

# 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 longport.openapi import Config, QuoteContext, SubType, PushQuote

# Load configuration from environment variables
config = Config.from_env()

# 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
resp = ctx.subscribe(["700.HK"], [SubType.Quote], is_first_push=True)

# Receive push duration to 30 seconds
sleep(30)

Trade API (Submit order)

from decimal import Decimal
from longport.openapi import TradeContext, Config, OrderType, OrderSide, TimeInForceType

# Load configuration from environment variables
config = Config.from_env()

# 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)

License

Licensed under either of

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

longport-1.0.21-cp312-none-win_amd64.whl (4.0 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

longport-1.0.21-cp312-none-win32.whl (3.5 MB view hashes)

Uploaded CPython 3.12 Windows x86

longport-1.0.21-cp312-cp312-manylinux_2_28_aarch64.whl (4.8 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.28+ ARM64

longport-1.0.21-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

longport-1.0.21-cp312-cp312-manylinux_2_12_i686.manylinux2010_i686.whl (5.3 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.12+ i686

longport-1.0.21-cp312-cp312-macosx_10_12_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

longport-1.0.21-cp312-cp312-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (8.4 MB view hashes)

Uploaded CPython 3.12 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

longport-1.0.21-cp311-none-win_amd64.whl (4.0 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

longport-1.0.21-cp311-none-win32.whl (3.5 MB view hashes)

Uploaded CPython 3.11 Windows x86

longport-1.0.21-cp311-cp311-manylinux_2_28_aarch64.whl (4.7 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.28+ ARM64

longport-1.0.21-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

longport-1.0.21-cp311-cp311-manylinux_2_12_i686.manylinux2010_i686.whl (5.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.12+ i686

longport-1.0.21-cp311-cp311-macosx_10_12_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

longport-1.0.21-cp311-cp311-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (8.4 MB view hashes)

Uploaded CPython 3.11 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

longport-1.0.21-cp310-none-win_amd64.whl (4.0 MB view hashes)

Uploaded CPython 3.10 Windows x86-64

longport-1.0.21-cp310-none-win32.whl (3.5 MB view hashes)

Uploaded CPython 3.10 Windows x86

longport-1.0.21-cp310-cp310-manylinux_2_28_aarch64.whl (4.7 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.28+ ARM64

longport-1.0.21-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

longport-1.0.21-cp310-cp310-manylinux_2_12_i686.manylinux2010_i686.whl (5.3 MB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.12+ i686

longport-1.0.21-cp310-cp310-macosx_10_12_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.10 macOS 10.12+ x86-64

longport-1.0.21-cp310-cp310-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (8.4 MB view hashes)

Uploaded CPython 3.10 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

longport-1.0.21-cp39-none-win_amd64.whl (4.0 MB view hashes)

Uploaded CPython 3.9 Windows x86-64

longport-1.0.21-cp39-none-win32.whl (3.5 MB view hashes)

Uploaded CPython 3.9 Windows x86

longport-1.0.21-cp39-cp39-manylinux_2_28_aarch64.whl (4.7 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.28+ ARM64

longport-1.0.21-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

longport-1.0.21-cp39-cp39-manylinux_2_12_i686.manylinux2010_i686.whl (5.3 MB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.12+ i686

longport-1.0.21-cp39-cp39-macosx_10_12_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.9 macOS 10.12+ x86-64

longport-1.0.21-cp39-cp39-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (8.4 MB view hashes)

Uploaded CPython 3.9 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

longport-1.0.21-cp38-none-win_amd64.whl (4.0 MB view hashes)

Uploaded CPython 3.8 Windows x86-64

longport-1.0.21-cp38-none-win32.whl (3.5 MB view hashes)

Uploaded CPython 3.8 Windows x86

longport-1.0.21-cp38-cp38-manylinux_2_28_aarch64.whl (4.7 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.28+ ARM64

longport-1.0.21-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

longport-1.0.21-cp38-cp38-manylinux_2_12_i686.manylinux2010_i686.whl (5.3 MB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.12+ i686

longport-1.0.21-cp38-cp38-macosx_10_12_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.8 macOS 10.12+ x86-64

longport-1.0.21-cp38-cp38-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (8.4 MB view hashes)

Uploaded CPython 3.8 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64

longport-1.0.21-cp37-none-win_amd64.whl (4.0 MB view hashes)

Uploaded CPython 3.7 Windows x86-64

longport-1.0.21-cp37-none-win32.whl (3.5 MB view hashes)

Uploaded CPython 3.7 Windows x86

longport-1.0.21-cp37-cp37m-manylinux_2_28_aarch64.whl (4.7 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.28+ ARM64

longport-1.0.21-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.3 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

longport-1.0.21-cp37-cp37m-manylinux_2_12_i686.manylinux2010_i686.whl (5.3 MB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.12+ i686

longport-1.0.21-cp37-cp37m-macosx_10_12_x86_64.whl (4.3 MB view hashes)

Uploaded CPython 3.7m macOS 10.12+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page