How to access your lemon.markets Trading API Account with one API call
We at lemon.markets provide the infrastructure so everyone can build their own brokerage experience at the stock market. In this blog post, we show you how to easily access your trading account information with one simple API call. Additionally, we dive a little deeper into our /account endpoint. Let us begin!
Signing up
The first step to accessing your account is to make sure that you actually have one. So, sign up on our dashboard and verify your account through the email that we send you after registration. You can then sign into the dashboard, where we already provide you with your first API Key.
Afterwards, we want to actually start using the API.
The easiest way to communicate with the lemon.markets API is to make use of the lemon.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 the Dashboard.
Finding your way around our API
Using that API Key, you can go ahead and start testing our API. You can find everything you need to know in our documentation. We created several endpoints concerned with everything related to your account. Using these endpoints, you can find information about your name (in case you forgot), your current balance, how much cash you have left to invest, the IBAN of your brokerage account and many many more things.
To retrieve information about your account, place a GET request against the following request URL:
1https://paper-trading.lemon.markets/v1/account/
Using the SDK, we can very easily do so:
1def get_account():
2 response=client.trading.account.get()
3 print(response)
Placing this request will result in a response similar to this one:
1{
2 "time": "2021-11-22T15:37:56.520+00:00",
3 "status": "ok",
4 "results": {
5 "created_at": "2021-10-12T10:29:49.769+00:00",
6 "account_id": "acc_pyNQNll99hQbXMCS0dRzHyKQCRKYHpy3zg",
7 "firstname": "Michael",
8 "lastname": "Burry",
9 "email": "m_burry@tradingapi.com",
10 "phone": "+491637876521",
11 "address": "Ritterstraße 2A 10969 Berlin",
12 "billing_address": "Ritterstraße 2A 10969 Berlin",
13 "billing_email": "m_burry@tradingapi.com",
14 "billing_name": "Michael Burry",
15 "billing_vat": "DE999999999",
16 "mode": "money",
17 "deposit_id": "K2057263187",
18 "client_id": "2057263",
19 "account_number": "2057263187",
20 "iban_brokerage": "DE12345678902057263",
21 "iban_origin": "DE123456789012345",
22 "bank_name_origin": "Test Bank",
23 "balance": 100000000,
24 "cash_to_invest": 80000000,
25 "cash_to_withdraw": 20000000,
26 "trading_plan": "basic",
27 "data_plan": "basic",
28 "tax_allowance": 8010000,
29 "tax_allowance_start": "2021-01-01",
30 "tax_allowance_end": "2021-01-01"
31 }
32}
As you can see, you get a lot of information on everything related to your account, like when it was created, what your current balance is, on which trading & data plan you are on and the IBAN of your brokerage account. Make sure to check out our documentation for in-depth information on all endpoint response parameters.
Withdrawing money
As you can see in the endpoint response above, you also have a response element called cash_to_withdraw. This is your current balance minus the current sum of your buy orders. Based on that number, you can very easily withdraw money into your reference account via:
1https://trading.lemon.markets/v1/account/
Use the following code snippet to withdraw money into your reference account:
1response = client.trading.account.withdraw(
2 amount=100000,
3 pin="1234"
4)
Define the amount you want to withdraw in your request body, along with your individual 4-digit PIN that you set during your onboarding process. After a successful API request, the specified amount is automatically withdrawn.
Retrieving your Bank statements
Obviously, as a user you are interested in staying up to date regarding all activity that is happening on your account. That is what the /bankstatements endpoint is there for. The endpoint returns a list of objects containing all activities from your last “active day” (in the paper money environment) and all activities from the last working day (in the real money environment), where, by default, an end of day balance is created for you. You can access your bank statements using the following request URL (in this example, we specify the request/what type of bankstatements we want to receive in the brackets):
1# get bank statements
2response = client.trading.account.get_bank_statements(
3 type='eod_balance',
4 from_="beginning"
5)
This will return a response similar to the following one:
1"time": "2021-12-20T12:48:06.199+00:00",
2"status": "ok",
3"mode": "paper",
4"results": [
5 {
6 "id": "bst_pyHLSwwFFd6r9fJsNbFhxrB3sl43xCP1GK",
7 "account_id": "acc_pyNQNll11hQbXMCS0dRzHyNKLZTYHpy3zg",
8 "type": "eod_balance",
9 "date": "2021-12-16",
10 "amount": 987033450,
11 "isin": null,
12 "isin_title": null
13 },
14 {
15 "id": "bst_pyQWCwwFFrBm9NhMYJ1yYDnhnMtMs64DNT",
16 "account_id": "acc_pyNQNll11nQbXMCS0dRzHyKQCRKYHpy3zg",
17 "type": "order_buy",
18 "date": "2021-12-16",
19 "amount": 8829000,
20 "isin": "US88160R1014",
21 "isin_title": "TESLA INC."
22 }
23 ],
24"previous": null,
25"next": null,
26"total": 2,
27"page": 1,
28"pages": 1
29}
As you can see, the last thing I did was I placed a buy order in the paper environment and an end of day balance was created for me.
Seeing some documents
Lastly, you have the chance to very easily access all important documents related to your account via the following URL:
1https://paper-trading.lemon.markets/v1/account/documents
Use the following code snippet to retrieve a list of documents:
1# get documents
2response = client.trading.account.get_documents()
This will provide you with a list similar to this one:
1{
2 "time": "2021-11-22T15:41:04.028+00:00"
3 "status": "ok",
4 "results": [
5 {
6 "id": "doc_pyNjNcc77ht3T3lH8dJa5fD8jhj2JHJ1xX",
7 "name": "account_opening.pdf",
8 "created_at": "2021-10-19T14:58:52.813Z",
9 "category": "kyc",
10 "public_url": "http://testpublicurl.de/",
11 "link": "'https://trading.lemon.markets/v1/account/documents/doc_pyNjNcc77ht3T3lH8dJa5fD8jhj2JHJ1xX",
12 "viewed_first_at": "2021-10-19T14:58:52.813Z",
13 "viewed_last_at": "2021-10-19T14:58:52.813Z"
14 }
15 ]
16}
In there, you find all documents, along with a download link. By clicking the link, you are redirected to a .pdf version of the file.
Alright, that was all there is to say about thelemon.markets account endpoints and what you can do with them. Make sure tosign up to our trading API to build your own brokerage experience at the stock market. Also, join ourSlack community, where we discuss all things trading and lemon.markets with more than 400 developers.
Looking forward to seeing you there 🍋 🚀
Marius from lemon.markets
You might also be interested in
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.
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! 💻 📈
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.
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.
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.