Ticker connect gets close very often.

vishwash_yadav
Ticker connection gets closed and it tries to reconnect after every 5 second as reconnect interval was 5, and error was unable to parse instrument token. My question is i haven't even added any new stock to list. if it was able to parse one time why not second time. Fine if was unable to parse also but which instrument token was unable to parse that also not clear. Please help me on this.
  • zartimus
    @vishwash_yadav Please provide the traceback and code.
  • vishwash_yadav
    vishwash_yadav edited June 2018
    which code you are asking. Please respond
  • sujith
    Can you paste your websocket API code here?
  • vishwash_yadav
    You mean code in KiteConnect dll, because i have not added any code for websocket. Simply i am calling Connect method to connect with Socket.
  • vishwash_yadav
    Please respond, today also after 15 mins means from 9:30 again it started giving error 'unable to parse instrument token', 'One or more errors occured'

    if it goes like this it's horrible.
  • tonystark
    Hi @vishwash_yadav

    Can you post the code that creates and subscribes the ticker? We will try to run the same and reproduce the issue.
  • vishwash_yadav
    private void initTicker()
    {
    ticker = new Ticker(MyAPIKey, MyAccessToken);

    ticker.OnTick += OnTick;
    ticker.OnReconnect += OnReconnect;
    ticker.OnNoReconnect += OnNoReconnect;
    ticker.OnError += OnError;
    ticker.OnClose += OnClose;
    ticker.OnConnect += OnConnect;
    ticker.OnOrderUpdate += OnOrderUpdate;

    ticker.EnableReconnect(Interval: 1, Retries: 50);
    ticker.Connect();
    }

    private void OnOrderUpdate(Order OrderData)
    {
    string symbol = string.Format("{0}:{1}", OrderData.Exchange, OrderData.Tradingsymbol);

    Events.RaiseRevievedOrderInfoEvent(OrderData);
    }

    private void OnConnect()
    {

    }

    private void OnClose()
    {

    }

    private void OnError(string Message)
    {

    }

    private void OnNoReconnect()
    {

    }

    private void OnReconnect()
    {

    }

    private void OnTick(Tick TickData)
    {
    try
    {
    Events.RaiseStockLTPChangedEvent(_instrumentToken[TickData.InstrumentToken], TickData.LastPrice);
    }
    catch (Exception)
    {

    }
    }
  • vishwash_yadav
    /*To Subscribe stock for LTP */


    private void Events_AskForStockSubscriptionEvent(string[] tradingSymbol, StockSubscribeMode mode, bool isSubscribe)
    {
    GetCode:
    try
    {
    if (isSubscribe)
    {
    var ltp = Kite.GetLTP(tradingSymbol);
    List tokens = new List();
    foreach (var item in ltp)
    {
    var token = item.Value.InstrumentToken;
    _instrumentToken[token] = item.Key;
    tokens.Add(token);
    }
    var ar = tokens.ToArray();
    ticker.Subscribe(ar);
    ticker.SetMode(ar, Mode: KiteConnect.Constants.MODE_LTP);
    }
    else
    {

    List tokens = new List();
    foreach (var item in tradingSymbol)
    {
    if (_instrumentToken.Any(s => s.Value.Equals(item)))
    {
    var first = _instrumentToken.FirstOrDefault(s => s.Value.Equals(tradingSymbol));
    tokens.Add(first.Key);
    }
    }
    var ar = tokens.ToArray();
    ticker.UnSubscribe(ar);
    }
    }
    catch (Exception)
    {
    goto GetCode;
    }
    }
  • tonystark
    Hi @vishwash_yadav

    The mentioned error happens when you send the instrument tokens as strings instead of integers. But your code looks fine. We need more data to debug this issue. Can you initialize ticker like below:

    Ticker ticker = new Ticker(MyAPIKey, MyAccessToken, Debug: true);

    This will print more data about the things happening behind the scenes in the debug console. Can you post the debug log you get when you subscribe and get the error?
  • vishwash_yadav
    {"a":"unsubscribe","v":[0]}
    {"a":"unsubscribe","v":[0]}
    {"a":"unsubscribe","v":[0]}
    {"a":"unsubscribe","v":[0]}

    Getting this message while unsubscribing and that is where it is trying to reconnect and resubscribe.
  • tonystark
    @vishwash_yadav

    Can you try to log "ar" used in this line of code?

    ticker.UnSubscribe(ar);

    I believe there is a token which is 0. And that is invalid.
  • NaveenR
    I am also getting the same error, Socket is getting disconnected around 10:00 AM daily, after reconnecting success it is giving below error : Error parsing instrument tokens.

    After error I am getting Lost ticker connection. Once it is getting disconnected it is not re-connecting. I also checked with the internet connectivity, not issues from internet provider. Internet connection is stable.

    I am using KiteConnect.dll run time version v4.0.30319. Please respond ASAP. This is happening to me for more than a week.
  • tonystark
    @NaveenR Can you post the debug logs from ticker at that time frame?

    Also, if your internet connection can deliver 10-20 KB/s that is enough for hundreds of tokens. The disconnections usually happen due to the proxies or firewalls at your end or at your ISP end. Some ISPs might disconnect long-standing connections after some time.

    Please post the log, we will check what's going on. Thanks.
  • NaveenR
    NaveenR edited October 2018
    I didn't receive any log, below is my code and the steps which I have tracked
    // Create a new Ticker instance
    ticker = new Ticker(sApiKey, sAccessToken, Reconnect: true, Debug: true);

    // Add handlers to events
    ticker.OnTick += onTick;
    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();
    Till 10:25 AM it was fine, once it is disconnected, it will reconnect, connect and will call error function.

    in onError function I am getting - Error parsing instrument tokens and then Lost ticker connection.

    Please suggest me if I am making any mistake in coding.

    Regards.
  • NaveenR
    I have also tracked the internet connection frequently. It is working fine, there is no issues with ISP
  • tonystark
    Are you building a console application or a GUI application? If it is a GUI application your logs will be in the Debug window or in the Output window. If it is a console application, by logs I meant whatever gets printed in the console. Can you post those logs at that time frame?

    Also, can you confirm that this happens exactly at 10.00 AM?
  • NaveenR
    I am building windows forms,
    I am receiving only "Error parsing instrument tokens" and "Lost ticker connection" under my log.

    private void onError(string Message)
    {
    Console.WriteLine(Message);
    logFile.WriteLogToFile("Socket Connection error : " + Message + DateTime.Now);
    }

    This happens around 09:50 AM to 10:00 AM daily. I am able to fetch the data till 09:50 AM. Once it got disconnected, I am not able to connect again.
  • tonystark
    Please check Debug -> Windows -> Output to see the logs from ticker
  • NaveenR
    "Error parsing instrument tokens", this is what I am getting under output.
    Is there any other kiteConnect.dll to find more information on debug mode?
  • rakeshr
    @NaveenR
    Make sure you are passing instrument tokens as numbers, not as string/character.
  • NaveenR
    I got something like this in my console at 9:40 AM
    Resubscribing
    {"a":"unsubscribe","v":[107265]}
    30
    {"a":"mode","v":["ltp", [107265]]}
    {"a":"mode","v":["quote", []]}
    {"a":"mode","v":["full", []]}
    Exception thrown: 'System.InvalidOperationException' in System.Windows.Forms.dll

    After disconnecting it is not reconnecting
  • tonystark
    tonystark edited October 2018
    @NaveenR

    Exception thrown: 'System.InvalidOperationException' in System.Windows.Forms.dll

    This is thrown from the UI. Can you double check the logic you are using? Also to isolate the source of bug maybe you should create a simple app with just ticker and try to reproduce this issue.
  • NaveenR
    I changed the code, now there is reconnecting issues.

    Below is my log from my console

    The thread 0x29f0 has exited with code 0 (0x0).
    4
    No of packets: 2
    No of bytes: 22
    Packet Length 8
    Packet Length 8
    15/11/2018 13:37:23 Heartbeat
    No of packets: 2
    No of bytes: 22
    Packet Length 8
    Packet Length 8
    4
    No of packets: 1
    No of bytes: 12
    Packet Length 8
    4
    The thread 0x170 has exited with code 0 (0x0).
    3
    The thread 0x2514 has exited with code 0 (0x0).
    2
    1
    0
    Resubscribing
    {"a":"unsubscribe","v":[7455745,1921537]}
    39
    {"a":"mode","v":["ltp", [7455745,1921537]]}
    {"a":"mode","v":["quote", []]}
    {"a":"mode","v":["full", []]}
    New interval 5
    5
    WebSocket Message: {"type":"error","data":"Error parsing instrument tokens."}
    WebSocket Message: {"type":"error","data":"Error parsing instrument tokens."}
  • tonystark
    @NaveenR Which version of the library are you using?
  • NaveenR
    Runtime Version : v4.0.30319
    Version : 3.0.0.0
  • tonystark
    Can you try the latest version? You can see the updates here: https://www.nuget.org/packages/Tech.Zerodha.KiteConnect
Sign In or Register to comment.