Understanding the lemon.markets Market Data API


blog photo
Published by Marius Sprenger on September 2, 2021
Insights


Hey there. My name is Marius, and I’m the community developer at lemon.markets, an early-stage startup from the heart of Berlin, Germany. We are working on building an infrastructure that enables developers to build their own brokerage experience at the stock market. In this blog post, I want to take a closer look at one of our core products: the Market Data API. Together, we will explore how the API is structured, what kind of data you can retrieve with it and how that might be helpful for your very own trading product.

What’s the point of getting Market Data?

If you ever were in the position of wanting to develop your own trading strategy (since you managed to click on this blog post and read until this sentence, we assume that the probability of this is rather high), you most certainly thought about using (historical) market data for it. While there are some examples where quantifiable market data plays a subordinate role (sentiment analysis of text data being the most prominent example), a large majority of (automated) trading strategies indeed work with historic or real-time stock prices to make a buy or sell decision. Check out one of our previous articles for an overview of popular, beginner-friendly trading strategies. You will notice that there are many ways to integrate market data into your trading strategy, which is why we do not want to dive too deep into what the best way to do so actually is.

However, we want to outline why we at lemon.markets think that Market Data is important. The answer lies (at least partly) in our name. The goal of lemon.markets was always to improve the experience of buying equities at the stock market. While we have a clear developer focus right now, the underlying principle goes much deeper. We want to avoid that people make uninformed investment decisions. How do you avoid that? We think by using data.

“Without data you’re just another person with an opinion”

W. Edwards Deming

This quote by the famous statistician W. Edwards Deming perfectly captures how we think about lemon.markets and making investment decisions in general. If you have access to extensive data, chances are high that you will make better decisions. Obviously, historical patterns do not necessarily imply future patterns, but they are definitely a good place to start. In the case of buying equities at the stock market, this data is market data. And that is how we can bridge the gap to the famous Market for Lemons Theory, which deals with information asymmetry between buyer and seller along the example of used cars (in American slang, used cars that turn out to be broken after purchasing them, are referred to as “lemons”). By giving the buyer (our customers who want to buy or sell equities) structured and extensive information and the freedom/flexibility to build their brokerage product based on this, we believe that we can close the information asymmetry gap at the stock market (at least to some extent). By looking at our users’ behaviour, we noticed that the market data endpoints are among the most frequently used ones, which pretty much proved our point that access to market data is an important use case and thus product for us at lemon.markets.

Market Data API Structure

Realising that market data is important is one thing. Translating that realisation into an API structure is another. Below, find the structure that we came up with.

The base URL of our Market Data API is:

1https://data.lemon.markets/v1/

Using that base URL, we implemented five different endpoints that fulfil different purposes. We will discuss those in a second. However, the principle behind each endpoint structure is the same. For example, if you want to retrieve instruments, you would use the/instruments/endpoint.To specify your request, you can use different query parameters (e.g. ?search to search for ISIN, WKN or Title, or ?type to search for stocks, bonds, ETFs, warrants or funds. Find out more about the respective endpoint query parameters and the Market Data API in general in our docs).

Using the Endpoints

Actually trying out the different endpoints is probably the best way to understand the API structure. Therefore, we set up a small example project that allows you to use all Market Data API endpoints conveniently. You can find the GitHub repository here.

To work with environment variables, we first want to create a file called .env.

Let us include the following variables in there:

  • API_KEY: Your API key
  • BASE_URL: “https://data.lemon.markets/v1/”
  • MIC: The Market Identifier Code of the Trading Venue you want to address.

Using the os and the python-dotenv packages, we can access those variables in our local setup (we’ll get to that in a second). But first, let us define a helper file, which we can use to perform requests. Let’s call it helper.py. Within that class,we can define a class RequestHandler that we can later use to conveniently make API requests through a predefined function. As the Market Data API only offers GET requests, we only need one function get_data().This allows us to conveniently use the function throughout our project ( → This way, we avoid duplicated code).

1import os
2import requests
3from dotenv import load_dotenv
4class RequestHandler:
5    load_dotenv()
6    url: str = os.environ.get("BASE_URL")
7    def get_data(self, endpoint: str):
8        response = requests.get(self.url + endpoint,
9                                headers={
10                                    "Authorization": "Bearer "+os.environ.get("API_KEY")
11                                })
12        return response.json()

Next, we want to create a model for our Market Data and create a class for it. Let’s call the file MarketData.py. Using the get_response function, we can pass a specific endpoint with the function to then get the data that we want.

1from dotenv import load_dotenv
2from helper import RequestHandler
3class MarketData(RequestHandler):
4    def get_response(self, endpoint):
5        load_dotenv()
6        endpoint = endpoint
7        response = self.get_data(endpoint)
8        return response

The class comes in handy for our main.py file, which we are going to work in next. In there, we can import the Market Data class using:

1from models.MarketData import MarketData

Subsequently, we can define a number of exemplary functions to show how our endpoints work.

Trading Venues

We can define a function get_venues() to retrieve all trading venues available on lemon.markets. By defining the variable endpoint, we can define what is added to our base URL. If we wanted to retrieve a specific venue, we could add ?mic={mic} to the endpoint and access our environment variable MIC through that.

Feel free to play around with it a little bit and see how the API reacts to your modified request URLs. Check out our documentation for specific information on the /venues/ endpoint.

1def get_venues():
2    endpoint = f'venues/'
3    response = MarketData().get_response(endpoint)
4    print(response)

Instruments

Our Market Data also offers the possibility to search for/filter for different instruments. Following a similar concept as above, we define the endpoint that is added to the base URL.

1def get_instruments():
2    endpoint = f'instruments/'
3    response = MarketData().get_response(endpoint)
4    print(response)

There are a number of different query parameters that we could attach to the /instruments/ endpoint, such as

  • ?isin → retrieve a specific instrument, identified by its ISIN
  • ?search → using this query parameter, we can, e.g., search for ISIN, WKN or Title
  • ?tradable → we can filter for instruments that are currently tradable

Again, check out our docs to find a full list of query parameters for the /instruments/ endpoint.

Quotes

Using the /quotes/ endpoint, you can access (well): Quotes. Using quotes can be helpful for your trading strategy, as you get information about the price and trading volume of a specific instrument (how it is “quoted” on the stock exchange). As quotes are always returned for a specific ISIN, that query parameter is mandatory, leading to the following “minimal” request URL with the ISIN for Coinbase:

1def get_quotes():
2    endpoint = f'quotes/?isin=US19260Q1076'
3    response = MarketData().get_response(endpoint)
4    print(response)

You could also add additional query parameters, such as ?decimals, which is a Boolean value with which you can decide between returning the numbers in the API response either as Decimal or Integer. In a similar fashion, you could also set the date in your preferred format (?epoch=true for UNIX Timestamp or ?epoch=false for the ISO Date). Again, check out our docs for a full list of query parameters for the /quotes/ endpoint.

OHLC Data

Using the /ohlc/ endpoint, you can retrieve the typical candlestick data (Open High Low Close) for specific instruments. Here, you need to additionally add {x1} to the URL, which specifies what type of OHLC data you want to retrieve (m1 for data on a per-minute basis, h1 for data on a per-hour basis, d1 for data on a per-day basis). Similar as with the /quotes/ endpoint, the ?isin query parameter is also mandatory here. By the way, you can also request data for multiple instruments by adding a second ISIN to the query parameters, either divided by a comma or by adding a second isin={ISIN}.

1def get_ohlc():
2    endpoint = f'ohlc/d1/?isin=DE0008404005'
3    response = MarketData().get_response(endpoint)
4    print(response)

Additionally, you also have the option to set a time range for which you want to retrieve the OHLC data by using the query parameters from and toYou can either specify a date in epoch or in ISO date formatCheck out the docs for more information on the OHLC endpoint.

Trades

Finally, you also have the option to retrieve trades for a specific instrument using the /trades/ endpoint. Same as with the /quotes/ and /ohlc/ endpoint, setting an ISIN as query parameter is mandatory, as the API returns previous trades for a specific instrument. Take a look at this basic request example.

1def get_trades():
2    endpoint = f'trades/?isin=US19260Q1076'
3    response = MarketData().get_response(endpoint)
4    print(response)

To specify your request, e.g. to get the trades for multiple instruments in a numbers and date format of your choice, check out the docs for more information on the /trades/ endpoint query parameters.

Possible Extensions

We tried to give you a condensed overview of our Market Data API. However, when talking market data, you almost naturally think about real-time data. We did not simply forget this feature, but we need some time until we can offer it to you. Right now, a clear priority for us is to build stable and scalable services, which is why we are tackling different projects one by one to “do things right”. We will of course immediately inform you when we have a stable real-time market data service. To stay up to date: sign up for lemon.markets now and join our Slack Community.

While we provide you with “raw” market data through our API, there are a lot of possibilities to use the data to calculate different key performance indicators that might help you in making an investment decision. Check out one of our previous articles that discusses 10 resources to get you started in automated trading.

And if you are still missing something: let us know. We believe that lemon.markets will never be “finished” and we are trying to integrate user feedback/suggestions whenever possible.

Takeaways

We hope you got a good understanding of our Market Data API. Let us sum up our main takeaways:

  • The Market Data API is a separate product within the lemon.markets universe
  • It consists of 5 different main endpoints:
  1. /venues/
  2. /instruments/
  3. /quotes/
  4. /ohlc/{x1}
  5. /trades/
  • You can specify your request for each endpoint by using Query Parameters

We are really excited to see all of your trading projects in action soon. We hope that the Market Data API will help you make better investment decisions. If you are in desperate need of inspiration for a project: check out our other blog articles where we present different use cases and ideas. If you are interested in getting featured on our blog with your own project, just send us an email to [email protected] and we will get back to you 🙂.

Looking forward to seeing you on lemon.markets soon.

Marius🍋

You might also be interested in

Understanding the lemon.markets Trading API

blog photo

lemon.markets was born out of the lack of European brokerages with a stable, reliable API. We designed our Trading API with the developer in mind, which means that its infrastructure is optimised such that you can use it to build almost any brokerage product you can imagine. Keep reading to learn more about its endpoints.

Trading terminology you need to know as a beginner

blog photo

The trading world is one known for its jargon - what’s the difference between a ‘bear’ and a ‘bull’ market, anyways? And why are financial instruments listed with two prices? If your trading journey is just getting started or you need a quick recap, keep reading to brush up on beginner trading terms. You’ll be an expert in no time!

10+ online resources for high-quality stock market news

blog photo

Staying informed of market development is crucial when it comes to making informed investment decisions. Whether you like listening to podcasts, reading news articles or interacting with like-minded individuals in an investing community, we’ve got a recommendation for you. Keep reading to find your match.

Dive Deeper

Find more resources to get started easily

Check out our documentation to find out more about our API structure, different endpoints and specific use cases.

Engage

Join lemon.markets community

Join our Slack channel to actively participate in our community, ask questions to other users and stay up to date at all times.

Contribute

Interested in building lemon.markets with us?

We are always looking for great additions to our team that help us build a brokerage infrastructure for the 21st century.

Products
Pricing
For Developers
SlackGithubBlog
© lemon.markets 2021Privacy PolicyImprint
All systems normal