-
Notifications
You must be signed in to change notification settings - Fork 0
/
input_cmd_validator.py
310 lines (249 loc) · 13.9 KB
/
input_cmd_validator.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
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
from common import *
class InputValidator():
def __init__(self, logger):
self.logger = logger
def validate_new_cmd(self, command):
try:
self.logger.info(compose_log_msg(getframeinfo(currentframe()),
f"New Cmd arrived: {command}"))
cmd_type = command.get("type")
if cmd_type is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"Cmd without type arrived: {command}"))
return False
if cmd_type == "SWITCH PRICE SOURCE":
return True
if cmd_type == "PRICE ADJUST":
order_id = command.get("order_id")
if order_id is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"Price adjust: order id is None: {command}"))
return False
account_type = command.get("account_type")
if account_type is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"Price adjust: account type is None: {command}"))
return False
price = command.get("price")
if price is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"Price adjust: new price was not provided {command}"))
return False
return True
if cmd_type == "NEW ORDER":
return self.validate_new_order_cmd(command)
if cmd_type == "CANCEL":
return self.validate_cancel_order_cmd(command)
if cmd_type == "GET_INFO":
section = command.get("section")
if section is None:
return False
if section == "BALANCE":
asset = command.get("asset")
if asset is None:
return False
return True
except Exception as e:
handle_exception(self.logger, e)
def validate_combined_order_cmd(self, command):
try:
required_order_params = ["close_order_params"]
order_type = "COMBINED"
check_result = self.check_required_params_present(required_order_params, command, order_type)
if not check_result:
return False
open_order_params = command.get("open_order_params", {})
open_order_type = open_order_params.get("order_type")
if open_order_type is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd with open params but without open_order type: {command}"))
return False
close_order_params = command.get("close_order_params")
if close_order_params is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without close order params: {command}"))
return False
close_order_type = close_order_params.get("order_type")
if close_order_type is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without close order type: {command}"))
return False
input_type = close_order_params.get("input_type")
if input_type is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without close order input_type param: {command}"))
return False
if close_order_type == "TRAILING_STOP":
if close_order_params.get("stop_conditions") is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} stop TRAILING order cmd without close order stop_conditions param: {command}"))
return False
if close_order_type == "INTELLECTUAL":
params = ["stop_conditions", "take_conditions"]
for param in params:
if close_order_params.get(param) is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd CLOSE INTELLECTUAL without close order {param} param: {command}"))
return False
if not isinstance(close_order_params.get(param), dict):
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd CLOSE INTELLECTUAL {param} param should be a dict: {command}"))
return False
return True
except Exception as e:
handle_exception(self.logger, e)
def validate_trailing_stop_order_cmd(self, command):
try:
order_type = "TRAILING_STOP"
cmd_side = command.get("side").upper()
required_order_params = ["stop_price_delta", "position_entrance_price", "take_price",
"stop_price_delta_after_take", "price_min_step"]
check_result = self.check_required_params_present(required_order_params, command, order_type)
if not check_result:
return False
stop_price_delta = command.get("stop_price_delta")
position_entrance_price = command.get("position_entrance_price")
take_price = command.get("take_price")
stop_price_delta_after_take = command.get("stop_price_delta_after_take")
if position_entrance_price >= take_price and cmd_side == "SELL":
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} {cmd_side} order cmd with stop position_entrance_price price >= take_price price arrived: {command}"))
return False
if position_entrance_price <= take_price and cmd_side == "BUY":
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} {cmd_side} order cmd with stop position_entrance_price price <= take_price price arrived: {command}"))
return False
if stop_price_delta < stop_price_delta_after_take:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} {cmd_side} order cmd with stop_price_delta > stop_price_delta_after_take arrived: {command}"))
return False
return True
except Exception as e:
handle_exception(self.logger, e)
def validate_oco_order_cmd(self, command):
try:
order_type = "OCO"
cmd_price = command.get("limit_price")
if cmd_price is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without price arrived: {command}"))
return False
cmd_price = command.get("trigger_price")
if cmd_price is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without price arrived: {command}"))
return False
return True
except Exception as e:
handle_exception(self.logger, e)
def validate_stop_market_order_cmd(self, command):
try:
order_type = "STOP_MARKET"
cmd_price = command.get("trigger_price")
if cmd_price is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without price arrived: {command}"))
return False
return True
except Exception as e:
handle_exception(self.logger, e)
def validate_stop_limit_order_cmd(self, command):
try:
order_type = "STOP_LIMIT"
trigger_price = command.get("trigger_price")
action_price = command.get("action_price")
if trigger_price is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without trigger price arrived: {command}"))
return False
if action_price is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without action price arrived: {command}"))
return False
if command.get("side").upper() == "SELL":
if action_price > trigger_price:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} side SELL order has action price > trigger_price arrived: {command}"))
return False
else:
if action_price < trigger_price:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} side BUY order has action price < trigger_price arrived: {command}"))
return False
return True
except Exception as e:
handle_exception(self.logger, e)
def validate_limit_order_cmd(self, command):
try:
order_type = "LIMIT"
cmd_price = command.get("action_price")
if cmd_price is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without price arrived: {command}"))
return False
return True
except Exception as e:
handle_exception(self.logger, e)
def validate_new_order_cmd(self, command):
try:
order_type = command.get("order_type")
if order_type is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New order cmd without order type arrived: {command}"))
return False
symbol = command.get("symbol")
if symbol is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New order cmd without symbol arrived: {command}"))
return False
cmd_side = command.get("side")
if cmd_side is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New order cmd without order side arrived: {command}"))
return False
cmd_account = command.get("account_type")
if cmd_account is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New order cmd without account specified arrived: {command}"))
return False
cmd_base = command.get("base")
cmd_quote = command.get("quote")
if cmd_base is None and cmd_quote is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New order cmd without neither quote nor base asset amount arrived: {command}"))
return False
is_repay = command.get("is_repay")
if cmd_account != "SPOT" and is_repay is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New order cmd without is_repay arrived: {command}"))
return False
return True
except Exception as e:
handle_exception(self.logger, e)
def validate_cancel_order_cmd(self, command):
try:
order_id = command.get("order_id")
if order_id is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"Cancel order cmd: without order id specified: {command}"))
return False
account_type = command.get("account_type")
if account_type is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"Cancel order cmd: without account_type specified: {command}"))
return False
return True
except Exception as e:
handle_exception(self.logger, e)
def check_required_params_present(self, required_params, command, order_type):
try:
for param in required_params:
param_value = command.get(param)
if param_value is None:
self.logger.error(compose_log_msg(getframeinfo(currentframe()),
f"New {order_type} order cmd without {param} arrived: {command}"))
return None
return True
except Exception as e:
handle_exception(self.logger, e)
return False