Stream Data using Python in 8 lines of Code
Tags: python, realtimedata, livestreamingdata, websocket
Published: 2023-11-16 13:42:40

Given the increasing prevalence of WebSockets, it is natural for novice developers to seek access. Creating a server and client for experimenting with data via WebSockets seems straightforward. Yet, it is significantly more advantageous to utilize a WebSocket that delivers real-time data.

Now, let's dive in.
To engage with this guide, ensure you have:

- Access to Google Colab or have Jupyter Notebook or Python installed.
- Fundamental proficiency in Python.

We will leverage Google Colab to make this tutorial exceptionally user-friendly.

```
!pip install tradermade==0.8.0
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zmxdlrhlot15hkakmqd.jpg)

After installing tradermade, import the stream from the library.

```
from tradermade import stream
```

Obtain your [WebSocket API key from TraderMade](https://tradermade.com/tutorials/how-to-start-a-websocket-trial-and-subscription-plan) by initiating a trial (a process that only takes seconds) and commence streaming live data. Use the "set_ws_key" command to establish the key, as demonstrated below.

```
api_key = “api_key”
# set streaming key — not the same as rest API key
stream.set_ws_key(api_key)
```
Specify the symbols you wish to reference, separated by commas. You have the option to utilize up to 10 currency pair symbols free of charge for a duration of two weeks.

```
stream.set_symbols(“USDJPY,EURGBP”)
```
After configuring the symbols and API key, we can establish a function that can be passed to the stream_data function, as illustrated below.

In this instance, we've employed the print_message function. However, you can use it for tasks like writing to a file or a Redis database.

```
def print_message(data):
print(f”Received: {data}”)
# Set the callback for receiving messages
stream.stream_data(print_message)
```

Initiate your program using the connect command after defining the function and passing it to the stream_data function.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qfl6s7n0q65l204rltoo.jpg)

And that’s it! You can see live data streaming instantly. You can further modify this function to parse the JSON responses and format the timestamp as you deem fit.

Hope this tutorial is helpful. Please leave your comments, like, and share this tutorial. We would love to hear from you.

> Also, take a glance at the original tutorial by Rahul Khanna on Medium:
> [Stream Data using Python in 8 lines of Code](https://khanna-rahul.medium.com/stream-data-using-python-in-8-lines-of-code-bacb3f3cf9c5)

> Here is the tutorial originally published on the TraderMade website:
> [Stream Data Using Python in 8 Lines of Code](Stream Data Using Python in 8 Lines of Code)

> Along with that, you can go through a few other Python-based tutorials:
> 1. [Build Your First Currency Converter App Using Python](https://tradermade.com/tutorials/build-your-first-currency-converter-app)

> 2. [Enhancing WebSocket Scalability with a Python WebSocket Proxy](https://tradermade.com/tutorials/enhancing-websocket-scalability-with-a-python-websocket-proxy)

> 3. [Create Real-time OHLC Data with Python WebSocket](https://tradermade.com/tutorials/ohlc-python-websocket)