Placing your first order with the lemon.markets Trading API


blog photo
Marius SprengerDecember 30, 2021
Trading


lemon.markets offers a simple API that allows you to create your own brokerage experience at the stock market. An important part of that is the possibility to place orders with one simple API call. In this blog post, I will walk you through the steps you need to take to place and activate an order with the lemon.markets Trading API. Ready, set, go đŸƒđŸ‘©â€đŸ’»

Title Card for "Placing your first order with the lemon.markets Trading API"

Getting access

Start by signing up on our Dashboard. After you verified your account, you can log in and use the first API Key that we already created for you by default. After that, you are ready to place your first order.

Place your first order

To place an order with lemon.markets, you need to address the trading API by making a POST request against the base URL and add /orders/ to it:

1https://paper-trading.lemon.markets/v1/orders/

The easiest way to communicate with the lemon.markets API is to make use of thelemon.markets 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 lemon.markets API. You can create your Trading and Market Data API Keys in theDashboard.

When placing an order, make sure to specify your order through your request body. Some of the parameters in there are required (isin to specify the instrument you want to buy, side to either buy or sell, or quantity to specify how many shares you wish to buy).

Others are optional (stop_price to place a stop market order, limit_price for a limit order, or both for a stop limit order, or notes if you want to add some notes with your order so you later know why you placed it in the first place).

See an example request in Python, using the lemon.markets Python SDK, below. You can check out our documentation for additional code snippets in JavaScript and more detailed explanations on different request parameters.

1# create buy order
2response = client.trading.orders.create(
3    isin='US88160R1014',
4    side='buy',
5    quantity=1,
6)

Place your order

Dealing with the API Response

When you successfully placed your order, the API responds with a 

1200 
2"status": "ok"

and outputs some additional response elements. For instance, you learn about the estimated_price or the regulatory information associated to your order. And you receive the order_id, which is crucial for the next (and final) step before your order can get executed. 

Activating an Order

Before we route your order to a trading venue, you first need to activate it. If you want to activate an order in the paper money environment, you can use the /activate endpoint to do so. Find out more about the topic of order activation here. Activate your order using the following endpoint:

1https://paper-trading.lemon.markets/v1/orders/{order_id}/activate/

See now why it is crucial that the POST request against the /orders/ endpoint returns the order_id? Using the ID, we can activate the order through:

1# create buy order first
2response = client.trading.orders.create(
3    isin='US88160R1014',
4    side='buy',
5    quantity=1,
6)
7# grab the order_id
8order_id = response.results.id
9# activate buy order
10response = client.trading.orders.activate(order_id=order_id)

Activate an order through its order_id

Note that you can set any arbitrary PIN in your request body in the paper money environment. Your own personal PIN becomes only relevant in the trading environment. On success, the order is activated and routed to the specified trading venue. You can now sit back and relax — your work here is done (for now).

Retrieving your Orders

You can retrieve all your orders by posting a GET request against the /orders/ endpoint:

1https://paper-trading.lemon.markets/v1/orders/

To specify your request, you can add Query Parameters to your request:

  • from & to: you can define a time range from which you want to retrieve orders
  • isin: add this query parameter to only see orders related to a specific isin
  • side: query parameter to see only buy or sell orders
  • status: filter for order status, e.g. inactive or executed
  • type: filter for different types of orders: market, stop, limit, stop_limit

See the example request below to learn how to use the query parameters in your request. In the example, we only want to see Tesla orders, therefore using theisinquery parameter:

1# get orders
2response = client.trading.orders.get(isin='US88160R1014')

Ideas for Inspiration

You now know how to place an order with the lemon.markets Trading API. Now: what to do with it? There are plenty of use cases. Think of the order placement as the final step in your awesome trading project. You have an automated trading strategies that buys stocks based on predefined criteria? Great, the order endpoint is triggered when a certain threshold in your algo is crossed. You built your own trading app to buy and sell stocks? Great, whenever you click the buy/sell button in your application, you will need the POST /orders/ endpoint. What are your additional ideas for how to use the /orders/ endpoint? Let us know 🙂

I hope you got a good understanding of one of our most crucial API endpoints. If you have any questions or want to get in touch with other liked-minded developers: join our growing Slack community and discuss your newest trading project idea. And for more inspiration: check out our other blog articles. 

Excited to see 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 lemon.markets 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.

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?
Get started with our DocumentationGet started with our DocumentationGet inspired on our BlogGet inspired on our Blog
© lemon.markets 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), lemon.markets 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.