Why We Use Apache Beam For Our Systematic Trading Data Pipeline

Robot Wealth

Robot Wealth
Visit: Robot Wealth



Apache Beam

Apache Beam is a unified programming model for batch and streaming data processing jobs. It comes with support for many runners such as Spark, Flink, Google Dataflow and many more (see here for all runners).

You can define your pipelines in Java, Python or Go. At this time the Java SDK is more mature with support for more database connections, but Python is being rapidly developed and comes at a close second, Go is still in its early stages of development.

In the Robot Wealth batch data pipeline, we rely on Beam (on the Google Dataflow runner) for:

  • Downloading data from various APIs
  • Loading it to Google Storage
  • Transforming and enriching the data
  • Calculating features
  • Loading it to Big Query
  • Data integrity checks

This gives us a scalable data pipeline which is also cost-efficient, because you only pay for Beam when you are using it.

Here is how our batch data pipeline currently looks:

Why We Use Apache Beam For Our Systematic Trading Data Pipeline

The great thing about running Beam on Google Cloud is how seamlessly everything works together. In fact, every connection between the technologies used in the pipeline has native support in Beam.

The most appealing features that make Beam the right choice for our data pipeline are

  • Autoscaling
  • GCP Integration
  • Easy to maintain codebase.

Apache Beam in Action in a Trading Workflow

Let’s take a look at Beam in action.

In the following code block, we will be defining a data integrity check pipeline that logs an error if OHLC data is faulty.

import apache_beam as beam
from apache_beam.options.pipeline_options import PipelineOptions
import logging
def price_integrity(row):
“””A function that checks the integrity of a OHLC row,
High must be the largest or equal data point in the row and Low must be the smallest or equal
row {dict} — A dict with Keys being the column name and values being the column value
bool — True if row has errors False if row is good

#Checks that the low(high) price is the lowest(highest) or equall
low_error = not(round(float(row[‘low’]),3) <= round(float(row['high']),3) and round(float(row['low']),3)<= round(float(row['open']),3) and round(float(row['low']),3) <= round(float(row['close']),3))
high_error = not(round(float(row[‘high’]),3) >= round(float(row[‘low’]),3) and round(float(row[‘high’]),3) >= round(float(row[‘open’]),3) and round(float(row[‘high’]),3) >= round(float(row[‘close’]),3))
if low_error or high_error:
logging.error(‘Integrity Check error uploading to datastore ….’)
return row
return row

def run():
options = PipelineOptions()
p = beam.Pipeline(options=options)
PriceCheck = (
| ‘Mimic BigQuery Read’ >> beam.Create([{‘ticker’:’RW’,’open’:10,’high’:11,’low’:8,’close’:10},
< {'ticker':'RW','open':10,'high':11,'low':8,'close':11},
| ‘Row Integrity Check’ >> beam.Map(price_integrity)
| ‘Print Results’ >> beam.Map(print)

Visit Robot Wealth to read the full article and download additional programming code: https://robotwealth.com/why-we-use-apache-beam-for-our-trading-data-pipeline/?pa=CEFCE36499

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from Robot Wealth and is being posted with permission from Robot Wealth. The views expressed in this material are solely those of the author and/or Robot Wealth and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.