Websocket not executing after While True:

samarthpathak4
samarthpathak4 edited December 2023 in Python client
Hello, I am trying to stream all banknifty current week options, the code was working fine for 1 day, after that it started giving error message of connection closed without any reason
  • samarthpathak4
    samarthpathak4 edited December 2023
    import logging
    from kiteconnect import KiteConnect
    from kiteconnect import KiteTicker
    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    import json
    import os
    from datetime import date
    import pandas as pd

    logging.basicConfig(level=logging.DEBUG)

    # Initialise
    api_key = "vfwf"
    api_secret = "348c"

    kite = KiteConnect(api_key=api_key)

    symbols = [738561, 5633]

    symbol_data = {instrument_token: [] for instrument_token in symbols}
    print(symbol_data)

    access_token = "CCY4SthXqQPXf2TZHK2YhhyPtTaHEmpi"

    kws = KiteTicker(api_key, access_token)

    # def on_ticks(ws, ticks): # noqa
    # # Callback to receive ticks.
    # #logging.info("Ticks: {}".format(ticks))
    # print("Main tick")

    def on_connect(ws, response): # noqa
    # Callback on successful connect.
    # Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
    ws.subscribe(symbols)

    # Set RELIANCE to tick in `full` mode.
    ws.set_mode(ws.MODE_FULL, symbols)

    def on_close(ws, code, reason):
    ws.stop()

    # Assign the callbacks.
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.connect()

    while True:
    def on_ticks(ws, ticks):
    getTicks(ticks)
    print('print')

    def getTicks(ticks):
    for x in ticks:
    data = x

    instrument_token = data.get('instrument_token')
    last_price = data.get('last_price')
    #last_traded_quantity = data.get('last_traded_quantity')
    #average_traded_price = data.get('average_traded_price')
    volume_traded = data.get('volume_traded')
    #total_buy_quantity = data.get('total_buy_quantity')
    #total_sell_quantity = data.get('total_sell_quantity')
    ohlc = data.get('ohlc')
    #change = data.get('change')
    oi = data.get('oi')
    #oi_day_high = data.get('oi_day_high')
    #oi_day_low = data.get('oi_day_low')
    last_trade_time = data.get('last_trade_time').strftime('%m/%d/%Y %H:%M:%S %p')
    exchange_timestamp = data.get('exchange_timestamp').strftime('%m/%d/%Y %H:%M:%S %p')

    response_data = {'instrument_token': instrument_token,
    'last_price':last_price,
    #'last_traded_quantity':last_traded_quantity,
    #'average_traded_price':average_traded_price,
    'volume_traded':volume_traded,
    #'total_buy_quantity':total_buy_quantity,
    #'total_sell_quantity':total_sell_quantity,
    'ohlc':ohlc,
    #'change':change,
    'oi':oi,
    #'oi_day_high':oi_day_high,
    #'oi_day_low':oi_day_low,
    'last_trade_time':last_trade_time,
    'exchange_timestamp':exchange_timestamp
    }
    #print(response_data)
    if instrument_token in symbol_data:
    # Append the value to the list associated with the symbol
    symbol_data[instrument_token].append(response_data)

    # Save the data to a JSON file
    save_to_json(instrument_token, symbol_data[instrument_token])
    #print('instr', symbol_data[instrument_token])
    print(f"Appended values to {instrument_token}: {symbol_data[instrument_token]}")
    else:
    print(f"Ignoring data for unknown symbol: {instrument_token}")

    def save_to_json(symbol, data_list):
    # Create a filename based on the current date and symbol

    file = symbol
    filename = f"{file}_{date.today()}.json"

    # Save the data to a JSON file
    with open(filename, 'w') as outfile:
    json.dump(data_list, outfile)
    kws.on_ticks=on_ticks
Sign In or Register to comment.