Working WebSocket Sample code to retrieve LTP - Python
-
Here is the working WebSocket sample code to retrieve LTP:
from smartapi import SmartConnect from smartapi import WebSocket obj=SmartConnect(api_key="your_secret_key") data = obj.generateSession('your_client_id', 'your_password') feedToken=obj.getfeedToken() FEED_TOKEN= feedToken CLIENT_CODE= 'your_client_id' token="nse_cm|26009" WS = WebSocket(FEED_TOKEN, CLIENT_CODE) def on_tick(ws, tick): for i in tick: for j, k in i.items(): if j == 'ltp': print(j, k) else: pass def on_connect(ws, response): ws.send_request(token) def on_close(ws, code, reason): ws.stop() # Assign the callbacks. WS.on_ticks = on_tick WS.on_connect = on_connect WS.on_close = on_close WS.connect()
-
Please share which version of SDK you are using.. I can only check the code depending on that..
-
@rajanprabu
OK..Is code is correct one ?
have used ss.connect() instead ws.connect(). Have changed it but no use at all
-
I don't use google colab so its difficult to comment. which versions of python sdk r u using ??
-
@rajanprabu
Any idea on below error. Using google.colab -
@James-Bond
No I am not getting any error on code provided above.
Its working fine. -
@admin and team is very helpful and they will reach out to you.
@James-Bond has compiled some nice set of instructions and codes here which you can look
https://smartapi.angelbroking.com/topic/325/faq-smart-api/5
I will write a complete page with sample scripts sometime soon..
-
@sudhakar please provide us your contact details to assist you.
-
@rajanprabu , Can you help with any sample code , API guide is use less,it does not have anything.
-
@sudhakar said in Working WebSocket Sample code to retrieve LTP - Python:
is there any way to get low, high , ltp and volumes, &
this you get it in WebSocket..
@sudhakar said in Working WebSocket Sample code to retrieve LTP - Python:
market watch (best 5 bids & ask)
yes via api call.. rate limit is 1 call per seconds if I remember right..
-
is there any way to get low, high , ltp and volumes, & market watch (best 5 bids & ask)?
-
@lavly what's your sdk version ??
-
@James-Bond yes
-
-
you can look at the key value and its corresponding explanation here
-
@James-Bond this code is giving following errorrs
ReactorNotRestartable Traceback (most recent call last)
<ipython-input-71-f3a22d8e176e> in <module>()
21 ss.on_close = on_close
22
---> 23 ss.connect( )3 frames
/usr/local/lib/python3.7/dist-packages/twisted/internet/base.py in startRunning(self)
841 raise error.ReactorAlreadyRunning()
842 if self._startedBefore:
--> 843 raise error.ReactorNotRestartable()
844 self._started = True
845 self._stopped = FalseReactorNotRestartable:
-
def on_tick(ws, tick):
for i in tick:
for j, k in i.items():
if j == 'ltp':
print(j, k)
else:
passIf j is ltp and k is the value of ltp then Instead of just LTP, I want to print Open,Close,High,Low,Bid Price, Ask Price. Any idea how?
-
This post is deleted! -
-