Menu Home

Quickly run up microservices for your trading apps

In todays world of microservices, Flask is a great compact framework in Python for quickly creating web services. For simple services, it's faster to work with then Django (which incidentally is great for more complex services/sites).

For instance, you can build a self-contained service that serves up random price data over JSON, in 15 lines of code!

You start by installing Flask:

pip install Flask

Then create a simple wrapper around your library. In this instance our library is one we have seen before which generates prices using a geometric Brownian motion process.

from flask import app, Flask, jsonify, request
import bm

app = Flask(__name__)

def gbm():
    args = request.args

    periods = int(args.get('periods'))
    start_price = float(args.get('startPrice'))
    mu = float(args.get('mu'))
    sigma = float(args.get('sigma'))
    delta = float(args.get('delta'))

    prices = bm.generate_gbm_prices(periods, start_price, mu, sigma, delta)
    return jsonify(result=prices.tolist())

if __name__ == "__main__":

We the run our application:

$ python
 * Running on (Press CTRL+C to quit) - - [23/Jun/2015 21:33:06] "GET /gbm?periods=100&startPrice=70.0&mu=0.05&sigma=0.30&delta=1.0 HTTP/1.1" 200 -

And verify we're getting data back!

service response

You can use the following unit test to validate the service:

import json
import unittest

from services import price_service

class ServicesTest(unittest.TestCase):
    def setUp(self): =

    def test_gbm(self):
        data = {
            'periods': 100,
            'startPrice': 70.0,
            'mu': 0.05,
            'sigma': 0.30,
            'delta': 1.0

        response ='/gbm', query_string=data)
        self.assertEqual(200, response.status_code)
        data = json.loads(response.get_data().decode('utf-8'))
        self.assertEqual(100, len(data['result']))

This gives you a nice easy way to scale up your dumb (non-persistent) services to reside onto separate hosts or containers.

Full code is available here.

Categories: Python

Tagged as:


5 replies

  1. Hi, nice post. I am just found out about Flask (and totally new to it). It seems like a great way to parallelize apps. One question I have is where the producer publishing the data? Additionally, would appending it in to a global queue work so other processes (say the strategy) can read off of it?


    1. In this example the producer is the price generator in our bm module. A web-based service such as this example works in a request/response manner, making it not directly suitable for publication to a queue. To achieve your goal, you have a few options.

      1. Create a component that continually polls the service and publishes the data to a queue (pretty inefficient).

      2. Write a queue publisher that repeatedly generates new data via the bm module publishing this data directly to a queue (I'd recommend this approach).

      3. Use websockets or HTTP2's server push technology. See here for more info

    1. Yeah, Django is great for building web-sites, but not lightweight services.

      There's also Tornado if you want async support built in.

Leave a Reply

Your email address will not be published. Required fields are marked *