How to use partially parameterized functions or class methods as ticker callbacks

abtExp
abtExp edited June 28 in Python client
I'm building a basic application and need some instance level control on the ticker, thus I'm trying to use class methods or even partially called functions to set callbacks for the ticker, but they are not working.

Using class methods, my code looks like this :

import pandas as pd
from kiteconnect import KiteTicker

class TICKER:
def __init__(self, instruments, api_key, access_token):
self.instrument_ticker = KiteTicker(api_key, access_token)
self.instruments = [int(v) for v in instruments.values()]
self.set_instrument_ticker_callbacks()
self.instrument_ticker.connect()

def connect_indices(self):
def on_connect(ws, response):
ws.subsribe(self.instruments)
ws.set_mode(ws.MODE_FULL, self.instruments)
return on_connect

def print_instrument_ticks(self, ticks):
curr_prices = [i['last_price'] for i in ticks]
self.subscribe_levels(curr_prices)
print(ticks)

def subsribe_levels(self, curr_prices):
return

def instrument_ticks(self):
def on_ticks(ws, ticks):
self.print_instrument_ticks(ticks)
return on_ticks

def instrument_ticker_close(self):
def on_close(ws, code, reason):
self.print_instrument_ticks(reason)
ws.close()
return on_close


def set_instrument_ticker_callbacks(self):
self.instrument_ticker.on_connect = self.connect_indices()
self.instrument_ticker.on_ticks = self.instrument_ticks()
self.instrument_ticker.on_close = self.instrument_ticker_close()

And using partial functions, it looks like this :
import pandas as pd


def on_instrument_connect(instruments):
def on_connect(ws, response):
ws.subsribe(instruments)
ws.set_mode(ws.MODE_FULL, instruments)

return on_connect


def on_instrument_ticks():
def on_ticks(ws, ticks):
print(ticks)
# Do Other Shit
return on_ticks


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

return on_close

But using both these methods, i get :
Connection error: 1006 - connection was closed uncleanly (I dropped the WebSocket TCP connection: 'KiteTicker' object has no attribute 'subsribe')

But if i use simple methods, it works fine.
What am i doing wrong?
  • tonystark
    As mentioned in the error message you have a typo in the function name. It is subscribe not subsribe.
  • abtExp
    :s WTF, HOW CAN I MISS THAT. Thanks @tonystark . I'm Dumb. Have A Nice Day. I'm Off.
This discussion has been closed.