Retrieving historical OHLC market data with the lemon.markets API

Marius Sprenger
lemon.markets
Published in
4 min readFeb 17, 2022

--

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.

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

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.

However, before we do that, we need to take care of some base work first.

The easiest way to communicate with the lemon.markets API is to make use of the lemon.markets Python SDK, which can easily be installed through:

pip install lemon

The initial step for using the SDK (and subsequently making requests to the API) is to instantiate a client, like this:

Note that in line 3 and 4 we are accessing environment variables, which you need to create and define before that step, typically in a .env file. Here, we are using separate API Keys for market data and trading, as required by the lemon.markets API. You can create your Trading and Market Data API Keys in the Dashboard.

Alright, after that is done, let us now take a look at a sample request to retrieve OHLC data:

You will notice a few things in there:

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

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

The ohlc endpoint specification: d1 data

With this addition 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 data on hourly basis. Alternatively, you could specify m1 (data on a per-minute basis) or d1 (data on a per-day basis).

Lastly, we specify the instrument we wish to get the data for, in this case Tesla.

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:

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 easily change that by using two additional query parameters: from and to.

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:

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 a 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

--

--

Marius Sprenger
lemon.markets

Half building lemon.markets, half PhD student focusing on the intersection between sports and technology. Interested in movies, sports and (surprise) technology