Multiple subscriptions

Added by Joachim about 2 years ago

I use this piece of code to subscribe to the public price feed for market 11 and instrument 101. What is the proper way of subscribing to multiple instruments?

market = 11
instrument = "101" 
cmd = {"cmd":"subscribe", "args":{"t":"price", "m":market, "i":instrument}}
num_bytes = ssl_socket.send(json.dumps(cmd) + "\n")
print "Subscription request sent for market = %d and instrument = %s (%d bytes)" % (market, instrument, num_bytes)
print "Reading stream" 
for i in range(10):
    output = ssl_socket.read(1024)
    print (output)
    time.sleep(1)
del ssl_socket
s.close()

Thanks!


Replies (5)

RE: Multiple subscriptions - Added by Lars about 2 years ago

Send one request (one json packet) per instrument.

RE: Multiple subscriptions - Added by Joachim about 2 years ago

Thank you! Works perfect! :)

RE: Multiple subscriptions - Added by Morten about 2 years ago

Hey Joachim,

How did you manage to get access to the feeds?

Morten

RE: Multiple subscriptions - Added by Joachim about 2 years ago

Hi Morten,

I used code from this post https://api.test.nordnet.se/boards/2/topics/2397

Then I changed this line to use the sessionKey provided by the login call instead of the base64 encoded buf.

#Send session key (changed this...)
cmd = {"cmd":"login", "args":{"session_key":buf, "service":"NEXTAPI"}}

# (...into this)
cmd = {"cmd":"login", "args":{"session_key":sessionKey, "service":"NEXTAPI"}}

Then, for every instrument I call the ssl_socket.send function with a subscribe command. Something like this:

instruments = ["101", "4870"] # ERIC B and FING B
market_id = 11
for i in range(0, len(instruments)):
    instrument = instruments[i]
    cmd = {"cmd": "subscribe", "args": {"t": "price", "m": market_id, "i": instrument}}
    numBytes = ssl_socket.send(json.dumps(cmd) + "\n")
    print("Subscription request sent for market = %d and instrument = %s (%d bytes)" % (market_id, instrument, numBytes))

RE: Multiple subscriptions - Added by Morten about 2 years ago

thanks for the update, i still get nothing unfortunately

(1-5/5)