Streaming
The Trading Economics API streaming endpoint can be used to receive live market data utilizing a persistent web socket connection. Streaming data from the API consists of making an authorization request and leaving the socket open to continually receive data. You can authorize using your API client credentials (key/secret). Then you will be able to subscribe to 1 or more of our streaming channels.
Get live market
Subscribe to markets streaming data.
import tradingeconomics as te
import json
te.login('you_private_key')
def on_message(ws, message):
print(json.loads(message))
te.subscribe('EURUSD:CUR')
te.run(on_message)
te.subscribe( ['EURUSD:CUR', 'AAPL:US', 'CL1:COM'] )
Clone the repository
git clone https://github.com/tradingeconomics/tradingeconomics-js
Access the folder
cd tradingeconomics-js/Examples/stream-nodejs/
Install dependencies
npm install
In userKey.js file, set-up your client key and secret. Then subscribe to markets.
Client = new te_client({
url: 'wss://stream.tradingeconomics.com/',
key: 'API_CLIENT_KEY', // <--
secret: 'API_CLIENT_SECRET' // <--
//reconnect: true
});
Client.subscribe('EURUSD:CUR')
Client.subscribe(['EURUSD:CUR', 'AAPL:US', 'CL1:COM'])
Only a single market topic EURUSD:CUR can be subscribed using guest:guest. For other markets topic, a “key:secret” is required.
using System.Net.WebSockets;
using System.Text;
try
{
using (var cws = new ClientWebSocket())
{
await cws.ConnectAsync(new Uri($"wss://stream.tradingeconomics.com/?client='your_private_key'"), CancellationToken.None);
if (cws.State == WebSocketState.Open)
{
var buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(@"{""topic"": ""subscribe"", ""to"": """ + "market_symbol" + @""" }"));
await cws.SendAsync(buffer, WebSocketMessageType.Binary, true, CancellationToken.None);
}
await Task.Delay(1024);
while (cws.State == WebSocketState.Open)
{
var buffer = new ArraySegment<byte>(new byte[1024]);
var result = await cws.ReceiveAsync(buffer, CancellationToken.None);
if (result.MessageType == WebSocketMessageType.Close)
{
await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
}
Console.WriteLine($"Receiving: {Encoding.UTF8.GetString(buffer.Array, 0, result.Count).Trim()}");
}
}
}
catch (Exception e)
{
Console.WriteLine($"Error with message: {e.Message}");
}
| s | pch | nch | price | dt | state | type | dhigh | dlow | o | prev | topic |
|---|---|---|---|---|---|---|---|---|---|---|---|
| “EURUSD:CUR” | 1.09 | 0.01205 | 1.11265 | 1689184474970 | open | currency | 1.1133 | 1.1002 | 1.09651 | 1.1006 | “EURUSD:CUR” |
| “EURUSD:CUR” | 1.1 | 0.01208 | 1.11268 | 1689184480179 | open | currency | 1.1133 | 1.1002 | 1.09651 | 1.1006 | “EURUSD:CUR” |
| “EURUSD:CUR” | 1.1 | 0.01206 | 1.11266 | 1689184482085 | open | currency | 1.1133 | 1.1002 | 1.09651 | 1.1006 | “EURUSD:CUR” |
Response fields
| Field | Type | Description | Example |
|---|---|---|---|
| s | string | Unique symbol used by Trading Economics | “EURUSD:CUR” |
| pch | number | Percentage change since the previous value | 0.13 |
| nch | number | Absolute net change since the previous value | 0.0013 |
| price | number | Latest market price | 1.1057 |
| dt | timestamp | Timestamp of the last market price (epoch milliseconds) | 1681474172576 |
| state | string | Market state (open or close) | “open” |
| type | string | Market type | “currency” |
| dhigh | number | Highest value of the day | 1.1075 |
| dlow | number | Lowest value of the day | 1.1042 |
| o | number | Opening price of the day | 1.0912 |
| prev | number | Previous closing price | 1.1044 |
| topic | string | Topic or subscription symbol | “EURUSD:CUR” |