Skip to main content

Python Sample for the Ember REST API

The following code demonstrates how to access the Ember REST API from Python. Additionally, this sample shows how to obtain REST API credentials from the KeyCloak service and perform a simple order submission.

import requests
import json
import hashlib
import hmac
import datetime
import uuid

API_KEY = 'w6AcfksrG7GiEFoN'
SECRET = 'gZ0kkI9p8bHHDaBjO3Cyij87SrToYPA3'
KEYCLOAK_URL = "http://localhost:8080"
USER = "admin"
PASSWORD = "deltixadmin"
CLIENT_ID = 'ember_monitor'

#get keycloak token to work with ember monitor
def getKeycloakToken():

header = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {
'grant_type': 'password',
'client_id': CLIENT_ID,
'username': USER,
'password': PASSWORD,
'scope': 'openid',
}
response = requests.post(KEYCLOAK_URL + "/auth/realms/marketmaker/protocol/openid-connect/token", headers=header, data=data)
accessDetails = response.json()
return accessDetails

accessData = getKeycloakToken()
token = accessData["access_token"]

order = json.dumps({
"orderId": uuid.uuid4().hex,
"timestamp": datetime.datetime.utcnow().isoformat(),
"side": "BUY",
"quantity": 1,
"symbol": "BTCUSD",
"orderType": "LIMIT",
"destinationId": "TWAP",
"limitPrice": 37000,
"timeInForce": "GOOD_TILL_CANCEL",
"attributes": [{"key": 6002, "value": "05:03:30"}, {"key": 6023, "value": "25"}]
});

signature = hmac.new(SECRET.encode('utf-8'), order.encode('utf-8'), hashlib.sha384).hexdigest()

headers = {
"Content-Type": "application/json",
'Authorization': 'bearer ' + token, #use token from keycloak for ember api requests
"X-API-KEY": API_KEY,
"X-SIGNATURE": signature,
}

response = requests.post('http://localhost:8988/api/v1/order/new', data=order, headers=headers)

print("Server responded with [%s] %s" % (response.status_code, response.text))

Configuration of API Keys is described here.

Algo orders

If you need to submit an algo order, for example TWAP, algo-specific parameters can be passed as custom order request attributes. Example below shows how to send TWAP order with custom attribute Duration(6002) and ParticipationPercent(6023):

order = json.dumps({
"orderId": uuid.uuid4().hex,
"timestamp": datetime.datetime.utcnow().isoformat(),
"side": "BUY",
"quantity": 1,
"symbol": "BTCUSD",
"orderType": "LIMIT",
"destinationId": "TWAP",
"limitPrice": 37000,
"timeInForce": "GOOD_TILL_CANCEL",
"exchangeId": "COINBASE"
"attributes": [{"key": 6002, "value": "05:03:30"}, {"key": 6023, "value", "25"}]
});