Python Streaming Tick Data Problem with SmartAPI Websocket's


  • Live streaming of stock price is not working for me in python program. It looks like running and in loop, but not displaying tick price.

    Can anyone please check and let me know if anything incorrect in the code.

    Here is the code

    7f250157-290f-4206-a29e-fb7851c776c8-image.png


    Output it is showing is "before connect", not showing "on connect" and tick value.


  • HI @adi3399 We are working on the web socket code we will update you soon with the eta.


  • @admin Thank you


  • This post is deleted!

  • This post is deleted!

  • @azkhan added on_open() function.. still it is not showing tick price info. this is new code

    7f98b4ae-2855-404c-a4df-6876335919d9-image.png


  • @adi3399 please update your SmartAPI Python library.


  • @azkhan Uninstalled and re-installed smartapi python package. There is progress now. it is opening websocket connection and showing response. I need stock price information.. how can I get price? Please advise.

    Below is the output of same code. It is showing actual client code correctly , but to show here I changed it to "my client code"

    before connect
    __on_open################
    {'task': 'cn', 'channel': 'NONLM', 'token': 'token number', 'user': 'my client id', 'acctid': 'my client id'}
    2021-07-15 13:51:05.092349 : Start task in the background
    {'task': 'hb', 'channel': '', 'token': 'token number', 'user': 'my client id', 'acctid': 'my client id'}
    2021-07-15 13:51:21.833013 : Start task in the background
    {'task': 'hb', 'channel': '', 'token': 'token number', 'user': 'my client id', 'acctid': 'my client id'}
    HeartBeat Sending Failed
    2021-07-15 13:51:29.792444 : Start task in the background
    {'task': 'hb', 'channel': '', 'token': 'token number', 'user': 'my client id', 'acctid': 'my client id'}
    HeartBeat Sending Failed
    2021-07-15 13:51:35.124963 : Start task in the background
    {'task': 'hb', 'channel': '', 'token': 'token number', 'user': 'my client id', 'acctid': 'my client id'}
    2021-07-15 13:51:51.841917 : Start task in the background
    {'task': 'hb', 'channel': '', 'token': 'token number', 'user': 'my client id', 'acctid': 'my client id'}
    HeartBeat Sending Failed
    2021-07-15 13:51:59.822315 : Start task in the background
    {'task': 'hb', 'channel': '', 'token': 'token number', 'user': 'my client id', 'acctid': 'my client id'}
    HeartBeat Sending Failed


  • @adi3399
    Go to this link to find the token information of all tradable instruments : https://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json

    Then Use this code to get live data streaming for any stock or Index
    Getting started with SmartAPI Websocket's :

    from smartapi import SmartConnect
    from smartapi import SmartWebSocket

    Live_Data_API = " Enter Your API Key Here"

    Username = "Enter Your Angel Broking User name"

    Password = "Enter Your Angel Broking Password"

    #create object of call
    obj=SmartConnect(api_key=Live_Data_API)

    #login api call

    data = obj.generateSession(Username,Password)
    refreshToken= data['data']['refreshToken']

    #fetch the feedtoken
    feedToken=obj.getfeedToken()

    #fetch User Profile
    userProfile= obj.getProfile(refreshToken)

    feed_token=092017047

    FEED_TOKEN=feedToken
    CLIENT_CODE=Username

    token="mcx_fo|224395"

    token="EXCHANGE|TOKEN_SYMBOL" #SAMPLE: nse_cm|2885&nse_cm|1594&nse_cm|11536&nse_cm|3045

    token="mcx_fo|226745&mcx_fo|220822&mcx_fo|227182&mcx_fo|221599"

    task="mw" # mw|sfi|dp

    ss = SmartWebSocket(FEED_TOKEN, CLIENT_CODE)

    def on_message(ws, message):
    print("Ticks: {}".format(message))

    def on_open(ws):
    print("on open")
    ss.subscribe(task,token)

    def on_error(ws, error):
    print(error)

    def on_close(ws):
    print("Close")

    Assign the callbacks.

    ss._on_open = on_open
    ss._on_message = on_message
    ss._on_error = on_error
    ss._on_close = on_close

    ss.connect()


  • @adi3399
    For more Info on websocket go to this link : https://smartapi.angelbroking.com/docs/WebSocketStreaming


  • I am new to python. Can you guide how to install all these in python.. I mean what software I shall install like "pycharm" or other. Then anything else I need to download

    Then what to be done , a complete guide for a new person.


  • HI @webseos You can download anaconda and use spyder for development also for detail understanding you can attend today's webinar on integration of smartapi using python..fr basic codes you can go through below thread.
    https://smartapi.angelbroking.com/topic/1194/basic-smartapi-integration-using-python