How to map the token number with their symbol name?
-
How to map the token number with their symbol name?
I have subscribe RELIANCE_EQ's token number.token = "nse_cm|2885" # Token number of RELIANCE_EQ task = "mw" # "mw"|"sfi"|"dp"
And stream the WebSocket. It's perfectly works.
[{'ak': 'ok', 'msg': 'heartbeat', 'task': 'hb'}] [] [{'e': 'nse_cm', 'ltp': '2038.50', 'ltq': '20', 'ltt': 'NA', 'name': 'sf', 'tk': '2885'}] [{'name': 'tm', 'tvalue': '31/03/2021 09:52:10'}] [{'ap': '2031.81', 'bp': '2038.05', 'bq': '44', 'bs': '121', 'c': '2029.30', 'cng': '09.20', 'e': 'nse_cm', 'lo': '2011.60', 'ltp': '2038.50', 'ltq': '10', 'ltt': '31/03/2021 09:52:10', 'name': 'sf', 'nc': '00.45', 'sp': '2038.50', 'tbq': '319613', 'tk': '2885', 'to': '2680052885.07', 'tsq': '321701', 'v': '1319047'}] [{'e': 'nse_cm', 'ltp': '2038.50', 'ltq': '25', 'ltt': 'NA', 'name': 'sf', 'tk': '2885'}] [{'name': 'tm', 'tvalue': '31/03/2021 09:52:11'}] [{'ap': '2031.81', 'bp': '2038.00', 'bq': '291', 'bs': '23', 'c': '2029.30', 'cng': '09.20', 'e': 'nse_cm', 'lo': '2011.60', 'ltp': '2038.50', 'ltq': '2', 'ltt': '31/03/2021 09:52:11', 'name': 'sf', 'nc': '00.45', 'sp': '2038.50', 'tbq': '320028', 'tk': '2885', 'to': '2680540519.47', 'tsq': '321777', 'v': '1319287'}] [{'e': 'nse_cm', 'ltp': '2038.50', 'ltq': '2', 'ltt': 'NA', 'name': 'sf', 'tk': '2885'}] [{'name': 'tm', 'tvalue': '31/03/2021 09:52:12'}] [{'ap': '2031.81', 'bp': '2038.05', 'bq': '161', 'bs': '12', 'c': '2029.30', 'cng': '09.20', 'e': 'nse_cm', 'lo': '2011.60', 'ltp': '2038.50', 'ltq': '6', 'ltt': '31/03/2021 09:52:12', 'name': 'sf', 'nc': '00.45', 'sp': '2038.50', 'tbq': '320288', 'tk': '2885', 'to': '2680570996.62', 'tsq': '321742', 'v': '1319302'}]
But the problem is it's showing messy especially when we subscribe multiple tokens. So how can we map the token number with their symbol name and display that like this__
RELIANCE_EQ:- tk: 2885, ltp:2038.50, to: 2680570996.62
Can anyone please give any idea to do that.......
-
@nayan_nandi You need to handle exceptions. Key 'tk' won't be in every ticks. You have to add additional if condition after for loop.
if single_company.get('tk'): #do something
-
@rajanprabu Can anyone please tell me what is wrong with this code__
token = "nse_cm|2885&nse_cm|3465&nse_cm|1660" task = "mw" # "mw"|"sfi"|"dp" ss = WebSocket(FEED_TOKEN, CLIENT_CODE) trading_portfolio = {"nse_cm|2885": "RELIANCE", "nse_cm|3465": "TATAMOTORS", "nse_cm|1660": "ITC"} def on_tick(ws, tick): for single_company in tick: inst_of_single_company = single_company['tk'] name = trading_portfolio[inst_of_single_company] print(single_company, name)
I'm getting error like this____
Unhandled Error Traceback (most recent call last): File "C:\Python 3.9.2\lib\site-packages\twisted\python\log.py", line 101, in callWithLogger return callWithContext({"system": lp}, func, *args, **kw) File "C:\Python 3.9.2\lib\site-packages\twisted\python\log.py", line 85, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "C:\Python 3.9.2\lib\site-packages\twisted\python\context.py", line 118, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "C:\Python 3.9.2\lib\site-packages\twisted\python\context.py", line 83, in callWithContext return func(*args, **kw) --- <exception caught here> --- File "C:\Python 3.9.2\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite why = getattr(selectable, method)() File "C:\Python 3.9.2\lib\site-packages\twisted\internet\tcp.py", line 246, in doRead return self._dataReceived(data) File "C:\Python 3.9.2\lib\site-packages\twisted\internet\tcp.py", line 251, in _dataReceived rval = self.protocol.dataReceived(data) File "C:\Python 3.9.2\lib\site-packages\twisted\protocols\tls.py", line 324, in dataReceived self._flushReceiveBIO() File "C:\Python 3.9.2\lib\site-packages\twisted\protocols\tls.py", line 290, in _flushReceiveBIO ProtocolWrapper.dataReceived(self, bytes) File "C:\Python 3.9.2\lib\site-packages\twisted\protocols\policies.py", line 107, in dataReceived self.wrappedProtocol.dataReceived(data) File "C:\Python 3.9.2\lib\site-packages\autobahn\twisted\websocket.py", line 290, in dataReceived self._dataReceived(data) File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 1207, in _dataReceived self.consumeData() File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 1219, in consumeData while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED: File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 1579, in processData fr = self.onFrameEnd() File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 1704, in onFrameEnd self._onMessageEnd() File "C:\Python 3.9.2\lib\site-packages\autobahn\twisted\websocket.py", line 318, in _onMessageEnd self.onMessageEnd() File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 628, in onMessageEnd self._onMessage(payload, self.message_is_binary) File "C:\Python 3.9.2\lib\site-packages\autobahn\twisted\websocket.py", line 321, in _onMessage self.onMessage(payload, isBinary) File "C:\Python 3.9.2\lib\site-packages\smartapi\webSocket.py", line 41, in onMessage self.factory.on_message(self, payload, is_binary) File "C:\Python 3.9.2\lib\site-packages\smartapi\webSocket.py", line 337, in _on_message self._parse_text_message(payload) File "C:\Python 3.9.2\lib\site-packages\smartapi\webSocket.py", line 377, in _parse_text_message self.on_ticks(self, data) File "C:\Users\Nayan\PycharmProjects\Angel_Broking\main.py", line 31, in on_tick inst_of_single_company = single_company['tk'] builtins.KeyError: 'tk' Unhandled Error Traceback (most recent call last): File "C:\Python 3.9.2\lib\site-packages\smartapi\webSocket.py", line 233, in connect reactor.run(**opts) File "C:\Python 3.9.2\lib\site-packages\twisted\internet\base.py", line 1423, in run self.mainLoop() File "C:\Python 3.9.2\lib\site-packages\twisted\internet\base.py", line 1436, in mainLoop reactorBaseSelf.doIteration(t) File "C:\Python 3.9.2\lib\site-packages\twisted\internet\selectreactor.py", line 143, in doSelect _logrun(selectable, _drdw, selectable, method) --- <exception caught here> --- File "C:\Python 3.9.2\lib\site-packages\twisted\python\log.py", line 101, in callWithLogger return callWithContext({"system": lp}, func, *args, **kw) File "C:\Python 3.9.2\lib\site-packages\twisted\python\log.py", line 85, in callWithContext return context.call({ILogContext: newCtx}, func, *args, **kw) File "C:\Python 3.9.2\lib\site-packages\twisted\python\context.py", line 118, in callWithContext return self.currentContext().callWithContext(ctx, func, *args, **kw) File "C:\Python 3.9.2\lib\site-packages\twisted\python\context.py", line 83, in callWithContext return func(*args, **kw) File "C:\Python 3.9.2\lib\site-packages\twisted\internet\selectreactor.py", line 154, in _doReadOrWrite self._disconnectSelectable(selectable, why, method == "doRead") File "C:\Python 3.9.2\lib\site-packages\twisted\internet\posixbase.py", line 306, in _disconnectSelectable selectable.connectionLost(failure.Failure(why)) File "C:\Python 3.9.2\lib\site-packages\twisted\internet\tcp.py", line 506, in connectionLost self._commonConnection.connectionLost(self, reason) File "C:\Python 3.9.2\lib\site-packages\twisted\internet\tcp.py", line 324, in connectionLost protocol.connectionLost(reason) File "C:\Python 3.9.2\lib\site-packages\twisted\protocols\tls.py", line 389, in connectionLost self._flushReceiveBIO() File "C:\Python 3.9.2\lib\site-packages\twisted\protocols\tls.py", line 290, in _flushReceiveBIO ProtocolWrapper.dataReceived(self, bytes) File "C:\Python 3.9.2\lib\site-packages\twisted\protocols\policies.py", line 107, in dataReceived self.wrappedProtocol.dataReceived(data) File "C:\Python 3.9.2\lib\site-packages\autobahn\twisted\websocket.py", line 290, in dataReceived self._dataReceived(data) File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 1207, in _dataReceived self.consumeData() File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 1219, in consumeData while self.processData() and self.state != WebSocketProtocol.STATE_CLOSED: File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 1571, in processData fr = self.onFrameData(payload) File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 1666, in onFrameData self._onMessageFrameData(payload) File "C:\Python 3.9.2\lib\site-packages\autobahn\twisted\websocket.py", line 309, in _onMessageFrameData self.onMessageFrameData(payload) File "C:\Python 3.9.2\lib\site-packages\autobahn\websocket\protocol.py", line 602, in onMessageFrameData self.frame_data.append(payload) builtins.AttributeError: 'NoneType' object has no attribute 'append'
-
You have to search for keys in the dictionary.. next tick comes with ltt NA because it was only volume/quote change and not transaction. Thats my understanding.
-
@nayan_nandi I see some patterns in both the responses. For the first response, you can see 'ltt' has the time value but in the second response, 'ltt' is NA. So I believe you can use that key to differentiate between the two. You have to check if this is true for all the responses.
-
@rjbanna Yes Sir, but here has two type of response.
[{'ap': '2031.81', 'bp': '2038.05', 'bq': '44', 'bs': '121', 'c': '2029.30', 'cng': '09.20', 'e': 'nse_cm', 'lo': '2011.60', 'ltp': '2038.50', 'ltq': '10', 'ltt': '31/03/2021 09:52:10', 'name': 'sf', 'nc': '00.45', 'sp': '2038.50', 'tbq': '319613', 'tk': '2885', 'to': '2680052885.07', 'tsq': '321701', 'v': '1319047'}] [{'e': 'nse_cm', 'ltp': '2038.50', 'ltq': '25', 'ltt': 'NA', 'name': 'sf', 'tk': '2885'}]
So how can I select first one programmatically, not second one?
-
@nayan_nandi You get 'tk' in tick response. Here you have subscribed to reliance and token for that is 2885. Now if you take a look at response, you would find key named 'tk' which is token for that scrip. You can compare the 'tk' to determine the corresponding scrip.