Skip to main content

Subscribe Position Events

Interface Description​

Subscribe to position lifecycle event notifications for subscribed accounts. Each notification provides the instrument identifier and related contract details for the lifecycle event. subscribeType only supports = 4. Event settlement messages require the latest SDK version.

Two independent channels are available. Both deliver the same events with the same payload, choose one of them:

ChannelTransportData formatClient dependency
gRPC ChannelgRPC server streaming over HTTP/2ProtobufgRPC and Protobuf libraries
SSE ChannelHTTP/2 POST, response is text/event-stream (Server-Sent Events)JSONAny HTTP client that supports HTTP/2

Common Rules​

One App Key can hold only one active subscription at a time, across gRPC and SSE. While a subscription is active, a new subscribe request of the same App Key is rejected and the active one is not affected. Close the existing subscription before switching channels.

gRPC​

Position events subscribe Proto protocol definition.​

Request Proto

message SubscribeRequest {
uint32 subscribeType = 4; // Subscription type
int64 timestamp = 2; // Timestamp
string contentType = 3; // Content type
string payload = 4; // Content
repeated string accounts = 5; // Account ID
}

Response Proto

message SubscribeResponse {
EventType eventType = 1; // Event type
uint32 subscribeType = 4; // Subscription type
string contentType = 3; // Subscription type
string payload = 4; // Content
string requestId = 5; // Request id
int64 timestamp = 6; // Timestamp
}

EventType enumeration

enum EventType {
SubscribeSuccess = 0; // Subscription succeeded
Ping = 1; // Heartbeat information
AuthError = 2; // Authentication error
NumOfConnExceed = 3; // Connection limit exceeded
SubscribeExpired = 4; // Subscription expired
}

Request Example​

In the following case, the _on_log method is used to output the log. The my_on_events_message method is to receive order status change messages.

import logging

from webull.trade.events.types import EVENT_TYPE_POSITION, POSITION_STATUS_CHANGED
from webull.trade.trade_events_client import TradeEventsClient

your_app_key = "<your_app_key>"
your_app_secret = "<your_app_secret>"
account_id = "<your_account_id>"
region_id = "sg"

# PRD env host: events-api.webull.com.sg
# Test env host: sg-events-api.uat.webullbroker.com
optional_api_endpoint = "<event_api_endpoint>"


def _on_log(level, log_content):
print(logging.getLevelName(level), log_content)


def my_on_events_message(event_type, subscribe_type, payload, raw_message):
if EVENT_TYPE_ORDER == event_type and ORDER_STATUS_CHANGED == subscribe_type:
print('----request_id:%s----' % payload['request_id'])
print(payload)
if EVENT_TYPE_POSITION == event_type and POSITION_STATUS_CHANGED == subscribe_type:
print('event payload:%s' % payload)
if EVENT_TYPE_OPTION == event_type and OPTION_STATUS_CHANGED == subscribe_type:
print('option payload:%s' % payload)

if __name__ == '__main__':

# Create EventsClient instance
trade_events_client = TradeEventsClient(your_app_key, your_app_secret, region_id)
# For non production environment, you need to set the domain name of the subscription service through eventsclient. For example, the domain name of the UAT environment is set here
# trade_events_client = TradeEventsClient(your_app_key, your_app_secret, region_id, host=optional_api_endpoint)
trade_events_client.on_log = _on_log

# Set the callback function when the event data is received.
# The data of order status change is printed here

trade_events_client.on_events_message = my_on_events_message
# Set the account ID to be subscribed and initiate the subscription. This method is synchronous
trade_events_client.do_subscribe([account_id])

Response Example​

Position event scene type

{
"account_id": "1276674499001173504",
"position_id": "037SDSML6O6DT0KHK6R4000000",
"quantity": "5",
"symbol": "AAPL261120C00305000",
"instrument_type": "OPTION",
"market": "US",
"status": "exercised",
"biz_type": "OPTION_EXERCISE"
}
DEBUG response:eventType: Ping
subscribeType: 4
contentType: "text/plain"
requestId: "ab39b532-3ad4-46c4-823d-3dff12e0f1b9"
timestamp: 1768994319673

SSE​

The SSE channel delivers the same events as the gRPC channel over a standard HTTP long connection. The client sends one POST request, and the server keeps the response open as a text/event-stream. No gRPC or Protobuf library is required.

Request​

POST https://<event_api_endpoint>/subscribe/sse

EnvironmentHost
Productionevents-api.webull.com.sg
Testsg-events-api.uat.webullbroker.com

Headers

The request is signed the same way as the OpenAPI HTTP APIs (x-app-key, x-request-id, x-timestamp, x-signature, x-signature-algorithm, x-signature-version, x-signature-nonce). The body digest of the signature is computed over the exact JSON bytes sent. In addition:

HeaderValue
Content-Typeapplication/json
Accepttext/event-stream (required, otherwise 400)

Body

JSON, at most 1 MB.

FieldTypeRequiredDescription
subscribeTypeintegerYesOnly 4 is supported.
timestampintegerYesRequest time, Unix epoch in milliseconds.
accountsstring arrayYesAccount IDs to subscribe, must not be empty.
{"subscribeType":4,"timestamp":1768994309000,"accounts":["<your_account_id>"]}

Response​

On success the server answers HTTP 200 and keeps writing frames. Each frame has an empty id: line, a data: line and an event: line, and frames are separated by a blank line. data is a JSON object with the fields below.

FieldTypeDescription
eventTypeintegerBusiness event type, only present on event frames. 1032 for this subscription.
subscribeTypeintegerSubscription type, 4.
contentTypestringtext/plain or application/json.
payloadstringFrame content. For application/json it is a JSON string, decode it a second time to get the event object.
requestIdstringRequest ID.
timestampintegerServer time, Unix epoch in milliseconds.

Stream example

id:
data: {"subscribeType":4,"contentType":"text/plain","payload":"7199000","requestId":"ab39b532-3ad4-46c4-823d-3dff12e0f1b9","timestamp":1768994310000}
event: subscribeSuccess

: heartbeat

id:
data: {"eventType":1032,"subscribeType":4,"contentType":"application/json","payload":"{\"account_id\":\"1276674499001173504\",\"position_id\":\"037SDSML6O6DT0KHK6R4000000\",\"quantity\":\"5\",\"symbol\":\"AAPL261120C00305000\",\"instrument_type\":\"OPTION\",\"market\":\"US\",\"status\":\"exercised\",\"biz_type\":\"OPTION_EXERCISE\"}","requestId":"ab39b532-3ad4-46c4-823d-3dff12e0f1b9","timestamp":1768994319673}
event: event

The payload of the event frame above, decoded:

{
"account_id": "1276674499001173504",
"position_id": "037SDSML6O6DT0KHK6R4000000",
"quantity": "5",
"symbol": "AAPL261120C00305000",
"instrument_type": "OPTION",
"market": "US",
"status": "exercised",
"biz_type": "OPTION_EXERCISE"
}

Request Example​

import logging

from webull.trade.events.types import ORDER_STATUS_CHANGED, EVENT_TYPE_ORDER, EVENT_TYPE_POSITION, \
POSITION_STATUS_CHANGED, EVENT_TYPE_OPTION, OPTION_STATUS_CHANGED
from webull.trade.trade_events_sse_client import TradeEventsSseClient

your_app_key = "<your_app_key>"
your_app_secret = "<your_app_secret>"
account_id = "<your_account_id>"
region_id = "sg"

# PRD env host: events-api.webull.com.sg
# Test env host: sg-events-api.uat.webullbroker.com
optional_api_endpoint = "<event_api_endpoint>"


def _on_log(level, log_content):
print(logging.getLevelName(level), log_content)


def my_on_connect(client, payload, response):
print('subscribed, duration:%s ms' % payload)


def my_on_events_message(event_type, subscribe_type, payload, raw_message):
if EVENT_TYPE_ORDER == event_type and ORDER_STATUS_CHANGED == subscribe_type:
print('----request_id:%s----' % payload['request_id'])
print(payload)
if EVENT_TYPE_POSITION == event_type and POSITION_STATUS_CHANGED == subscribe_type:
print('event payload:%s' % payload)
if EVENT_TYPE_OPTION == event_type and OPTION_STATUS_CHANGED == subscribe_type:
print('option payload:%s' % payload)


def my_on_fatal_error(client, response):
print('subscription stopped, event:%s, reason:%s' % (response.event, response.payload))
# Return True to keep reconnecting, e.g. while the subscription is being renewed.
return False


if __name__ == '__main__':

# Create SSE EventsClient instance
trade_events_client = TradeEventsSseClient(your_app_key, your_app_secret, region_id)
# For non production environment, you need to set the domain name of the subscription service through eventsclient. For example, the domain name of the UAT environment is set here
# trade_events_client = TradeEventsSseClient(your_app_key, your_app_secret, region_id, host=optional_api_endpoint)
trade_events_client.on_log = _on_log
trade_events_client.on_connect = my_on_connect
trade_events_client.on_events_message = my_on_events_message
trade_events_client.on_fatal_error = my_on_fatal_error

# One App Key can hold only one active subscription across gRPC and SSE.
# Close the gRPC subscription first, otherwise the request is rejected with HTTP 429 and retried.
# This method is synchronous.
trade_events_client.do_subscribe([account_id])

Response Example​

Position event scene type

{
"account_id": "1276674499001173504",
"position_id": "037SDSML6O6DT0KHK6R4000000",
"quantity": "5",
"symbol": "AAPL261120C00305000",
"instrument_type": "OPTION",
"market": "US",
"status": "exercised",
"biz_type": "OPTION_EXERCISE"
}
subscribeType: 4
contentType: "application/json"
requestId: "ab39b532-3ad4-46c4-823d-3dff12e0f1b9"