Widget SDK Reference
This SDK reference provides comprehensive details about the methods, interfaces, and message types available in the TakeProfit Widget SDK. It is designed to help developers effectively integrate and utilize the SDK to build interactive widgets within the TakeProfit platform.
- Interfaces
- Message Type Guards
- Classes and Methods
- TPWidgetSDK
- Methods
connect()
disconnect()
hideLoader()
getWidgetState()
getChannelState()
subscribeWidgetChange(callback)
unsubscribeWidgetChange(callback)
subscribeChannelChange(callback)
unsubscribeChannelChange(callback)
requestChangeSecurity(securityID, callback)
addMenuItem(label, callback, disabled)
updateMenuItem(menuItemId, disabled, menuItemLabel)
removeMenuItem(menuItemId)
- Methods
- TPWidgetSDK
- Internal Mechanisms
- Usage Examples
- Error Handling
- Best Practices
- Appendix
The TakeProfit Widget SDK facilitates the integration of custom widgets into the TakeProfit platform. It provides a set of APIs for managing widget states, handling security changes, and interacting with platform events. The SDK leverages the postMessage
API to enable communication between the embedded widget (via an iframe) and the parent platform.
Interfaces
Channel
Represents a trading channel within the TakeProfit platform, containing information about the current security and its identifier.
- Properties:
ID
(string
): Unique identifier for the channel.securityID
(SecurityID | null
): Identifier for the associated security.null
if no security is selected.security
(Security | null
): Details of the current security.null
if no security is selected.
WidgetStateInfo
Represents the current state of the widget, including its active status and display mode.
- Properties:
widgetID
(string
): Unique identifier for the widget.channelID
(string | null
): Identifier of the associated channel.null
if not associated with any channel.active
(boolean
): Indicates whether the widget is active.fullscreen
(boolean
): Indicates whether the widget is displayed in fullscreen mode.
Security
Provides detailed information about a specific financial security, such as a stock.
- Properties:
ID
(SecurityID
): Unique identifier for the security (e.g.,"AAPL;BATS"
).ticker
(string
): Ticker symbol of the security (e.g.,"AAPL"
).precision
(number
): Decimal precision for pricing.name
(string
): Full name of the security (e.g.,"Apple Inc."
).exchangeCode
(string | undefined
): Code of the exchange.iconURL
(string | undefined
): URL to the security’s icon.iconBase64
(string | undefined
): Base64-encoded icon data.iconBackgroundColor
(string | undefined
): Background color for the icon.className
(string | undefined
): Classification name (e.g.,"Common stocks"
).classNameTopLevel
(string | undefined
): Top-level classification (e.g.,"Equity"
).companyName
(string | undefined
): Name of the company.countryCode
(string | undefined
): Country code (e.g.,"US"
).exchangeAlias
(string | undefined
): Alias for the exchange (e.g.,"NASDAQ"
).industryName
(string | undefined
): Name of the industry (e.g.,"Radio & TV communications equipment"
).
Message Types
Defines the structure of messages exchanged between the widget and the TakeProfit platform.
WidgetUpdateMessage
Updates the state of the widget.
- Properties:
type
('widgetUpdate'
): Message type identifier.payload
(WidgetStateInfo
): New state information for the widget.
ChannelUpdateMessage
Updates the state of a channel.
- Properties:
type
('channelUpdate'
): Message type identifier.payload
(Channel
): New channel information.
RequestSecurityChange
Requests a change in the selected security.
- Properties:
type
('requestSecurityChange'
): Message type identifier.payload
:requestId
(number
): Unique identifier for tracking the request.securityID
(SecurityID
): Identifier of the new security to be selected.
SecurityChangeResult
Indicates the result of a security change request.
- Properties:
type
('securityChangeResult'
): Message type identifier.payload
:isSuccessful
(boolean
): Indicates if the security change was successful.requestId
(number
): Identifier for tracking the request.error
(string
): Error message if the request failed.
RequestAddMenuItem
Requests the addition of a menu item to the widget header.
- Properties:
type
('requestAddMenuItem'
): Message type identifier.payload
:requestId
(number
): Unique identifier for tracking the request.menuItemLabel
(string
): Label for the new menu item.eventName
(string
): Event name triggered when the menu item is clicked.menuItemId
(number
): Identifier for the new menu item.disabled
(boolean
): Indicates if the menu item should be disabled.
MenuEventMessage
Triggered when a menu item is clicked.
- Properties:
type
('menuEvent'
): Message type identifier.payload
:eventName
(string
): Name of the event associated with the clicked menu item.
Other Message Types
RequestInitData
: Used to request initial data. No payload.RequestCloseConnect
: Used to request the closing of a connection. No payload.
Union Type: Message
A union type encompassing all possible message types exchanged between the widget and the platform.
Message Type Guards
Type guard functions to determine the type of incoming messages.
isWidgetUpdateMessage
Checks if a message is a WidgetUpdateMessage
.
isChannelUpdateMessage
Checks if a message is a ChannelUpdateMessage
.
isSecurityChangeResult
Checks if a message is a SecurityChangeResult
.
isMenuItemRequestResult
Checks if a message is a ManageMenuItemResults
type.
isMenuEventMessage
Checks if a message is a MenuEventMessage
.
Classes and Methods
TPWidgetSDK
The primary class for interacting with the TakeProfit platform. It provides methods for establishing connections, managing widget and channel states, handling security changes, and managing menu items.
Methods
Method | Description |
---|---|
connect() | Establishes a connection with the platform and initializes message handling. |
disconnect() | Removes message event listeners, effectively disconnecting the widget from the platform. |
hideLoader() | Signals the platform to hide the loading screen, indicating that the widget is ready. |
getWidgetState() | Retrieves the current state of the widget. |
getChannelState() | Retrieves the current state of the channel. |
subscribeWidgetChange(callback) | Subscribes to widget state changes. |
unsubscribeWidgetChange(callback) | Unsubscribes from widget state changes. |
subscribeChannelChange(callback) | Subscribes to channel state changes. |
unsubscribeChannelChange(callback) | Unsubscribes from channel state changes. |
requestChangeSecurity(securityID, callback) | Requests a change in the selected security. |
addMenuItem(label, callback, disabled) | Adds a new menu item to the widget header. |
updateMenuItem(menuItemId, disabled, menuItemLabel) | Updates an existing menu item in the widget header. |
removeMenuItem(menuItemId) | Removes a menu item from the widget header. |
connect()
Description: Establishes a connection with the TakeProfit platform by adding a message event listener and requesting initial data.
Usage:
Behavior:
- Adds an event listener for messages from the parent window.
- Sends a
RequestInitData
message to the parent to retrieve initial widget and channel states.
disconnect()
Description: Disconnects the widget from the TakeProfit platform by removing the message event listener.
Usage:
Behavior:
- Removes the message event listener, stopping any further communication with the platform.
hideLoader()
Description: Signals the TakeProfit platform to hide the loading screen, indicating that the widget has finished loading and is ready for interaction.
Usage:
Behavior:
- Sends a
RequestHideLoader
message to the parent window.
getWidgetState()
Description: Retrieves the current state of the widget.
Returns: WidgetStateInfo | null
- The current widget state or null
if not available.
Usage:
getChannelState()
Description: Retrieves the current state of the channel.
Returns: Channel | null
- The current channel state or null
if not available.
Usage:
subscribeWidgetChange(callback)
Description: Subscribes to changes in the widget state. The provided callback is invoked whenever the widget state updates.
Parameters:
callback
((widgetState: WidgetStateInfo | null) => void
): Function to be called with the new widget state.
Usage:
unsubscribeWidgetChange(callback)
Description: Unsubscribes from widget state changes, removing the specified callback from the subscription list.
Parameters:
callback
((widgetState: WidgetStateInfo | null) => void
): The callback function to remove.
Usage:
subscribeChannelChange(callback)
Description: Subscribes to changes in the channel state. The provided callback is invoked whenever the channel state updates.
Parameters:
callback
((channelState: Channel | null) => void
): Function to be called with the new channel state.
Usage:
unsubscribeChannelChange(callback)
Description: Unsubscribes from channel state changes, removing the specified callback from the subscription list.
Parameters:
callback
((channelState: Channel | null) => void
): The callback function to remove.
Usage:
requestChangeSecurity(securityID, callback)
Description: Requests a change in the selected security. The callback is invoked with the result of the request.
Parameters:
securityID
(SecurityID
): Identifier of the new security to select (e.g.,"AAPL;BATS"
).callback
((result: SecurityChangeResult) => void
): Function to handle the result of the security change request.
Returns: void
Usage:
addMenuItem(label, callback, disabled)
Description: Adds a new menu item to the widget header. When the menu item is clicked, the provided callback is executed.
Parameters:
label
(string
): The display label of the menu item.callback
(() => void
): Function to execute when the menu item is clicked.disabled
(boolean
, optional): Indicates whether the menu item should be disabled. Defaults tofalse
.
Returns: Promise<number>
- Resolves with the ID of the added menu item if successful.
Usage:
updateMenuItem(menuItemId, disabled, menuItemLabel)
Description: Updates an existing menu item in the widget header.
Parameters:
menuItemId
(number
): The ID of the menu item to update, as returned byaddMenuItem
.disabled
(boolean
, optional): Indicates whether the menu item should be disabled.menuItemLabel
(string
, optional): New label for the menu item.
Returns: Promise<boolean>
- Resolves to true
if the update was successful.
Usage:
removeMenuItem(menuItemId)
Description: Removes a menu item from the widget header.
Parameters:
menuItemId
(number
): The ID of the menu item to remove, as returned byaddMenuItem
.
Returns: Promise<boolean>
- Resolves to true
if the removal was successful.
Usage:
Internal Mechanisms
Understanding the internal workings of the SDK can help developers troubleshoot issues and implement advanced functionalities.
Message Handling
The SDK communicates with the TakeProfit platform via the postMessage
API. It listens for incoming messages from the parent window and processes them based on their type.
- Security: Only messages from trusted origins (
takeprofit.com
) are processed. - Type Guards: The SDK uses type guard functions (e.g.,
isWidgetUpdateMessage
) to determine the type of incoming messages. - State Updates: Upon receiving relevant messages, the SDK updates internal state variables (
widgetState
,channelState
) and notifies subscribed callbacks.
Subscription Management
The SDK maintains various subscription lists and maps to handle callbacks for different events:
- Widget and Channel Changes: Arrays (
widgetChangeSubscriptions
,channelChangeSubscriptions
) store callback functions that are invoked when the widget or channel state updates. - Security and Menu Item Requests: Maps (
changeSecuritySubscriptions
,menuItemSubscriptions
) track callbacks associated with specific request IDs, ensuring the correct callback is executed when a response is received. - Menu Item Events: A map (
menuItemsCallbacks
) associates event names with callback functions, allowing the SDK to execute the appropriate callback when a menu item is clicked.
Usage Examples
Initializing the SDK
To start using the SDK, initialize the connection and hide the loader once the widget is ready.
Changing Security
Request a change in the selected security and handle the result.
Managing Menu Items
Add a new menu item and handle its click event.
Subscribing to State Changes
Listen for updates to the widget and channel states.
Error Handling
The SDK uses Promises for methods that involve asynchronous operations (e.g., adding, updating, removing menu items). It’s essential to handle both successful and failed operations to ensure a smooth user experience.
Example:
Ensure that callbacks appropriately handle success and error scenarios to maintain widget stability.
Best Practices
-
Unique
requestId
: The SDK automatically incrementsmessageCounter
to generate uniquerequestId
s. Avoid manually settingrequestId
s to prevent conflicts. -
Subscription Management: Always unsubscribe from state changes when they’re no longer needed to prevent memory leaks.
-
Error Handling: Implement comprehensive error handling for all SDK methods to gracefully manage failures.
-
Performance: Minimize the number of subscriptions and callbacks to essential ones to optimize performance.
Appendix
Type Definitions
For a complete understanding of the types used within the SDK, refer to the Type Definitions below.
SecurityID
A template literal type representing the security identifier in the format "TICKER;EXCHANGE"
.
ManageMenuItemResults
A union type encompassing the results of menu item operations.
Message
A union type that includes all possible message types exchanged between the widget and the platform.
Additional Interfaces and Types
RequestUpdateMenuItem
Requests an update to an existing menu item in the widget header.
- Properties:
type
('requestUpdateMenuItem'
): Message type identifier.payload
:requestId
(number
): Unique identifier for tracking the request.menuItemId
(number
): ID of the menu item to update.disabled
(boolean
, optional): Indicates if the menu item should be disabled.menuItemLabel
(string
, optional): New label for the menu item.
RequestRemoveMenuItem
Requests the removal of a menu item from the widget header.
- Properties:
type
('requestRemoveMenuItem'
): Message type identifier.payload
:requestId
(number
): Unique identifier for tracking the request.menuItemId
(number
): ID of the menu item to remove.
AddMenuItemResult
Indicates the result of a menu item addition request.
- Properties:
type
('addMenuItemResult'
): Message type identifier.payload
:isSuccessful
(boolean
): Indicates if the menu item was successfully added.requestId
(number
): Identifier for tracking the request.error
(string
): Error message if the request failed.
UpdateMenuItemResult
Indicates the result of a menu item update request.
- Properties:
type
('updateMenuItemResult'
): Message type identifier.payload
:isSuccessful
(boolean
): Indicates if the menu item was successfully updated.requestId
(number
): Identifier for tracking the request.error
(string
): Error message if the request failed.
RemoveMenuItemResult
Indicates the result of a menu item removal request.
- Properties:
type
('removeMenuItemResult'
): Message type identifier.payload
:isSuccessful
(boolean
): Indicates if the menu item was successfully removed.requestId
(number
): Identifier for tracking the request.error
(string
): Error message if the request failed.
Type Definitions
SecurityID
A template literal type representing the security identifier in the format "TICKER;EXCHANGE"
.
ManageMenuItemResults
A union type encompassing the results of menu item operations.
Message
A union type that includes all possible message types exchanged between the widget and the platform.