Close Navigation
Learn more about IBKR accounts
Why We Use Apache Beam For Our Systematic Trading Data Pipeline

Why We Use Apache Beam For Our Systematic Trading Data Pipeline

Posted June 8, 2020
Ajet Luka
Robot Wealth

Excerpt

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
Arguments:
row {dict} — A dict with Keys being the column name and values being the column value
Returns:
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:
row.update({‘error’:True})
logging.error(‘Integrity Check error uploading to datastore ….’)
return row
else:
row.update({‘error’:False})
return row

def run():
options = PipelineOptions()
p = beam.Pipeline(options=options)
PriceCheck = (
p
| ‘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},
{‘ticker’:’RW’,’open’:11,’high’:11,’low’:8,’close’:9},
{‘ticker’:’RW’,’open’:10,’high’:12,’low’:8,’close’:10},
{‘ticker’:’RW’,’open’:10,’high’:11,’low’:8,’close’:17}])
| ‘Row Integrity Check’ >> beam.Map(price_integrity)
| ‘Print Results’ >> beam.Map(print)
)
p.run()
run()

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 Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or Robot Wealth and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.