Retrieving historical OHLC market data with the lemon.markets API


blog photo
Marius SprengerFebruary 17, 2022
Product


lemon.markets is an early-stage FinTech from Berlin, Germany dedicated to building a trading API for developers that allows everyone to create their own brokerage experience at the stock market. One of our core products is the Market Data API that allows you to conveniently retrieve different types of market data. 

Title Card for "Retrieving historical OHLC market data with the lemon.markets API"

Access to reliable, extensive and up-to date stock market data is still hard to find these days. The lemon.markets Market Data API offers market data in three different formats: 

  • Quotes
  • Trades
  • Open High Low Close (OHLC)

In this blog post, we focus on OHLC market data. Ready, set, GO(OHLC).. 😉

OHLC Data explained

If you visualise OHLC Data in a graph, they result in the typical candle stick charts that you sometimes see in a stock market context. See below for a candle stick charts example from our community member Peter who built a Python tool to visualise stock market data

Candle Stick Charts AMAZON

Candle Stick Charts AMAZON

In general, OHLC data is pretty much “What you see is what you get”, meaning that you get 4 different types of prices:

Open: The open price for a stock within a specific timeframe

High: The highest price for a stock within a specific timeframe

Low: The lowest price for a stock within a specific timeframe

Close: The close price for a stock within a specific timeframe

Here, you can find more information on OHLC data and use cases in which it might be beneficial. 

The /ohlc endpoint

Using the lemon.markets Market Data API, you can very easily retrieve OHLC data for an instrument (or multiple instruments) of your choice. Let us take a look at a sample request: 

1import requests
2request = requests.get("https://data.lemon.markets/v1/ohlc/h1/?isin=US0231351067", 
3                       headers={"Authorization": "Bearer YOUR-API-KEY"})
4print(request.json())

You will notice a few things in there:

1The base URL: https://data.lemon.markets/v1/

This is the URL that will be used for all endpoints in the Market Data API.

1The ohlc endpoint specification: https://data.lemon.markets/v1/ohlc/h1/

With this addition to the base URL you can specify that you wish to retrieve OHLC data and which type. Here, we are using h1, which means that the API will return hour to hour data. Alternatively, you could specify m1 (data on a per-minute basis) or d1 (data on a per-day basis). 

Lastly, we add the query parameter to specify the instrument we wish to get the data for:

1isin query parameter: https://data.lemon.markets/v1/ohlc/h1/?isin=US0231351067

We could also add additional ISINs by using commas. Let’s throw Coinbase in there: 

1https://data.lemon.markets/v1/ohlc/h1/?isin=US0231351067,US19260Q1076

Alright, now we’re good to go to actually send the request. Using the request with one ISIN will result in a response similar to this one:

1{
2    "results": [
3        {
4            "isin": "US0231351067",
5            "o": 3009.0,
6            "h": 3013.5,
7            "l": 2995.0,
8            "c": 2995.0,
9            "t": "2022-01-04T15:00:00.000+00:00",
10            "mic": "XMUN"
11        }
12    ],
13    "previous": null,
14    "next": null,
15    "total": 1,
16    "page": 1,
17    "pages": 1
18}

The API returns an object for each OHLC entry for the respective instrument. Each object contains the ISIN, OHLC prices, the timestamp for the time interval you requested as well as the market identifier code (mic) of the trading venue where the data comes from. As you can see, you currently only get one object. This is because the API returns only the latest OHLC entry by default. However, we can very easily change that by using two additional query parameters: from and to. By specifying the date in the YYYY-MM-DD format, we can tweak our API response. 

1import requests
2request = requests.get("https://data.lemon.markets/v1/ohlc/h1/?isin=US0231351067&from=2022-01-03&to=2022-01-04", 
3                       headers={"Authorization": "Bearer YOUR-API-KEY"})
4print(request.json())

Basically, the request means that we want to retrieve the hourly OHLC data for January 3rd 2022. The response now looks a bit more extensive:

1{
2    "results": [
3        {
4            "isin": "US0231351067",
5            "o": 2959.0,
6            "h": 2965.5,
7            "l": 2954.0,
8            "c": 2963.0,
9            "t": "2022-01-03T07:00:00.000+00:00",
10            "mic": "XMUN"
11        },
12        [...],
13        {
14            "isin": "US0231351067",
15            "o": 3013.5,
16            "h": 3018.5,
17            "l": 3008.5,
18            "c": 3018.5,
19            "t": "2022-01-03T20:00:00.000+00:00",
20            "mic": "XMUN"
21        }
22    ],
23    "previous": null,
24    "next": null,
25    "total": 14,
26    "page": 1,
27    "pages": 1
28}

As you can see from the timestamps, each object now represents one hour throughout the day and you can integrate that information into your trading strategy. 

Additional Query Parameters

Besides from and to, there are many additional query parameters which you can use to customise your API response. For example, if you are used to working with Integers, you can simply set ?decimals=false and all numbers in the API response will be returned as type int . Similarly, you can customise the date format. Setting ?epoch=true will result in the API returning t as UNIX timestamp. 

Take a look at our documentation to learn more about the /ohlc endpoint and the available query parameters. 

Takeaways

I hope you got a good overview of the /ohlc endpoint functionality and are now fully inspired to start using it. Make sure to sign up to lemon.markets to start your very own trading project. We are looking forward to seeing what you are building with us. 

Best,

Marius from 🍋.markets

You might also be interested in

blog photo

Blog 38 - The market maker explained

Market Makers are crucial to provide liquidity to stock exchanges. In this blog post, we talk about what Market Makers do and why they are useful.

blog photo

5 (+1) YouTube channels for FinTech enthusiasts 

YouTube is a great way to learn about new things, including financial education or coding. Therefore, in this article we’d like to introduce you to 5 YouTube channels to level up your trading literacy.

blog photo

A short introduction to derivatives

In this article, we'd like to introduce you derivatives - they come up in finance and leave a lot of people scratching their heads, though it's totally worth it! Curious about hearing what's the difference between investing and trading, the coherence between finance and weather & why the Greeks even appear here? We'll discuss who’s using them, what they are and how they can be valued in the following.

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.

Need any help?
Ask a question in our CommunityAsk a question in our CommunityGet started with our DocumentationGet started with our DocumentationGet inspired on our BlogGet inspired on our Blog
© lemon.markets 2021Privacy PolicyImprint
All systems normal

As a tied agent under § 3 Sec. 2 WplG on the account and under the liability of DonauCapital Wertpapier GmbH, Passauer Str. 5, 94161 Ruderting (short: DonauCapital), lemon.markets GmbH offers you the receipt and transmission of orders for clients (§ 2 Sec. 2 Nr. 3 WpIG) of financial instruments according to § 3 Sec. 5 WpIG as well as brokerage of accounts.