-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
366 lines (291 loc) · 10.8 KB
/
main.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
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
import asyncio
import logging
import time
import numpy as np
import random
from apscheduler.schedulers.asyncio import AsyncIOScheduler
logging.basicConfig(
level=logging.INFO,
format=f"--%(asctime)s [%(levelname)s] [async_loop] [%(funcName)s] %(message)s",
handlers=[
logging.StreamHandler(),
],
)
log = logging
MODE_AWAIT = 0
MODE_FUTURE = 1
class TradingClass:
def __init__(self, num_strategies, strat_chunk_size, mode):
self.loop = asyncio.get_event_loop()
self.strat_chunk_size = strat_chunk_size
self.mode = mode
self.strats = self.load_strategies(num_strategies)
self.positions = self.init_positions_from_strats()
self.strategy_locks: dict[str, bool] = {}
self.last_chunk_loop_time = 0
self.strats_processed = 0
def locking_strat(func): # noqa
"""
Decorator to lock strategy from running if it is already running
:param func: Function to decorate
:type func: function
:return: Decorated function
:rtype: function
"""
# This nice line raises warning in PyCharm, but it works
# https://stackoverflow.com/questions/69362223/parameter-self-unfilled-when-using-decorators-even-after-instantiating-obje
# async def wrapper(*args): self, strat = args
# Decorator inside class
# https://stackoverflow.com/questions/1263451/python-decorators-in-classes
async def wrapper(self, strat):
self.lock_strat(strat)
try:
result = await func(self, strat) # noqa
finally:
self.unlock_strat(strat)
return result
return wrapper
@property
def positions_len(self):
"""
Get number of positions
:return: Number of positions
:rtype: int
"""
return len([x for x in self.positions.values() if x > 0])
async def stats(self, sleep_time):
"""
Checkup asyncio congestion. It should printout roughly every sleep_time seconds
:param sleep_time: Time to sleep between printouts
:type sleep_time: int
:return: None
:rtype: None
"""
while True:
start_time = time.time()
await asyncio.sleep(sleep_time)
actual_time = round(time.time() - start_time, 2)
log.info(
f"strats processed:[{'{:,}'.format(self.strats_processed)}], max chunk time:[{self.last_chunk_loop_time}]"
f" pos:[{self.positions_len}] stats time:[{actual_time}]")
@staticmethod
def get_chunk(lst: list, n: int) -> list:
"""
Yield successive n-sized chunks from lst
:param lst: List to split
:type lst: list
:param n: Size of chunk
:type n: int
:return: Yielded chunk
:rtype: list
"""
for i in range(0, len(lst), n):
yield lst[i: i + n]
@staticmethod
def load_strategies(num_strategies: int) -> dict:
"""
# Imitate loading strategies from DB or file
:param num_strategies: Number of strategies to load
:type num_strategies: int
:return: Dictionary with strategies
:rtype: dict
"""
strats_dict = {}
for strat_id in range(num_strategies):
strats_dict[strat_id] = {}
strats_dict[strat_id]["strat"] = str(strat_id)
strats_dict[strat_id]["enter_limit"] = False
strats_dict[strat_id]["exit_exp"] = False
return strats_dict
def init_positions_from_strats(self):
"""
Init positions from strats
:return: Positions dictionary
:rtype: dict
"""
positions = {}
for strat in self.strats:
positions[strat] = 0
return positions
async def run_trading_cycle(self):
"""
Run trading cycle in asyncio tasks
:return: None
:rtype: None
"""
# Usual case with chunks
for chunk in self.get_chunk(list(self.strats), self.strat_chunk_size):
loop.create_task(self.start_main_loop(chunk))
log.info(f"Run [{len(self.strats)}] strategies in [{self.strat_chunk_size}] chunks")
def is_strat_locked(self, strat):
"""
Check if strategy is locked
:param strat: Strategy to check
:type strat: str
:return: True if locked, False if not
:rtype: bool
"""
return strat in self.strategy_locks and self.strategy_locks[strat]
def lock_strat(self, strat):
"""
Lock strategy
:param strat: Strategy to lock
:type strat: str
:return: None
:rtype: None
"""
self.strategy_locks[strat] = True
def unlock_strat(self, strat):
"""
Unlock strategy
:param strat: Strategy to unlock
:type strat: str
:return: None
:rtype: None
"""
if strat in self.strategy_locks:
self.strategy_locks[strat] = False
@locking_strat # noqa
async def enter_limit_task(self, strat):
"""
Enter order imitation task
:param strat: Strategy to enter
:type strat: str
:return: None
:rtype: None
"""
log.debug(f"Enter limit [{strat}] started")
# Imitate sending order to exchange
await asyncio.sleep(random.uniform(0, 0.5))
# Imitate receiving execution info from exchange websocket
self.positions[strat] += 1
self.strats[strat]["enter_limit"] = False
log.debug(f"Enter limit [{strat}] finished")
@locking_strat # noqa
async def exit_on_exp_task(self, strat):
"""
Exit order imitation task
:param strat: Strategy to exit
:type strat: str
:return: None
:rtype: None
"""
log.debug(f"Exit on exp [{strat}] started")
log.debug(f"Exit on exp [{strat}] - cancel all orders")
# Imitate canceling all orders
await asyncio.sleep(random.uniform(0, 0.5))
# Imitate evaluating min notional filter
if np.random.random() > 0.5:
log.debug(f"Exit on exp [{strat}] - eval min notional filter")
await asyncio.sleep(random.uniform(1, 3))
# Imitate sending market exit order
log.debug(f"Exit on exp [{strat}] - sending market exit order")
await asyncio.sleep(random.uniform(0, 0.5))
# Imitate receiving execution info from exchange websocket
self.positions[strat] -= 1
self.strats[strat]["exit_exp"] = False
log.debug(f"Exit on exp [{strat}] finished")
async def start_main_loop(self, strats):
"""
Start main strategy loop
:param strats: Strategies to run
:type strats: list
:return: None
:rtype: None
"""
log.debug(f"Loop started [{len(strats)}]")
while True:
start_time = time.time()
# Shuffle strategies to randomize order
np.random.shuffle(strats)
for strat in strats:
# Strategy params
sp = self.strats[strat]
# Send enter order
if sp["enter_limit"] and self.positions[strat] == 0:
# Check if strategy is not locked
if not self.is_strat_locked(strat):
# Send enter task
if self.mode == MODE_FUTURE:
self.loop.create_task(self.enter_limit_task(strat))
else:
await self.enter_limit_task(strat)
# Send emergency exit
elif sp["exit_exp"] and self.positions[strat] > 0:
# Check if strategy is not locked
if not self.is_strat_locked(strat):
# Send exit task
if self.mode == MODE_FUTURE:
self.loop.create_task(self.exit_on_exp_task(strat))
else:
await self.exit_on_exp_task(strat)
# Pause after each strategy
await asyncio.sleep(0.0)
# Posion check (0 or 1)
assert self.positions[strat] in (0, 1), f"Position [{self.positions[strat]}] violation on [{strat}]"
# Pause after each strat chunk
await asyncio.sleep(0.0)
actual_time = round(time.time() - start_time, 2)
# Stats
if actual_time > self.last_chunk_loop_time:
self.last_chunk_loop_time = actual_time
self.strats_processed += 1 * self.strat_chunk_size
log.debug(f"Loop finished [{len(strats)}] - {actual_time}")
async def recalc_signals(self):
"""
Imitate calculation of signals with technical analysis, machine learning, etc.
:return: None
"""
log.debug("Recalc signals started")
for strat in self.strats:
if self.positions[strat] == 0:
if np.random.random() > 0.9:
self.strats[strat]["enter_limit"] = True
log.debug(f"Recalc signals - enter limit [{strat}]")
if not self.strats[strat]["enter_limit"] and self.positions[strat] > 0:
if np.random.random() > 0.85:
self.strats[strat]["exit_exp"] = True
log.debug(f"Recalc signals - exit exp [{strat}]")
log.debug("Recalc signals finished")
@staticmethod
def handle_exception(outer_loop, context):
"""
Handle exception in event loop
:param outer_loop: Outer loop
:type outer_loop: asyncio.AbstractEventLoop
:param context: Exception context
:type context: dict
:return: None
:rtype: None
"""
log.error(f"Exception in outer loop: {context}")
outer_loop.stop()
async def main():
# Checkup asyncio congestion
loop.create_task(tcl.stats(sleep_time=3))
# Run trading cycle
await tcl.run_trading_cycle()
# Init scheduler
scheduler.configure(timezone="utc")
scheduler.add_job(tcl.recalc_signals, trigger="cron", second="*/10")
log.getLogger("apscheduler.executors.default").setLevel(logging.WARNING)
scheduler.start()
log.debug("Loop done")
if __name__ == "__main__":
# Set main class
tcl = TradingClass(
num_strategies=100_000,
strat_chunk_size=500,
mode=MODE_FUTURE
)
# Init asyncio loop and exception handler
loop = asyncio.get_event_loop()
loop.set_exception_handler(tcl.handle_exception)
# Scheduler config and run
scheduler: AsyncIOScheduler = AsyncIOScheduler()
try:
loop.create_task(main())
loop.run_forever()
finally:
scheduler.shutdown()
log.debug("Successfully shutdown")