-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_bot_agent.py
221 lines (188 loc) · 7.26 KB
/
func_bot_agent.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
from func_private import place_market_order, check_order_status
from datetime import datetime, timedelta
#from func_messaging import send_message
import time
from pprint import pprint
# Class: Agent for managing opening and checking trades
class BotAgent:
"""
Primary function of BotAgent handles opening and checking order status
"""
# Initialize class
def __init__(
self,
client,
market_1,
market_2,
base_side,
base_size,
base_price,
quote_side,
quote_size,
quote_price,
accept_failsafe_base_price,
z_score,
half_life,
hedge_ratio,
):
# Initialize class variables
self.client = client
self.market_1 = market_1
self.market_2 = market_2
self.base_side = base_side
self.base_size = base_size
self.base_price = base_price
self.quote_side = quote_side
self.quote_size = quote_size
self.quote_price = quote_price
self.accept_failsafe_base_price = accept_failsafe_base_price
self.z_score = z_score
self.half_life = half_life
self.hedge_ratio = hedge_ratio
# Initialze output variable
# Pair status options are FAILED, LIVE, CLOSE, ERROR
self.order_dict = {
"market_1": market_1,
"market_2": market_2,
"hedge_ratio": hedge_ratio,
"z_score": z_score,
"half_life": half_life,
"order_id_m1": "",
"order_m1_size": base_size,
"order_m1_side": base_side,
"order_time_m1": "",
"order_id_m2": "",
"order_m2_size": quote_size,
"order_m2_side": quote_side,
"order_time_m2": "",
"pair_status": "",
"comments": "",
}
# Check order status by id
def check_order_status_by_id(self, order_id):
# Allow time to process
time.sleep(2)
# Check order status
order_status = check_order_status(self.client, order_id)
# Guard: If order cancelled move onto next Pair
if order_status == "CANCELED":
print(f"{self.market_1} vs {self.market_2} - Order cancelled...")
self.order_dict["pair_status"] = "FAILED"
return "failed"
# Guard: If order not filled wait until order expiration
if order_status != "FAILED":
time.sleep(15)
order_status = check_order_status(self.client, order_id)
# Guard: If order cancelled move onto next Pair
if order_status == "CANCELED":
print(f"{self.market_1} vs {self.market_2} - Order cancelled...")
self.order_dict["pair_status"] = "FAILED"
return "failed"
# Guard: If not filled, cancel order
if order_status != "FILLED":
self.client.private.cancel_order(order_id=order_id)
self.order_dict["pair_status"] = "ERROR"
print(f"{self.market_1} vs {self.market_2} - Order error...")
return "error"
# Return live
return "live"
# Open trades
def open_trades(self):
# Print status
print("---")
print(f"{self.market_1}: Placing first order...")
print(
f"Side: {self.base_side}, Size: {self.base_size}, Price: {self.base_price}"
)
print("---")
# Place Base Order
try:
base_order = place_market_order(
self.client,
market=self.market_1,
side=self.base_side,
size=self.base_size,
price=self.base_price,
reduce_only=False,
)
# Store the order id
self.order_dict["order_id_m1"] = base_order["order"]["id"]
self.order_dict["order_time_m1"] = datetime.now().isoformat()
except Exception as e:
self.order_dict["pair_status"] = "ERROR"
self.order_dict["comments"] = f"Market 1 {self.market_1}: , {e}"
return self.order_dict
# Ensure order is live before processing
order_status_m1 = self.check_order_status_by_id(self.order_dict["order_id_m1"])
# Guard: Aborder if order failed
if order_status_m1 != "live":
self.order_dict["pair_status"] = "ERROR"
self.order_dict["comments"] = f"{self.market_1} failed to fill"
return self.order_dict
# Print status - opening second order
print("---")
print(f"{self.market_2}: Placing second order...")
print(
f"Side: {self.quote_side}, Size: {self.quote_size}, Price: {self.quote_price}"
)
print("---")
# Place Quote Order
try:
quote_order = place_market_order(
self.client,
market=self.market_2,
side=self.quote_side,
size=self.quote_size,
price=self.quote_price,
reduce_only=False,
)
# Store the order id
self.order_dict["order_id_m2"] = quote_order["order"]["id"]
self.order_dict["order_time_m2"] = datetime.now().isoformat()
except Exception as e:
self.order_dict["pair_status"] = "ERROR"
self.order_dict["comments"] = f"Market 2 {self.market_2}: , {e}"
return self.order_dict
# Ensure order is live before processing
order_status_m2 = self.check_order_status_by_id(self.order_dict["order_id_m2"])
# Guard: Aborder if order failed
if order_status_m2 != "live":
self.order_dict["pair_status"] = "ERROR"
self.order_dict["comments"] = f"{self.market_1} failed to fill"
# Close order 1:
try:
close_order = place_market_order(
self.client,
market=self.market_1,
side=self.quote_side,
size=self.base_size,
price=self.accept_failsafe_base_price,
reduce_only=True,
)
# Ensure order is live before proceeding
time.sleep(2)
order_status_close_order = check_order_status(
self.client, close_order["order"]["id"]
)
if order_status_close_order != "FILLED":
print("ABORT PROGRAM")
print("Unexpected Error")
print(order_status_close_order)
# Send Message
#send_message("Failed to execute. Code red. Error code: 100")
# ABORT
exit(1)
except Exception as e:
self.order_dict["pair_status"] = "ERROR"
self.order_dict["comments"] = f"Close Market 1 {self.market_1}: , {e}"
print("ABORT PROGRAM")
print("Unexpected Error")
print(order_status_close_order)
# Send Message
#send_message("Failed to execute. Code red. Error code: 101")
# ABORT
exit(1)
# Return success result
else:
self.order_dict["pair_status"] = "LIVE"
return self.order_dict