Python WebSocket Client - Real-Time Order Flow (Trade Tape)
19 August 2021
In this tutorial, I will show you how to write a python program to retrieve real-time Order Flow(Trade Tape) data from TraderMades’s Forex API Order Flow (Trade Tape) Service. TraderMade offers real-time Order Flow (Trade Tape) data for a large range of Forex, Metals, and CFD’s more information can be found on our order flow page.
Let’s get started
Before we start we need to set our coding environment, we will do this in 3 simple steps.
Setup
1) Setup Python
2) Install Pip
3) Setup Project
Step 1. Install Python
Python 3.9.1 is the latest stable release and we recommend you use this where possible.
For Windows: You can download the windows installer from python.org or alternatively use the python app from the windows app store. For Linux: It's best to update apt-get before the install so run $sudo apt-get update $sudo apt-get install python3.9
Step 2. Install pip
For Windows: pip is installed by defualt For Linux: $sudo apt-get install python3-pip
Step 3. Setup Project
Create a new dir that you wish to store your program in, I have created one called /webSocketOrderFlowClient.
Now we can install the libs we require, for this example, we only need to install one external lib and that is the WebSocket Client.
For Windows and Linux pip install websocket_client
Let's write some code
Inside your directory create a new file orderFlowTestClient.py you can do this in your favourite editor or Notepad/VI if you are just getting started.
As this is a live WebSocket we want the program to continue to run whilst we have a live connection. For this, we use the thread class and the WebSocket run_forever() option.
Import the libs
import websocket import time try: import thread except ImportError: import _thread as thread f = open("webSocketOrderFlowTester.log", "a")
Create Functions
We also need to create the functions that will handle the callbacks from the WebSocket-OrderFlowClient class. These are standard handlers and will be the same for any WebSocket. For the TraderMade WebSocket on_open, we need to send back our login details these can be obtained by signing up for a free order flow trial.
def on_message(ws, message): print(message) f.flush() def on_error(ws, error): print(error) def on_close(ws): print("### closed ###") def on_open(ws): def run(*args): ws.send("{"userKey":"USER_KEY") thread.start_new_thread(run, ()) I
Now we have the logger and the handler we need to create the WebSocket, we will do this in the main function of the program. When you sign up for a trial of the Order Flow (Trade Tape) feed you will be given a connection URL this needs to be substituted into the following code block.
if __name__ == "__main__": ws = websocket.WebSocketApp("ORDER_FEED_ADDRESS", on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever()
Running the program
For Windows: $python orderFlowTestClient.py For Linux: $sudo python3 orderFlowTestClient.py
And that is it! You will now get Live Order Flow (Trade Tape) rates in the log and also in your console.
Connected {'id':4208407324,'acc':'0159809','s':'AUDJPY','amt':-10000.0,'ts':'20210819-08:06:38.039000','price':78.596,'time':1629360398} {'id':4208407344,'acc':'0319192','s':'AUDJPY','amt':-11000.0,'ts':'20210819-08:06:38.509000','price':78.599,'time':1629360398} {'id':4208407354,'acc':'0901885','s':'EURCHF','amt':-2000.0,'ts':'20210819-08:06:38.524000','price':1.07085,'time':1629360398} {'id':4208407374,'acc':'0159809','s':'AUDJPY','amt':-50000.0,'ts':'20210819-08:06:39.679000','price':78.602,'time':1629360399} {'id':4208407384,'acc':'0159809','s':'AUDJPY','amt':-500000.0,'ts':'20210819-08:06:39.679000'price':78.602,'time':1629360399} {'id':4208407394,'acc':'0159809','s':'AUDJPY','amt':-45000.0,'ts':'20210819-08:06:39.679000','price':78.602,'time':1629360399} {'id':4208407404,'acc':'0175519','s':'GBPJPY','amt':15000.0,'ts':'20210819-08:06:40.382000','price':150.21,'time':1629360400} {'id':4208407414,'acc':'0319192','s':'AUDJPY','amt':-10000.0,'ts':'20210819-08:06:40.970000','price':78.599,'time':1629360400}
Below is the full code.
import websocket import time try: import thread except ImportError: import _thread as thread f = open("webSocketTester.log", "a") def on_message(ws, message): print(message) f.write(message + " " ) f.flush() def on_error(ws, error): print(error) def on_close(ws): print("### closed ###") def on_open(ws): def run(*args): ws.send("{"userKey":"USER_KEY") thread.start_new_thread(run, ()) if __name__ == "__main__": ws = websocket.WebSocketApp("ORDER_FEED_ADDRESS", on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever()