-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_bybit.mojo
464 lines (346 loc) · 12.9 KB
/
test_bybit.mojo
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
import time
from time import time_function
from memory import unsafe
import sys
from base.mo import *
from base.c import *
from base.thread import *
from base.sj_dom import *
from base.sj_ondemand import OndemandParser
from base.fixed import Fixed
from base.httpclient import HttpClient, VERB_GET, Headers, QueryParams
from base.websocket import *
from ylstdlib.time import time_ns
from core.okxconsts import *
from core.okxclient import *
from testing import assert_equal, assert_true, assert_false
from base.moutil import *
from core.bybitmodel import *
from core.sign import hmac_sha256_b64, hmac_sha256_hex
from base.yyjson import *
from core.bybitclient import *
from core.bybitclientjson import *
from core.bybitws import *
from core.env import env_load
from trade.config import load_config
# pip install fastapi uvicorn[standard]
fn test_httpclient_perf():
# https://api.bybit.com/v3/public/time
var base_url = "https://api.bybit.com"
var path = "/v3/public/time"
# var base_url = "https://pss.bdstatic.com"
# var path = "/static/superman/js/super_ext-a0b60bd05d.js"
var client = HttpClient(base_url)
# var headers = SSMap()
var headers = Headers()
headers["a"] = "aaaaaaaaaaaaaaaa"
headers["b"] = "aaaaaaaaaaaaaaaa"
var res = client.get(path, headers)
logd("status_code: " + str(res.status_code))
logd("text: " + res.text)
fn test_websocket() raises:
# https://socketsbay.com/test-websockets
# var base_url = "wss://socketsbay.com/wss/v2/1/demo/"
# var host = "socketsbay.com"
# var port = "443"
# var path = "/wss/v2/1/demo/"
# wss://echo.websocket.org
# var host = "echo.websocket.org"
# var port = "443"
# var path = "/"
logd("test_websocket")
var testnet = False
var private = False
var category = "linear"
var host = "stream-testnet.bybit.com" if testnet else "stream.bybit.com"
var port = "443"
var path = "/v5/private" if private else "/v5/public/" + category
var ws = WebSocket(host=host, port=port, path=path)
var id = ws.get_id()
var on_connect = ws.get_on_connect()
var on_heartbeat = ws.get_on_heartbeat()
var on_message = ws.get_on_message()
set_on_connect(id, on_connect^)
set_on_heartbeat(id, on_heartbeat^)
set_on_message(id, on_message^)
ws.connect()
logi("connect done")
run_forever()
_ = ws^
fn get_on_message() -> on_message_callback:
fn wrapper(msg: String):
logi("get_on_message=" + msg)
return wrapper
fn test_bybitws() raises:
logd("test_bybitws")
var ws = BybitWS(
is_private=False,
testnet=False,
access_key="",
secret_key="",
category="linear",
topics="orderbook.1.BTCUSDT",
)
var id = ws.get_id()
var on_connect = ws.get_on_connect()
var on_heartbeat = ws.get_on_heartbeat()
# var on_message = get_on_message()
var on_message = ws.get_on_message()
set_on_connect(id, on_connect)
# set_on_heartbeat(id, on_heartbeat^)
# set_on_message(id, on_message^)
## var topics = List[String]()
## topics.append("orderbook.1.BTCUSDT")
## ws.set_subscription(topics)
# ws.connect()
logi("connect done")
# run_forever()
# perform_ioc_poll()
_ = ws^
fn sum_int_list(v: List[Int]) raises -> Int:
var result: Int = 0
for item in v:
result += item[]
return result
fn test_bybitclient() raises:
var app_config = load_config("config.toml")
var access_key = app_config.access_key
var secret_key = app_config.secret_key
var testnet = app_config.testnet
var client = BybitClient(
testnet=testnet, access_key=access_key, secret_key=secret_key
)
client.set_verbose(True)
# Preparation phase
var server_time = client.fetch_public_time()
logi(str(server_time))
# _ = seq_photon_thread_sleep_ms(200)
var category = "linear"
# var symbol = "BTCUSDT"
var symbol = "XRPUSDT"
var exchange_info = client.fetch_exchange_info(category, symbol)
logi(str(exchange_info))
# <ExchangeInfo: symbol=BTCUSDT, tick_size=0.10000000000000001, step_size=0.001>
var kline = client.fetch_kline(
category, symbol, interval="1", limit=5, start=0, end=0
)
for item in kline:
logi(str(item[]))
# test_orderbook_parse_body()
var ob = client.fetch_orderbook(category, symbol, 5)
logi("-----asks-----")
for item in ob.asks:
logi(str(item[]))
logi("-----bids-----")
for item in ob.bids:
logi(str(item[]))
var switch_position_mode_res = client.switch_position_mode(
category, symbol, "3"
)
logi("res=" + str(switch_position_mode_res))
# retCode=1, retMsg=Open orders exist, so you cannot change position mode
# var set_leverage_res = client.set_leverage(category, symbol, "10", "10")
# logi("res=" + str(set_leverage_res))
# var side = "Buy"
# var order_type = "Limit"
# var qty = "0.001"
# var price = "3000"
# var place_order_res = client.place_order(category, symbol, side, order_type, qty, price, position_idx=1)
# logi("res=" + str(place_order_res))
# # retCode=1, retMsg=params error: The number of contracts exceeds minimum limit allowed
# var cancel_order_res = client.cancel_order(category, symbol, "4d822437-a502-49d6-8aa7-55a602920b3f")
# logi("res=" + str(cancel_order_res))
# var cancel_orders_res = client.cancel_orders(category, symbol)
# for item in cancel_orders_res:
# logi("item=" + str(item))
# var fetch_balance_res = client.fetch_balance("CONTRACT", "USDT")
# for item in fetch_balance_res:
# logi("item=" + str(item))
# var fetch_orders_res = client.fetch_orders(category, symbol)
# for item in fetch_orders_res:
# logi("item=" + str(item))
# var fetch_positions_res = client.fetch_positions(category, symbol)
# for item in fetch_positions_res:
# logi("item=" + str(item))
# <PositionInfo: symbol=BTCUSDT, position_idx=1, side=Buy, size=0.015, avg_price=40869.30666667, position_value=613.0396, leverage=1.0, mark_price=42191.30, position_mm=0.0000075, position_im=6.130396, take_profit=0.00, stop_loss=0.00, unrealised_pnl=19.8299, cum_realised_pnl=838.09142572, created_time=1682125794703, updated_time=1706790560723>
# Close position
# var side = "Buy"
# var order_type = "Limit"
# var qty = "0.001"
# var price = "3000"
# var res = client.place_order(category, symbol, "Sell", "Market", qty, "", position_idx=1)
# logi("res=" + str(res))
logi("Done!!!")
run_forever()
_ = client^
fn test_ob_price() raises:
# [2024-04-01 16:38:21.662] [debug] res.status_code=200 text={"retCode":0,"retMsg":"OK","result":{"s":"BTCUSDT","b":[["69445.9","49.567"],["69445.7","87.003"],["69445.6","38.998"],["69445.5","57.319"],["69445.3","96.109"]],"a":[["69448.3","106.435"],["69448.5","93.792"],["69448.7","81.649"],["69448.9","75.391"],["69449.1","40.254"]],"ts":1711960701415,"u":1603656,"seq":9092034245},"retExtInfo":{},"time":1711960701661}
# [2024-04-01 16:38:21.662] [info] ask.value=69448300000000000 bid.value=69445899999999992
# [2024-04-01 16:38:21.662] [info] ask=69448.3 bid=69445.899999999992
# [2024-04-01 16:38:21.662] [info] mid=69447.099999999996
var app_config = load_config("config.toml")
var access_key = app_config.access_key
var secret_key = app_config.secret_key
var testnet = app_config.testnet
var client = BybitClient(
testnet=testnet, access_key=access_key, secret_key=secret_key
)
client.set_verbose(True)
var category = "linear"
var symbol = "BTCUSDT"
# var symbol = "XRPUSDT"
var text = String(
'{"retCode":0,"retMsg":"OK","result":{"s":"BTCUSDT","b":[["69445.9","49.567"],["69445.7","87.003"],["69445.6","38.998"],["69445.5","57.319"],["69445.3","96.109"]],"a":[["69448.3","106.435"],["69448.5","93.792"],["69448.7","81.649"],["69448.9","75.391"],["69449.1","40.254"]],"ts":1711960701415,"u":1603656,"seq":9092034245},"retExtInfo":{},"time":1711960701661}'
)
var asks = List[OrderBookItem]()
var bids = List[OrderBookItem]()
var parser = DomParser(1024 * 100)
var doc = parser.parse(text)
var ret_code = doc.get_int("retCode")
var ret_msg = doc.get_str("retMsg")
if ret_code != 0:
raise Error("error retCode=" + str(ret_code) + ", retMsg=" + ret_msg)
var result = doc.get_object("result")
var a_list = result.get_array("a")
var list_iter_a = a_list.iter()
while list_iter_a.has_element():
var obj = list_iter_a.get()
var i_arr_list = obj.array()
var price = i_arr_list.at_str(0)
var qty = i_arr_list.at_str(1)
asks.append(OrderBookItem(price, qty))
_ = obj^
list_iter_a.step()
_ = a_list^
var b_list = result.get_array("b")
var list_iter_b = b_list.iter()
while list_iter_b.has_element():
var obj = list_iter_b.get()
var i_arr_list = obj.array()
var price = i_arr_list.at_str(0)
var qty = i_arr_list.at_str(1)
bids.append(OrderBookItem(price, qty))
list_iter_b.step()
_ = b_list^
_ = result^
_ = doc^
_ = parser^
_ = text^
var ob = OrderBook(asks, bids)
# var ob = client.fetch_orderbook(category, symbol, 5)
logi("ob=" + str(ob))
var ask = ob.asks[0].price
var bid = ob.bids[0].price
logi("ask.value=" + str(ask.value()) + " bid.value=" + str(bid.value()))
logi("ask=" + str(ask) + " bid=" + str(bid))
var mid = (ask / Fixed(2)) + (bid / Fixed(2))
logi("mid=" + str(mid))
logi("Done!!!")
run_forever()
_ = client^
fn test_bybit_perf() raises:
var app_config = load_config("config.toml")
var access_key = app_config.access_key
var secret_key = app_config.secret_key
var testnet = app_config.testnet
var client = BybitClient(
testnet=testnet, access_key=access_key, secret_key=secret_key
)
client.set_verbose(True)
# Preparation phase
var server_time = client.fetch_public_time()
logi(str(server_time))
_ = seq_photon_thread_sleep_ms(200)
var category = "linear"
var symbol = "BTCUSDT"
var side = "Buy"
var order_type = "Limit"
var qty = "0.001"
var price = "10000"
# Test order placement speed
var times = 30
var order_times = List[
Int
]() # Record the time taken for each order placement
var cancel_times = List[
Int
]() # Record the time taken for each order cancellation
var start_time = time_us()
for i in range(times):
# logi("i=" + str(i))
var order_start = time_us()
var res = client.place_order(
category, symbol, side, order_type, qty, price, position_idx=1
)
var order_end = time_us()
logi(
str(i)
+ ":Place order returns="
+ str(res)
+ " Time consumption: "
+ str(order_end - order_start)
+ " us"
)
var order_id = res.order_id
var cancel_start = time_us()
var res1 = client.cancel_order(category, symbol, order_id)
var cancel_end = time_us()
logi(
str(i)
+ ":Cancel order returns="
+ str(res1)
+ " Time consumption: "
+ str(cancel_end - cancel_start)
+ " us"
)
order_times.append(int(order_end - order_start))
cancel_times.append(int(cancel_end - cancel_start))
_ = seq_photon_thread_sleep_ms(500)
var end_time = time_us()
var total_time = end_time - start_time
logi("Total time consumed:" + str(total_time) + " us")
logi(
"Average time taken for order placement:"
+ str(sum_int_list(order_times) / len(order_times))
+ " us"
)
logi(
"Average time taken for order cancellation:"
+ str(sum_int_list(cancel_times) / len(cancel_times))
+ " us"
)
logi("Done!!!")
run_forever()
_ = client^
fn run_forever():
seq_photon_join_current_vcpu_into_workpool(seq_photon_work_pool())
fn handle_term(sig: c_int) raises -> None:
print("handle_term")
sys.exit()
fn photon_handle_term(sig: c_int) raises -> None:
print("photon_handle_term")
sys.exit()
fn perform_ioc_poll() -> None:
var ioc = seq_asio_ioc()
while True:
seq_asio_ioc_poll(ioc)
sleep(0.001)
fn main() raises:
_ = seq_ct_init()
var ret = seq_photon_init_default()
seq_init_photon_work_pool(2)
seq_init_log(LOG_LEVEL_DBG, "")
# seq_init_log(LOG_LEVEL_OFF, "")
# seq_init_net(0)
# seq_init_net(1)
logi("Initialization return result: " + str(ret))
seq_init_signal(handle_term)
seq_init_photon_signal(photon_handle_term)
# while True:
# test_httpclient_perf()
# test_websocket()
# test_bybitclient()
test_bybitws()
# test_ob_price()
# logi("The program is prepared and ready, awaiting events...")
# run_forever()