Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: )

jhaanuj
jhaanuj edited December 2020 in General
Full error message: Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: close reason without close code)

I was trying to do something inside on_ticks() method, which was working great in Google Colab notebook, but it didn't work in my local Jupyter Notebook. Then I tried to remove everything and just keep the basic functions, still showing the same error.


from kiteconnect import KiteTicker, KiteConnect
import pandas as pd
from datetime import datetime, timedelta
import time
import csv

# Initialise
api_key = open('api_key.txt','r').read()
access_token = open('access_token.txt','r').read()
kws = KiteTicker(api_key, access_token)

#Getting the tokens for NIFTY based on date, and strike price
df = pd.read_csv('instrumenttoken.csv')

expiry = input("Enter the expiry in YYYY-MM-DD format: ")
print(expiry)
strike_pe = int(input("Enter the strike for PUT option: "))
print(df.loc[(df['expiry'] == expiry) & (df['instrument_type'] == 'PE') & (df['strike'] == strike_pe)])
instrument_token_pe = df.loc[(df['expiry'] == expiry) & (df['instrument_type'] == 'PE') & (df['strike'] == strike_pe)]['instrument_token'].values[0]
trading_symbol_pe = df.loc[(df['expiry'] == expiry) & (df['instrument_type'] == 'PE') & (df['strike'] == strike_pe)]['tradingsymbol'].values[0]


strike_ce = int(input("Enter the strike for CALL option: "))
print(df.loc[(df['expiry'] == expiry) & (df['instrument_type'] == 'CE') & (df['strike'] == strike_ce)])
instrument_token_ce = df.loc[(df['expiry'] == expiry) & (df['instrument_type'] == 'CE') & (df['strike'] == strike_ce)]['instrument_token'].values[0]
trading_symbol_ce = df.loc[(df['expiry'] == expiry) & (df['instrument_type'] == 'CE') & (df['strike'] == strike_ce)]['tradingsymbol'].values[0]

tokens_dict = {instrument_token_pe:trading_symbol_pe, instrument_token_ce:trading_symbol_ce }
print(tokens_dict)
token = [instrument_token_pe, instrument_token_ce]
print(token)
symbol = [trading_symbol_pe, trading_symbol_ce]


#Receiving the ticks data and collecting the useful values like the name, timestamp and price
def on_ticks(ws, ticks):
# Callback to receive ticks.
print("Ticks: {}".format(ticks))


def on_connect(ws, response):
# Callback on successful connect.
ws.subscribe(token)
ws.set_mode(ws.MODE_FULL, token)



# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect



kws.connect()



EDIT 1: Something strange is happening, the token variable inside on_connect() method is creating a delay somehow, on entering the values manually it is giving me the values. I have tried to use global token but it didn't work. But there has to be another way of providing the values from saved variables.
Sign In or Register to comment.