C# websocket

ak1194
using System;
using KiteConnect;

namespace ConsoleApp__data
{
class Program
{
static void Main(string[] args)
{

String MyAPIKey = "xxxxxxxx";
String MyAccessToken = "xxxxxxxxxx";
// Create a new Ticker instance
Ticker ticker = new Ticker(MyAPIKey, MyAccessToken);

// Add handlers to events
ticker.OnTick += onTick;
ticker.OnOrderUpdate += OnOrderUpdate;
ticker.OnReconnect += onReconnect;
ticker.OnNoReconnect += oNoReconnect;
ticker.OnError += onError;
ticker.OnClose += onClose;
ticker.OnConnect += onConnect;

// Engage reconnection mechanism and connect to ticker
ticker.EnableReconnect(Interval: 5, Retries: 50);
ticker.Connect();

// Subscribing to NIFTY50 and setting mode to LTP
ticker.Subscribe(Tokens: new UInt32[] { 738561 });
ticker.SetMode(Tokens: new UInt32[] { 738561 }, Mode: Constants.MODE_FULL);


// Disconnect ticker before closing the application
// ticker.Close();
}

private static void onConnect()
{
Console.WriteLine("Connected ticker");
}

private static void onError(string Message)
{
Console.WriteLine("Error: " + Message);
}

private static void onClose()
{
Console.WriteLine("Closed ticker");
}

private static void oNoReconnect()
{
Console.WriteLine("Not reconnecting");
}

private static void onReconnect()
{
Console.WriteLine("Reconnecting");
}

// Example onTick handler
private static void onTick(Tick TickData)
{
Console.WriteLine("LTP: " + TickData.LastPrice);
}



}
}
I am just trying websocket print to get the ltp, but there is no data, output as follows :
"connected ticker" and end of program with 0
how to get the ltp in console print. what i am doing wrong?
Tagged:
  • sujith
    You can only send a message for the subscription after it gets connected. So subscribe inside the onConnect.
  • ak1194
    ak1194 edited February 19
    using System;
    using KiteConnect;

    namespace ConsoleApp__data
    {
    class Program
    {
    static Ticker ticker =new Ticker("xxxxx", "xxxxx");
    static void Main(string[] args)
    {


    // Add handlers to events
    ticker.OnTick += onTick;
    ticker.OnOrderUpdate += OnOrderUpdate;
    ticker.OnReconnect += onReconnect;
    ticker.OnNoReconnect += oNoReconnect;
    ticker.OnError += onError;
    ticker.OnClose += onClose;
    ticker.OnConnect += onConnect;

    // Engage reconnection mechanism and connect to ticker
    ticker.EnableReconnect(Interval: 5, Retries: 50);
    ticker.Connect();


    // Disconnect ticker before closing the application
    // ticker.Close();
    }

    private static void onConnect()
    {
    Console.WriteLine("Connected ticker");
    // Subscribing to NIFTY50 and setting mode to LTP
    ticker.Subscribe(Tokens: new UInt32[] { 738561 });
    ticker.SetMode(Tokens: new UInt32[] { 738561 }, Mode: Constants.MODE_FULL);


    }

    private static void onError(string Message)
    {
    Console.WriteLine("Error: " + Message);
    }

    private static void onClose()
    {
    Console.WriteLine("Closed ticker");
    }

    private static void oNoReconnect()
    {
    Console.WriteLine("Not reconnecting");
    }

    private static void onReconnect()
    {
    Console.WriteLine("Reconnecting");
    }

    // Example onTick handler
    private static void onTick(Tick TickData)
    {
    Console.WriteLine("LTP: " + TickData.LastPrice);
    }

    private static void OnOrderUpdate(Order OrderData)
    {
    Console.WriteLine("OrderUpdate " + Utils.JsonSerialize(OrderData));
    }

    }
    }
    still not able to get ltp in console, program just ends with code 0. But I am trying to get the ltp to console.writeline @sujith help here!
  • ak1194
    referred kite c# page, similar code only found. Trying to solve this error
  • sujith
    Can you run it in debug mode and paste the complete stacktrace here?
  • ak1194
    @sujith after long time i gave a try again, the program does not through any error, but the ontick() does not function and program exit with 0.

    Also, copied even the example program from https://github.com/zerodhatech/dotnetkiteconnect/blob/master/KiteConnectSample/Program.cs for websocket method.

    output of program -
    Connected ticker
    Closed ticker

    program exit with 0.
    does not make sense why ltp not printing, can you gave a example from your end
  • trade_then

    Sorry if i am wrong. but this is hilarious.
    I apologize.
    But you are having a couple month long problem.
    because you forgot to put Console.ReadLine() or Read()
    at the end of your program.

    Of course your program terminates with just "Connected" message.
    there is nothing to stop your program from running to its end.

    this observation is based on the snippet you provide above.
    if it is something else. i apologise.

    Thanks
    Regards

  • tonystark
    Sample project is provided to demonstrate the function calls. You are not suppose to run it as it is.
    That being said you missed the critical part of that code to keep your program running so that ticker can function. :)

    Console.ReadKey();
  • ak1194
    ak1194 edited April 29
    Thanks @trade_then and @tonystark :), My bad, now its working
  • ak1194
    ak1194 edited April 29
    TickData.Change will always return '0' as value ? I have tested for 3 tickers, all return 0. TickData.Change is days percentage change right? @tonystark @trade_then @sujith
  • ak1194

    attached locals variable captured for reference @tonystark @trade_then, is there a problem with change in websocket
  • sujith
    We don't receive change data from the exchange. You need to calculate as mentioned here.
  • ak1194
    ak1194 edited April 29
    @sujith Thanks
  • trade_then
    @ak1194

    in above comments @sujith mentions that.

    You can only send a message for the subscription after it gets connected. So subscribe inside the onConnect.
    in case you wonder why your code works even when you subscribe outside of onConnect.
    The reason for that is this line.

    because here websocket connection is waited and does not return untill connected.
    otherwise ticker.Subscribe(Tokens: new UInt32[] { 738561 });
    would not have worked outside OnConnect.

    maybe it is different in other language bindings for kiteconnect. but C# one makes it work
    for you because of that line.

    Thanks
    Regards.
  • ak1194
    ak1194 edited April 29
    @trade_then got it.. thanks for looking into it
Sign In or Register to comment.