CPlugin

Realtime reference

Besides the REST calls, WebAPI pushes live data over SignalR — a protocol on top of WebSockets (with fallbacks to Server-Sent Events and long polling) that has official clients for JavaScript, .NET and Java. There is one hub for each trading platform; the pages below list every call a client can make, every event the server sends and every field of the data.

The open-source SDKs for TypeScript, Python, .NET and PowerShell wrap the REST calls and these hubs; use a SignalR client directly when your language has no SDK, or to see exactly what goes over the wire.

Connecting

  1. Address. https://cloud.mywebapi.com/hubs/mt4/v2?tradePlatform=<id> for Production, or the same path on https://pre.mywebapi.com for the Sandbox. tradePlatform is the id (GUID) of a trade platform registered in Toolbox. It is required and fixed for the whole connection: the platform is not an argument of any call and is not repeated in any event. To work with several platforms, open one connection for each.
  2. Token. The same OAuth 2.0 client-credentials token as for REST (scope webapi, from the token server of the environment you connect to). Give it to the SignalR client through its access-token option (accessTokenFactory in JavaScript), which the client calls for every connection attempt. A client that cannot do that can append signalr_token=<token> to the address.
  3. Permissions. The API client needs access to the platform, and each call lists the grant it requires (for example Request (read) on symbols) — the same grid as for REST, set in Toolbox. A refused connection or call fails with a message from the server.
  4. Lifetime. The server ends a connection when the access token it was opened with expires. Open the next one with a fresh token. A connection may hold at most 500 symbol subscriptions; symbol names are matched case-insensitively and a name longer than 64 characters is refused.
import * as signalR from "@microsoft/signalr";

const connection = new signalR.HubConnectionBuilder()
  .withUrl("https://cloud.mywebapi.com/hubs/mt4/v2?tradePlatform=<trade platform id>", {
    accessTokenFactory: () => getToken(),   // return a current token
  })
  .withAutomaticReconnect()
  .build();

connection.on("OnMT4ConnectionStatus", ({ connected }) => console.log("trade platform connected:", connected));
connection.on("OnTick", tick => console.log(tick.symbol, tick.bid, tick.ask));

// after every reconnect: nothing is replayed and the subscriptions are gone (see below)
connection.onreconnected(async () => {
  await connection.invoke("SubscribeToTicks", "EURUSD");
  // ...subscribe to everything else you use, then read the current state over REST
});

await connection.start();
await connection.invoke("SubscribeToTicks", "EURUSD");

Connection status

The event OnMT4ConnectionStatus carries { connected: boolean }: whether WebAPI is connected to the trade platform behind this hub. It is the only event that needs no subscribing call. The server sends it once right after the client connects — so opening a connection can take up to 25 seconds, as the server waits for its own connection to the trade platform to come up first — and again whenever the state changes.

Reconnecting and missed events

Push model and stream model

A subscription (SubscribeTo…) is a call that returns at once; from then on the server pushes events to the connection, each delivered to the handler registered with connection.on(eventName, …). Stop it with the matching UnsubscribeFrom…. A stream (Stream…) is a call that returns a sequence of items over connection.stream(…), which ends when the client cancels it or disconnects. Both deliver the same kind of data; use whichever fits your client. Events are one argument each — the payload object — and every payload is listed on the hub's page with its JSON property names exactly as sent (camelCase).

Batched ticks. A tick subscription sends one event per tick. SubscribeToTicksBatch sends the ticks of one symbol collected over a short interval as one event instead, which costs far less traffic at high tick rates; a batched subscriber gets no per-tick event for that symbol. Batching is about traffic, not latency — the per-tick subscription remains the lowest-latency option.

Arguments: send every one

SignalR does not apply default values declared on the server. A raw SignalR client must pass every argument of a call: a call with one missing fails with an error from the server. Where a parameter has a default, its description says which value to send for it (for example 200), and where it accepts null, send null to get the default behaviour (for example, null as the symbol of a tick stream means all symbols). The examples on the hub pages already show complete calls.