Path: blob/master/src/ticker/ZerodhaTicker.py
301 views
import logging1import json23from kiteconnect import KiteTicker45from ticker.BaseTicker import BaseTicker6from instruments.Instruments import Instruments7from models.TickData import TickData89class ZerodhaTicker(BaseTicker):10def __init__(self):11super().__init__("zerodha")1213def startTicker(self):14brokerAppDetails = self.brokerLogin.getBrokerAppDetails()15accessToken = self.brokerLogin.getAccessToken()16if accessToken == None:17logging.error('ZerodhaTicker startTicker: Cannot start ticker as accessToken is empty')18return1920ticker = KiteTicker(brokerAppDetails.appKey, accessToken)21ticker.on_connect = self.on_connect22ticker.on_close = self.on_close23ticker.on_error = self.on_error24ticker.on_reconnect = self.on_reconnect25ticker.on_noreconnect = self.on_noreconnect26ticker.on_ticks = self.on_ticks27ticker.on_order_update = self.on_order_update2829logging.info('ZerodhaTicker: Going to connect..')30self.ticker = ticker31self.ticker.connect(threaded=True)3233def stopTicker(self):34logging.info('ZerodhaTicker: stopping..')35self.ticker.close(1000, "Manual close")3637def registerSymbols(self, symbols):38tokens = []39for symbol in symbols:40isd = Instruments.getInstrumentDataBySymbol(symbol)41token = isd['instrument_token']42logging.info('ZerodhaTicker registerSymbol: %s token = %s', symbol, token)43tokens.append(token)4445logging.info('ZerodhaTicker Subscribing tokens %s', tokens)46self.ticker.subscribe(tokens)4748def unregisterSymbols(self, symbols):49tokens = []50for symbol in symbols:51isd = Instruments.getInstrumentDataBySymbol(symbol)52token = isd['instrument_token']53logging.info('ZerodhaTicker unregisterSymbols: %s token = %s', symbol, token)54tokens.append(token)5556logging.info('ZerodhaTicker Unsubscribing tokens %s', tokens)57self.ticker.unsubscribe(tokens)5859def on_ticks(self, ws, brokerTicks):60# convert broker specific Ticks to our system specific Ticks (models.TickData) and pass to super class function61ticks = []62for bTick in brokerTicks:63isd = Instruments.getInstrumentDataByToken(bTick['instrument_token'])64tradingSymbol = isd['tradingsymbol']65tick = TickData(tradingSymbol)66tick.lastTradedPrice = bTick['last_price']67tick.lastTradedQuantity = bTick['last_quantity']68tick.avgTradedPrice = bTick['average_price']69tick.volume = bTick['volume']70tick.totalBuyQuantity = bTick['buy_quantity']71tick.totalSellQuantity = bTick['sell_quantity']72tick.open = bTick['ohlc']['open']73tick.high = bTick['ohlc']['high']74tick.low = bTick['ohlc']['low']75tick.close = bTick['ohlc']['close']76tick.change = bTick['change']77ticks.append(tick)7879self.onNewTicks(ticks)8081def on_connect(self, ws, response):82self.onConnect()8384def on_close(self, ws, code, reason):85self.onDisconnect(code, reason)8687def on_error(self, ws, code, reason):88self.onError(code, reason)8990def on_reconnect(self, ws, attemptsCount):91self.onReconnect(attemptsCount)9293def on_noreconnect(self, ws):94self.onMaxReconnectsAttempt()9596def on_order_update(self, ws, data):97self.onOrderUpdate(data)9899100