-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmonitor_positions.py
More file actions
170 lines (130 loc) · 5.65 KB
/
monitor_positions.py
File metadata and controls
170 lines (130 loc) · 5.65 KB
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
import config
import exch_api # exchanges
from libs.aux_functions import send_chat_message
import libs.sqltools as sqltools
sql = sqltools.sql()
import time, traceback
# List of users to ignore
ignore_users = [386061351]
# Get the array with active markets
markets_arr = [row[0] for row in config.markets_list]
# Get user list
def get_user_list():
# Empty array
tmp_dict = {}
sql_string = "SELECT userid, name FROM user_info"
rows = sql.query(sql_string)
if rows != []:
for row in rows:
userid = int(row[0])
name = row[1]
tmp_dict[userid] = name
return tmp_dict
# Overall array of tasks
def tasks_update(type = 'jobs'):
# Empty array
tasks_arr = []
if type == 'jobs': # checking jobs
sql_string = "SELECT * FROM jobs"
rows = sql.query(sql_string)
if rows != []:
for row in rows:
id = row[0]
market = row[1] # market
userid = row[16]
entry = row[11]
exch = row[15]
strategy = row[18]
short_flag = row[19]
last_update = row[20]
tmp_dict = {'userid': userid, 'strategy': strategy, 'short_flag': short_flag, 'exchange': exch, 'last_update': last_update, 'market': market, 'id':id}
tasks_arr.append(tmp_dict)
elif type == 'positions':
sql_string = "SELECT user, strategy, exchange, key_id FROM keys"
rows = sql.query(sql_string)
if rows != []:
for row in rows:
userid = row[0]
strategy = row[1]
exchange = row[2]
keyid = row[3]
tmp_dict = {'userid': userid, 'strategy': strategy, 'exchange': exchange, 'keyid': keyid}
tasks_arr.append(tmp_dict)
return tasks_arr
## Confirming we have jobs
def confirm_jobs(position, userid, markets_arr):
if position['market'] in markets_arr:
sql_string = "SELECT * FROM jobs WHERE userid = {} and market = '{}' ".format(userid, position['market'])
rows = sql.query(sql_string)
if rows != []:
return True
else:
return False
else:
return True
# Pre
warnings_arr = []
####### Cycle checking #############
while True:
warnings_repeated_arr = []
current_ts = time.time()
# List of users
user_list = get_user_list()
## Check tasks -> positions
tasks = tasks_update(type='jobs')
for task in tasks:
print('Checking task for the user {}, strategy {}'.format(task['userid'], task['strategy']))
position = None
e_api = exch_api.api(task['userid'], strategy=task['strategy'])
all_positions = e_api.getpositions(task['exchange'], task['market'], do_retry=False)
if all_positions != [{}]:
position = e_api.getpositions(task['exchange'], task['market'], do_retry=False)[0]
# If job on no positions
if position is None:
issue_str = "\nWarning: orphan job {} for user {} ({})".format(task['id'], task['userid'], user_list[int(task['userid'])])
if issue_str in warnings_arr:
warnings_repeated_arr.append(issue_str)
else:
warnings_arr.append(issue_str)
# If job stopped updating
if task['last_update'] is not None:
if (current_ts - task['last_update'])/60 > 5:
issue_str = "\nWarning: job {} stopped updating for user {} ({})".format(task['id'], task['userid'], user_list[int(task['userid'])])
if issue_str in warnings_arr:
warnings_repeated_arr.append(issue_str)
else:
warnings_arr.append(issue_str)
## Check positions -> tasks
tasks = tasks_update(type='positions')
for task in tasks:
print('Checking {}:{}, {}'.format(task['userid'], task['keyid'], task['strategy']))
if task['userid'] not in ignore_users: # not for everyone
e_api = exch_api.api(task['userid'], strategy=task['strategy'])
all_positions = e_api.getpositions(task['exchange'], do_retry=False)
if all_positions != [{}]:
positions_each = e_api.getpositions(task['exchange'], do_retry=False)
if positions_each is not None:
for position in positions_each:
check_ok = confirm_jobs(position, task['userid'], markets_arr)
if not check_ok:
issue_str = "\nWarning: no jobs for open position on {} user {} ({}). Position: {}".format(
position['market'], task['userid'], user_list[int(task['userid'])], position)
if issue_str in warnings_arr:
warnings_repeated_arr.append(issue_str)
else:
warnings_arr.append(issue_str)
else:
issue_str = "\nWarning: key {} is disabled or inactive for the user {} ({}) | {}".format(
task['keyid'], task['userid'], user_list[int(task['userid'])], task['strategy'])
if issue_str in warnings_arr:
warnings_repeated_arr.append(issue_str)
else:
warnings_arr.append(issue_str)
# Processing
if warnings_repeated_arr != []:
str_send = 'Issues / warnings\n\n'
for elem in warnings_repeated_arr:
str_send = '{}{}\n'.format(str_send, elem)
send_chat_message(config.telegram_chat_id, str_send)
print("Sleeping...")
time.sleep(1800)