Streaming Forex with Python SocketIO

03/09/2021 at 04:20PM

After Python Websocket I decided to do a Python SocketIO implementation as its quite useful. I won't bore you with the details of the differences between Websocket and SocketIO but if you are interested here is a detailed article. As we work along with the tutorial you’ll see why Socketio is a popular choice.


Before we go any further for simplicity purposes we are only using client code and using a live Socketio Server that pushes forex data (in this case TraderMade). Why do it you ask? Well if you starting out it's much better to work on one side of the example before taking a deep dive into the subject. Also, it gives a real-life example that is actually in use.


So let’s begin


Get SocketIO Key

You will first need an API key to get live forex data from Socketio Server, you can do that by signup at https://marketdata.tradermade.com/signup and selecting a SocketIO Trial as shown below. Once you have your key keep it safe.


Setup Code

First, Install key Libraries to set up


python-engineio==3.14.2
python-socketio==4.3.1

There might other dependencies you may need but the above two needs to be exact for the example to work.


Quick SocketIO Intro

Before we go further a quick rundown on what Socketio actually does in python.


1) In SocketIO, messages are sent and received by both client and server as events.

2) Events help communicate between the two parties without colliding.


Ok, no need to worry about the second point, it will become clearer as we go through the code. To know in detail you can read more here.


Client-Side Code

We will go one by one through each part of the code. Firstly we will import Socketio then make an object called “sio” as shown below. This will do the work of creating a client and connecting to the URL.


import socketio

# standard Python
sio = socketio.Client()


Once we have the object we will set up an event to initially connect to the Socketio server (TraderMade). As shown below we can set up an event that has def connect() which will be invoked initially when we connect to the server.


@sio.event
def connect():
    print("I'm connected!")
    sio.emit('login', {'userKey': 'Your Streaming API Key'})


On connected, we will emit to an event on the server which in this case is “login”. So essentially we are sending the user key in JSON format we got earlier from signing up to the server to identify ourselves.

Now it would have become a little clearer that some events are listening for a certain kind of information. Let's see what happens next.


@sio.even
def connect_error():
    print("The connection failed!")

@sio.on('handshake')
def on_message(data):
    print('HandShake', data)
    sio.emit('symbolSub', {'symbol': 'EURUSD'})


Connect_error is self-explanatory so let's dissect the handshake event. So handshake is an event that the server (TraderMade) emits to (send to). So we need it on our SocketIO client-side code otherwise we will miss the chain of communication and never get data.


Now it would have become clearer that some events are pushing data. On receiving a handshake we will print the data sent to us.


Received from Server


Welcome to the TMS Data Feed

Now we can ask for forex data but how will the server know what we want. Well for that we will subscribe to Symbol by emitting to an event called “symbolSub” and we will send {‘symbol’: ‘EURUSD’} as data from the client-side.


@sio.on('price')
def on_message(data):
    print('Price Data ', data)

sio.connect('https://marketdata.tradermade.com')


Well, now it quite straight forward to see what's happening in the above event. The URL at the end is needed to finally connect once we understand the code.

Voila! We are getting Forex rates for EURUSD with a timestamp.


Received from Server


Price Data EURUSD 1.20543 1.20543 1.20543 20210303–14:27:59.496


Here is the whole code.


import socketio

# standard Python
sio = socketio.Client()

@sio.event
def connect():
    print("I'm connected!")
    sio.emit('login', {'userKey': 'streaming_api_key'})

@sio.event
def connect_error():
    print("The connection failed!")

@sio.event
def message(data):
    print('I received a message!')

@sio.on('handshake')
def on_message(data):
    print('HandShake', data)
    sio.emit('symbolSub', {'symbol': 'USDJPY'})
    sio.emit('symbolSub', {'symbol': 'GBPUSD'})
    sio.emit('symbolSub', {'symbol': 'EURUSD'})

@sio.on('price')
def on_message(data):
    print('Price Data ', data)


sio.connect('https://marketdata.tradermade.com')

Hope this helps. I will follow up with a server example very soon.