Market data enrichment and transformation
Transform raw market data in real-time to provide insights into market trends, asset health, and trade opportunities.
Overview
Understanding the fast-moving market is pivotal to making informed trading decisions. The market is constantly influenced by many external factors that are not reflected in the raw market data, such as geopolitical events, economic indicators, and indisutry-specific news. These factors often create rapid fluctuations that are not immediately apparent in raw market data. To make sense of these shifts, traders need external data to better understand the context behinds these price movements.
Compiling and analyzing this information in real-time is key to understanding the market and making better trading decisions. With real-time data analysis, traders can process and join raw market data and external signals as they happen. By combining these data streams, traders gain a comprehensive, up-to-the-second view of market conditions. This allows them to act quickly and confidently, making quick adjustments to maximize profits and mitigate risks.
In this tutorial, you will learn how to join market data with external data to gain a holistic view of each asset.
Prerequisites
- Ensure that the PostgreSQL interactive terminal,
psql
, is installed in your environment. For detailed instructions, see Download PostgreSQL. - Install and run RisingWave. For detailed instructions on how to quickly get started, see the Quick start guide.
- Ensure that a Python environment is set up and install the psycopg2 library.
Step 1: Set up the data source tables
Once RisingWave is installed and deployed, run the two SQL queries below to set up the tables. You will insert data into these tables to simulate live data streams.
-
The table
raw_market_data
tracks raw market data of each asset, such as the price, volume, and bid-ask price. -
The table
enrichment_data
contains external data that adds context to the raw market data. It includes additional metrics such as historical volatility, sector performance, and sentiment scores.
Step 2: Run the data generator
To keep this demo simple, a Python script is used to generate and insert data into the tables created above.
Clone the awesome-stream-processing repository.
Navigate to the market_data_enrichment folder.
Run the data_generator.py
file. This Python script utilizes the psycopg2
library to establish a connection with RisingWave so you can generate and insert synthetic data into the tables positions
and market_data
.
If you are not running RisingWave locally or using default credentials, update the connection parameters accordingly:
Step 3: Create materialized views
In this demo, you will create three materialized views to better understand the market.
Materialized views contain the results of a view expression and are stored in the RisingWave database. The results of a materialized view are computed incrementally and updated whenever new events arrive and do not require to be refreshed. When you query from a materialized view, it will return the most up-to-date computation results.
Calculate bid-ask spread
The avg_price_bid_ask_spread
materialized view calculates the average price and average bid-ask spread for each asset in five-minute time windows by using TUMBLE()
and grouping by the assed_id
and the time window.
To learn more about TUMBLE()
, see Time windows.
You can query from avg_price_bid_ask_spread
to see the results.
Calculate rolling price volatility
The rolling_volatility
materialized view uses the stddev_samp
function to calculate the rolling price volatility within five-minute time windows by using TUMBLE()
and grouping by the assed_id
and the time window.
You can query from rolling_volatility
to see the results.
Obtain a comprehensive view of each asset
The enriched_market_data
materialized view combines the transformed market data with the enrichment data. TUMBLE()
is used to group the data from enrichment_data
into five-minute time windows for each asset. Then it is joined with the volatility and bid-ask spread data.
By combining these data sources, you can obtain a more holistic view of each asset, empowering you to make more informed market decisions.
You can query from enriched_market_data
to see the results.
When finished, press Ctrl+C
to close the connection between RisingWave and psycopg2
.
Summary
In this tutorial, you learn:
- How to get time-windowed aggregate results by using the tumble time window function.
- How to join data sources with materialized views.
Was this page helpful?