API Forex
Forex.com (StoneX / CityIndex)
Forex.com is a retail trading venue operated by StoneX. The component wraps the Trading REST API and the Lightstreamer-based streaming service used by Forex.com, StoneX and CityIndex (all three share the same platform). The following APIs are supported:
- Streaming API: Lightstreamer TLCP 2.5 over WebSocket. Price ticks, order updates, position updates, account margin and quotes are pushed in real time after a successful REST LogOn.
- REST API: session login (UserName + Password + AppKey), trade and stop-limit order management, account queries and market data. Uses HTTPS.
Components
- TsgcWSAPI_Forex: main client. Holds Credentials, exposes REST_API, the internal Lightstreamer streaming client, and the OnForex* events.
- TsgcHTTP_API_Forex: REST component, reachable through the REST_API property of the main client.
- TsgcWSPClient_Lightstreamer: internal TLCP 2.5 subprotocol client used for streaming subscriptions (PRICES / ORDERS / QUOTES / CLIENTACCOUNTMARGIN / TRADEMARGIN).
Published Properties
- Credentials: group of fields required to log in to the REST API.
- UserName: your Forex.com / StoneX / CityIndex account user name.
- Password: account password.
- AppKey: application key issued by the venue.
- AppVersion: optional client version string.
- AppComments: free-form text logged on login.
- StreamingHost: Lightstreamer host (defaults to the public streaming host).
- AutoReconnect: when enabled, forwards to the underlying WebSocket client WatchDog so that the streaming session reconnects automatically after a network drop (by default true).
- REST_API: reference to the TsgcHTTP_API_Forex component.
- Lightstreamer: reference to the internal TsgcWSPClient_Lightstreamer subprotocol client (AdapterSet is fixed at STREAMINGALL).
- SessionToken: read-only. Session token returned by the REST LogOn call; used as LS_password for Lightstreamer.
- ClientAccountId / TradingAccountId: read-only. Populated after a successful LogOn.
Published Events
OnForexConnect
Fired once the streaming session has been created and subscriptions can start.
OnForexDisconnect
Fired when the streaming session ends (server close, network drop or explicit Disconnect).
OnForexException
Raised on REST errors (including LogOn failure) and on streaming session faults.
OnForexPriceTick
Fired for every PRICES update: Bid, Offer, Price, Change, Direction, AuditId, TickDate.
OnForexOrderUpdate
Fired for every ORDERS update (new, filled, cancelled, rejected).
OnForexPositionUpdate
Fired for every open-position update on the active TradingAccount.
OnForexAccountMargin
Fired for every CLIENTACCOUNTMARGIN / TRADEMARGIN update (equity, margin, free margin, currency).
OnForexQuote
Fired for every QUOTES update (request-for-quote workflow).
Most common uses
Sample
Create the client, set credentials, connect, subscribe to a market price stream and disconnect.
oForex := TsgcWSAPI_Forex.Create(nil);
oForex.Credentials.UserName := '<username>';
oForex.Credentials.Password := '<password>';
oForex.Credentials.AppKey := '<appkey>';
oForex.OnForexPriceTick := ForexPriceTick;
oForex.Connect;
oForex.WatchMarket(401484830); // EUR/USD market id
// ... receive price ticks in OnForexPriceTick ...
oForex.Disconnect;