Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sreenivasdoosa
GitHub Repository: sreenivasdoosa/sdoosa-algo-trade-python
Path: blob/master/src/ticker/ZerodhaTicker.py
301 views
1
import logging
2
import json
3
4
from kiteconnect import KiteTicker
5
6
from ticker.BaseTicker import BaseTicker
7
from instruments.Instruments import Instruments
8
from models.TickData import TickData
9
10
class ZerodhaTicker(BaseTicker):
11
def __init__(self):
12
super().__init__("zerodha")
13
14
def startTicker(self):
15
brokerAppDetails = self.brokerLogin.getBrokerAppDetails()
16
accessToken = self.brokerLogin.getAccessToken()
17
if accessToken == None:
18
logging.error('ZerodhaTicker startTicker: Cannot start ticker as accessToken is empty')
19
return
20
21
ticker = KiteTicker(brokerAppDetails.appKey, accessToken)
22
ticker.on_connect = self.on_connect
23
ticker.on_close = self.on_close
24
ticker.on_error = self.on_error
25
ticker.on_reconnect = self.on_reconnect
26
ticker.on_noreconnect = self.on_noreconnect
27
ticker.on_ticks = self.on_ticks
28
ticker.on_order_update = self.on_order_update
29
30
logging.info('ZerodhaTicker: Going to connect..')
31
self.ticker = ticker
32
self.ticker.connect(threaded=True)
33
34
def stopTicker(self):
35
logging.info('ZerodhaTicker: stopping..')
36
self.ticker.close(1000, "Manual close")
37
38
def registerSymbols(self, symbols):
39
tokens = []
40
for symbol in symbols:
41
isd = Instruments.getInstrumentDataBySymbol(symbol)
42
token = isd['instrument_token']
43
logging.info('ZerodhaTicker registerSymbol: %s token = %s', symbol, token)
44
tokens.append(token)
45
46
logging.info('ZerodhaTicker Subscribing tokens %s', tokens)
47
self.ticker.subscribe(tokens)
48
49
def unregisterSymbols(self, symbols):
50
tokens = []
51
for symbol in symbols:
52
isd = Instruments.getInstrumentDataBySymbol(symbol)
53
token = isd['instrument_token']
54
logging.info('ZerodhaTicker unregisterSymbols: %s token = %s', symbol, token)
55
tokens.append(token)
56
57
logging.info('ZerodhaTicker Unsubscribing tokens %s', tokens)
58
self.ticker.unsubscribe(tokens)
59
60
def on_ticks(self, ws, brokerTicks):
61
# convert broker specific Ticks to our system specific Ticks (models.TickData) and pass to super class function
62
ticks = []
63
for bTick in brokerTicks:
64
isd = Instruments.getInstrumentDataByToken(bTick['instrument_token'])
65
tradingSymbol = isd['tradingsymbol']
66
tick = TickData(tradingSymbol)
67
tick.lastTradedPrice = bTick['last_price']
68
tick.lastTradedQuantity = bTick['last_quantity']
69
tick.avgTradedPrice = bTick['average_price']
70
tick.volume = bTick['volume']
71
tick.totalBuyQuantity = bTick['buy_quantity']
72
tick.totalSellQuantity = bTick['sell_quantity']
73
tick.open = bTick['ohlc']['open']
74
tick.high = bTick['ohlc']['high']
75
tick.low = bTick['ohlc']['low']
76
tick.close = bTick['ohlc']['close']
77
tick.change = bTick['change']
78
ticks.append(tick)
79
80
self.onNewTicks(ticks)
81
82
def on_connect(self, ws, response):
83
self.onConnect()
84
85
def on_close(self, ws, code, reason):
86
self.onDisconnect(code, reason)
87
88
def on_error(self, ws, code, reason):
89
self.onError(code, reason)
90
91
def on_reconnect(self, ws, attemptsCount):
92
self.onReconnect(attemptsCount)
93
94
def on_noreconnect(self, ws):
95
self.onMaxReconnectsAttempt()
96
97
def on_order_update(self, ws, data):
98
self.onOrderUpdate(data)
99
100