tradingview_ws icon indicating copy to clipboard operation
tradingview_ws copied to clipboard

Unable to get required data ex. bid, ask, open, close

Open ankitparmar007 opened this issue 3 years ago • 1 comments

Hello there, I have cloned your repo and tried locally, there was a requirement of more data to me, so I have added more fields such as bid,ask after adding fields shown below

self.send_message(ws, "quote_set_fields", [
              session,"bid","ask","ch","chp","exchange","lp","lp_time","volume"])

Getting this as output as

result ~m~111~m~{"m":"qsd","p":["qs_xdnrbwczlxya",{"n":"SP:SPX","s":"ok","v":{"lp_time":1676569566,"lp":4134.73,"ch":-12.88}}]}

Tried both way, without session and with session.

Would be appreciated, if you could look into this and help me to find the values.

ankitparmar007 avatar Feb 16 '23 17:02 ankitparmar007

You can replace the socket_quote socket function as below:

def socket_quote(self, ws, callback):
        key_mapping = {
            'price': 'lp',
            'volume': 'volume',
            'change': 'ch',
            'change_percent': 'chp',
            'bid': 'bid',
            'bid_size': 'bid_size',
            'ask': 'ask',
            'ask_size': 'ask_size'
        }
        while True:
            try:
                result = ws.recv()

                if "quote_completed" in result or "session_id" in result:
                    continue

                res = re.findall("^.*?({.*)$", result)

                if len(res) != 0:
                    jsonres = json.loads(res[0])
                    # logging.debug(f'Payload Raw Message: {jsonres["p"][1]}')
                    if jsonres["m"] == "qsd":
                        symbol = jsonres["p"][1]["n"]
                        data = jsonres["p"][1]['v']
                        map_obj = {
                            "symbol": symbol
                        }
                        for key, value in key_mapping.items():
                            map_obj[key] = data.get(value, None)
                        # logging.debug(f'Map quote object: {map_obj}')
                        callback(map_obj)
                else:
                    # ping packet
                    self.send_ping_packet(ws ,result)
            except KeyboardInterrupt:
                break
            except:
                continue

nguyenthdat avatar Feb 20 '23 13:02 nguyenthdat