Quick Start
Stream real-time predictions
Here is a quick way to start streaming market predictions using python or node.js. Make sure to instal the requirements and get your API keys before running the sample.
pip install websocket-client
pip install rel
import websocket, rel, hmac, base64, hashlib, json, time, sys
api_key = "add your api key here"
api_secret = 'add your api secret key here'
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws, close_status_code, close_msg):
print("websocket closed")
def on_open(ws):
now = int(time.time()*1000)
message = {
"message_type": "subscribe",
"predictions": [
{
"type": "price_movement",
"exchange_id": "dydx",
"symbol": "BTC-USD"
}
],
"heartbeat": True,
"timestamp": now,
"api_key": api_key
}
message_to_sign = message['message_type'] + message['api_key'] + str(message['timestamp'])
message['signature'] = sign(api_secret, message_to_sign)
ws.send(json.dumps(message))
def sign(secret, message):
signature = hmac.new(base64.b64decode(secret), message.encode('utf-8'), digestmod=hashlib.sha512).digest()
signature_b64 = base64.b64encode(signature).decode()
return signature_b64
if __name__ == "__main__":
ws = websocket.WebSocketApp("wss://ap-northeast-1.gravitan.ai/socket/v1",
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever(dispatcher=rel, reconnect=5)
rel.signal(2, rel.abort)
rel.dispatch()
npm install ws
const WebSocket = require('ws');
const apiKey = 'your api key here';
const privateKey = 'your private api key here';
const ws = new WebSocket('wss://ap-northeast-1.gravitan.ai/socket/v1');
ws.on('open', function open() {
let now = Date.now();
let message = {
message_type: "subscribe",
"predictions": [
{
"prediction_type": "price_movement",
"exchange_id": "dydx",
"symbol": "BTC-USD"
}
],
"heartbeat": True,
timestamp: now,
api_key: apiKey
}
message_to_sign = message.message_type + message.api_key + message.timestamp
const signature = signMessage(privateKey, message_to_sign);
message.signature = signature;
ws.send(JSON.stringify(message));
});
ws.on('message', function incoming(data) {
console.log(data);
});
ws.on('close', function close() {
console.log('disconnected');
});
ws.on('error', function error(err) {
console.error('erro happed ', err);
});
function signMessage(secret, message) {
var key = Buffer.from(secret, 'base64');
var hmac = crypto.createHmac('sha512', key);
var signature = hmac.update(message).digest('base64');
return signature;
}
How to get your API keys
Your API requests are authenticated using an API key. Any request that does not include a valid API key will return an error message.
Prediction data
{
"message_type":"price_movement",
"sequence":1689108541502000,
"timestamp":"2023-07-11T20:48:58.830000Z",
"exchange_id":"dydx",
"symbol":"BTC-USD",
"direction":0,
"threshold":"0.00002",
"horizon":10
}
Last updated