2016.04.21 10:08 AM
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("WARNING: this application overwrites: .u.upd function on q process");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 tickQTable 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);}
}
// Define other methods and classes here
public struct Update
{
public string Symbol; // symbol - friendly name, such as "CME FUTURE ES Jun16"public UpdateType Type; // Trade, BidQuote or AskQuotepublic double Price;public string Condition; // Not used herepublic DateTime DateTime; // DateTime in ETpublic string Exchange; // Not usedpublic int Volume;}
public enum UpdateType
{
Trade, BidQuote, AskQuote}
}
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;
2016.04.21 10:56 AM
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.