-
Notifications
You must be signed in to change notification settings - Fork 0
/
bithumb_ws_test.py
executable file
·149 lines (113 loc) · 2.93 KB
/
bithumb_ws_test.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
from decimal import Decimal
import exchanges
import json
import market
import traceback
import websocket
url = 'wss://wss.bithumb.com/public'
if __name__ == '__main__':
market_ = market.BasicMarket('ETH/KRW', ('ETH', 'KRW'), 'ETH', 'Bithumb', Decimal('100'), Decimal('0.00000001'))
i = 0
currencies = ['BTC', 'ETH', 'LTC', 'XMR', 'DASH']
def o(ws):
pass
def m(ws, msg_str):
msg = json.loads(msg_str)
if msg['header']['service'] == 'orderbook':
print(msg['header']['currency'])
print(msg['data'], end='\n\n')
global i
send_data = json.dumps({
'currency': currencies[i % len(currencies)],
'service': 'orderbook'
})
ws.send(send_data)
i += 1
def c(ws):
print("Closing")
def e(ws, error):
print(error)
traceback.print_tb(error.__traceback__)
header = ['Origin: https://www.bithumb.com']
#websocket.enableTrace(True)
wsapp = websocket.WebSocketApp(
url,
on_open=o,
on_message=m,
on_close=c,
on_error=e,
header=header
)
try:
wsapp.run_forever()
except KeyboardInterrupt:
pass
print("Done")
class Level2BithumbClient(object):
def __init__(self, pairs):
self.pairs = pairs
self.markets = []
self._wss = []
def start(self):
if self.is_running():
return
# I looked these tick sizes up myself because there's no way to grab
# them programmatically without a web crawler. Subject to change.
tick_sizes = {
BITCOIN: Decimal('1000'),
ETHEREUM: Decimal('100'),
DASH: Decimal('100'),
LITECOIN: Decimal('50'),
ETHEREUM_CLASSIC: Decimal('10'),
RIPPLE: Decimal('1'),
BCASH: Decimal('500'),
MONERO: Decimal('100'),
ZCASH: Decimal('100'),
QTUM: Decimal('10'),
BGOLD: Decimal('100'),
EOS: Decimal('1')
}
step_size = Decimal('0.00000001')
for pair in self.pairs:
name = '{}/{}'.format(*pair)
symbol = pair[0]
exchange = exchanges.BithumbExchange.instance()
tick_size = tick_sizes[symbol]
market_ = market.BasicMarket(name, pair, symbol, exchange, tick_size, step_size)
self.markets.append(market_)
ws_url = 'wss://wss.bithumb.com/public'
header = 'Origin: https://bithumb.com'
ws = websocket.create_connection(ws_url, header=header)
data = { 'currency': symbol, 'service': 'orderbook' }
msg = json.dumps(data).encode()
ws.send(msg)
header = ['Origin: https://www.bithumb.com']
for pair in self.pairs:
name = '{}/{}'.format()
def stop(self):
if not self.is_running():
return
print("Stopping Level2BithumbClient...")
for ws in self._wss:
ws.close()
def is_running(self):
pass
def _create_onopen(self, markets):
def _onopen(ws):
pass
return _onopen
def _create_onmessage(self, markets):
msg_num = 0
def _onmessage(ws, msg):
pass
return _onmessage
def _create_onerror(self, markets):
def _onerror(ws, error):
print(error)
traceback.print_tb(error.__traceback__)
ws.close()
return _onerror
def _create_onclose(self, markets):
def _onclose(ws):
pass
return _onclose