Price feed empty on test system

Added by David over 4 years ago

FACTS
  • I work against the test system.
  • I have successfully created a python NordnetConnector class that connects to the rest api.
  • I log into the public feed and subscribe to some prices.
  • My code seem to work (no errors) but it only receives empty packages from the feed.

FEED CODE

async def simple_echo_client(nc : NordnetConnector):

    f = nc.rest.loginResponse.public_feed
    reader, writer = await asyncio.open_connection(f.hostname, f.port)

    payload = {'cmd':'login', 'args':{'session_key' : nc.rest.loginResponse.session_key, 'service': 'NEXTAPI'}}
    writer.write(simplejson.dumps(payload).encode('UTF-8'))

    payload = {"cmd":"subscribe", "args":{"t":"price", "i":"1869", "m":30}}
    await  writer.drain()

    payload = {"cmd":"subscribe", "args":{"t":"price", "i":"101", "m":11}}
    writer.write(simplejson.dumps(payload).encode('UTF-8'))

    await  writer.drain()

    print("Lines received")
    while True:
        line = await reader.readline()
        print('L', line)

    writer.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(simple_echo_client(nc))

OUTPUT

L b''
L b''
L b''
.
.
.


Replies (4)

RE: Price feed empty on test system - Added by Nordnet Aleksandar over 4 years ago

Hi David,

I'm not too familiar with python or the libs you are using, could you make sure that you are sending a newline after each of your commands that you write to the socket?

What i think could be happening is that none of your commands are being processed and the socket is being closed remotely (after 10 seconds of inactivity).
And it appears that you loop over the function so it probably just restarts the whole process and prints 1 line per closed socket.

/Aleks

RE: Price feed empty on test system - Added by David over 4 years ago

Hi,

thanks! There where two errors in my code:

  1. I missed the newline requirement.
  2. I missed the ssl=True optional requirement in asyncio.open_connection

Working Code

def d2s(d : dict ):
    s = simplejson.dumps(d) + '\n'
    return s.encode('UTF-8')

async def simple_echo_client(nnc : NordnetConnector):
    f = nnc.rest.loginResponse.public_feed
    reader, writer = await asyncio.open_connection(f.hostname, f.port, ssl=True)
    payload = {'cmd':'login', 'args':{'session_key' : nnc.rest.loginResponse.session_key}}

    writer.write(d2s(payload))
    await  writer.drain()
    payload = {"cmd":"subscribe", "args":{"t":"price", "i":"101", "m":11}}
    writer.write(d2s(payload))
    await  writer.drain()

    print("Lines received")
    while True:
        line = await reader.readline()
        print('L', line)
        if not line:
            break
    writer.close()

loop = asyncio.get_event_loop()
loop.run_until_complete(simple_echo_client(nc))

Resonse

L b'{"type":"price","data":{"i":"101","m":11,"trade_timestamp":1455010921185,"tick_timestamp":1455010921185,"bid":72.85,"bid_volume":12,"ask":0.0000,"ask_volume":0,"close":72.95,"high":75.00,"last":73.50,"last_volume":10,"low":72.95,"open":75.00,"vwap":74.08,"turnover":1703.85,"turnover_volume":23}}\n'

RE: Price feed empty on test system - Added by Ulf over 4 years ago

Hej David
Skulle du kunna lista de bibliotek som du importerar. Intressant för alla Pyton programmerare som försöker sig på detta.
Ulf

RE: Price feed empty on test system - Added by David over 4 years ago

Yes,

the only required library for this code is the asyncio (https://docs.python.org/3/library/asyncio.html) library. My NordnetConnector is just a simple wrapper around the rest API. The keywords async and await are new in python 3.5 so the this code must be run under python3.5. The async library tries to enable framework for event based programming without the hassle of threads and callbanks. If you change the call sign of simple_echo_client to instead take arguments:

  • hostname
  • port
  • session_key

the python file should run with with just

import asyncio
import simplejson

statements at the top

(1-4/4)