public trade and price feed example

Added by Philip over 7 years ago

Hi All,

I find it hard to understand the documentation on this topic.
Can anyone help me with a sample on how to subscribe and handle public trade and price feed?

I have the session key and public/private feed information from login but I just don't know how to use this information to access the public feed.

C# or VB.NET example will really help.

Thanks


Replies (12)

RE: public trade and price feed example - Added by Hannu over 7 years ago

Hi,
I found it difficult too. This is how I did it with java.

1. create ssl socket to public_feed (hostname from the rest login)
"public_feed":{"port":443,
"hostname":"pub.api.test.nordnet.se",
"encrypted":true}}

2. send login command to socket
String loginCmd = "{\"cmd\":\"login\", \"args\":{ \"session_key\":\""
+ sessionKey + "\", \"service\":\"NEXTAPI\"}}\n";
3. send other commands:
example String cmd_trades = "{\"cmd\":\"subscribe\", \"args\":{\"t\":\"trade\", \"i\":\"24311\", \"m\":24}}\n";

4. Start reading socket. You should receive hearbeats and trade info : {"cmd":"heartbeat","args":{}}

RE: public trade and price feed example - Added by Philip over 7 years ago

Thanks hannu,

So any idea if its via tcp or ?

Also if anyone has a .NET example it will really help a lot.

Thanks

RE: public trade and price feed example - Added by Nordnet Simon over 7 years ago

Hi, it is SSL over TCP so SSLStream class in .NET would work.

We don't have an example ready. But there is an example in the Microsoft documentation of SSLStream that should send you in the right direction.

RE: public trade and price feed example - Added by Philip over 7 years ago

Finally we have been able to read public feed using SSLStream.

thanks

RE: public trade and price feed example - Added by Martin over 7 years ago

I'm having trouble connecting with SSL socket. Which certificate file did you use???

Can anyone post .net sample code? - Added by Johan over 7 years ago

Everything seems to be fine but I dont get any response from the server.

RE: public trade and price feed example - Added by Johan over 7 years ago

I had a look with WireShark, as far as I can tell it looks ok until I write the logincommand and flush it. Then I get an encrypted alert (21) response from the server.
According to Wikipedia: [[http://en.wikipedia.org/wiki/Transport_Layer_Security]] this means:
21 Decryption failed fatal TLS only, reserved

My code for writing the logincommand to the sslStream:

            string loginCommandJsonString = JsonConvert.SerializeObject(loginCommand);
            _streamWriter.WriteLine(loginCommandJsonString);
            _streamWriter.Write(@"<EOF>");
            _streamWriter.Flush();

As far as I can tell the serialized logincommand looks ok.

Any idea about what to do next?

RE: public trade and price feed example - Added by Nordnet Simon over 7 years ago

Try this and see if it works:


            string loginCommandJsonString = JsonConvert.SerializeObject(loginCommand) + "\n";
            _streamWriter.Write(loginCommandJsonString);
            _streamWriter.Flush();


EDIT: I changed WriteLine to Write.

RE: public trade and price feed example - Added by Johan over 7 years ago

I set the property NewLine = "\n" for the streamWriter so it should work with streamWriter.WriteLine, confirmed by test below. I will of course try your suggestion but I am behind a proxy at work now so I am gonna test it when I get home.

Can you see anything in the server logs for the time I made my post above?

Sidenote: is it possible to send private messages on this forum?

        [Test]
        public void StreamTest()
        {
            MemoryStream memoryStream = new MemoryStream();
            var encoding = Encoding.UTF8;
            StreamWriter streamWriter = new StreamWriter(memoryStream, encoding)
                                            {
                                                AutoFlush = false,
                                                NewLine = "\n" 
                                            };
            streamWriter.WriteLine("Test");
            streamWriter.Flush();
            //memoryStream.Write("Test",encoding);

            StreamReader streamReader = new StreamReader(memoryStream, encoding);
            memoryStream.Position = 0;
            string readString = streamReader.ReadToEnd();
            Console.Out.WriteLine(readString);
            //string readString = memoryStream.ReadString(encoding);
        }

RE: public trade and price feed example - Added by Johan over 7 years ago

        [Test]
        public void StreamTest()
        {
            MemoryStream memoryStream = new MemoryStream();
            var encoding = Encoding.UTF8;
            StreamWriter streamWriter = new StreamWriter(memoryStream, encoding)
                                            {
                                                AutoFlush = false,
                                                NewLine = "\n" 
                                            };
            streamWriter.WriteLine("Test");
            streamWriter.Flush();
            //memoryStream.Write("Test",encoding);

            StreamReader streamReader = new StreamReader(memoryStream, encoding);
            memoryStream.Position = 0;
            string readString = streamReader.ReadToEnd();
            Assert.AreEqual("Test\n",readString);
            //string readString = memoryStream.ReadString(encoding);
        }

Edited test.

Sidenote, is it possible to edit posts on this forum?

RE: public trade and price feed example - Added by Johan over 7 years ago

What is the proper encoding for the commandstrings?

RE: public trade and price feed example - Added by Johan over 7 years ago

It works now! I dont see any "<EOF>" as in the MSDN example in the heartbeats though.

(1-12/12)