CFDs are complex instruments and come with a high risk of losing money rapidly due to leverage. 66 % of retail investor accounts lose money when trading CFDs with this provider. You should consider whether you understand how CFDs work and whether you can afford to take the high risk of losing your money.

[ANNOUNCEMENT] WebSocket API

I’m proud to announce the Product Quotes Websocket API. This websocket API allows a proper channel to subscribe for product quote updates in real time via websockets protocol.

The main functionalities of this websocket API are the following:

Product Subscription request:

// subscription request
{
   "op": "subscribe",
   "productNames": [ productNames ]
}

// response
// the products returned are the products requested which were successfully added. 
{
   "op": "subscribed",
   "status": OK | WARN,
   "timestamp": ecochTimestamp,
   "products": [ {"productName": ..., "quote": ... } ]
}

Important: Don’t send more than 100 product names per subscribe request. In order to subscribe to more than a hundred products it is possible to send multiple subscribe requests. This recommendation is a consequence of having a maximum frame size of 65536 bytes.

Unsubscription

Once subscribed to products, it is possible to unsubscribe from them. There could be an unsubscription from several products or from all.

// request
{
   "op": "unsubscribe",
   "productNames": [ productNames ]
}


{
   "op": "unsubscribeAll"
}

// response
// the productNames are the product names with effective unsubscription
{
   "op": "unsubscribed",
   "status": "OK",
   "timestamp": epoch,
   "productNames": [ productNames ]
}

Product Quotes

New product quotes will be received for subscribed products.

{"op": "pq", "productName": fullProductName, "quote": quote, "timestamp": epochTimestamp}

Heartbeats

There will be a heartbeat being sent from the server periodically to detect if the websocket is still opened and working.

{"op": "hb", "timestamp": epochTimestamp}

Example of websocket API usage in Python 3

# requires websockets (sudo pip install websockets)
# requires asyncio
# requires usage of Python 3
import asyncio
import websockets
import json
from io import StringIO

async def test():
    url = "ws://api.darwinex.com/quotewebsocket"
    bearerToken = "<place-your-access-token>"
    stringIO = StringIO()
    async with websockets.connect(url, extra_headers={'Authorization' : 'Bearer ' + bearerToken}) as websocket:
       #Client async code
       json.dump({ 'op': 'subscribe', 'productNames' : [ 'DWC.4.20', 'ULI', 'DWZ' ] }, stringIO)
       await websocket.send(stringIO.getvalue())
       while True:
        response = await websocket.recv()
        print(response)

asyncio.get_event_loop().run_until_complete(test())

8 Likes

In addition, we have improved the existing Product Quotes API to allow streaming of quotes via HTTP REST for a time longer than 1 minute. In fact, the streaming should be working now for an hour.

5 Likes

:muscle:

Serious improvements, thanks a lot!

4 Likes

A great improvement, thank you!

5 Likes

Other taste. Using aiohttp and pandas implementation:

import aiohttp
import pandas as pd
import json
from IPython.display import display, clear_output


quotes = pd.DataFrame()
bearerToken = 'your-token-here'
session = aiohttp.ClientSession()
async with session.ws_connect("ws://api.darwinex.com/quotewebsocket", headers={'Authorization' : 'Bearer ' + bearerToken}) as ws:
    
    await ws.send_json({ 'op': 'subscribe', 'productNames' : [ 'DWC.4.20', 'ULI', 'DWZ' ] })

    async for msg in ws:
        if msg.data[7:9] == 'pq':
            quotes = quotes.append(json.loads(msg.data), ignore_index=True)
            clear_output(wait=True)
            display(quotes.tail(7))
5 Likes