import logging import webbrowser #import csv import pandas as pd import KiteAlgorithmClass as alg from kiteconnect import KiteConnect, KiteTicker def on_ticks(ws, ticks): # Callback to receive ticks. print("Inside on_ticks") logging.debug("Ticks: {}".format(ticks)) for tick in ticks: for stock in stocks: if stock.instrument==tick["instrument_token"]: stock.update(tick) ##if stock.recommend!=0: ## if recommend =1 buy, 2 close sell and open buy position, -2 close buy and open sell position def on_connect(ws, response): # Callback on successful connect. # Subscribe to a list of instrument_tokens (RELIANCE and ACC here). ws.subscribe(instruments) # Set stocks to tick in `quote` mode. ws.set_mode(ws.MODE_QUOTE, instruments) print("On connect complete") def on_close(ws, code, reason): # On connection close stop the main loop # Reconnection will not happen after executing `ws.stop()` #square off all positions ws.stop() logging.basicConfig(level=logging.DEBUG) MyApiKey="my api key" MyApiSecret="my api secret" kite = KiteConnect(api_key=MyApiKey) # Redirect the user to the login url obtained # from kite.login_url(), and receive the request_token # from the registered redirect url after the login flow. # Once you have the request_token, obtain the access_token # as follows. print(kite.login_url()) webbrowser.open(kite.login_url()) MyToken = input("Enter your Token after logging in: ") data = kite.generate_session(MyToken, api_secret=MyApiSecret) kite.set_access_token(data["access_token"]) # Fetch all orders #kite.orders() # Get instruments #kite.instruments() stocks=[] instruments=[738561] #NSEdict=pd.read_csv('NSE_EQdict.csv', header=None, index_col=0, squeeze=True).to_dict() for instrument in instruments: stocks.append(alg.Stock(kite,instrument,'RELIANCE')) print("Before KiteTicker") kws = KiteTicker(MyApiKey, data["access_token"]) print("After KiteTicker") #stocks=[] #stocks.append(alg.Stock(738561,0)) #stocks.append(alg.stock(5633,0)) # Assign the callbacks. kws.on_ticks = on_ticks kws.on_connect = on_connect kws.on_close = on_close # Infinite loop on the main thread. Nothing after this will run. # You have to use the pre-defined callbacks to manage subscriptions. print("Before kws.connect") kws.connect() print("After kws,connect")