-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsample_test.py
277 lines (236 loc) · 9.98 KB
/
sample_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
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
# -*- coding: utf-8 -*-
import json
from bitoproClient.bitopro_util import get_current_timestamp
from bitoproClient.bitopro_restful_client import BitoproRestfulClient, CandlestickResolution, OrderStatus, WithdrawProtocol
import bitoproClient.bitopro_websocket_client as bitopro_ws
account = ""
apiKey = ""
apiSecret = ""
def bitopro_restful_test():
bitopro_client = BitoproRestfulClient(apiKey, apiSecret)
'''
Open restful test
'''
# [GET] list of currencies
response = bitopro_client.get_currencies()
print("List of currencies: ", response)
# [GET] limitations and fees
response = bitopro_client.get_limitations_and_fees()
print("List limitations and fees: ", response)
# [GET] order book
pair = "BTC_USDT"
response = bitopro_client.get_order_book(pair)
print("Order book: ", response)
# [GET] tickers
response = bitopro_client.get_tickers(pair)
print("Tickers: ", response)
# [GET] trades
response = bitopro_client.get_trades(pair)
print("Trades: ", response)
# [GET] candlestick
response = bitopro_client.get_candlestick(pair, CandlestickResolution._1d, 1650707415, 1678355415)
print("Candlestick: ", response)
# [GET] trading pairs
response = bitopro_client.get_trading_pairs()
print("Trading pairs: ", response)
'''
Auth restful test
'''
# [GET] account balance
response = bitopro_client.get_account_balance()
print("Account balance:", response)
pair = "BTC_USDT"
order_id = ""
# [POST] Create a limit order
open_list = []
for i in range(5):
create_order_response = bitopro_client.create_an_order(action="BUY", amount=0.0001, price=30500, pair=pair, client_id=223445)
if create_order_response is not None:
open_list.append(create_order_response["orderId"])
print("Limit order created:", create_order_response)
# [Get] get all open orders
open_orders_response = bitopro_client.get_open_orders(pair)
if open_orders_response is not None:
print("open orders:", open_orders_response)
# [POST] Create a market order
# create_order_response = bitopro_client.create_an_order(action="BUY", amount=34, pair="BTC_USDT", type=OrderType.Market)
# if create_order_response is not None:
# order_id = create_order_response["orderId"]
# print("Market order created:", create_order_response)
# [GET] Get an order
for open_order in open_list:
get_an_order_response = bitopro_client.get_an_order(pair, open_order)
print("Get an order:", get_an_order_response)
# [DELETE] Cancel the order
for open_order in open_list:
cancel_order_response = bitopro_client.cancel_an_order(open_order, pair=pair)
print("Order cancelled:", cancel_order_response)
# [GET] get all orders
response = bitopro_client.get_all_orders(pair, start_timestamp=1679299428000, status=OrderStatus.Completed)
print("All orders:", response)
# [GET] get all trades
response = bitopro_client.get_trades_list(pair)
print("All trades:", response)
# [POST] Create batch order, create 10 orders at once
pair = "BTC_USDT"
batch_orders:dict = {}
batch_orders[pair] = []
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
})
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
})
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
})
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
})
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
})
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
})
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
})
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
})
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
})
batch_orders[pair].append({
**{"pair": pair},
**{"action": "BUY"},
**{"amount": str(0.0001)},
**({"price": str(10500)}),
**{"timestamp": get_current_timestamp()},
**{"type": "LIMIT"},
**{"clientId": 95279527}
})
create_batch_orders_response = bitopro_client.create_batch_order(batch_orders[pair])
reply = None
if create_batch_orders_response is not None:
batch_orders[pair] = []
reply = create_batch_orders_response["data"]
for order in reply:
batch_orders[pair].append(order["orderId"])
print("Batch orders created:", reply)
# [PUT] Cancel multiple orders
cancel_multiple_orders_response = bitopro_client.cancel_multiple_orders(batch_orders)
print("Cancel multiple orders:", cancel_multiple_orders_response)
# [DELETE] Cancel all order
cancel_all_orders_response = bitopro_client.cancel_all_orders("all")
print("Cancel all order:", cancel_all_orders_response)
# [GET] Deposit history
deposit_history_response = bitopro_client.get_deposit_history("USDT")
print("Deposit history:", deposit_history_response)
# [GET] Withdraw history
withdraw_history_response = bitopro_client.get_withdraw_history("BTC")
print("Withdraw history:", withdraw_history_response)
# [GET] GET Withdraw
get_withdraw_response = bitopro_client.get_withdraw("USDT", "")
print("GET withdraw:", get_withdraw_response)
# [POST] Withdraw
get_withdraw_response = bitopro_client.withdraw("USDT", WithdrawProtocol.ERC20, "", 50.5, "")
print("GET withdraw:", get_withdraw_response)
def bitopro_websocket_test():
symbols_list = {"eth_btc": 5, "BTC_TWD": 1, "ETH_TWD": 20, "BITO_ETH": 1}
symbols = ["BTC_TWD", "ETH_TWD", "BITO_ETH"]
# [PUBLIC] GET Order book
bitoproWs_order_book = bitopro_ws.BitoproOrderBookWs(symbols_list, websocket_handler)
bitoproWs_order_book.init_websocket()
bitoproWs_order_book.start()
# [PUBLIC] GET Ticket
bito_websocket_ticker = bitopro_ws.BitoproTickerkWs(symbols, websocket_handler)
bito_websocket_ticker.init_websocket()
bito_websocket_ticker.start()
# [PUBLIC] GET Trade
bito_websocket_trades = bitopro_ws.BitoproTradesWs(symbols, websocket_handler)
bito_websocket_trades.init_websocket()
bito_websocket_trades.start()
# [Private] GET active orders
bito_websocket_user_order = bitopro_ws.BitoproUserOrdersWs(account, apiKey, apiSecret, websocket_handler)
bito_websocket_user_order.init_websocket()
bito_websocket_user_order.start()
# [Private] GET account balance
bito_websocket_user_balance = bitopro_ws.BitoproUserBlanceWs(account, apiKey, apiSecret, websocket_handler)
bito_websocket_user_balance.init_websocket()
bito_websocket_user_balance.start()
# [Private] GET user trade
bito_websocket_user_trade = bitopro_ws.BitoproUserTradeWs(account, apiKey, apiSecret, websocket_handler)
bito_websocket_user_trade.init_websocket()
bito_websocket_user_trade.start()
# [Private] GET history orders
bito_websocket_history_orders = bitopro_ws.BitoproHistoryOrders(account, apiKey, apiSecret, websocket_handler)
bito_websocket_history_orders.init_websocket()
bito_websocket_history_orders.start()
def websocket_handler(message:str):
reply = json.loads(message)
if reply["event"] == "ACCOUNT_BALANCE":
print("ACCOUNT_BALANCE: ", reply, end="\n\n")
elif reply["event"] == "ACTIVE_ORDERS":
print("ACTIVE_ORDERS: ", reply, end="\n\n")
elif reply["event"] == "ORDER_BOOK":
print("ORDER_BOOK: ", reply, end="\n\n")
elif reply["event"] == "TICKER":
print("TICKER: ", reply, end="\n\n")
elif reply["event"] == "TRADE":
print("TRADE: ", reply, end="\n\n")
elif reply["event"] == "USER_TRADE":
print("User trade: ", reply, end="\n\n")
elif reply["event"] == "RECENT_HISTORY_ORDERS":
print("History orders: ", reply, end="\n\n")
else:
print("Else: ", reply, end="\n\n")
if __name__ == "__main__":
bitopro_restful_test()
bitopro_websocket_test()