On this page I will try to place a new order using the Revolut API.
I want to trade 0.1 BTC to USD at a price of 70.0000 (this is just an example). Here is the code for the Python Script Node and below a screenshot of the output. It works - the Insufficient Balance message is correct, I did not have sufficient BTC available in my account.
import knime.scripting.io as knio
import requests
import base64
import time
import uuid
import json
from pathlib import Path
from nacl.signing import SigningKey
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
# 1. Load your Private Key
pem_data = Path("private.pem").read_bytes()
private_key_obj = serialization.load_pem_private_key(
pem_data,
password=None,
backend=default_backend()
)
# Extract raw bytes for PyNaCl
raw_private = private_key_obj.private_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PrivateFormat.Raw,
encryption_algorithm=serialization.NoEncryption()
)
# 2. Prepare the message
timestamp = str(int(time.time() * 1000))
myuuid = uuid.uuid4()
payload_dict = {
"client_order_id": str(myuuid),
"symbol": "BTC-USD",
"side": "sell",
"order_configuration": {
"limit": {
"base_size": "0.1",
"price": "70000.00",
"execution_instructions": [
"post_only"
]
}
}
}
payload = json.dumps(payload_dict, separators=(',', ':'))
method = "POST"
path = "/api/1.0/orders"
query = "" # No query for balances endpoint
body = payload
# Concatenate without separators
message = f"{timestamp}{method}{path}{query}{body}".encode('utf-8')
# 3. Sign and Encode
signing_key = SigningKey(raw_private)
signed = signing_key.sign(message)
signature = base64.b64encode(signed.signature).decode()
url = "https://revx.revolut.com/api/1.0/orders"
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json',
'X-Revx-Timestamp': timestamp,
'X-Revx-Signature': signature,
'X-Revx-API-Key': 'API key goes here'
}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
knio.output_tables[0] = knio.input_tables[0]
As another example, I tried to place an order to convert 10$ worth of bitcoin into USD. Below you can see the output. The order was filled.
BTW, If I now execute the Order History Node we saw before, we will see the new order now shows up:
With this, we have the necessary building blocks in place to trade on Revolut. Further on, I will add nodes to gather historic values of assets (BTC, USD, EUR, ....) and try to build n agent that can make autonomous trading decisions. I will continue on the next page