TradeContext

Trade context

Parameters:

Name Type Description Default
config Config

Configuration object

required

account_balance(currency=None)

Get account balance

Parameters:

Name Type Description Default
currency Optional[str]

Currency

None

Returns:

Type Description
List[AccountBalance]

Account list

Examples:

::

from longport.openapi import TradeContext, Config

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

resp = ctx.account_balance()
print(resp)

cancel_order(order_id)

Cancel order

Parameters:

Name Type Description Default
order_id str

Order ID

required

Examples:

::

from longport.openapi import TradeContext, Config

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

ctx.cancel_order("709043056541253632")

cash_flow(start_at, end_at, business_type=None, symbol=None, page=None, size=None)

Get cash flow

Parameters:

Name Type Description Default
start_at datetime

Start time

required
end_at datetime

End time

required
business_type Optional[Type[BalanceType]]

Balance type

None
symbol Optional[str]

Target security code

None
page Optional[int]

Start page (Default: 1)

None
size Optional[int]

Page size (Default: 50)

None

Returns:

Type Description
List[CashFlow]

Cash flow list

Examples:

::

from datetime import datetime
from longport.openapi import TradeContext, Config

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

resp = ctx.cash_flow(
    start_at = datetime(2022, 5, 9),
    end_at = datetime(2022, 5, 12),
)
print(resp)

estimate_max_purchase_quantity(symbol, order_type, side, price=None, currency=None, order_id=None, fractional_shares=False)

Estimating the maximum purchase quantity for Hong Kong and US stocks, warrants, and options

Parameters:

Name Type Description Default
symbol str

Security symbol

required
order_type Type[OrderType]

Order type

required
side Type[OrderSide]

Order side

required
price Optional[Decimal]

Estimated order price,

None
currency Optional[str]

Settlement currency

None
order_id Optional[str]

Order ID, required when estimating the maximum purchase quantity for a modified order

None
fractional_shares bool

Get the maximum fractional share buying power

False

Returns:

Type Description
EstimateMaxPurchaseQuantityResponse

Response

Examples:

::

from longport.openapi import TradeContext, Config, OrderType, OrderSide

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

resp = ctx.estimate_max_purchase_quantity(
    symbol = "700.HK",
    order_type = OrderType.LO,
    side = OrderSide.Buy,
)
print(resp)

fund_positions(symbols=None)

Get fund positions

Parameters:

Name Type Description Default
symbols Optional[List[str]]

Filter by fund codes

None

Returns:

Type Description
FundPositionsResponse

Fund positions

Examples:

::

from longport.openapi import TradeContext, Config

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

resp = ctx.fund_positions()
print(resp)

history_executions(symbol=None, start_at=None, end_at=None)

Get history executions

Parameters:

Name Type Description Default
symbol Optional[str]

Filter by security code, example: 700.HK, AAPL.US

None
start_at Optional[datetime]

Start time

None
end_at Optional[datetime]

End time

None

Returns:

Type Description
List[Execution]

Execution list

Examples:

::

from datetime import datetime
from longport.openapi import TradeContext, Config

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

resp = ctx.history_executions(
    symbol = "700.HK",
    start_at = datetime(2022, 5, 9),
    end_at = datetime(2022, 5, 12),
)
print(resp)

history_orders(symbol=None, status=None, side=None, market=None, start_at=None, end_at=None)

Get history orders

Parameters:

Name Type Description Default
symbol Optional[str]

Filter by security code

None
status Optional[List[Type[OrderStatus]]]

Filter by order status

None
side Optional[Type[OrderSide]]

Filter by order side

None
market Optional[Type[Market]]

Filter by market type

None
start_at Optional[datetime]

Start time

None
end_at Optional[datetime]

End time

None

Returns:

Type Description
List[Order]

Order list

Examples:

::

from datetime import datetime
from longport.openapi import TradeContext, Config, OrderStatus, OrderSide, Market

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

resp = ctx.history_orders(
    symbol = "700.HK",
    status = [OrderStatus.Filled, OrderStatus.New],
    side = OrderSide.Buy,
    market = Market.HK,
    start_at = datetime(2022, 5, 9),
    end_at = datetime(2022, 5, 12),
)
print(resp)

margin_ratio(symbol)

Get margin ratio

Parameters:

Name Type Description Default
symbol str

Security symbol

required

Returns:

Type Description
MarginRatio

Margin ratio

Examples:

::

from longport.openapi import TradeContext, Config

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

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

order_detail(order_id)

Get order detail

Parameters:

Name Type Description Default
order id

Order id

required

Returns:

Type Description
OrderDetail

Order detail

Examples:

::

from longport.openapi import TradeContext, Config

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

resp = ctx.order_detail("701276261045858304")
print(resp)

replace_order(order_id, quantity, price=None, trigger_price=None, limit_offset=None, trailing_amount=None, trailing_percent=None, remark=None)

Replace order

Parameters:

Name Type Description Default
quantity Decimal

Replaced quantity

required
price Optional[Decimal]

Replaced price

None
trigger_price Optional[Decimal]

Trigger price (LIT / MIT Order Required)

None
limit_offset Optional[Decimal]

Limit offset amount (TSLPAMT / TSLPPCT Required)

None
trailing_amount Optional[Decimal]

Trailing amount (TSLPAMT / TSMAMT Required)

None
trailing_percent Optional[Decimal]

Trailing percent (TSLPPCT / TSMAPCT Required)

None
remark Optional[str]

Remark (Maximum 64 characters)

None

Examples:

::

from decimal import Decimal
from longport.openapi import TradeContext, Config

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

ctx.replace_order(
    order_id = "709043056541253632",
    quantity = Decimal(100),
    price = Decimal(100),
)

set_on_order_changed(callback)

Set order changed callback, after receiving the order changed event, it will call back to this function.

stock_positions(symbols=None)

Get stock positions

Parameters:

Name Type Description Default
symbols Optional[List[str]]

Filter by stock codes

None

Returns:

Type Description
StockPositionsResponse

Stock positions

Examples:

::

from longport.openapi import TradeContext, Config

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

resp = ctx.stock_positions()
print(resp)

submit_order(symbol, order_type, side, submitted_quantity, time_in_force, submitted_price=None, trigger_price=None, limit_offset=None, trailing_amount=None, trailing_percent=None, expire_date=None, outside_rth=None, remark=None)

Submit order

Parameters:

Name Type Description Default
symbol str

Security code

required
order_type Type[OrderType]

Order type

required
side Type[OrderSide]

Order Side

required
submitted_quantity Decimal

Submitted quantity

required
time_in_force Type[TimeInForceType]

Time in force type

required
submitted_price Optional[Decimal]

Submitted price

None
trigger_price Optional[Decimal]

Trigger price (LIT / MIT Required)

None
limit_offset Optional[Decimal]

Limit offset amount (TSLPAMT / TSLPPCT Required)

None
trailing_amount Optional[Decimal]

Trailing amount (TSLPAMT / TSMAMT Required)

None
trailing_percent Optional[Decimal]

Trailing percent (TSLPPCT / TSMAPCT Required)

None
expire_date Optional[date]

Long term order expire date (Required when time_in_force is GoodTilDate)

None
outside_rth Optional[Type[OutsideRTH]]

Enable or disable outside regular trading hours

None
remark Optional[str]

Remark (Maximum 64 characters)

None

Returns:

Type Description
SubmitOrderResponse

Response

Examples:

::

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

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

resp = ctx.submit_order(
    side = OrderSide.Buy,
    symbol = "700.HK",
    order_type = OrderType.LO,
    submitted_price = Decimal(50),
    submitted_quantity = Decimal(200),
    time_in_force = TimeInForceType.Day,
    remark = "Hello from Python SDK",
)
print(resp)

subscribe(topics)

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

resp = ctx.submit_order(
    side = OrderSide.Buy,
    symbol = "700.HK",
    order_type = OrderType.LO,
    submitted_price = Decimal(50),
    submitted_quantity = Decimal(200),
    time_in_force = TimeInForceType.Day,
    remark = "Hello from Python SDK",
)
print(resp)
sleep(5)  # waiting for push event

today_executions(symbol=None, order_id=None)

Get today executions

Parameters:

Name Type Description Default
symbol Optional[str]

Filter by security code

None
order_id Optional[str]

Filter by Order ID

None

Returns:

Type Description
List[Execution]

Execution list

Examples:

::

from longport.openapi import TradeContext, Config

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

resp = ctx.today_executions(symbol = "700.HK")
print(resp)

today_orders(symbol=None, status=None, side=None, market=None, order_id=None)

Get today orders

Parameters:

Name Type Description Default
symbol Optional[str]

Filter by security code

None
status Optional[List[Type[OrderStatus]]]

Filter by order status

None
side Optional[Type[OrderSide]]

Filter by order side

None
market Optional[Type[Market]]

Filter by market type

None
order_id Optional[str]

Filter by order id

None

Returns:

Type Description
List[Order]

Order list

Examples:

::

from longport.openapi import TradeContext, Config, OrderStatus, OrderSide, Market

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

resp = ctx.today_orders(
    symbol = "700.HK",
    status = [OrderStatus.Filled, OrderStatus.New],
    side = OrderSide.Buy,
    market = Market.HK,
)
print(resp)

unsubscribe(topics)

Unsubscribe

Parameters:

Name Type Description Default
topics List[Type[TopicType]]

Topic list

required