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
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)
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)
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