forked from Hattorius/Tradingview-ticker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathticker.py
179 lines (156 loc) · 6.58 KB
/
ticker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import asyncio, websockets, random, json, threading, time, sqlite3
from datetime import datetime
def createRandomToken(length=12):
chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
return ''.join(random.choice(chars) for i in range(length))
def getEpoch():
return int(time.time())
class ticker:
def __init__(self, symbols='BINANCE:BTCUSDT', save=False, database_name='database.db', split_symbols=False, verbose=False):
self.loop = asyncio.get_event_loop()
if isinstance(symbols, str):
symbols = [symbols]
self.symbols = symbols
self.states = {}
for symbol in symbols:
self.states[symbol] = {'volume': 0, 'price': 0, 'change': 0, 'changePercentage': 0}
self.save = save
self.connected = False
self.database_name = database_name
self.split_symbols = split_symbols
self.verbose = verbose
if verbose:
self.saves = 0
# Connect to database
async def connectToDatabase(self):
if self.save:
self.db = sqlite3.connect(self.database_name)
self.createSqlite3Table()
self.connected = True
# Create Sqlite3 table
def createSqlite3Table(self):
if self.split_symbols:
for symbol in self.symbols:
self.db.execute("""CREATE TABLE IF NOT EXISTS '{}' (
volume real NOT NULL,
price real NOT NULL,
timestamp integer NOT NULL
)""".format(symbol))
else:
self.db.execute("""CREATE TABLE IF NOT EXISTS ticker_data (
volume real NOT NULL,
price real NOT NULL,
ticker text NOT NULL,
timestamp integer NOT NULL
)""")
# Insert data into table
def insertData(self, volume, price, ticker):
if self.save:
if self.split_symbols:
self.db.execute("INSERT INTO '"+ticker+"' VALUES (?, ?, ?)", (volume, price, getEpoch()))
else:
self.db.execute("INSERT INTO ticker_data VALUES (?, ?, ?, ?)", (volume, price, ticker, getEpoch()))
self.db.commit()
# Connect to websocket
async def connect(self):
self.connection = await websockets.connect("wss://data.tradingview.com/socket.io/websocket", origin="https://www.tradingview.com")
await self.waitForMessages()
await asyncio.Future()
# Loop waiting for messages
async def waitForMessages(self):
await self.authenticate()
if self.save and not self.connected:
await self.connectToDatabase()
while True:
messages = await self.readMessage(await self.connection.recv())
for message in messages:
self.parseMessage(message)
# Convert message string to object
async def readMessage(self, message):
messages = message.split('~m~')
messagesObj = []
for message in messages:
if '{' in message or '[' in message:
messagesObj.append(json.loads(message))
else:
if '~h~' in message:
await self.connection.send("~m~{}~m~{}".format(len(message), message))
return messagesObj
# Convert object to message string
def createMessage(self, name, params):
message = json.dumps({'m': name, 'p': params})
return "~m~{}~m~{}".format(len(message), message)
# Send message
async def sendMessage(self, name, params):
message = self.createMessage(name, params)
await self.connection.send(message)
# Send authentication messages and subscribe to price & bars
async def authenticate(self):
self.cs = 'cs_' + createRandomToken()
await self.sendMessage("set_auth_token", ["unauthorized_user_token"])
await self.sendMessage("chart_create_session", [self.cs, ""])
for symbol in self.symbols:
qs = 'qs_' + createRandomToken()
await self.sendMessage("quote_create_session", [qs])
await self.sendMessage("quote_set_fields", [qs, "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","rchp","rtc"])
await self.sendMessage("quote_add_symbols",[qs, symbol, {"flags":['force_permission']}])
await self.sendMessage("quote_fast_symbols", [qs, symbol])
return 0
# this is so fricking messy
def parseMessage(self, message):
if message is None: return
try:
message['m']
except KeyError:
return None
if message['m'] == 'qsd':
self.forTicker(message)
# parse ticker data
def forTicker(self, receiveddata):
symbol = receiveddata['p'][1]['n']
data = receiveddata['p'][1]['v']
try:
self.states[symbol]['volume'] = data['volume']
except KeyError:
self.states[symbol]['volume'] += 0
try:
self.states[symbol]['price'] = data['lp']
except KeyError:
self.states[symbol]['price'] += 0
try:
self.states[symbol]['changePercentage'] = data['chp']
except KeyError:
self.states[symbol]['changePercentage'] += 0
try:
self.states[symbol]['change'] = data['ch']
except KeyError:
self.states[symbol]['change'] += 0
self.insertData(self.states[symbol]['volume'], self.states[symbol]['price'], symbol)
if self.verbose:
self.saves += 1
# send status messages every 5 seconds if enabled
async def giveAnUpdate(self):
while True:
await asyncio.sleep(5)
print("{}: Watching {} tickers → received {} updates".format(datetime.now().strftime("%d/%m/%Y %H:%M:%S"), len(self.symbols), self.saves))
self.saves = 0
# start ticker in new thread
def start(self):
self.loop = asyncio.new_event_loop()
def _start(loop):
asyncio.set_event_loop(loop)
self.task = loop.create_task(self.connect())
if self.verbose:
self.updateTask = loop.create_task(self.giveAnUpdate())
loop.run_forever()
t = threading.Thread(target=_start, args=(self.loop,))
t.start()
self.thread = t
# stop it :(
def stop(self):
self.task.cancel()
if self.verbose:
self.updateTask.cancel()
self.loop.stop()
self.thread.join()
self.db.close()