forked from robswc/tradingview-webhooks-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor_orderbook.py
102 lines (85 loc) · 2.77 KB
/
monitor_orderbook.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
import subprocess
from ftx import ThreadedWebsocketManager
import api_keys
import subprocess
import generic_functions as func
from decimal import Decimal
import time
import os
import requests
from order_book import OrderBook
API = api_keys.FTX_KEY
SECRET = api_keys.FTX_SECRET
symbol = 'DOGE-PERP'
# orderbook = {}
def test_orderbook_init():
global ob
ob = OrderBook(max_depth=10)
#ob.bids = {{val: val for val in range(20)}}
#ob.asks = {val: val for val in range(10, 30)}
ob.bids = {}
ob.asks = {}
# assert len(ob) == 20
def on_OBupdate(payload):
global ob
if payload['channel'] == 'orderbook':
if payload['type'] == 'update':
for i in payload['data']['bids']:
if i[1] == 0:
# print(i[0],"bid removed")
try:
del ob.bids[Decimal(i[0])]
except:
pass
else:
# print(i[0],"bid added")
ob.bids[Decimal(i[0])] = Decimal(i[1])
for i in payload['data']['asks']:
if i[1] == 0:
# print(i[0],"ask removed")
try:
del ob.asks[Decimal(i[0])]
except:
pass
else:
# print(i[0],"ask added")
ob.asks[Decimal(i[0])] = Decimal(i[1])
def getTopOrders(ob):
# convert orderbook to dict
topbid = None
topask = None
def convert(x):
if isinstance(x, Decimal):
return float(x)
if isinstance(x, dict):
return {k: float(v) for k, v in x.items()}
ob_dict = ob.to_dict(to_type=convert)
try:
if topbid != float(list(ob_dict['bid'].keys())[0]):
topbid=float(list(ob_dict['bid'].keys())[0])
# print("First Bid ", topbid)
if topask != float(list(ob_dict['ask'].keys())[0]):
topask=float(list(ob_dict['ask'].keys())[0])
# print("First Ask ", topask)
except:
pass
return ob_dict, topbid, topask
def subscribe_book(symbol):
wsm = ThreadedWebsocketManager(API, SECRET)
wsm.start()
name = 'market_connection'
wsm.start_socket(on_OBupdate, socket_name=name)
wsm.subscribe(name, channel="orderbook", op="subscribe", market=symbol)
if __name__ == '__main__':
subprocess.call('clear', shell=True)
try:
test_orderbook_init()
subscribe_book(symbol)
while True:
fullob, topbid, topask = getTopOrders(ob)
print("First Bid ", topbid)
print("First Ask ", topask)
time.sleep(.25)
except KeyboardInterrupt:
print('Interrupted')
os._exit(0)