Python Streaming Tick Data Problem.
-
Hi,
following is the code i'm using to stream live stock data in python pycharm, anyone could please let me know weather the code is write or do i have to change something at below code.
@bhaveshjain since you have told in one of the threadpost that python error is solved but i'm getting same output as shown below,from smartapi.smartSocket import SmartSocket from smartapi import SmartConnect api_key = "my API key" client_code = "my client code" password = "my password" obj = SmartConnect(api_key=api_key) data = obj.generateSession(client_code, password) tokens = obj.generateToken(data["data"]["refreshToken"]) feed_token = tokens["data"]["feedToken"] token = "nse_cm|2885" ss = SmartSocket(feed_token, client_code) def on_tick(ws, tick): print("Ticks: {}".format(tick)) def on_connect(ws, response): ws.send_request(token) # Assign the callbacks. ss.on_ticks = on_tick ss.on_connect = on_connect ss.connect()
Following is the Output i'm receiving.
PAYLOAD eJyLrlZKzFayUsrPVtJRKkksBrGT84Ds3OJ0EDM/Ly81uSQ1Rak2FgAaGQ1z DATA b'x\x9c\x8b\xaeVJ\xccV\xb2R\xca\xcfV\xd2Q*I,\x06\xb1\x93\xf3\x80\xec\xdc\xe2t\x103?//5\xb9$5E\xa96\x16\x00\x1a\x19\rs' PAYLOAD eJyLrlZKzFayUsrPVtJRKkksBrGT84Ds3OJ0EDM/Ly81uSQ1Rak2FgAaGQ1z DATA b'x\x9c\x8b\xaeVJ\xccV\xb2R\xca\xcfV\xd2Q*I,\x06\xb1\x93\xf3\x80\xec\xdc\xe2t\x103?//5\xb9$5E\xa96\x16\x00\x1a\x19\rs'
So i request any fellow traders also @bhaveshjain if you can provide me a snippet of python code for streaming tick data.
-
@dorje Can i get this code please, I have been trying alot, but only getting above three lines and not feed..
-
@rjbanna Ohh.. that was the reason, no wonder I was not able to save data in list or anything.
-
@admin hi admin now it working fine.
-
@admin I'm still facing the websocket disconnection issue after 10 minutes for Python SDK
-
Hi @admin ,
Tried WebSocket connection but got disconnected to socket after 5 minutes.
Below is the error which I got in my NodeJS SDK.close:: CloseEvent {target: WebSocket, type: 'close', wasClean: true, reason: '', code: 1000} arg1: CloseEvent {target: WebSocket, type: 'close', wasClean: true, reason: '', code: 1000} code: 1000 reason: '' target: WebSocket {_events: {…}, _eventsCount: 4, _maxListeners: undefined, _binaryType: 'nodebuffer', _closeCode: 1000} _binaryType: 'nodebuffer' _bufferedAmount: 0 _closeCode: 1000 _closeFrameReceived: true _closeFrameSent: true _closeMessage: '' _closeTimer: Timeout {_called: false, _idleTimeout: -1, _idlePrev: null, _idleNext: null, _idleStart: 317581} _events: {open: ƒ, message: ƒ, error: ƒ, close: ƒ} _eventsCount: 4 _extensions: {permessage-deflate: PerMessageDeflate} _isServer: false _maxListeners: undefined _protocol: '' _readyState: 3 _receiver: Receiver {_writableState: WritableState, writable: false, _events: {…}, _eventsCount: 0, _maxListeners: undefined} _redirects: 0 _req: null _sender: Sender {_extensions: {…}, _socket: TLSSocket, _firstFragment: true, _compress: false, _bufferedBytes: 0} _socket: TLSSocket {_tlsOptions: {…}, _secureEstablished: true, _securePending: false, _newSessionPending: false, _controlReleased: true} _url: 'wss://omnefeeds.angelbroking.com/NestHtml5Mobile/socket/stream' get binaryType: ƒ binaryType() {\n return this._binaryType;\n } set binaryType: ƒ binaryType(type) {\n if (!BINARY_TYPES.includes(type)) return;\n\n this._binaryType = type;\n\n //\n // Allow to change `binaryType` on the fly.\n //\n if (this._receiver) this._receiver._binaryType = type;\n } get bufferedAmount: ƒ bufferedAmount() {\n if (!this._socket) return this._bufferedAmount;\n\n return this._socket._writableState.length + this._sender._bufferedBytes;\n } get extensions: ƒ extensions() {\n return Object.keys(this._extensions).join();\n } get onclose: ƒ get() {\n const listeners = this.listeners(method);\n for (let i = 0; i < listeners.length; i++) {\n if (listeners[i]._listener) return listeners[i]._listener;\n }\n\n return undefined;\n } set onclose: ƒ set(listener) {\n const listeners = this.listeners(method);\n for (let i = 0; i < listeners.length; i++) {\n //\n // Remove only the listeners added via `addEventListener`.\n //\n if (listeners[i]._listener) this.removeListener(method, listeners[i]);\n }\n this.addEventListener(method, listener);\n } get onerror: ƒ get() {\n const listeners = this.listeners(method);\n for (let i = 0; i < listeners.length; i++) {\n if (listeners[i]._listener) return listeners[i]._listener;\n }\n\n return undefined;\n } set onerror: ƒ set(listener) {\n const listeners = this.listeners(method);\n for (let i = 0; i < listeners.length; i++) {\n //\n // Remove only the listeners added via `addEventListener`.\n //\n if (listeners[i]._listener) this.removeListener(method, listeners[i]);\n }\n this.addEventListener(method, listener);\n } get onmessage: ƒ get() {\n const listeners = this.listeners(method);\n for (let i = 0; i < listeners.length; i++) {\n if (listeners[i]._listener) return listeners[i]._listener;\n }\n\n return undefined;\n } set onmessage: ƒ set(listener) {\n const listeners = this.listeners(method);\n for (let i = 0; i < listeners.length; i++) {\n //\n // Remove only the listeners added via `addEventListener`.\n //\n if (listeners[i]._listener) this.removeListener(method, listeners[i]);\n }\n this.addEventListener(method, listener);\n } get onopen: ƒ get() {\n const listeners = this.listeners(method);\n for (let i = 0; i < listeners.length; i++) {\n if (listeners[i]._listener) return listeners[i]._listener;\n }\n\n return undefined;\n } set onopen: ƒ set(listener) {\n const listeners = this.listeners(method);\n for (let i = 0; i < listeners.length; i++) {\n //\n // Remove only the listeners added via `addEventListener`.\n //\n if (listeners[i]._listener) this.removeListener(method, listeners[i]);\n }\n this.addEventListener(method, listener);\n } get protocol: ƒ protocol() {\n return this._protocol;\n } get readyState: ƒ readyState() {\n return this._readyState;\n } get url: ƒ url() {\n return this._url;\n } __proto__: EventEmitter type: 'close' wasClean: true __proto__: Event
-
-
-
Hi @admin ,
Any update regarding this issue?
-
As these APIs are in Beta Release. & A Beta phase generally begins when the software is feature complete but likely to contain a number of known or unknown bugs. So we are likely to identify some known/unknown bugs.
-
@rjbanna Our development team should fix web socket disconnection issue by tomorrow. If it is fixed during the day, I'll share an update here.
-
@rjbanna said in Python Streaming Tick Data Problem.:
data = bytes((zlib.decompress(data)).decode("utf-8"), 'utf-8')
thankyou @rjbanna now it is working :)
-
@dorje I have modified the webSocket.py file code so it is now returning the parsable response and you can get data from it. Below is the code if you are interested and don't want to wait for the development team to fix it. You just need to replace _parse_text_message() function from env/lib/python3/site-packages/smartapi/webSocket.py file with below code. This will return dict.
def _parse_text_message(self, payload): """Parse text message.""" # Decode unicode data if not six.PY2 and type(payload) == bytes: payload = payload.decode("utf-8") data = base64.b64decode(payload) try: data = bytes((zlib.decompress(data)).decode("utf-8"), 'utf-8') data = json.loads(data.decode('utf8').replace("'", '"')) data = json.loads(json.dumps(data, indent=4, sort_keys=True)) except ValueError: return self.on_ticks(self, data)
-
@dorje I agree with you, they are doing kind of testing from our side by claiming free API.
-
@rjbanna ok..that i never thought silly of me thats why i was confused even when i was not using print function it was printing the streaming data. @admin and @bhaveshjain it is really bad that you advertising it as a free API but i think for full operation it will take some time, so at least give us time frame for full launch of the service.
-
@dorje @bhaveshjain I am having the same issue. Actually the values printed on the terminal is not from on_ticks() method. It is simple string that is being printed because in the webSocket.py file, there is print() function which only prints the data but it doesn't send any data to the client so we can't parse it. And also the API closes connection after 10 minutes and giving error code of 1000.
-
@admin ok now i have got the streaming output as:
[{"ak":"ok","task":"cn","msg":"connected"}] [{"ak":"ok","task":"mw","msg":"mw"}] [{"ak":"ok","task":"mw","msg":"mw"}] [] [{"tvalue":"10\/12\/2020 10:35:00","name":"tm"{"to":"5592405812.40","lo":"2001.00","e":"nse_cm","sp":"2008.00","c":"2026.95","ltp":"2008.00","ltq":"27","tk":"2885","bs":"714","ltt":"10\/12\/2020 10:35:00","tsq":"781238","v":"2780424","bp":"2007.95","cng":"-18.95","bq":"39","ap":"2011.35","nc":"-00.93","name":"sf","tbq":"518673"}, {"to":"2281841541.00","lo":"2785.05","e":"nse_cm","sp":"2792.35","c":"2810.80","ltp":"2792.35","ltq":"3","tk":"11536","bs":"9","ltt":"10\/12\/2020 10:35:00","tsq":"199536","v":"813926","bp":"2791.40","cng":"-18.45","bq":"15","ap":"2803.50","nc":"-00.66","name":"sf","tbq":"139632"}]
but issue is i'm not able to parse this dictionary in a list using python , or say get a data from dictionary, can anyone help me out in this one, also there is some using with streaming service in python as the connection closes on its own after sometime.
-
@admin yes thankyou now it is working
-
@dorje @mihirk We have updated our Python SDK.
Request you to please re-install and refer the following URL to establish a websocket connection: https://github.com/angelbroking-github/smartapi-python/blob/main/README.md
-
@mihirk yes but it was of no help