Issue: Data from Websocket to Python dataframes

bobbydreamsforu
Hi all,
I’m new bee for python. For KITE API (Pyhon), is it possible to transfer websocket data directly to pandas dataframes or panels. I’d like to access live data using dataframes / panels to execute my code. Can someone focus your light and help me in this issue.
How far panels useful in accessing data from websocket. Pls help me with your knowledge.

With regards,
Satish
8885227220
[email protected]
  • tonystark
    tonystark edited November 2017
    Hi @bobbydreamsforu,

    You can use pandas data frames to store tick data for further processing. Here is a basic example to convert ticks to panda DataFrame:
    from kiteconnect import WebSocket
    import datetime
    import pandas as pd

    #columns in data frame
    df_cols = ["Token", "LTP", "Volume"]

    data_frame = pd.DataFrame(data=[],columns=df_cols, index=[])

    def on_tick(ticks, ws):
    global data_frame, df_cols

    data = dict() #keeps extracted tick data

    for tick in ticks:
    token = tick["instrument_token"]
    ltp = tick["last_price"]
    volume = tick["volume"]
    timestamp = str(datetime.datetime.now().time())

    data[timestamp] = [token, ltp, volume]

    tick_df = pd.DataFrame(data.values(), columns=df_cols, index=data.keys()) #convert data to a DataFrame
    data_frame = data_frame.append(tick_df) # append to existing

    print "DataFrame: \n", data_frame


    def on_connect(ws):
    print "Connected"
    ws.subscribe([53480455]) #subscribe for ticks

    #initialize
    kws = WebSocket("api_key", "public_token", "user_id")
    kws.on_tick = on_tick
    kws.on_connect = on_connect
    kws.connect()
  • batbark
    @tonystark 1) what if there are multiple scrips? what should I change in the above code?
    2) how to use this data stored in dataframes to create ohlc 15min candles
  • sujith
    @batbark,
    1) what if there are multiple scrips? what should I change in the above code?
    You just need to pass the tokens like this
    ws.subscribe([53480455, 256265])
    The above works for multiple instruments also.
    2) how to use this data stored in dataframes to create ohlc 15min candles
    This is outside the scope of Kite Connect. I am afraid we can't help you with that.
This discussion has been closed.