0xrushi / tradingview-scraper

MIT License
323 stars 104 forks source link

Code for getting live streaming data from trading view by sending heartbeat every 10 seconds #1

Closed rahulmr closed 3 years ago

rahulmr commented 4 years ago

First of all thanks for writing such beautiful code. As you have already written code for getting candle / bar data using websocket, could you please enhance it for live streaming data also.

The difference I found is that for streaming data, every 10 seconds a message is sent ~m~5~m~~h~19 where the last digit changes. Further it also seems that ~m~5~m~~h~99 then changes to ~m~6~m~~h~100

You can select any crypto data for the same like BTCUSDT so that it is live 24x7

Thanks in advance.

rahulmr commented 4 years ago

Coded as below - little messy - please improve it.

import websocket
import time
import threading
import json

SOCKET = "wss://data.tradingview.com/socket.io/websocket"

headers = {
    "Accept-Encoding": "gzip, deflate, br",
    # "Accept-Language": "en-US,en;q=0.9",
    # "Cache-Control": "no-cache",
    # "Connection": "Upgrade",
    "Host": "data.tradingview.com",
    "Origin": "https://www.tradingview.com",
    # "Pragma": "no-cache",
    # "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
    # "Sec-WebSocket-Key": "Qf9IDRKqcgNBrNs7X4FK9w==",
    # "Sec-WebSocket-Version": 13,
    # "Upgrade": "websocket",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
}

"""
~m~55~m~{"m":"chart_create_session","p":["cs_zEcm9GqyQdK0",""]}
'~m~52~m~{"m":"quote_create_session","p":["qs_dl0OygXkO4uu"]}'
~m~98~m~{"m":"quote_add_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}
~m~68~m~{"m":"quote_fast_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT"]}
~m~6~m~~h~157
~m~411~m~{"m":"quote_fast_symbols","p":["qs_x72fChUYomPp","MCX:GOLDGUINEAN2020","NSE:NIFTY","MCX:GOLDPETALN2020","NSE:BAJFINANCE","MCX:SILVERMQ2020","NSE:SBIN","NSE:BANKNIFTY","NSE:RBLBANK","NSE:INDIAVIX","NSE:INDUSINDBK","NSE:CIPLA","NSE:SUNTV","MCX:NATURALGAS1!","MCX:SILVERMIC1!","MCX:CRUDEOIL1!","MCX:GOLDM1!","NSE:BANKNIFTY1!","SGX:IN1!","OANDA:USDINR","NSE:DABUR","NSE:BERGEPAINT","NASDAQ:TSLA","BINANCE:BTCUSDT"]}
~m~68~m~{"m":"quote_fast_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT"]}
~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}

"""

def on_open(ws):
    print('opened connection')
    # def run(*args):
    #     for i in range(30):
    #         time.sleep(1)
    #         ws.send("Hello %d" % i)
    #     time.sleep(1)
    #     ws.close()
    #     print("thread terminating...")
    # threading.start_new_thread(run, ())
    time.sleep(2)
    # ws.send('~m~524~m~{"m":"set_auth_token","p":["eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9.eyJ1c2VyX2lkIjo5OTIxMjA1LCJleHAiOjE2MDM2NzgyMDcsImlhdCI6MTYwMzY2MzgwNywicGxhbiI6IiIsImV4dF9ob3VycyI6MSwicGVybSI6IiIsInN0dWR5X3Blcm0iOiJQVUI7eXNueXc5aUVOY0dTeEhJQk9pNGJUUDFIczJreVg2Y1EsUFVCO0ZQRlJnWU5FOTZiZEI3MXBBZ1RSUGdIa3dLWGswZnJXIiwibWF4X3N0dWRpZXMiOjMsIm1heF9mdW5kYW1lbnRhbHMiOjB9.LTCdVfkkquhStte9UU_xWiVJE-ZBIoShUrPQP6vywh1ep3S894qEpk3h509utD5vmz8vgAzcJRZKy3eKPMY-bh81gg76WRjwdjJ2RM2YnoQ7tAhKF0wK78-JFg_3BfcTmude1ypJu_7I5NJgeF8RqM78ymJ6OTiKzgu84ZrMRr4"]}')
    ws.send('~m~54~m~{"m":"set_auth_token","p":["unauthorized_user_token"]}')
    ws.send('~m~55~m~{"m":"chart_create_session","p":["cs_zEcm9GqyQdK0",""]}')
    ws.send('~m~52~m~{"m":"quote_create_session","p":["qs_x72fChUYomPp"]}')
    ws.send('~m~344~m~{"m":"quote_set_fields","p":["qs_x72fChUYomPp","ch","chp","current_session","description","local_description","language","exchange","fractional","is_tradable","lp","lp_time","minmov","minmove2","original_name","pricescale","pro_name","short_name","type","update_mode","volume","currency_code","logoid","currency-logoid","base-currency-logoid"]}')

    ws.send('~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}')
    ws.send('~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BNBUSDT",{"flags":["force_permission"]}]}')
    ws.send('~m~91~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","NSE:SBIN",{"flags":["force_permission"]}]}')
    ws.send('~m~98~m~{"m":"quote_fast_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT","BINANCE:BNBUSDT", "NSE:SBIN"]}')

def on_close(ws):
    print('closed connection')

def on_message(ws, message):
    # p = message.split('~', -1)[4]
    # data = json.loads(p)
    # print(data)
    # print(f'received message :: {message}')
    if 'lp' in message:
        p = message.split('~', -1)[4]
        data = json.loads(p)
        # print(data)
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        symbol = data['p'][1]['n']
        ltp = data['p'][1]['v']['lp']
        volume = data['p'][1]['v']['volume']
        if symbol.upper() == "NSE:SBIN":
            print(f'tick :: {timestamp} :: {symbol} :: {ltp} :: {volume}')

# ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_data=on_data,
#                             on_close=on_close, on_message=on_message, header=headers)
# ws.run_forever()

if __name__ == "__main__":
    websocket.enableTrace(False)
    ws = websocket.WebSocketApp(
        SOCKET, on_message=on_message, on_open=on_open, on_close=on_close)
    wst = threading.Thread(target=ws.run_forever)
    wst.daemon = True
    wst.start()

    conn_timeout = 60
    while not ws.sock.connected and conn_timeout:
        time.sleep(1)
        conn_timeout -= 1

    while ws.sock is not None:
        time.sleep(10)

Output as below:

opened connection
tick :: 2020-10-26 15:16:44 :: NSE:SBIN :: 196.65 :: 43263601
tick :: 2020-10-26 15:16:47 :: NSE:SBIN :: 196.55 :: 43281572
tick :: 2020-10-26 15:16:48 :: NSE:SBIN :: 196.5 :: 43284530
tick :: 2020-10-26 15:16:49 :: NSE:SBIN :: 196.65 :: 43284705
tick :: 2020-10-26 15:16:50 :: NSE:SBIN :: 196.55 :: 43289608
tick :: 2020-10-26 15:16:53 :: NSE:SBIN :: 196.65 :: 43302304
tick :: 2020-10-26 15:16:58 :: NSE:SBIN :: 196.6 :: 43306025
tick :: 2020-10-26 15:16:59 :: NSE:SBIN :: 196.55 :: 43310465
tick :: 2020-10-26 15:17:00 :: NSE:SBIN :: 196.65 :: 43313272
tick :: 2020-10-26 15:17:04 :: NSE:SBIN :: 196.6 :: 43324023
tick :: 2020-10-26 15:17:05 :: NSE:SBIN :: 196.7 :: 43327660
tick :: 2020-10-26 15:17:06 :: NSE:SBIN :: 196.65 :: 43327834
tick :: 2020-10-26 15:17:07 :: NSE:SBIN :: 196.6 :: 43329298
tick :: 2020-10-26 15:17:09 :: NSE:SBIN :: 196.65 :: 43329746
tick :: 2020-10-26 15:17:10 :: NSE:SBIN :: 196.6 :: 43332710
tick :: 2020-10-26 15:17:12 :: NSE:SBIN :: 196.65 :: 43334585
tick :: 2020-10-26 15:17:15 :: NSE:SBIN :: 196.55 :: 43336631
tick :: 2020-10-26 15:17:16 :: NSE:SBIN :: 196.6 :: 43337236
tick :: 2020-10-26 15:17:18 :: NSE:SBIN :: 196.7 :: 43352825
tick :: 2020-10-26 15:17:20 :: NSE:SBIN :: 196.55 :: 43359141
tick :: 2020-10-26 15:17:22 :: NSE:SBIN :: 196.65 :: 43362175
tick :: 2020-10-26 15:17:23 :: NSE:SBIN :: 196.55 :: 43364062
tick :: 2020-10-26 15:17:25 :: NSE:SBIN :: 196.6 :: 43364601
tick :: 2020-10-26 15:17:27 :: NSE:SBIN :: 196.55 :: 43371410
tick :: 2020-10-26 15:17:28 :: NSE:SBIN :: 196.7 :: 43384268
tick :: 2020-10-26 15:17:30 :: NSE:SBIN :: 196.6 :: 43385580
tick :: 2020-10-26 15:17:32 :: NSE:SBIN :: 196.7 :: 43388415
tick :: 2020-10-26 15:17:33 :: NSE:SBIN :: 196.6 :: 43390152
tick :: 2020-10-26 15:17:35 :: NSE:SBIN :: 196.65 :: 43392443
tick :: 2020-10-26 15:17:36 :: NSE:SBIN :: 196.7 :: 43392859
tick :: 2020-10-26 15:17:37 :: NSE:SBIN :: 196.65 :: 43395097
tick :: 2020-10-26 15:17:39 :: NSE:SBIN :: 196.6 :: 43395708
tick :: 2020-10-26 15:17:40 :: NSE:SBIN :: 196.65 :: 43395846
tick :: 2020-10-26 15:17:43 :: NSE:SBIN :: 196.5 :: 43419135
tick :: 2020-10-26 15:17:46 :: NSE:SBIN :: 196.65 :: 43424576
tick :: 2020-10-26 15:17:47 :: NSE:SBIN :: 196.6 :: 43426038
tick :: 2020-10-26 15:17:52 :: NSE:SBIN :: 196.5 :: 43436492
tick :: 2020-10-26 15:17:53 :: NSE:SBIN :: 196.6 :: 43437217
tick :: 2020-10-26 15:17:55 :: NSE:SBIN :: 196.5 :: 43439284
tick :: 2020-10-26 15:17:56 :: NSE:SBIN :: 196.6 :: 43442305
tick :: 2020-10-26 15:17:57 :: NSE:SBIN :: 196.65 :: 43444488
tick :: 2020-10-26 15:17:58 :: NSE:SBIN :: 196.5 :: 43454282
tick :: 2020-10-26 15:17:59 :: NSE:SBIN :: 196.65 :: 43458267
tick :: 2020-10-26 15:18:01 :: NSE:SBIN :: 196.5 :: 43461217
closed connection
sammathew000 commented 3 years ago

Is this script, stills works? Am getting this error

raise WebSocketBadStatusException("Handshake status %d", status) websocket._exceptions.WebSocketBadStatusException: Handshake status 403

sammathew000 commented 3 years ago

closed connection Traceback (most recent call last): File "C:\test.py", line 95, in <module> while not ws.sock.connected and conn_timeout: AttributeError: 'NoneType' object has no attribute 'connected'

0xrushi commented 3 years ago

Coded as below - little messy - please improve it.

import websocket
import time
import threading
import json

SOCKET = "wss://data.tradingview.com/socket.io/websocket"

headers = {
    "Accept-Encoding": "gzip, deflate, br",
    # "Accept-Language": "en-US,en;q=0.9",
    # "Cache-Control": "no-cache",
    # "Connection": "Upgrade",
    "Host": "data.tradingview.com",
    "Origin": "https://www.tradingview.com",
    # "Pragma": "no-cache",
    # "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
    # "Sec-WebSocket-Key": "Qf9IDRKqcgNBrNs7X4FK9w==",
    # "Sec-WebSocket-Version": 13,
    # "Upgrade": "websocket",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
}

"""
~m~55~m~{"m":"chart_create_session","p":["cs_zEcm9GqyQdK0",""]}
'~m~52~m~{"m":"quote_create_session","p":["qs_dl0OygXkO4uu"]}'
~m~98~m~{"m":"quote_add_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}
~m~68~m~{"m":"quote_fast_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT"]}
~m~6~m~~h~157
~m~411~m~{"m":"quote_fast_symbols","p":["qs_x72fChUYomPp","MCX:GOLDGUINEAN2020","NSE:NIFTY","MCX:GOLDPETALN2020","NSE:BAJFINANCE","MCX:SILVERMQ2020","NSE:SBIN","NSE:BANKNIFTY","NSE:RBLBANK","NSE:INDIAVIX","NSE:INDUSINDBK","NSE:CIPLA","NSE:SUNTV","MCX:NATURALGAS1!","MCX:SILVERMIC1!","MCX:CRUDEOIL1!","MCX:GOLDM1!","NSE:BANKNIFTY1!","SGX:IN1!","OANDA:USDINR","NSE:DABUR","NSE:BERGEPAINT","NASDAQ:TSLA","BINANCE:BTCUSDT"]}
~m~68~m~{"m":"quote_fast_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT"]}
~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}

"""

def on_open(ws):
    print('opened connection')
    # def run(*args):
    #     for i in range(30):
    #         time.sleep(1)
    #         ws.send("Hello %d" % i)
    #     time.sleep(1)
    #     ws.close()
    #     print("thread terminating...")
    # threading.start_new_thread(run, ())
    time.sleep(2)
    # ws.send('~m~524~m~{"m":"set_auth_token","p":["eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9.eyJ1c2VyX2lkIjo5OTIxMjA1LCJleHAiOjE2MDM2NzgyMDcsImlhdCI6MTYwMzY2MzgwNywicGxhbiI6IiIsImV4dF9ob3VycyI6MSwicGVybSI6IiIsInN0dWR5X3Blcm0iOiJQVUI7eXNueXc5aUVOY0dTeEhJQk9pNGJUUDFIczJreVg2Y1EsUFVCO0ZQRlJnWU5FOTZiZEI3MXBBZ1RSUGdIa3dLWGswZnJXIiwibWF4X3N0dWRpZXMiOjMsIm1heF9mdW5kYW1lbnRhbHMiOjB9.LTCdVfkkquhStte9UU_xWiVJE-ZBIoShUrPQP6vywh1ep3S894qEpk3h509utD5vmz8vgAzcJRZKy3eKPMY-bh81gg76WRjwdjJ2RM2YnoQ7tAhKF0wK78-JFg_3BfcTmude1ypJu_7I5NJgeF8RqM78ymJ6OTiKzgu84ZrMRr4"]}')
    ws.send('~m~54~m~{"m":"set_auth_token","p":["unauthorized_user_token"]}')
    ws.send('~m~55~m~{"m":"chart_create_session","p":["cs_zEcm9GqyQdK0",""]}')
    ws.send('~m~52~m~{"m":"quote_create_session","p":["qs_x72fChUYomPp"]}')
    ws.send('~m~344~m~{"m":"quote_set_fields","p":["qs_x72fChUYomPp","ch","chp","current_session","description","local_description","language","exchange","fractional","is_tradable","lp","lp_time","minmov","minmove2","original_name","pricescale","pro_name","short_name","type","update_mode","volume","currency_code","logoid","currency-logoid","base-currency-logoid"]}')

    ws.send('~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}')
    ws.send('~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BNBUSDT",{"flags":["force_permission"]}]}')
    ws.send('~m~91~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","NSE:SBIN",{"flags":["force_permission"]}]}')
    ws.send('~m~98~m~{"m":"quote_fast_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT","BINANCE:BNBUSDT", "NSE:SBIN"]}')

def on_close(ws):
    print('closed connection')

def on_message(ws, message):
    # p = message.split('~', -1)[4]
    # data = json.loads(p)
    # print(data)
    # print(f'received message :: {message}')
    if 'lp' in message:
        p = message.split('~', -1)[4]
        data = json.loads(p)
        # print(data)
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        symbol = data['p'][1]['n']
        ltp = data['p'][1]['v']['lp']
        volume = data['p'][1]['v']['volume']
        if symbol.upper() == "NSE:SBIN":
            print(f'tick :: {timestamp} :: {symbol} :: {ltp} :: {volume}')

# ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_data=on_data,
#                             on_close=on_close, on_message=on_message, header=headers)
# ws.run_forever()

if __name__ == "__main__":
    websocket.enableTrace(False)
    ws = websocket.WebSocketApp(
        SOCKET, on_message=on_message, on_open=on_open, on_close=on_close)
    wst = threading.Thread(target=ws.run_forever)
    wst.daemon = True
    wst.start()

    conn_timeout = 60
    while not ws.sock.connected and conn_timeout:
        time.sleep(1)
        conn_timeout -= 1

    while ws.sock is not None:
        time.sleep(10)

Output as below:

opened connection
tick :: 2020-10-26 15:16:44 :: NSE:SBIN :: 196.65 :: 43263601
tick :: 2020-10-26 15:16:47 :: NSE:SBIN :: 196.55 :: 43281572
tick :: 2020-10-26 15:16:48 :: NSE:SBIN :: 196.5 :: 43284530
tick :: 2020-10-26 15:16:49 :: NSE:SBIN :: 196.65 :: 43284705
tick :: 2020-10-26 15:16:50 :: NSE:SBIN :: 196.55 :: 43289608
tick :: 2020-10-26 15:16:53 :: NSE:SBIN :: 196.65 :: 43302304
tick :: 2020-10-26 15:16:58 :: NSE:SBIN :: 196.6 :: 43306025
tick :: 2020-10-26 15:16:59 :: NSE:SBIN :: 196.55 :: 43310465
tick :: 2020-10-26 15:17:00 :: NSE:SBIN :: 196.65 :: 43313272
tick :: 2020-10-26 15:17:04 :: NSE:SBIN :: 196.6 :: 43324023
tick :: 2020-10-26 15:17:05 :: NSE:SBIN :: 196.7 :: 43327660
tick :: 2020-10-26 15:17:06 :: NSE:SBIN :: 196.65 :: 43327834
tick :: 2020-10-26 15:17:07 :: NSE:SBIN :: 196.6 :: 43329298
tick :: 2020-10-26 15:17:09 :: NSE:SBIN :: 196.65 :: 43329746
tick :: 2020-10-26 15:17:10 :: NSE:SBIN :: 196.6 :: 43332710
tick :: 2020-10-26 15:17:12 :: NSE:SBIN :: 196.65 :: 43334585
tick :: 2020-10-26 15:17:15 :: NSE:SBIN :: 196.55 :: 43336631
tick :: 2020-10-26 15:17:16 :: NSE:SBIN :: 196.6 :: 43337236
tick :: 2020-10-26 15:17:18 :: NSE:SBIN :: 196.7 :: 43352825
tick :: 2020-10-26 15:17:20 :: NSE:SBIN :: 196.55 :: 43359141
tick :: 2020-10-26 15:17:22 :: NSE:SBIN :: 196.65 :: 43362175
tick :: 2020-10-26 15:17:23 :: NSE:SBIN :: 196.55 :: 43364062
tick :: 2020-10-26 15:17:25 :: NSE:SBIN :: 196.6 :: 43364601
tick :: 2020-10-26 15:17:27 :: NSE:SBIN :: 196.55 :: 43371410
tick :: 2020-10-26 15:17:28 :: NSE:SBIN :: 196.7 :: 43384268
tick :: 2020-10-26 15:17:30 :: NSE:SBIN :: 196.6 :: 43385580
tick :: 2020-10-26 15:17:32 :: NSE:SBIN :: 196.7 :: 43388415
tick :: 2020-10-26 15:17:33 :: NSE:SBIN :: 196.6 :: 43390152
tick :: 2020-10-26 15:17:35 :: NSE:SBIN :: 196.65 :: 43392443
tick :: 2020-10-26 15:17:36 :: NSE:SBIN :: 196.7 :: 43392859
tick :: 2020-10-26 15:17:37 :: NSE:SBIN :: 196.65 :: 43395097
tick :: 2020-10-26 15:17:39 :: NSE:SBIN :: 196.6 :: 43395708
tick :: 2020-10-26 15:17:40 :: NSE:SBIN :: 196.65 :: 43395846
tick :: 2020-10-26 15:17:43 :: NSE:SBIN :: 196.5 :: 43419135
tick :: 2020-10-26 15:17:46 :: NSE:SBIN :: 196.65 :: 43424576
tick :: 2020-10-26 15:17:47 :: NSE:SBIN :: 196.6 :: 43426038
tick :: 2020-10-26 15:17:52 :: NSE:SBIN :: 196.5 :: 43436492
tick :: 2020-10-26 15:17:53 :: NSE:SBIN :: 196.6 :: 43437217
tick :: 2020-10-26 15:17:55 :: NSE:SBIN :: 196.5 :: 43439284
tick :: 2020-10-26 15:17:56 :: NSE:SBIN :: 196.6 :: 43442305
tick :: 2020-10-26 15:17:57 :: NSE:SBIN :: 196.65 :: 43444488
tick :: 2020-10-26 15:17:58 :: NSE:SBIN :: 196.5 :: 43454282
tick :: 2020-10-26 15:17:59 :: NSE:SBIN :: 196.65 :: 43458267
tick :: 2020-10-26 15:18:01 :: NSE:SBIN :: 196.5 :: 43461217
closed connection

I did add this https://github.com/rushic24/tradingview-scraper/blob/master/livestreamtest.py but the data is getting repeated sometimes, not sure why.

rahulmr commented 3 years ago

Coded as below - little messy - please improve it.

import websocket
import time
import threading
import json

SOCKET = "wss://data.tradingview.com/socket.io/websocket"

headers = {
    "Accept-Encoding": "gzip, deflate, br",
    # "Accept-Language": "en-US,en;q=0.9",
    # "Cache-Control": "no-cache",
    # "Connection": "Upgrade",
    "Host": "data.tradingview.com",
    "Origin": "https://www.tradingview.com",
    # "Pragma": "no-cache",
    # "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
    # "Sec-WebSocket-Key": "Qf9IDRKqcgNBrNs7X4FK9w==",
    # "Sec-WebSocket-Version": 13,
    # "Upgrade": "websocket",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"
}

"""
~m~55~m~{"m":"chart_create_session","p":["cs_zEcm9GqyQdK0",""]}
'~m~52~m~{"m":"quote_create_session","p":["qs_dl0OygXkO4uu"]}'
~m~98~m~{"m":"quote_add_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}
~m~68~m~{"m":"quote_fast_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT"]}
~m~6~m~~h~157
~m~411~m~{"m":"quote_fast_symbols","p":["qs_x72fChUYomPp","MCX:GOLDGUINEAN2020","NSE:NIFTY","MCX:GOLDPETALN2020","NSE:BAJFINANCE","MCX:SILVERMQ2020","NSE:SBIN","NSE:BANKNIFTY","NSE:RBLBANK","NSE:INDIAVIX","NSE:INDUSINDBK","NSE:CIPLA","NSE:SUNTV","MCX:NATURALGAS1!","MCX:SILVERMIC1!","MCX:CRUDEOIL1!","MCX:GOLDM1!","NSE:BANKNIFTY1!","SGX:IN1!","OANDA:USDINR","NSE:DABUR","NSE:BERGEPAINT","NASDAQ:TSLA","BINANCE:BTCUSDT"]}
~m~68~m~{"m":"quote_fast_symbols","p":["qs_dl0OygXkO4uu","BINANCE:BTCUSDT"]}
~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}

"""

def on_open(ws):
    print('opened connection')
    # def run(*args):
    #     for i in range(30):
    #         time.sleep(1)
    #         ws.send("Hello %d" % i)
    #     time.sleep(1)
    #     ws.close()
    #     print("thread terminating...")
    # threading.start_new_thread(run, ())
    time.sleep(2)
    # ws.send('~m~524~m~{"m":"set_auth_token","p":["eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzUxMiJ9.eyJ1c2VyX2lkIjo5OTIxMjA1LCJleHAiOjE2MDM2NzgyMDcsImlhdCI6MTYwMzY2MzgwNywicGxhbiI6IiIsImV4dF9ob3VycyI6MSwicGVybSI6IiIsInN0dWR5X3Blcm0iOiJQVUI7eXNueXc5aUVOY0dTeEhJQk9pNGJUUDFIczJreVg2Y1EsUFVCO0ZQRlJnWU5FOTZiZEI3MXBBZ1RSUGdIa3dLWGswZnJXIiwibWF4X3N0dWRpZXMiOjMsIm1heF9mdW5kYW1lbnRhbHMiOjB9.LTCdVfkkquhStte9UU_xWiVJE-ZBIoShUrPQP6vywh1ep3S894qEpk3h509utD5vmz8vgAzcJRZKy3eKPMY-bh81gg76WRjwdjJ2RM2YnoQ7tAhKF0wK78-JFg_3BfcTmude1ypJu_7I5NJgeF8RqM78ymJ6OTiKzgu84ZrMRr4"]}')
    ws.send('~m~54~m~{"m":"set_auth_token","p":["unauthorized_user_token"]}')
    ws.send('~m~55~m~{"m":"chart_create_session","p":["cs_zEcm9GqyQdK0",""]}')
    ws.send('~m~52~m~{"m":"quote_create_session","p":["qs_x72fChUYomPp"]}')
    ws.send('~m~344~m~{"m":"quote_set_fields","p":["qs_x72fChUYomPp","ch","chp","current_session","description","local_description","language","exchange","fractional","is_tradable","lp","lp_time","minmov","minmove2","original_name","pricescale","pro_name","short_name","type","update_mode","volume","currency_code","logoid","currency-logoid","base-currency-logoid"]}')

    ws.send('~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT",{"flags":["force_permission"]}]}')
    ws.send('~m~98~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","BINANCE:BNBUSDT",{"flags":["force_permission"]}]}')
    ws.send('~m~91~m~{"m":"quote_add_symbols","p":["qs_x72fChUYomPp","NSE:SBIN",{"flags":["force_permission"]}]}')
    ws.send('~m~98~m~{"m":"quote_fast_symbols","p":["qs_x72fChUYomPp","BINANCE:BTCUSDT","BINANCE:BNBUSDT", "NSE:SBIN"]}')

def on_close(ws):
    print('closed connection')

def on_message(ws, message):
    # p = message.split('~', -1)[4]
    # data = json.loads(p)
    # print(data)
    # print(f'received message :: {message}')
    if 'lp' in message:
        p = message.split('~', -1)[4]
        data = json.loads(p)
        # print(data)
        timestamp = time.strftime("%Y-%m-%d %H:%M:%S")
        symbol = data['p'][1]['n']
        ltp = data['p'][1]['v']['lp']
        volume = data['p'][1]['v']['volume']
        if symbol.upper() == "NSE:SBIN":
            print(f'tick :: {timestamp} :: {symbol} :: {ltp} :: {volume}')

# ws = websocket.WebSocketApp(SOCKET, on_open=on_open, on_data=on_data,
#                             on_close=on_close, on_message=on_message, header=headers)
# ws.run_forever()

if __name__ == "__main__":
    websocket.enableTrace(False)
    ws = websocket.WebSocketApp(
        SOCKET, on_message=on_message, on_open=on_open, on_close=on_close)
    wst = threading.Thread(target=ws.run_forever)
    wst.daemon = True
    wst.start()

    conn_timeout = 60
    while not ws.sock.connected and conn_timeout:
        time.sleep(1)
        conn_timeout -= 1

    while ws.sock is not None:
        time.sleep(10)

Output as below:

opened connection
tick :: 2020-10-26 15:16:44 :: NSE:SBIN :: 196.65 :: 43263601
tick :: 2020-10-26 15:16:47 :: NSE:SBIN :: 196.55 :: 43281572
tick :: 2020-10-26 15:16:48 :: NSE:SBIN :: 196.5 :: 43284530
tick :: 2020-10-26 15:16:49 :: NSE:SBIN :: 196.65 :: 43284705
tick :: 2020-10-26 15:16:50 :: NSE:SBIN :: 196.55 :: 43289608
tick :: 2020-10-26 15:16:53 :: NSE:SBIN :: 196.65 :: 43302304
tick :: 2020-10-26 15:16:58 :: NSE:SBIN :: 196.6 :: 43306025
tick :: 2020-10-26 15:16:59 :: NSE:SBIN :: 196.55 :: 43310465
tick :: 2020-10-26 15:17:00 :: NSE:SBIN :: 196.65 :: 43313272
tick :: 2020-10-26 15:17:04 :: NSE:SBIN :: 196.6 :: 43324023
tick :: 2020-10-26 15:17:05 :: NSE:SBIN :: 196.7 :: 43327660
tick :: 2020-10-26 15:17:06 :: NSE:SBIN :: 196.65 :: 43327834
tick :: 2020-10-26 15:17:07 :: NSE:SBIN :: 196.6 :: 43329298
tick :: 2020-10-26 15:17:09 :: NSE:SBIN :: 196.65 :: 43329746
tick :: 2020-10-26 15:17:10 :: NSE:SBIN :: 196.6 :: 43332710
tick :: 2020-10-26 15:17:12 :: NSE:SBIN :: 196.65 :: 43334585
tick :: 2020-10-26 15:17:15 :: NSE:SBIN :: 196.55 :: 43336631
tick :: 2020-10-26 15:17:16 :: NSE:SBIN :: 196.6 :: 43337236
tick :: 2020-10-26 15:17:18 :: NSE:SBIN :: 196.7 :: 43352825
tick :: 2020-10-26 15:17:20 :: NSE:SBIN :: 196.55 :: 43359141
tick :: 2020-10-26 15:17:22 :: NSE:SBIN :: 196.65 :: 43362175
tick :: 2020-10-26 15:17:23 :: NSE:SBIN :: 196.55 :: 43364062
tick :: 2020-10-26 15:17:25 :: NSE:SBIN :: 196.6 :: 43364601
tick :: 2020-10-26 15:17:27 :: NSE:SBIN :: 196.55 :: 43371410
tick :: 2020-10-26 15:17:28 :: NSE:SBIN :: 196.7 :: 43384268
tick :: 2020-10-26 15:17:30 :: NSE:SBIN :: 196.6 :: 43385580
tick :: 2020-10-26 15:17:32 :: NSE:SBIN :: 196.7 :: 43388415
tick :: 2020-10-26 15:17:33 :: NSE:SBIN :: 196.6 :: 43390152
tick :: 2020-10-26 15:17:35 :: NSE:SBIN :: 196.65 :: 43392443
tick :: 2020-10-26 15:17:36 :: NSE:SBIN :: 196.7 :: 43392859
tick :: 2020-10-26 15:17:37 :: NSE:SBIN :: 196.65 :: 43395097
tick :: 2020-10-26 15:17:39 :: NSE:SBIN :: 196.6 :: 43395708
tick :: 2020-10-26 15:17:40 :: NSE:SBIN :: 196.65 :: 43395846
tick :: 2020-10-26 15:17:43 :: NSE:SBIN :: 196.5 :: 43419135
tick :: 2020-10-26 15:17:46 :: NSE:SBIN :: 196.65 :: 43424576
tick :: 2020-10-26 15:17:47 :: NSE:SBIN :: 196.6 :: 43426038
tick :: 2020-10-26 15:17:52 :: NSE:SBIN :: 196.5 :: 43436492
tick :: 2020-10-26 15:17:53 :: NSE:SBIN :: 196.6 :: 43437217
tick :: 2020-10-26 15:17:55 :: NSE:SBIN :: 196.5 :: 43439284
tick :: 2020-10-26 15:17:56 :: NSE:SBIN :: 196.6 :: 43442305
tick :: 2020-10-26 15:17:57 :: NSE:SBIN :: 196.65 :: 43444488
tick :: 2020-10-26 15:17:58 :: NSE:SBIN :: 196.5 :: 43454282
tick :: 2020-10-26 15:17:59 :: NSE:SBIN :: 196.65 :: 43458267
tick :: 2020-10-26 15:18:01 :: NSE:SBIN :: 196.5 :: 43461217
closed connection

I did add this https://github.com/rushic24/tradingview-scraper/blob/master/livestreamtest.py but the data is getting repeated sometimes, not sure why.

No issues it is bound to happen, it is not perfect. Those errors are expected. scraping is never perfect.

na81cho commented 3 years ago

hola con este código se puede sacar el histórico del RSI ?

0xrushi commented 3 years ago

I'm closing this, I've added the details of discussion here