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()

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.

For now, contact us at [email protected] to get an API key and set up an account. A self-service protal will be made available in the near future.

Prediction data

Below is a sample prediction data for price movement. More detail on different types of predictions can be found here.

{
    "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