Retrieving historical OHLC market data with the API

blog photo
Marius SprengerFebruary 17, 2022
Product 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 API"

Access to reliable, extensive and up-to date stock market data is still hard to find these days. The 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 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 API is to make use of the Python SDK, which can easily be installed through:

1pip install lemon

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

1def __init__(self):
2        self.client = api.create(
3            market_data_api_token=os.getenv("DATA_API_KEY"),
4            trading_api_token=os.getenv("TRADING_API_KEY"),
5            env="paper"
6    )

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

1# get ohlc
2response = client.market_data.ohlc.get(
3    isin=['US0231351067'],
4    period='h1',
5    from_=datetime(2022, 1, 2)

You will notice a few things in there:

1The base URL:

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

1The ohlc endpoint specification:

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 Amazon.

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:

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

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. 


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 to start your very own trading project. We are looking forward to seeing what you are building with us. 


Marius from 🍋.markets

You might also be interested in

blog photo

Using Time Series Forecasting to predict stock prices 🔮

In this article you will learn what time series forecasting is and how its application in finance looks like. Then you'll also dive into Facebook's Prophet Model for Time Series Forecasting and use it together with the Market Data API to forecast the development of the Tesla Stock.

blog photo

Dummies Guide to Trading with Machine Learning

Ever wonder how a trader with decades of experience on thousands of stocks and lightning fast reaction times might perform in the market? With some machine learning knowledge, you might be able to automate such a trader yourself! 💻 📈

blog photo

4 reasons why YOU should automate your trading strategy

In the current volatile market conditions, everyone is trying to find ways to minimise portfolio loss. In that context, have you ever thought about automating your trading strategy? In this article, we will dive into 4 reasons for doing so. Expect to learn how it can save you time, make your trading more efficient and lead to data-based decisions.

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.


Interested in building 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?
Get started with our DocumentationGet started with our DocumentationGet inspired on our BlogGet inspired on our Blog
© 2023Privacy PolicyImprint
Systems are down

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), GmbH offers you the receipt and transmission of orders for clients (§ 2 Sec. 2 Nr. 3 WpIG) of financial instruments according to § 2 Sec. 5 WpIG as well as brokerage of accounts.