Trading Stocks Made Easy with the lemon.markets API
This is a guest post written by Joanne Snel, community developer at lemon.markets.
Picture this: Your friends have been on your back to start investing right now, “while the market is down.” You finally concede but are overwhelmed by all of the trading applications available. Even after you decide, you’re not done. A lot of time goes into choosing what to invest in; reading financial news; monitoring developments related to your investments; and checking on your portfolio. Maybe there was a reason you held off on getting involved in the stock market in the first place.
Now, picture this: Your friends are still on your back to start investing, but rather than trialing different trading applications until you find one you like, you cut out the middleman. You’re a developer, after all, and you’re pretty good at automating repetitive tasks. Why not use a trading API that allows you to trade right from your integrated development environment of choice? And, even better, what if you could automate the screening, trading, and monitoring processes? Maybe the barriers to entry aren’t that high after all.
Three lines of code
No matter which provider you use to invest, there’s always technology between you and the stock market. Some platforms are powered by an API, whose primary purpose is to facilitate trading from a developer point of view. These APIs can make placing a trade as simple as:
python if upwardsTrend(): placeTrade(“Coinbase”, 10, now)
Depending on the provider, this may be more or less complicated. But before we dive into the “who,” let’s discuss the “why.” Trading with an API will require a bit more knowledge and effort up front. Why should you invest the time?
Algorithmic trading
Many developers turn to a trading and market data API because they want to set up an algorithmic trading strategy. There are many different ways to define your trading algorithm. A lot of them are based on technical analysis of market data. For example, you could place a trade whenever a stock’s price moves above its 250-day moving average. Or, you could forecast prices with a Long Short-Term Memory model or Hidden Markov Model to indicate trading opportunities. These machine-learning algorithms usually require large amounts of fast-moving data, which would not be feasible to parse by hand.
Conversely, such an API could also be used to optimize trading strategies that can be performed manually but are tedious. For example, imagine a trader purchases 10 shares of an exchange-traded fund every month to gradually build his or her portfolio and mitigate risk. Such a cost-averaging strategy can easily be translated into less than 25 lines of code. The upside? Once this program is deployed, the effort spent on maintenance and analysis is significantly less than performing this task by hand. A massive time saver.
A trading API doesn’t even need to be used for a trading strategy. Think in terms of contextual integrations or financial apps. Developers might want to bring trading into the apps where people spend most of their time anyways–perhaps through Telegram. What if you could trade through Alexa with voice commands? Developers may wish to build their own front-end trading solution, which displays exactly the information they need.
The use cases for a trading API are almost endless. At lemon.markets, we recognize this and are working to make our API as flexible as possible.
Why should you choose lemon.markets?
lemon.markets offers two APIs: a Trading API and a Market Data API. With the Trading API, you can place a trade with just one POST request:
python import requests import json // Place buy order for Coinbase requests.post("https://paper-trading.lemon.markets/v1/orders/", data=json.dumps({ "isin": "US19260Q1076", "expires_at": "2022-02-28", "side": "buy", "stop_price": 5000000, "limit_price": 5300000, "quantity": 1, "venue": "XMUN", "space_id": "123456789", "notes": "I want to attach a note to this order" "idempotency": "1234abcd" }), headers={"Authorization": "Bearer YOUR-API-KEY"})
With the Market Data API, you can obtain historical data in a practical JSON format, like the following response for Coinbase’s open-high-low-close (OHLC) prices:
json { "time": "2022-02-26T15:35:27.064+00:00", "results": [ { "isin": "US19260Q1076", "o": 220.5, "h": 221.5, "l": 206.0, "c": 207.0, "v": 978, "t": "2022-01-05T00:00:00.000+00:00", "mic": "XMUN" }, … { "isin": "US19260Q1076", "o": 202.5, "h": 203.5, "l": 193.8, "c": 196.4, "v": 1015, "t": "2022-01-18T00:00:00.000+00:00", "mic": "XMUN" } ], "previous": null, "next": "https://data.lemon.markets/v1/ohlc/d1/?from=2022-01-01&to=2022-02-25&isin=us19260q1076&page=2", "total": 38, "page": 1, "pages": 4 }
Because we know how important it is to have real-time market data, we’re also working on live market-data streaming. This is a feature we’ll launch in the near future.
You may have noticed the URL we used to place trades includes the words “paper-trading.” That’s because lemon.markets offers the ability to test your code with risk-free “paper trades” before deploying it with your hard-earned money. We also offer a URL for regular trading.
As we’re an API-first company, we emphasize informative, easy-to-follow documentation. That means detailed explanations of all the endpoints we provide, along with plenty of code samples to get you started. We also provide descriptive articles, tutorials, code repositories, and videos on the APIs and projects that can be built. Our community members are heavily involved in these content pieces, sharing suggestions on topics and submitting PRs for our projects.
If you need help, we have a vibrant and responsive community of over 500 developers on Slack. Several of our community members have created SDKs and made them publicly available for everyone.
How do I get started?
Postman is the perfect tool to get started with lemon.markets. It allows you to get familiar with the APIs and their responses without having to commit to a project. We’ve set up a lemon.markets public workspace in Postman that can be used to test our available endpoints. If you’d like to try it out, all you need to do is fork it and follow the next few steps:
- Depending on whether you’ll be making paper trades or using actual money, you’ll need to fork the appropriate environment into your own workspace.
- Sign up to lemon.markets here. Upon creating your account, you’ll be provided with an API key, which you’ll use to place all requests.
- Fill in your API key and place your first request.
Let’s check Coinbase’s share price on the Munich stock exchange and place an order. The first step is to determine how to reference Coinbase. At lemon.markets, we use the International Securities Identification Number (ISIN) to refer to unique financial instruments. You can search for the Coinbase ISIN using the /instruments endpoint.
From this request, we can see the ISIN for Coinbase is US19260Q1076. We can use that information to retrieve more information about Coinbase, such as the current share price. We can use the /quotes endpoint to retrieve that data.
The ask price for Coinbase is 173.80 euros. Let’s place a paper trade using the /orders endpoint.
We’ve just simulated a buy order for three shares of Coinbase! However, as a regulatory step, lemon.markets requires that you activate all your orders. Therefore, we need to collect the order ID and place another request.
That’s all it takes to place an order with lemon.markets.
Let’s take a closer look at what we just did
We can see the mock buy order was executed at 173.40 euros per share—even more favorable than the ask price we saw earlier.
That should give you a preview of how to use the lemon.markets workspace in Postman.
Check it out for yourself and explore the different options that lemon.markets has to offer by signing up here, visiting our community on Slack, or emailing [email protected]. We look forward to seeing you!
What do you think about this topic? Tell us in a comment below.