How Does OnTick Work ?

DA1077
Hi,

I have below question regarding OnTick Method -

Suppose I have subscribed to 200 Instruments. How will OnTick Get triggered ? Will it be triggered for every single Instrument as and when there is a trade on that instrument or will it club 2/3/More Instruments Data and pass that as a single packet ? I am asking this question because when I subscribed to 200 Scripts in my Python code and printed them ( Using Pandas Dataframe) I normally got a Packet which contained more than one Instruments data. But when I used InTick in my Visual Basic code I am getting data for only one instrument at a time via OnTick callback method. Please clarify how Ontick is triggered ? For every single Instrument as and when there is a trade on that instrument(Which seems to be the case in my VB code) or will it club 2/3/More Instruments Data and pass that as a single packet(Which seems to be the case in my Python code) ?

Thanks,
Amitabha
  • tonystark
    tonystark edited November 2017
    Hi @DA1077,

    OnTick event gives data for one instrument. So if the server sends data for 10 instruments in a packet OnTick will be called 10 times with corresponding instruments.

    And as you inferred, it is different from python implementation where ticks are sent as an array.
  • DA1077
    Hi ajinasokan,

    Thank for the clarification. One more issue I am facing in Visual Basic is that the OnTick for every tick is getting called twice. I checked thoroughly if the handler is getting called twice in my code but I found that its called only once from one place. Please let me know if there is any issue at your end for the OnTick Mechanism. Attaching the sample op file for your reference.

    Thanks,
    Amitabha
  • tonystark
    Hi @DA1077,

    Can you explain what is "Thread 0" in the log file?

    Also, I would suggest you try a simple program with just ticker to see if this is an issue with the library. If the issue still persists even in that situation let us know.
  • DA1077
    Hi ajinasokan,

    In earlier op file Thread0 is simply an extra hardcoded string appended before writing the tick details.

    As per your suggestion I have run a simple code with just ticker but still found that the events (like connect / OnTick ) are getting called twice. Attached another sample file as the op of the simple code.

    The format of the file is defined by below code inside OnTick.

    My.Computer.FileSystem.WriteAllText("C:\Users\nandy\Desktop\OGGN-New\TickData1.txt", Now.ToString("hh:mm:ss.fff") & " " & tickdata.InstrumentToken & " " & tickdata.Open & " " & tickdata.High & " " & tickdata.Low & " " & tickdata.LastPrice & " " & tickdata.Volume & vbCrLf, True)


    As you can see from the op file that the same ticks are called twice. Please check at our end and let me know if this is an issue at your end.

    Thanks,
    Amitabha
  • tonystark
    tonystark edited November 2017
    Hi @DA1077,

    Can you please share your complete sample VB code?
  • DA1077
    DA1077 edited November 2017
    Here it is -
      Imports System.ComponentModel
    Imports System
    Imports System.IO
    Imports KiteConnect

    Dim user As User
    Public WithEvents tickerPredefined As Ticker
    Public kite As Kite

    Private Sub btnTestTicker_Click(sender As Object, e As EventArgs) Handles btnTestTicker.Click
    tickerPredefined = New Ticker(tbxAPIKey.Text, tbxUserID.Text, tbxPublicToken.Text)
    AddHandler tickerPredefined.OnTick, AddressOf Ontick
    AddHandler tickerPredefined.OnConnect, AddressOf Onconnect
    tickerPredefined.Connect()
    tickerPredefined.Subscribe(Tokens:={"53471751", "53466887", "53474823", "53372423", "53499143"})
    tickerPredefined.SetMode(Tokens:={"53471751", "53466887", "53474823", "53372423", "53499143"}, Mode:="quote")
    End Sub


    Private Sub Ontick(tickdata As Tick) Handles tickerPredefined.OnTick
    My.Computer.FileSystem.WriteAllText("C:\Users\nandy\Desktop\OGGN-New\TickData1.txt", Now.ToString("hh:mm:ss.fff") & " " & tickdata.InstrumentToken & " " & tickdata.Open & " " & tickdata.High & " " & tickdata.Low & " " & tickdata.LastPrice & " " & tickdata.Volume & vbCrLf, True)
    End Sub

    Private Sub Onconnect() Handles tickerPredefined.OnConnect
    ConnectionStatus = tickerPredefined.IsConnected
    If ConnectionStatus = True Then
    MsgBox("Connection established")
    Else
    MsgBox("No connection established")
    End If
    End Sub

    NOTE : For above code I get the message "Connection established" twice and also the ticks are written twice in the file which I shared in my last post.
  • tonystark
    tonystark edited November 2017
    Hi @DA1077,

    You don't have to do both AddHandler and Handles. Only one is required. Since you are doing both here it adds two event handlers to the tick event.

    Either comment out these lines
    AddHandler tickerPredefined.OnTick, AddressOf Ontick
    AddHandler tickerPredefined.OnConnect, AddressOf Onconnect
    or just define ticker WithEvents and use Handle ticker.OnTick and Handles ticker.OnConnect.

    WithEvents with Handles keyword is equivalent to AddHandler.
  • DA1077
    Hi ajinasokan,

    Thank You for the clarification. Now its working perfectly fine.

    I have one more query. I have switched the Tick mode to FULL and want to get the Bids and Offers. I am using below code to access that but I am getting error. The code and error are as below. Please let me know the code I should use to access the Bids and Offers from Tick in FULL Mode.

    Code Used:
    ===========
    My.Computer.FileSystem.WriteAllText("C:\Users\nandy\Desktop\OGGN-New\TickData2.txt", Now.ToString("hh:mm:ss.fff") & " " & tickdata.InstrumentToken & " " & tickdata.Open & " " & tickdata.High & " " & tickdata.Low & " " & tickdata.LastPrice & " " & tickdata.Volume & " " & CStr(tickdata.Bids(0).Orders) & " " & CStr(tickdata.Offers(0).Orders) & " " & vbCrLf, True)


    Error Received:
    ==============
    System.NullReferenceException: 'Object reference not set to an instance of an object.'

    Thanks,
    Amitabha
  • sujith
    sujith edited November 2017
    Hi Amitabha,
    Please open a new thread for new queries, it will help others who are facing the same issue.
    If we answer here, it gets buried and becomes difficult to search.
This discussion has been closed.