First steps

This tutorial is currently available for Python and JavaScript

Prerequisites

First of all, get your Access Token from the Auth page on the dashboard.

Python
Javascript
Python

In order to perform the requests, please install the Requests package (pip install requests).

Javascript

Install a requests package of your choice or take the standard built-in package for fetching data. In this tutorial, we will use Axios (npm install axios) as this package is very convenient.

Information about Account Details and Cash to Invest

For most requests made on the API the account is needed as the requests are made on behalf of the account. To get the account, we have to fetch the /accounts/ endpoint:

Python
JavaScript
Python
import requests
request = requests.get("https://api.lemon.markets/rest/v1/accounts/",
headers={"Authorization": "Token YOUR_TOKEN_HERE"})
print(request.content)
JavaScript
import axios from 'axios';
axios.get('https://api.lemon.markets/rest/v1/accounts/', {headers: {
'Authorization': 'Token YOUR_TOKEN_HERE'
}})
.then((response) => {
console.log(response.data)
})
.catch((e) => {
console.log(e)
})

This is going to return something like:

{
"count": 1,
"next": null,
"previous": null,
"results":[
{
"name": "Demo Account by Me",
"type": "demo",
"currency": "EUR",
"uuid": "76a4b7e6-99ac-498f-afc2-d6fb267f3b2"
}
]
}

The UUID (in this case 76a4b7e6-99ac-498f-afc2-d6fb267f3b2) is needed for all upcoming requests.

We really like to know our account balance and the cash to invest we got at hand. The cash_to_invest is very important to us as we always like to know if we got enough cash to run our strategy flawlessly. By the way, the cash_to_invest is the total_balance minus the volume of open orders. For instance, if you got an limit order of 2x Tesla for 400€ and your total balance is 10.000€, your cash_to_invest would be 9.200€. To get those information we have to fetch the /state/ endpoint with our account UUID.

Python
JavaScript
Python
request = requests.get("https://api.lemon.markets/rest/v1/accounts/ACCOUNT_UUID_FROM_ABOVE/state/",
headers={"Authorization": "Token YOUR_TOKEN_HERE"})
print(request.content)
JavaScript
axios.get('https://api.lemon.markets/rest/v1/accounts/ACCOUNT_UUID_FROM_ABOVE/state/', {headers: {
'Authorization': 'Token YOUR_TOKEN_HERE'
}})
.then((response) => {
console.log(response.data)
})
.catch((e) => {
console.log(e)
})

The output should look like this:

{
cash_to_invest: 10000.0,
total_balance: 10000.0
}

Awesome! We still got our 10k at hand, let us buy some Tesla stocks now.

Create an order

To create an order we have to perform a post request to the API.

Python
JavaScript
Python
request = requests.post("https://api.lemon.markets/rest/v1/accounts/ACCOUNT_UUID/orders/",
data={"instrument": "US88160R1014",
"quantity": 3,
"limit_price": 320.0,
"valid_until": 1600128000,
"side": "buy"},
headers={"Authorization": "Token YOUR_TOKEN_HERE"}
)
print(request.content)
JavaScript
axios.post('https://api.lemon.markets/rest/v1/accounts/ACCOUNT_UUID/orders/',
{
"instrument": "US88160R1014",
"quantity": 3,
"limit_price": 320.0,
"valid_until": 1600128000,
"side": "buy"},
{headers: {'Authorization': 'Token YOUR_TOKEN_HERE' }
})
.then((response) => {
console.log(response.data)
})
.catch((e) => {
console.log(e)
})

The response should look like this:

{
instrument: "US88160R1014",
limit_price: 340,
quantity: 3,
side: "buy",
status: "open",
stop_price: null,
type: "limit",
uuid: "d3c95d03-8f6f-4dc1-9e61-019892392a4d",
valid_until: 1600128000
}

Alrighty! Let us check out if we could snap some Tesla shares. We have to therefore use the UUID (in our case d3c95d03-8f6f-4dc1-9e61-019892392a4d).

Retrieve the order

Python
JavaScript
Python
request = requests.get("https://api.lemon.markets/rest/v1/accounts/ACCOUNT_UUID/orders/ORDER_UUID_FROM_PREVIOUS_STEP/",
headers={"Authorization": "Token YOUR_TOKEN_HERE"}
)
print(request.content)
JavaScript
axios.get('https://api.lemon.markets/rest/v1/accounts/ACCOUNT_UUID/orders/ORDER_UUID_FROM_PREVIOUS_STEP/',
{headers: {'Authorization': 'Token YOUR_TOKEN_HERE' }
})
.then((response) => {
console.log(response.data)
})
.catch((e) => {
console.log(e)
})

The response should look like this:

{
average_price: 320.0,
created_at: 1599424686.662437,
instrument: {
isin: "US88160R1014",
title: "Tesla, Inc",
wkn: "A1CX3T"
},
limit_price: 320,
processed_at: 1599559200,
processed_quantity: 3,
quantity: 3,
side: "buy",
status: "executed",
stop_price: null,
type: "limit",
uuid: "d3b95d03-8f6f-4dc1-9e61-019892392a4d",
valid_until: 1600128000
}

Lucky you! You just snapped 3x Tesla shares 💸. There are two attributes in the response which might wonder you: the average_price and the processed_quantity. Due to partial execution there might the case that only 2x shares were bought for 319.90€ and 1x later for 320.20€. The average_price is never higher than the limit_price when you buy a stock or lower when you sell a stock.

If you got any more questions, feel free to reach out to us!