Error in websocket call

manisankar
hi

am creating a strategy and validating the same in every tick. But i need a condition like whether its coming to tick for the first time in the market so i declared a variable in the top and checking that variable in the tick but am getting the below error:

ERROR:websocket:error from callback >: local variable 'initial' referenced before assignment

Inside Tickf


SAMPLE CODE(python):

from kiteconnect import WebSocket

from kiteconnect import KiteConnect


kws = WebSocket("xxxx", "xxxx", "USERNAME")

kite = KiteConnect(api_key="xxx")

print "acctoken",acctoken

kite.set_access_token(acctoken)

initial=True


print "initial",initial

def place_order(symbol,qty,otype,price):
# Place an order
try:
if(otype!="Nan"):
# order_id = kite.order_place(tradingsymbol=symbol,
# exchange="NSE",
# transaction_type=otype,
# quantity=qty,
# order_type="LIMIT",
# price=price,
# product="MIS")

# print("Order placed. ID is", order_id)
# return order_id
print "symbol",symbol,"qty",qty,"otype",otype,"price",price
else:
return "null"
except Exception as e:
print("Order placement failed", e.message)
return e.message
# Fetch all orders
print(kite.orders())




# Callback for tick reception.
def on_tick(tick, ws):
print "Inside Tickf"
if(initial):
result=validatestrategy(tick)
initial=False
else:
execStrategy(result)


# Callback for successful connection.
def on_connect(ws):
readInstrumentList(ws)

#print "stock csv read"
# Subscribe to a list of instrument_tokens (RELIANCE and ACC here).
#print "on_connect"
#ws.subscribe([738561])

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


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

# print "before connect"
# Infinite loop on the main thread. Nothing after this will run.
# You have to use the pre-defined callbacks to manage subscriptions.
kws.connect()



Any one can fix this
  • waashi
    this is a python issue not specific to kite, use 'global initial' before using the variable, read about 'global' in python
  • kaushikvelingkar
    Yes its is Global variable issue. You are defining the Initial outside function. To correct it try saying " Global Initial within the function. This way the function understands that it has to access global variable
Sign In or Register to comment.