Executing your first order with the lemon.markets API (beginner-friendly)

Marius Sprenger
7 min readFeb 26, 2021

If you are unfamiliar with the concept of a REST-API (or are still learning), it might sometimes be hard or frustrating to get started or develop further expertise. While we at lemon.markets are building a product from techies for techies, a core (if not the most important) principle is the goal of democratizing the brokerage market and building technology that gives each and every one the chance to trade equities the way he or she wants to.

A typical lemon.markets Dashboard

We think that an important part of that is to lower the entry burden for everyone that is interested in using our services, independent of the programming skills they have. The software developer community is still growing and not everyone who might be willing to build a trading algorithm or analyse market data, might (as of now) actually have the technological capabilities to do so.

Therefore, this blog post is the first one in a series of beginner-friendly step-by-step tutorials that get you started with our API. For the beginning, we focus on a core functionality that you can do with lemon.markets: Placing an order (I guess in the end we are all interested in seeing how our favourite companies evolve and participate in their success).

Alright, let’s get started.

Step 1:

If you do not have an account, yet: start by signing up in our web app. If you already have an account, go to step 3. (We assume that we do not have to teach you how to sign up for something on the Internet, right? 😉 However, make sure to choose a cool username for street cred).

Step 2:

Click on the confirmation link in your inbox and activate your account. Afterwards, you can log in.

Step 3:

When you are logged in, you are going to see a very empty dashboard. Makes sense, since you haven’t used the API, yet. Let us change that.

Step 4:

Go to the Auth & Strategies page. Here, you can create a new access token, which is pretty much the most important tool for you, since most requests will need be performed with authentication.

Step 5:

Click on the “Create new Access Token with strategy” button (marked in red in the screenshot below) and follow the steps.

Auth Page

Step 6:

In the early stages of our product, we require you to connect your access token to a strategy, which is why you have to create the strategy first. You can give it a name, a description and decide whether to make it public or private. Afterwards, specify your access token. If you finish the process, you should be able to see the newly created strategy and access token on the Auth page (as shown below). The access token, which you will use now is the one in the red box. Don’t worry, we’ll delete it before this tutorial goes live, so there is no point in playing around with it 😃. Simply create your own, it’s easy as ABC.

Step 7:

We’ll leave the dashboard for now and focus on writing a short python script for our first order execution. If you are familiar with the REST standard, you will know that you can use any programming language you like. However, we will do this exemplary tutorial in Python, as we launched a Python SDK that makes things a bit easier and because we think that it is a rather easy language to get started. The Python editor of our choice is PyCharm. If you are a student or part of a research institution, you are eligible for a Professional account (which is highly recommended). After installing PyCharm, open the application and create a new project. Define the environment for the location the project is saved in and use python3 as base interpreter. Afterwards, click on create.

Step 8:

We are greeted with an example Python script named main.py. We can use that later, but let us focus on something else right now: integrating the lemon.markets python SDK into our project. You can do it via the command line through:

pip install lemon-markets-sdk

Alternatively, we can also do it directly in PyCharm (which is the approach we chose for this tutorial). To do so, click on Preferences, go to your project’s area (in our case “Project: executeOrder”) and click on Python Interpreter. You should be greeted with a list of installed packages for the project that looks something like this:

As we can see, the lemon.markets SDK is not yet installed. To change that, click on the “+” at the bottom, which will bring you to an area where you can specifically search for packages. Type “lemon-markets-sdk” into the search bar, and you should be able to find it. Click on “install package”.

If the package was installed successfully, you can close the window. You should now be able to see it in your list of installed packages. Additionally, please make sure that the “pytz” and the “requests” package are also installed. They are normally super standard, but if you are just starting out, you might not have installed them, yet.

Great, we just made great progress. Time to relax a little.

Step 9:

Now, it is time to dive into our actual Python script. As a first step, import all relevant packages for your script.

from lemon_markets.token import Token
from lemon_markets.order import Order
import datetime

The first two lines access our lemon.markets Python SDK and make the order execution easier. The third line is important for setting a specific timer for our order — we will come to that in a second.

Step 10:

We now need to go back to the our dashboard, or more specifically: to our Auth page. Copy the access token you created in step 6 and go back to your Python script.

Step 11:

Define a function and give it a name like execute_order(). Next, define your token and use the Token class from the SDK to assign a value to it. To do so, paste the access token ID that you copied from the dashboard into the brackets. Now, you can access the account that is connected to that specific token.

Now, we need to specify our order. We at lemon.markets identify orders via their International Securities Identification Number (ISIN). You can find the ISIN for all equities that are available for trading on lemon.markets in our dashboard. Simply go to the search page and type in what you are looking for. I guess we are all craving for a bit of vacation right now, so let us assume we are in the mood to buy some Airbnb stock. After searching for “airbnb”, we get our result. Copy the ISIN and paste it into your Python script by assigning it to a variable called instrument.

We can access the Order class from our SDK and specify all variables that are relevant for us. In our example, we create a market order, meaning that the order is executed at the next available market price, but you can also create other types of orders like limit, stop or stop_limit orders (see our documentation for more information on that topic). One stock is enough for us right now, so we set quantity = 1. Important is also to specify how long the order is supposed to be valid. Now, our datetime package comes in handy, as we can simply use it to set a date in the future. However, you could also set a “manual” date by inserting a UNIX timestamp (there are some helpful websites like this one that do most of the work for you). Afterwards, create the order through new_order.create(), which is another helpful command from the SDK.

After defining the execute_order function, you can call it below so that it is executed when the script is run.

Step 12:

You can run your script by right-clicking on the file in the left column. If everything worked, the process should finish with exit code 0. To check if your order was properly placed, go back to your dashboard. The airbnb market order should now appear in the dashboard and under Orders & Transactions.

This pretty much concludes our beginner friendly tutorial. We hope that we were able to explain some core functionalities in a way that is also understandable for people that so far had little or no contact with using APIs. Let us know what you think, if you can use more of similar tutorials or if that was too easy for you and you are in need of something more complex. The best way to reach us is via our Slack community.

Have a great day and happy coding/trading :)

The 🍋 team.

--

--

Marius Sprenger

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