Creating your own Market Data Charts with the lemon.markets API


blog photo
Published by Marius Sprenger on October 27, 2021
Insights


Hey there. My name is Marius and I am part of lemon.markets, an early-stage startup from the ❤️ of Berlin, Germany. We are working on building an API that lets you create your own brokerage experience at the stock market. In this blog post, I want to dive deeper into a project that emerged from our community. We will take a look at how you can use the lemon.markets API to conveniently create your own Market Data charts using Python. Let’s get started straight away 🚀. 

A quick thought on our community

Around three months ago, we relaunched our API and are now live with our closed beta. By now, we are happy to support more than 100 developers in bringing their projects to life. We decided to go for the closed beta approach to sustainably scale up with the growing demand and to be able to to address every bug report and feature request individually. This way we want to ensure that we have a stable API when we transition from a closed to an open beta. A positive side effect of the closed beta approach is that the small group of users allows us to really be closely involved with community projects and see firsthand what our users are building. 

In this blog post, we are tackling a project by one of our community members who built a Python project that lets you visualise market data for an instrument of your choice.  

The project was developed by Peter, one of our dearest members who’s been supporting us with feedback and input almost since the beginning.  Preferably, he works with Python in PyCharm and is enjoying the wide range of available packages that save him a lot of time during development. He started to develop a general interest in the stock market earlier this year and started digging into different (online) resources to learn more about the topic. Besides improving his knowledge in that area by creating different projects with the lemon.markets API, he also pursued other programming projects like robotics model cars, or (more recently) aggregating and visualising available COVID-19 data. If then there is some more time left, he recently started with his new hobby Golf, where he enjoys the combination of being outside and getting some exercise. Great to have you on board, Peter.

This is our Community member Peter

Why visualising your data can be a major advantage 

It’s not a coincidence that market data is such a crucial part in the lemon.markets API. In fact, a while ago we split the lemon.markets API into two parts to make it clear that we are offering two separate projects: one Trading API and one Market Data API. Check out this blog post to find out more about the Market Data API, its structure and all included endpoints. In general, we are offering different types of market data in our API:

  • you can get previous quotes using the /quotes/ endpoint. 
  • you can get Open High Low Close (OHLC) data on a per-minute/per-hour/per-day basis using the /ohlc/ endpoint(s).
  • you can get previous trades using the /trades/ endpoint.

Through our API structure, you can get market data in the format of your choice for up to 10 instruments per request (simply use query parameters within your request URL). In general, we believe that extensive market data can help you make better and more informed investment decisions. This can either happen through developing buy or sell rules as part of your automated trading strategy (see one of our previous blog posts for some inspiration) or by visually inspecting them. You might have guessed by now: we are focussing on the latter in this blog post. 

There are many different approaches when it comes to visually analysing stock prices. This article is a great starting point in our opinion. While there are different strategies when it comes to analysing stock chart indicators, a common denominator is the desire to find trends in a stock price chart to then be able to react on that and buy or sell a stock. 

For instance, in the case of a moving average, a crossover (e.g. by comparing a 50-day and 200-day moving average, find out more here) can trigger a buy signal when the 50-day MA crosses the 200-day MA. There are also other signals based on historic data such as the relative-strength index (RSI) or the on-balance volume (OBV). Generally speaking, there are (almost) no limits to visual signals and we feel that it is our job to design our API structure in such a way that you can easily create all of those signals in your own trading application. 

That is exactly what Peter did in his project and we like it for a number of reasons:

  • it takes a pure numerical output and transforms it into something visual, which might be more “digestible” for many of our users, a feature we actively decided not to offer on our own. Instead, we know that our users can build way better solutions for these kinds of problem. In the meantime, we are focusing on building (a) stable API service(s).
  • it can actually be a great starting point to get familiar with our Market Data API and to get a feeling for the type of data that we are offering. 
  • it leaves a lot of space for your own creativity, as Peter’s project allows extensions in many parts (we actually drafted out some ideas for that ourselves, see below if you’re interested). 

Implementation

You can find the GitHub repository of Peter’s Market Data Charts project here. As this is a community project, the code ownership fully belongs to Peter and we are just cheering him on from the sideline 👯

Let us dive a little deeper into the implementation.

If you take a look at the project, you will notice that it consist of three main files:

1plot_candlestick_chart.py
2plot_candlestick_Interactive.py
3plot_candlestick_w_dash.py

They can be considered as a kind of “evolution”, with the first project being a simple Python script that outputs a graph for an instrument of your choice and the last one being a dashboard that runs in your browser with a lot of interactive customisation options. 

Plot Candlestick Chart

An order is characterised by its ISIN, among other features — again, see our documentation for the full run-down. An order needs to be placed, activated and retrieved. These functionalities can be seen in the Order class as defined below.

The first script is the most basic version, where you specify the ISIN of a specific stock you are interested in (Amazon in the example below) and output a candle stick chart with data from the last 31 days. 

There are a number of packages that Peter’s project uses that are worth mentioning. For instance, it uses pandas to structure the OHLC data and continue working with it conveniently. We already mentioned pandas in one our our earlier blog posts as an extremely helpful library for data manipulation/handling and this project once again proves its usefulness.

Screenshot from PyCharm project

Interactive Candlestick Chart

The second “evolutionary” stage within the Python project is a more interactive (hence the name) version. After specifying the stock you are interested in (we are once again using Amazon to showcase this), the script creates a chart and lets you interact with it directly in your browser.

Similar to the Plot Candlestick chart, the graph uses the OHLC data, which you can easily retrieve using our API (see our documentation on how to do that here). The result is a simple graph that displays the Open, High, Low and Close data.

You can get a sneak peak of the result below. The script uses plotly to create the graph, which is convenient since building interactive graphs with it is super intuitive. You can also use the bar at the bottom to specify your time range by dragging the slider from the left to the right.

Normal OHLC output graph

Slider used to create a more narrow time range

Plot Candlestick with Dash

The final, most complex, but therefore also the most interactive script is:

1plot_candlestick_w_dash.py

Essentially, it is a proper application that runs in your browser and gives you a lot of customisability to visualise market data for an instrument of your choice. In addition to pandas and plotly, this Python script uses dash, which allows you to build an intuitive and interactive web application with different UI elements like dropdowns, input fields or buttons. Built on top of plotly, it integrates nicely with charts of all sorts.

Let us take a quick sneak peek at how this turns out in Peter’s project:

UI when ISIN is not specified

UI after you inserted an ISIN

The application lets you enter an ISIN of your choice (surprisingly, we are using Amazon again). Afterwards the UI is updated and you are presented with additional options. If you scroll down, you can see the familiar candlestick chart that displays the OHLC data. Unlike in the previous project, you now additionally have the option to select different time frames, e.g. the data from the last day, week or month. This is convenient as it allows you to adapt the visualisation to your needs: if you are more interested in short-term data you can go for the day/week data, and if you are a long-term kind of guy/gal, you can look at the monthly/yearly development (although we still need some more time to provide market data deep into the past, but you get the point 😉).

OHLC Data for the last day

OHLC Data for the last week

OHLC Data for the last month

If you scroll even further down the page, the project additionally outputs a line chart with four lines for OHLC data. Essentially, the information provided is the same as in the chart above, but some of you might prefer one presentation over the other. You can also hover over the lines to get detailed information for a specific date.

Line Chart for OHLC data

Code adjustments

We at lemon.markets strongly believe in the power of open source (find out how we think about our community here) and we are really happy that our users are continuously building projects that bring themselves (and potentially others) value and fulfil a need for them that no other product on the market can. We think of ourselves as enablers — providing the infrastructure that allows you to build anything you can imagine. Therefore, we are focusing most of our dev resources on building a product that does just that. However, we will also always strive to give guidance and input on specific community projects, just like this one. We developed the idea behind the project in close collaboration with Peter and were in constant exchange about its state. We also took a look at the code after the project was finished and have some suggestions on how you can make the underlying code even stronger (you can see where this is headed — we are now handing it over to the community). Take look at the GitHub repository to get a better understanding of the code :)

For instance, the TOKEN_KEY is stored directly in the script. It might be an idea to outsource this to an environment variable to avoid leaking sensitive credentials on GitHub. Same goes for the Market Identifier Code or the API base URL(s). It makes sense to use those as environment variables, as this allows flexibility in the long term. For example, the base URL might change at some point (we are still super early-stage and things are changing fast). In that scenario, you would simply need to change one environment variable instead of looking for every code section individually and replacing every single URL.

Especially in the plot_candlestick_chart.py chart, it could be an idea to work more with functions and classes and call the main function at the end with:

1if __name__ == '__main__':

In general, if you look at all three Python scripts, you will notice some repetitions (for instance when the candlestick chart is created or the OHLC data is retrieved from our API). To keep the scripts shorter (especially the plot_candlestick_w_dash.py file is rather long and it is hard to keep an overview), it might be an idea to better structure the overall project to be able to reuse different components in different scripts. For that, the Model-View-Controller concept might be a good idea that allows to write scalable code that is easy to maintain in the long term.

These, however, are only suggestions from our side for an overall great project that was really fun to accompany. Feel free to open a PR or get in touch with Peter. He is super happy to talk about potential project improvements. And if you want to bring your own project to life: join our Slack community or ping us at [email protected].

Potential additions

A software project is never finished. If you are a developer yourself, we feel like you might know what we are talking about. In our opinion, this is both the beauty and the dread of writing software. For Peter’s project specifically, we have a number of ideas for additional features that we quickly want to talk about (maybe someone gets inspired to open a PR, who knows?).

The project currently focuses on OHLC data. However, our API also offers Quotes and Trades data (see here). It might be an idea to also offer the possibility to display that kind of data, e.g. through a separate chart or a checkbox next the chart that lets you choose the option(s) that you prefer. It could also be an idea to work with dropdowns instead of buttons to select the type of data you are interested in. Currently, this section takes up a lot of space. Working with dropdowns could greatly facilitate the UX.

Our API allows to retrieve market data for more than one instrument: simply attach multiple ISINs to the ?isin query parameter. This could come in handy for Peter’s project. Why not display two instruments in one chart? This could be a great starting point towards a Pairs Trading Strategy (not sure what that means? → check out our recent blog post about beginner-friendly trading strategies).

Takeaways

These are just some quick ideas from our side. Do you have one on your own? Fantastic! Make sure to bring it to life. We are really excited to see continuous collaboration on this project.

We hope you got a good glimpse into Peter’s work. We think that it is a great starting point to getting familiar with our Market Data API and evolving your skills towards a trading strategy that is based on visual inspection of market data. Make sure to join our waitlist to start building a project on your own and see you on lemon.markets soon 🍋

Marius and the lemon.markets Team

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