DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)
  • Python Async/Sync: Understanding and Solving Blocking (Part 1)
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch

Trending

  • Bringing Intelligence Closer to the Source: Why Real-Time Processing is the Heart of Edge AI
  • How to Format Articles for DZone
  • Detecting Bugs and Vulnerabilities in Java With SonarQube
  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  1. DZone
  2. Coding
  3. Languages
  4. Streaming Forex With Python SocketIO

Streaming Forex With Python SocketIO

Learn how to get live Forex data via SocketIO using Python. This tutorial helps you understand the steps and processes to obtain a streaming Forex data feed.

By 
Rahul Khanna user avatar
Rahul Khanna
·
Apr. 24, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
1.7K Views

Join the DZone community and get the full member experience.

Join For Free

This tutorial takes you through a Python SocketIO implementation. Understanding the difference between SocketIO and WebSocket is essential. However, that is not the scope of this article. Please go through this tutorial to explore more about it. To realize the reason behind the increasing popularity of SocketIO, I would like to request you work along with this tutorial. 

We will use the client code and SocketIO server responding with TraderMade’s Forex data. I am clarifying these aspects in the beginning as it is easy to work on one side of the example before diving deeper into the topic. At the same time, you will experience a practically used, real-life example.

On this note, let’s start.  

Acquire Your SocketIO Key

It is essential to obtain your API key to retrieve live Forex data from the SocketIO server. You can sign up for free. Then, select SocketIO trial. You will get an API key. It is advisable to note this key securely for future reference.

Code Setup

We will begin the process by installing the required libraries. It is important to note that TraderMade’s SocketIO is compatible with Version 2X only.

Python
 
python-engineio==3.14.2

python-socketio==4.3.1


We may need other dependencies, yet these two should be exactly the same to work with this example.

A Brief on the Method To Retrieve Live Forex Data

Let’s understand how real-time Forex data is obtained through SocketIO using Python:

  1. The data transfer between client and server occurs as events in SocketIO.
  2. The events establish communication between the client and server.

Read this tutorial further to know more about how events help execute communication between client and server.

Client-Side Code

We will go step by step. To start with, we will import SocketIO. Then create an object — Sio, to create a client. We will use this to connect to the URL.

Python
 
import socketio
# standard Python
sio = socketio.Client()


Firstly, we will obtain an object and then set up an event to initiate a connection with TraderMade’s SocketIO server. We will place an event with def connect (). When we connect with the server, this function will be called on. 

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


After getting connected, we will release an event to the server — “login.” After signing up initially, we received an API key. We will send that API key in JSON format to the server to set our identity. You might have realized that some events listen to specific information. We will keep perceiving the next steps.

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

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


We can easily make out what Connect-error is. TraderMade’s server sends or emits an event — handshake. We need this event on the SocketIO client-side code. It is essential to maintain the communication chain and obtain data.

Thus, you can realize that some events send data. After receiving the handshake event, we will print the data transmitted.

Python
 
Received from Server
Welcome to the TMS Data Feed


It is possible to obtain Forex data. However, the server should know that we need that. At this stage, we need to subscribe to a Symbol by discharging an event — SymbolSub.  Along with that, we will send {‘symbol’: ‘EURUSD’} as the data from the client side.

Python
 
@sio.on('price')
def on_message(data):
    print('Price Data ', data)
sio.connect('https://marketdata.tradermade.com')


We can see how the above event pushes data. We require the URL at the end to set up the connection once we know the code. There it is! We can now obtain Forex rates for EURUSD also with a timestamp.

Python
 
Received from Server
Price Data EURUSD 1.20543 1.20543 1.20543 20210303–14:27:59.496


Please have a glance at the complete set of codes here:

Python
 
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 anticipate that the tutorial will help you. We will come back with more such tutorials.

Event Python (language) Socket.IO

Published at DZone with permission of Rahul Khanna. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Python Async/Sync: Advanced Blocking Detection and Best Practices (Part 2)
  • Python Async/Sync: Understanding and Solving Blocking (Part 1)
  • Prompt Injection Is Real, So I Built a Python Firewall for LLM Pipelines
  • Building Threat Intelligence Pipelines Using Python, APIs, and Elasticsearch

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook