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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. RxJava in Action with Financial Market Data: Part 1

RxJava in Action with Financial Market Data: Part 1

RxJava is reactive and reactive is hot. Learn how to use RxJava (and learn reactive programming on the way) with this hands-on example using financial data.

Daniel Sebban user avatar by
Daniel Sebban
·
Jul. 22, 15 · Tutorial
Like (5)
Save
Tweet
Share
6.72K Views

Join the DZone community and get the full member experience.

Join For Free

In one of my recent projects I automated a trading strategy using Iteractive Brokers Java API, the perfect companion framework to handle large amount of live and historical data is RxJava. Reading the doc and examples of RxJava can be intimidating and quite abstract, here is a hands on example of how to use it and what it can do:

  • Feed market data to the RxJava marketDataObservable class
  • Aggregate tick data to 1-minute bars, using groupBy, flatmap and buffer operators

1. Feed market data to the RxJava marketDataObservable class

 public void subscribeRealTimeData(Instrument instrument) {
    controller.reqTopMktData(instrument.ibContract, "232", false, new ApiController.ITopMktDataHandler() 
    {
        @Override
        public void tickPrice(TickType tickType, double price, int canAutoExecute) {

            if (tickType == TickType.ASK) 
            {
                log.info("IB tick " + new Date() + " price " + price);
                LivePriceEvent priceEvent = new LivePriceEvent(System.currentTimeMillis(), instrument, new BigDecimal(price).setScale(3, RoundingMode.UP));
                marketDataObservable.push(priceEvent);
            }

        }

Now each time a tick arrives from IB, it will be pushed to our Obseravble. Now we can now fold the data as we want using the different opearator of RxJava Observable

2. Aggregate tick data to 1-minute bars

public void aggregateLiveMinuteBar() {

    observable().
            ofType(LivePriceEvent.class). //filter on live ticks
            groupBy(LivePriceEvent::getInstrument). // group by instrument i.e AAPL, GOOG
            flatMap(grouped -> grouped.buffer(2, 1)). // take each 2 consecutive events
            subscribe(listOf2 -> {
                LivePriceEvent lastEvent = listOf2.get(0);
                int lastMinute = new DateTime(lastEvent.getCreateTimestamp()).minuteOfHour().get();
                int currentMinute = new DateTime(listOf2.get(1).getCreateTimestamp()).minuteOfHour().get();
        //when minute is crossed , we push the result back in the observable to make it available to other subscribers
        if (lastMinute != currentMinute) {
                    push(new LiveBarEvent(TimeUnit.MINUTES, lastEvent.createTimestamp, lastEvent.getInstrument(), lastEvent.getPrice()));
                }

    });


}

3. Running the minute bar aggregator against IB demo feed

just follow the instructions from the github

$ git clone [https://github.com/dsebban/blog-post-1] rx-ib
$ cd rx-ib
$ mvn package
$ foreman start

you should see something like this

daniel@daniel-desktop:~/Projects/dice_bot/blog-post-1$ foreman start

16:22:37 ib.1   | started with pid 29935
16:22:37 app.1  | started with pid 29937
16:22:47 app.1  | Server Version:76
16:22:47 app.1  | TWS Time at connection:20150717 16:22:44 IST
16:22:47 app.1  | Jul 17, 2015 4:22:47 PM daniels.reactive.blog.InteractiveBrokersFeed$2 connected
16:22:47 app.1  | INFO: connected
16:22:48 app.1  | Jul 17, 2015 4:22:48 PM daniels.reactive.blog.InteractiveBrokersFeed$2 message
16:22:48 app.1  | SEVERE: id -1 errocode = 2119msg Market data farm is connecting:ibdemo
16:22:48 app.1  | Jul 17, 2015 4:22:48 PM daniels.reactive.blog.InteractiveBrokersFeed$2 message
16:22:48 app.1  | SEVERE: id -1 errocode = 2104msg Market data farm connection is OK:ibdemo
16:22:48 app.1  | Jul 17, 2015 4:22:48 PM daniels.reactive.blog.InteractiveBrokersFeed$1 tickPrice
16:22:48 app.1  | INFO: IB tick Fri Jul 17 16:22:48 IDT 2015 price 122.09
16:22:48 app.1  | Jul 17, 2015 4:22:48 PM daniels.reactive.blog.InteractiveBrokersFeed$1 tickPrice
16:22:48 app.1  | INFO: IB tick Fri Jul 17 16:22:48 IDT 2015 price 122.08
16:22:52 app.1  | Jul 17, 2015 4:22:52 PM daniels.reactive.blog.InteractiveBrokersFeed$1 tickPrice
16:22:52 app.1  | INFO: IB tick Fri Jul 17 16:22:52 IDT 2015 price 122.09
16:22:58 app.1  | Jul 17, 2015 4:22:58 PM daniels.reactive.blog.InteractiveBrokersFeed$1 tickPrice
16:22:58 app.1  | INFO: IB tick Fri Jul 17 16:22:58 IDT 2015 price 122.08
16:23:00 app.1  | Jul 17, 2015 4:23:00 PM daniels.reactive.blog.InteractiveBrokersFeed$1 tickPrice
16:23:00 app.1  | INFO: IB tick Fri Jul 17 16:23:00 IDT 2015 price 122.09
16:23:00 app.1  | Jul 17, 2015 4:23:00 PM daniels.reactive.blog.Main lambda$main$0
16:23:00 app.1  | INFO: minute = 22 val=LiveBarEvent(barDuration=MINUTES, createTimestamp=1437139378855, instrument=APPL, price=122.080)

To be continued (Part 2 taking advantage of your multicore cpu with observeOn RxJava operator )

Data (computing)

Published at DZone with permission of Daniel Sebban. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Playwright vs. Cypress: The King Is Dead, Long Live the King?
  • How To Generate Code Coverage Report Using JaCoCo-Maven Plugin
  • NEXT.JS 13: Be Dynamic Without Limits
  • AWS Cloud Migration: Best Practices and Pitfalls to Avoid

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: