How to get continuous Live Feed for a stock



  • How will i get the continuous Live feed for a stock. I am using the Upstox Node.js library where we have a method upstox.getLiveFeeds() but this I think will give the response only once what if I want the live update on price every time it is changing so that I can build my strategy on updated price.

    Also in Websocket we have a event for getting message for live feed :

    upstox.on("liveFeed", function(message) {
    //message for live feed
    });

    What is the response of the above fragment? Can we get the updated value using this websocket event?

    Or Can we use upstox.getLiveFeeds() method to get the updated price asynchronously everytime it is getting updated?

    My requirement is to get the updated LTP, High, Low, Open value during the trading session to update my strategy. Please help me with this.

    Thanks in advance.


  • Developer

    Hi @rupesh93 ,
    You have to make a websocket and call subscribe to get updated feed. After that you will start getting feed on websocket.
    Refer https://upstox.com/developer/api/v1/docs/#subscribe for details.



  • @archana said in How to get continuous Live Feed for a stock:

    https://upstox.com/developer/api/v1/docs/#subscribe

    HI Archana .... I am trying to get the data over socket, but don't get anything.
    On Subscribing a symbol to get data, we get response as true, but after that data streaming does not start.

    Please help or share the working code snippet for same with sample response log.

    Regards
    Rahul


  • Developer

    @Jogisant, which SDK you are using? Can you share your code snippet?



  • @Archana

    We are using Python based code

    u.get_master_contract('NSE_EQ')
    print ("Socket connect code executed")
    def event_handler_socket_open():
    print("SOCKET OPEN********" + str(datetime.datetime.now()))
    print("\n\n")

            def event_handler_quote_update(message):
                    print("********QUOTE UPDATE****************" + str(datetime.datetime.now()))
                    print(message)
                    now =datetime.datetime.now()
                    t ="%s%s%s%s%s" % (now.year, now.month, now.day, now.hour, now.minute)
                    filename = "PH/QU-%s.json" % (t)
                    with open(filename, 'a') as outfile:
                            json.dump(message, outfile)
                    print("\n\n")
            try:
                    u.set_on_quote_update(event_handler_quote_update)
            except:
                    pass
            def event_handler_order_update(message):
                    print("********ORDER UPDATE****************" + str(datetime.datetime.now()))
                    print(message)
                    print("\n\n")
    
            u.set_on_order_update(event_handler_order_update)
    
            def event_handler_trade_update(message):
                    print("********TRADE UPDATE****************" + str(datetime.datetime.now()))
                    print(message)
                    print("\n\n")
    
            u.set_on_order_update(event_handler_order_update)
    
            def event_handler_trade_update(message):
                    print("********TRADE UPDATE****************" + str(datetime.datetime.now()))
                    print(message)
                    print("\n\n")
    
            u.set_on_trade_update(event_handler_trade_update)
    
            def event_handler_error(err, r):
                    print("********ERROR HANDLER****************" + str(datetime.datetime.now()))
                    print(err, r)
                    print("\n\n")
            u.set_on_error(event_handler_error)
    
            def event_handler_socket_disconnect():
                    print("********IN SOCKET DISCONNECTED Function ****************" + str(datetime.datetime.now()))
                    print("\n\n")
            # Uncomment For Reconnection Logic
                    u.start_websocket(False)
                    print("coming out of disconnect")
    
            u.set_on_disconnect(event_handler_socket_disconnect)
    # u.unsubscribe(u.get_instrument_by_symbol('NSE_EQ', 'ONGC'), LiveFeedType.LTP)
    # u.subscribe(u.get_instrument_by_symbol('NSE_EQ', 'ONGC'), LiveFeedType.LTP)
    
            u.start_websocket(False)
            print("******************************")
    

    This result in to missing of data every one minute


  • Developer

    @Jogisant, you are using > now =datetime.datetime.now() to identify time of feed arrival and after that, you are doing file operation. File operations generally take times in seconds why don't you push into a queue and there would be other thread reading from this queue and dumping to file.
    Why don't you use timestamp value passed in the feed(https://upstox.com/developer/api/v1/docs/#live-feed) to detect feed time and use this time to detect feed arrival?



  • @archana

    Thanks much for your inputs

    We have removed file handling thing from code now and saving all data in one single file.. Will revert back to you after checking



  • I was storing the data into dictionary and using the dictionary in code which is much faster for me than writing to file. Give a try if it sui0_1529590808706_LiveUpdate.PNG ts you.


Log in to reply
 

Looks like your connection to Upstox Forum was lost, please wait while we try to reconnect.