2

I'm currently trying to subscribe to my tickerplant using qsharp however when I try to get the message data I just get System.Int64[] to be returned or system.object[] as shown here: enter image description here

C# code taken from the sample:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using NetMQ;
using System.Text.RegularExpressions;
using qSharp;
namespace SubscribeToData
{
    class Program
    {
        static void Main(string[] args)
        {
            QCallbackConnection q = new QCallbackConnection("localhost", 5005);
            try
            {

                Console.WriteLine("conn: " + q + "  protocol: " + q.ProtocolVersion);
                Console.WriteLine("Press <ENTER> to close application");
                q.DataReceived += OnData;
                q.ErrorOccured += OnError;
                q.Open();
                Console.WriteLine("conn: " + q + "  protocol: " + q.ProtocolVersion);
                Console.WriteLine("Press <ENTER> to close application");
                Object response = q.Sync(".u.sub", "trade", ""); // subscribe to tick
                QTable model = (QTable)((Object[])response)[1]; // get table model
                Console.WriteLine(model);
                q.StartListener();
                Console.ReadLine();
                q.StopListener();
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error occured: " + e);
                Console.ReadLine();
            }
            finally
            {
                q.Close();
            }



        }
        static void OnData(object sender, QMessageEvent message)
        {
            Object data = message.Message.Data;
            Console.WriteLine(sender);
            Console.WriteLine(message.Message.Data);
            Console.WriteLine("message type: " + message.Message.MessageType + " size: " + message.Message.MessageSize + " isCompressed: " + message.Message.Compressed + " endianess: " + message.Message.Endianess);
            PrintResult(message.Message.Data);
            if (data is Object[])
            {
                 Console.WriteLine("TEST");
                Console.WriteLine(message);
                // unpack upd message
                Object[] args = ((Object[])data);
                Console.WriteLine("testargs");
                Console.WriteLine(args[0].ToString());
                Console.WriteLine(args[1]);
                Console.WriteLine(args[2].GetType());
                Console.WriteLine(data);
                if (args.Length == 3 && args[0].Equals("upd") && args[2] is QTable)
                {
                    QTable table = (QTable)args[2];
                    foreach (QTable.Row row in table)
                    {
                        Console.WriteLine(row);
                    }
                }
            }
        }
        static void PrintResult(object obj)
        {
            if (obj == null)
            {
                Console.WriteLine("::");
            }
            else if (obj is QDictionary)
            {
                PrintResult(obj as QDictionary);
            }
            else
            {
                Console.WriteLine(obj);
            }
        }
        static void PrintResult(QDictionary d)
        {
            foreach (QDictionary.KeyValuePair e in d)
            {
                Console.WriteLine(e.Key + "| " + e.Value);
            }
        }
        static void OnError(object sender, QErrorEvent error)
        {
            Console.Error.WriteLine("Error received via callback: " + error.Cause.Message);
        }
    }

And for my q code:

Tickerplant:

trade:([]daytime:();instrumenttype:();symbol:();expiration:();mtype:();price:();cond:();exchange:();volume:());
quote:([]daytime:();instrumenttype:();symbol:();expiration:();mtype:();price:();cond:();exchange:();volume:());
upath:"tick/u.q";
@[system;"l ",upath;{-2"Failed to load u.q from ",x," : ",y,
                       ". Please make sure u.q is accessible.",
                       " kdb+tick can be downloaded from http://code.kx.com/wsvn/code/kx/kdb+tick";
                       exit 2}[upath]]
.u.init[]
.u.upd:insert;

I publish using

.u.pub[`trade;2,2,2,2,2,1,1,1,1]

Now the problem is it seems that it does receive the QMessage but is just parsing incorrectly?

Maciej Lach
  • 1,622
  • 3
  • 20
  • 27
Rtrader
  • 917
  • 4
  • 11
  • 26
  • I'm confused as to what your problem is........you're publishing a list of longs (int64) and you're receiving a list of int64s. Are you expecting to receive a QTable type? But you haven't published a QTable from the tickerplant – terrylynch Apr 25 '16 at 13:34
  • Try publishing a table from the tickerplant, i.e. .u.pub[\`tab;([] col1:\`a\`b\`c;col2:1 2 3)] – terrylynch Apr 25 '16 at 13:38
  • @terrylynch I did expect a Qtable from the tickerplant, however I'm thinking that publishing a table from the tickerplant is inefficient compared to publishing one record – Rtrader Apr 25 '16 at 13:40
  • 1
    Yes, publishing records is more efficient and that is what tickerplants generally do. And kdb is able to insert records into tables in that manner, e.g. \`trade insert 2,2,2,2,2,1,1,1,1 In your case though, using qSharp, you'd have to handle that record accordingly. For example, you can't check "&& args[2] is QTable" because the message published will not contain a QTable it will contain a mixed list of records. In other words, the record (list) you published from the tickerplant doesn't get turned into a table along the way, it remains a list. – terrylynch Apr 25 '16 at 13:50
  • @terrylynch , thanks, that makes sense do you know is there anyway to read the record using qsharp? – Rtrader Apr 25 '16 at 14:08
  • 1
    Not that I'm aware of other than the exxeleron contribution (https://github.com/exxeleron/qSharp). You would read the record itself as another object where arg[0] is your first field (daytime), arg[1] is your next field (instrumenttype) etc etc. Ultimately it might be easier to publish as tables from the tickerplant - depending on how much throughput/volume you expect! – terrylynch Apr 25 '16 at 14:21
  • 1
    As @terrylynch wrote, record (q mixed list) is parsed as `object[]` in qSharp. Each element of the list is parsed individually and parsed as described [here](https://github.com/exxeleron/qSharp/blob/master/doc/Type-Conversion.md). – Maciej Lach Apr 28 '16 at 07:13

0 Answers0