Subscribing an indicator

I have been trying to get my program to subscribe to tick bars for a given symbol and I can’t seem to get it to work. I’ve read the documentation and I don’t understand what I am supposed to do differently. Below is the code for subscribing and opening a webSocket I’ve used:

import websocket
import requests

import IRONBEAM_Client as client


def get_streamId(token):

    streamId = client.getStreamId(token)
    return streamId

def subscribe(token, streamId):

    base_url = "https://demo.ironbeamapi.com/v2"
    endpoint = f"/indicator/{streamId}/tickBars/subscribe"
    
    url = f"{base_url}{endpoint}?token={token['token']}"

    payload = {
        "symbol": "XCME:NQ.M25",
        "period": 1,
        "barType": "DAILY",
        "loadSize": 100
    }

    try:
        resp = requests.post(url, json=payload)
        resp.raise_for_status()
        data = resp.json()
        print("Subscribe tick bars response:", data)
    except requests.HTTPError as e:
        print("Failed to subscribe to tick bars:", e, e.response.text)


def open_stream(token, streamId):
    url = f"wss://demo.ironbeamapi.com/v2/stream/{streamId}?token={token['token']}"

    def on_open(ws):
        print("Stream opened")

    def on_message(ws, message):
        print("Received message:", message)
    
    # Callback: print errors.
    def on_error(ws, error):
        print("WebSocket error:", error)
    
    # Callback: inform when the connection is closed.
    def on_close(ws, close_status_code, close_msg):
        print("WebSocket closed with code:", close_status_code, "message:", close_msg)
    
    # Create the WebSocketApp with the callbacks.
    ws_app = websocket.WebSocketApp(url,
                                    on_open=on_open,
                                    on_message=on_message,
                                    on_error=on_error,
                                    on_close=on_close)


    ws_app.run_forever()

I call these functions in this order :

streamId = get_streamId(token)
subscribe(token, streamId)
open_stream(token, streamId)

The error I am getting is:
Failed to subscribe to tick bars: 400 Client Error: Bad Request for url:…{“additionalProperties”:{},“error1”:“Can’t subscribe to tick bars”,“status”:“ERROR”,“message”:“Error”}

I know that the token is good because I can use it in other contexts like getting my account balance or other stuff, the streamId is also correct because if I don’t call subscribe() the stream opens and I get pinged.

Any help would be greatly appreciated

For anyone having the same problem I found out what I did wrong. The stream needs to be opened before you can actually subscribe to a symbol.

I also ran into this issue. Although it’s not obvious from reading the documentation, you need to open the stream before creating the subscription.

streamId = get_streamId(token)
open_stream(token, streamId)
subscribe(token, streamId)
1 Like