Streaming Forex with Python SocketIO

03/09/2021 at 04:20PM

In this tutorial, let’s go for a Python SocketIO implementation. It is vital to understand the difference between SocketIO and WebSocket. However, that’s not the scope of this tutorial. Please go through another tutorial to learn more about it. I would request you to work along with the tutorial, and you would understand why SocketIO is gaining popularity. 

Before diving deeper into the process, I would like to tell you that we will use the client code and a live SocketIO server that pushes TraderMade’s forex data to keep it simple. Now, you may ask why we are doing so? As you start, you will realise that working on one side of the example is better before digging deeper into the subject. Similarly, you will experience a real-life example which is practically used. 

Shall we begin?

Obtain SocketIO Key

To begin with, get an API key to obtain live forex data from the SocketIO server. To get the API key, please sign up for free at https://tradermade.com/signup and choose a SocketIO Trial. As you get the key, note it down securely. 

Setting up Code

Initially, we will install important Libraries to set up. (Please note that our SocketIo is compatible only with Version 2X.) 

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

Though you require a few other dependencies, the two must be exact for this example.

A Quick Introduction to SocketIO

Before moving ahead, let’s glance at the role of SocketIo in Python:

  1. The client and server send and receive messages as events in SocketIO.
  2. A smooth and collision-free communication between the two parties is established through events.

You will get more clarity on the second point as you go through the code. You can read further to learn more about it. 

Code for the Client-Side

Let’s follow small parts of the code one by one. Initially, we will import SocketIO. Then we will create an object named “Sio” to create a client and connect to the URL. 

import socketio

# standard Python
sio = socketio.Client()

As we have an object, let’s set up an event to establish a connection with the SocketIO server of TraderMade. Let’s set up the event with def connect() as shown below. This function will be called on as we connect to the server.

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

When connected, we will discharge an event to the server - “login.” Notably, we will convey the user key in JSON format received while signing up to the server and setting up our identity. Here, you might have realised that some events listen to a particular type of information. So, let’s observe the outcome further.

@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 explains what it is. So, we can cut up the handshake event and move ahead. Notably, ‘handshake’ is an event emitted by the TraderMade server. We require that on our SocketIO client-side code. This is crucial, as, without this event, we may miss the communication chain and cannot obtain the data.

From this, it may be clear that some events push data. As we receive a handshake, we will print the received data.

Received from Server
Welcome to the TMS Data Feed

We can seek the forex data now. However, how would the server understand that we need it? To do that, we need to subscribe to Symbol by discharging an event known as “SymbolSub”. Then, we need to 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')

Now, we can see the outcome of the above event. Once we know the code, we need the URL at the end to connect. And that’s it! We can now get Forex Rates for EURUSD along with a timestamp. 

Received from Server
Price Data EURUSD 1.20543 1.20543 1.20543 20210303–14:27:59.496
Take a look at the entire code here:
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')

I hope the tutorial helps. Stay tuned to get more information.