forked from enarjord/passivbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
njit_clock.py
805 lines (779 loc) · 30.5 KB
/
njit_clock.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
from __future__ import annotations
import os
import numpy as np
from njit_funcs import (
calc_pnl_long,
calc_pnl_short,
calc_new_psize_pprice,
qty_to_cost,
cost_to_qty,
calc_ema,
round_dn,
round_up,
round_,
calc_bankruptcy_price,
calc_close_grid_long,
calc_close_grid_short,
calc_min_entry_qty,
find_entry_qty_bringing_wallet_exposure_to_target,
calc_delay_between_fills_ms_bid,
calc_delay_between_fills_ms_ask,
calc_clock_qty,
)
if "NOJIT" in os.environ and os.environ["NOJIT"] == "true":
print("not using numba")
def njit(pyfunc=None, **kwargs):
def wrap(func):
return func
if pyfunc is not None:
return wrap(pyfunc)
else:
return wrap
else:
print("using numba")
from numba import njit
@njit
def calc_clock_price_bid(lower_ema_band, highest_bid, ema_dist_lower, price_step):
return min(highest_bid, round_dn(lower_ema_band * (1 - ema_dist_lower), price_step))
@njit
def calc_clock_price_ask(upper_ema_band, lowest_ask, ema_dist_upper, price_step):
return max(lowest_ask, round_up(upper_ema_band * (1 + ema_dist_upper), price_step))
@njit
def calc_clock_entry_long(
balance: float,
psize_long: float,
pprice_long: float,
highest_bid: float,
emas: float,
utc_now_ms: float,
prev_clock_fill_ts_entry: float,
inverse: bool,
qty_step: float,
price_step: float,
min_qty: float,
min_cost: float,
c_mult: float,
ema_dist_entry: float,
qty_pct_entry: float,
we_multiplier_entry: float,
delay_weight_entry: float,
delay_between_fills_ms_entry: float,
wallet_exposure_limit: float,
) -> (float, float, str, float, float):
if psize_long == 0.0 or utc_now_ms - prev_clock_fill_ts_entry > calc_delay_between_fills_ms_bid(
pprice_long,
highest_bid,
delay_between_fills_ms_entry,
delay_weight_entry,
):
wallet_exposure_long = qty_to_cost(psize_long, pprice_long, inverse, c_mult) / balance
if wallet_exposure_long < wallet_exposure_limit * 0.99:
# entry long
bid_price_long = calc_clock_price_bid(emas.min(), highest_bid, ema_dist_entry, price_step)
qty_long = calc_clock_qty(
balance,
wallet_exposure_long,
bid_price_long,
inverse,
qty_step,
min_qty,
min_cost,
c_mult,
qty_pct_entry,
we_multiplier_entry,
wallet_exposure_limit,
)
new_psize_long, new_pprice_long = calc_new_psize_pprice(
psize_long, pprice_long, qty_long, bid_price_long, qty_step
)
wallet_exposure_after_fill = (
qty_to_cost(new_psize_long, new_pprice_long, inverse, c_mult) / balance
)
if wallet_exposure_after_fill > wallet_exposure_limit * 1.01:
qty_long = find_entry_qty_bringing_wallet_exposure_to_target(
balance,
psize_long,
pprice_long,
wallet_exposure_limit,
bid_price_long,
inverse,
qty_step,
c_mult,
)
if qty_long != 0.0:
qty_long = max(
qty_long,
calc_min_entry_qty(bid_price_long, inverse, qty_step, min_qty, min_cost),
)
new_psize_long, new_pprice_long = calc_new_psize_pprice(
psize_long, pprice_long, qty_long, bid_price_long, qty_step
)
if qty_long > 0.0:
return (qty_long, bid_price_long, "clock_entry_long", new_psize_long, new_pprice_long)
return (0.0, 0.0, "clock_entry_long", 0.0, 0.0)
@njit
def calc_clock_close_long(
balance: float,
psize_long: float,
pprice_long: float,
lowest_ask: float,
emas: float,
utc_now_ms: float,
prev_clock_fill_ts_close: float,
inverse: bool,
qty_step: float,
price_step: float,
min_qty: float,
min_cost: float,
c_mult: float,
ema_dist_close: float,
qty_pct_close: float,
we_multiplier_close: float,
delay_weight_close: float,
delay_between_fills_ms_close: float,
wallet_exposure_limit: float,
):
if psize_long > 0.0:
delay = calc_delay_between_fills_ms_ask(
pprice_long, lowest_ask, delay_between_fills_ms_close, delay_weight_close
)
if utc_now_ms - prev_clock_fill_ts_close > delay:
ask_price_long = calc_clock_price_ask(emas.max(), lowest_ask, ema_dist_close, price_step)
wallet_exposure_long = qty_to_cost(psize_long, pprice_long, inverse, c_mult) / balance
qty_long = min(
psize_long,
calc_clock_qty(
balance,
wallet_exposure_long,
ask_price_long,
inverse,
qty_step,
min_qty,
min_cost,
c_mult,
qty_pct_close,
we_multiplier_close,
wallet_exposure_limit,
),
)
if qty_long > 0.0:
if round_(psize_long - qty_long, qty_step) < calc_min_entry_qty(
ask_price_long, inverse, qty_step, min_qty, min_cost
):
# close whole pos; include leftovers
return (-psize_long, ask_price_long, "clock_close_long")
return (-qty_long, ask_price_long, "clock_close_long")
return (0.0, 0.0, "clock_close_long")
@njit
def calc_clock_entry_short(
balance: float,
psize_short: float,
pprice_short: float,
lowest_ask: float,
emas: float,
utc_now_ms: float,
prev_clock_fill_ts_entry: float,
inverse: bool,
qty_step: float,
price_step: float,
min_qty: float,
min_cost: float,
c_mult: float,
ema_dist_entry: float,
qty_pct_entry: float,
we_multiplier_entry: float,
delay_weight_entry: float,
delay_between_fills_ms_entry: float,
wallet_exposure_limit: float,
) -> (float, float, str, float, float):
if psize_short == 0.0 or utc_now_ms - prev_clock_fill_ts_entry > calc_delay_between_fills_ms_ask(
pprice_short, lowest_ask, delay_between_fills_ms_entry, delay_weight_entry
):
wallet_exposure_short = qty_to_cost(psize_short, pprice_short, inverse, c_mult) / balance
if wallet_exposure_short < wallet_exposure_limit * 0.99:
# entry short
ask_price_short = calc_clock_price_ask(emas.max(), lowest_ask, ema_dist_entry, price_step)
qty_short = -calc_clock_qty(
balance,
wallet_exposure_short,
ask_price_short,
inverse,
qty_step,
min_qty,
min_cost,
c_mult,
qty_pct_entry,
we_multiplier_entry,
wallet_exposure_limit,
)
new_psize_short, new_pprice_short = calc_new_psize_pprice(
-abs(psize_short), pprice_short, qty_short, ask_price_short, qty_step
)
wallet_exposure_after_fill = (
qty_to_cost(new_psize_short, new_pprice_short, inverse, c_mult) / balance
)
if wallet_exposure_after_fill > wallet_exposure_limit * 1.01:
qty_short = -find_entry_qty_bringing_wallet_exposure_to_target(
balance,
psize_short,
pprice_short,
wallet_exposure_limit,
ask_price_short,
inverse,
qty_step,
c_mult,
)
if qty_short != 0.0:
qty_short = -max(
abs(qty_short),
calc_min_entry_qty(ask_price_short, inverse, qty_step, min_qty, min_cost),
)
new_psize_short, new_pprice_short = calc_new_psize_pprice(
-abs(psize_short), pprice_short, qty_short, ask_price_short, qty_step
)
if qty_short != 0.0:
return (
-abs(qty_short),
ask_price_short,
"clock_entry_short",
-abs(new_psize_short),
new_pprice_short,
)
return (0.0, 0.0, "clock_entry_short", 0.0, 0.0)
@njit
def calc_clock_close_short(
balance: float,
psize_short: float,
pprice_short: float,
highest_bid: float,
emas: float,
utc_now_ms: float,
prev_clock_fill_ts_close: float,
inverse: bool,
qty_step: float,
price_step: float,
min_qty: float,
min_cost: float,
c_mult: float,
ema_dist_close: float,
qty_pct_close: float,
we_multiplier_close: float,
delay_weight_close: float,
delay_between_fills_ms_close: float,
wallet_exposure_limit: float,
):
psize_short = abs(psize_short)
if psize_short > 0.0:
pprice_diff_short = (highest_bid / pprice_short - 1) if pprice_short > 0.0 else 0.0
delay = calc_delay_between_fills_ms_bid(
pprice_short, highest_bid, delay_between_fills_ms_close, delay_weight_close
)
if utc_now_ms - prev_clock_fill_ts_close > delay:
bid_price_short = calc_clock_price_bid(
emas.min(), highest_bid, ema_dist_close, price_step
)
wallet_exposure_short = qty_to_cost(psize_short, pprice_short, inverse, c_mult) / balance
qty_short = min(
psize_short,
calc_clock_qty(
balance,
wallet_exposure_short,
bid_price_short,
inverse,
qty_step,
min_qty,
min_cost,
c_mult,
qty_pct_close,
we_multiplier_close,
wallet_exposure_limit,
),
)
if qty_short > 0.0:
if round_(psize_short - qty_short, qty_step) < calc_min_entry_qty(
bid_price_short, inverse, qty_step, min_qty, min_cost
):
# close whole pos; include leftovers
return (psize_short, bid_price_short, "clock_close_short")
return (qty_short, bid_price_short, "clock_close_short")
return (0.0, 0.0, "clock_close_short")
@njit
def backtest_clock(
hlc,
starting_balance,
maker_fee,
inverse,
do_long,
do_short,
backwards_tp,
qty_step,
price_step,
min_qty,
min_cost,
c_mult,
ema_span_0,
ema_span_1,
ema_dist_entry,
ema_dist_close,
qty_pct_entry,
qty_pct_close,
we_multiplier_entry,
we_multiplier_close,
delay_weight_entry,
delay_weight_close,
delay_between_fills_minutes_entry,
delay_between_fills_minutes_close,
min_markup,
markup_range,
n_close_orders,
wallet_exposure_limit,
):
# hlc [[ts, high, low, close]] 1m
timestamps = hlc[:, 0]
highs = hlc[:, 1]
lows = hlc[:, 2]
closes = hlc[:, 3]
if wallet_exposure_limit[0] == 0.0:
do_long = False
if wallet_exposure_limit[1] == 0.0:
do_short = False
# (long, short)
delay_between_fills_ms_entry = (
delay_between_fills_minutes_entry[0] * 60 * 1000,
delay_between_fills_minutes_entry[1] * 60 * 1000,
)
delay_between_fills_ms_close = (
delay_between_fills_minutes_close[0] * 60 * 1000,
delay_between_fills_minutes_close[1] * 60 * 1000,
)
psize_long, pprice_long, psize_short, pprice_short = 0.0, 0.0, 0.0, 0.0
close_grid_long = [(0.0, np.inf, "")]
close_grid_short = [(0.0, 0.0, "")]
balance_long, balance_short = starting_balance, starting_balance
# older versions sometimes had negative delay weights
delay_weight_entry = (abs(delay_weight_entry[0]), abs(delay_weight_entry[1]))
delay_weight_close = (abs(delay_weight_close[0]), abs(delay_weight_close[1]))
spans_long = [ema_span_0[0], (ema_span_0[0] * ema_span_1[0]) ** 0.5, ema_span_1[0]]
spans_long = np.array(sorted(spans_long)) if do_long else np.ones(3)
spans_short = [ema_span_0[1], (ema_span_0[1] * ema_span_1[1]) ** 0.5, ema_span_1[1]]
spans_short = np.array(sorted(spans_short)) if do_short else np.ones(3)
assert max(spans_long) < len(hlc), "ema span long larger than len(prices)"
assert max(spans_short) < len(hlc), "ema span short larger than len(prices)"
spans_long = np.where(spans_long < 1.0, 1.0, spans_long)
spans_short = np.where(spans_short < 1.0, 1.0, spans_short)
max_span_long = int(round(max(spans_long)))
max_span_short = int(round(max(spans_short)))
emas_long, emas_short = np.repeat(closes[0], 3), np.repeat(closes[0], 3)
alphas_long = 2.0 / (spans_long + 1.0)
alphas__long = 1.0 - alphas_long
alphas_short = 2.0 / (spans_short + 1.0)
alphas__short = 1.0 - alphas_short
prev_clock_fill_ts_entry_long, prev_clock_fill_ts_close_long = 0, 0
prev_clock_fill_ts_entry_short, prev_clock_fill_ts_close_short = 0, 0
fills_long, fills_short, stats = [], [], []
next_stats_update = 0
closest_bkr_long, closest_bkr_short = 1.0, 1.0
for k in range(1, len(hlc)):
# process stats
if timestamps[k] >= next_stats_update:
bkr_price_long = calc_bankruptcy_price(
balance_long,
psize_long,
pprice_long,
0.0,
0.0,
inverse,
c_mult,
)
bkr_price_short = calc_bankruptcy_price(
balance_short,
0.0,
0.0,
psize_short,
pprice_short,
inverse,
c_mult,
)
closest_bkr_long = min(closest_bkr_long, abs(bkr_price_long - closes[k]) / closes[k])
closest_bkr_short = min(closest_bkr_short, abs(bkr_price_short - closes[k]) / closes[k])
upnl_long = calc_pnl_long(pprice_long, closes[k], psize_long, inverse, c_mult)
upnl_short = calc_pnl_short(pprice_short, closes[k], psize_short, inverse, c_mult)
equity_long = balance_long + upnl_long
equity_short = balance_short + upnl_short
stats.append(
(
timestamps[k],
bkr_price_long,
bkr_price_short,
psize_long,
pprice_long,
-psize_short,
pprice_short,
closes[k],
closest_bkr_long,
closest_bkr_short,
balance_long,
balance_short,
equity_long,
equity_short,
)
)
if equity_long <= 0.05:
do_long = False
if equity_short <= 0.05:
do_short = False
next_stats_update = min(timestamps[-1], timestamps[k] + 1000 * 60 * 60) # hourly
if do_long:
emas_long = calc_ema(alphas_long, alphas__long, emas_long, closes[k - 1])
# simulate what orders were placed previous minute
closes_long = [(0.0, np.inf, "")]
if psize_long != 0.0:
ask_price_long = calc_clock_price_ask(
emas_long.max(), closes[k - 1], ema_dist_close[0], price_step
)
if highs[k] > ask_price_long:
# clock close long
clock_close_long = calc_clock_close_long(
balance_long,
psize_long,
pprice_long,
closes[k - 1],
emas_long,
timestamps[k - 1],
prev_clock_fill_ts_close_long,
inverse,
qty_step,
price_step,
min_qty,
min_cost,
c_mult,
ema_dist_close[0],
qty_pct_close[0],
we_multiplier_close[0],
delay_weight_close[0],
delay_between_fills_ms_close[0],
wallet_exposure_limit[0],
)
else:
clock_close_long = (0.0, 0.0, "")
# check if markup close
if psize_long > 0.0 and highs[k] > pprice_long * (1 + min_markup[0]):
close_grid_long = calc_close_grid_long(
backwards_tp[0],
balance_long,
psize_long,
pprice_long,
closes[k - 1],
0.0,
0,
0,
inverse,
qty_step,
price_step,
min_qty,
min_cost,
c_mult,
wallet_exposure_limit[0],
min_markup[0],
markup_range[0],
n_close_orders[0],
0.0,
0.0,
0.0,
0.0,
)
# check whether to modify close grid
if close_grid_long and close_grid_long[0][0] != 0.0:
if clock_close_long[1] <= close_grid_long[0][1]:
close_grid_long = calc_close_grid_long(
backwards_tp[0],
balance_long,
max(0.0, round_(psize_long - abs(clock_close_long[0]), qty_step)),
pprice_long,
closes[k - 1],
0.0,
0,
0,
inverse,
qty_step,
price_step,
min_qty,
min_cost,
c_mult,
wallet_exposure_limit[0],
min_markup[0],
markup_range[0],
n_close_orders[0],
0.0,
0.0,
0.0,
0.0,
)
closes_long = [clock_close_long] + close_grid_long
else:
closes_long = close_grid_long
elif clock_close_long[0] != 0.0:
closes_long = [clock_close_long]
bid_price_long = calc_clock_price_bid(
emas_long.min(), closes[k - 1], ema_dist_entry[0], price_step
)
if lows[k] < bid_price_long:
# clock entry long
clock_entry_long = calc_clock_entry_long(
balance_long,
psize_long,
pprice_long,
closes[k - 1],
emas_long,
timestamps[k - 1],
prev_clock_fill_ts_entry_long,
inverse,
qty_step,
price_step,
min_qty,
min_cost,
c_mult,
ema_dist_entry[0],
qty_pct_entry[0],
we_multiplier_entry[0],
delay_weight_entry[0],
delay_between_fills_ms_entry[0],
wallet_exposure_limit[0],
)
if clock_entry_long[0] != 0.0:
prev_clock_fill_ts_entry_long = timestamps[k]
psize_long, pprice_long = clock_entry_long[3], clock_entry_long[4]
upnl = calc_pnl_long(pprice_long, closes[k], psize_long, inverse, c_mult)
equity_long = balance_long + upnl
pnl = 0.0
fee_paid = (
-qty_to_cost(clock_entry_long[0], clock_entry_long[1], inverse, c_mult)
* maker_fee
)
balance_long += fee_paid
fills_long.append(
(
k,
timestamps[k],
pnl,
fee_paid,
balance_long,
equity_long,
clock_entry_long[0],
clock_entry_long[1],
psize_long,
pprice_long,
"clock_entry_long",
)
)
while closes_long:
if closes_long[0][0] != 0.0 and highs[k] > closes_long[0][1]:
# close long pos
if "clock" in closes_long[0][2]:
prev_clock_fill_ts_close_long = timestamps[k]
close_qty = min(psize_long, abs(closes_long[0][0]))
if close_qty == 0.0:
break
pnl = calc_pnl_long(pprice_long, closes_long[0][1], close_qty, inverse, c_mult)
fee_paid = -qty_to_cost(close_qty, closes_long[0][1], inverse, c_mult) * maker_fee
balance_long += pnl + fee_paid
psize_long = max(0.0, round_(psize_long - close_qty, qty_step))
if psize_long == 0.0:
pprice_long = 0.0
prev_clock_fill_ts_entry_long = 0
upnl = calc_pnl_long(pprice_long, closes[k], psize_long, inverse, c_mult)
equity_long = balance_long + upnl
fills_long.append(
(
k,
timestamps[k],
pnl,
fee_paid,
balance_long,
equity_long,
-close_qty,
closes_long[0][1],
psize_long,
pprice_long,
closes_long[0][2],
)
)
closes_long = closes_long[1:]
if do_short:
emas_short = calc_ema(alphas_short, alphas__short, emas_short, closes[k - 1])
closes_short = [(0.0, 0.0, "")]
if psize_short != 0.0:
bid_price_short = calc_clock_price_bid(
emas_short.min(), closes[k - 1], ema_dist_close[1], price_step
)
if lows[k] < bid_price_short:
clock_close_short = calc_clock_close_short(
balance_short,
psize_short,
pprice_short,
closes[k - 1],
emas_short,
timestamps[k - 1],
prev_clock_fill_ts_close_short,
inverse,
qty_step,
price_step,
min_qty,
min_cost,
c_mult,
ema_dist_close[1],
qty_pct_close[1],
we_multiplier_close[1],
delay_weight_close[1],
delay_between_fills_ms_close[1],
wallet_exposure_limit[1],
)
else:
clock_close_short = (0.0, 0.0, "")
# check if markup close
if psize_short > 0.0 and lows[k] < pprice_short * (1 - min_markup[1]):
close_grid_short = calc_close_grid_short(
backwards_tp[1],
balance_short,
psize_short,
pprice_short,
closes[k - 1],
0.0,
0,
0,
inverse,
qty_step,
price_step,
min_qty,
min_cost,
c_mult,
wallet_exposure_limit[1],
min_markup[1],
markup_range[1],
n_close_orders[1],
0.0,
0.0,
0.0,
0.0,
)
if close_grid_short and close_grid_short[0][0] != 0.0:
if clock_close_short[1] >= close_grid_short[0][1]:
close_grid_short = calc_close_grid_short(
backwards_tp[1],
balance_short,
-max(
0.0,
round_(abs(psize_short) - abs(clock_close_short[0]), qty_step),
),
pprice_short,
closes[k - 1],
0.0,
0,
0,
inverse,
qty_step,
price_step,
min_qty,
min_cost,
c_mult,
wallet_exposure_limit[1],
min_markup[1],
markup_range[1],
n_close_orders[1],
0.0,
0.0,
0.0,
0.0,
)
closes_short = [clock_close_short] + close_grid_short
else:
closes_short = close_grid_short
elif clock_close_short[0] != 0.0:
closes_short = [clock_close_short]
ask_price_short = calc_clock_price_ask(
emas_short.max(), closes[k - 1], ema_dist_entry[1], price_step
)
if highs[k] > ask_price_short:
clock_entry_short = calc_clock_entry_short(
balance_short,
psize_short,
pprice_short,
closes[k - 1],
emas_short,
timestamps[k - 1],
prev_clock_fill_ts_entry_short,
inverse,
qty_step,
price_step,
min_qty,
min_cost,
c_mult,
ema_dist_entry[1],
qty_pct_entry[1],
we_multiplier_entry[1],
delay_weight_entry[1],
delay_between_fills_ms_entry[1],
wallet_exposure_limit[1],
)
if clock_entry_short[0] != 0.0:
prev_clock_fill_ts_entry_short = timestamps[k]
psize_short, pprice_short = abs(clock_entry_short[3]), clock_entry_short[4]
upnl = calc_pnl_short(pprice_short, closes[k], psize_short, inverse, c_mult)
equity_short = balance_short + upnl
pnl = 0.0
fee_paid = (
-qty_to_cost(clock_entry_short[0], clock_entry_short[1], inverse, c_mult)
* maker_fee
)
balance_short += fee_paid
fills_short.append(
(
k,
timestamps[k],
pnl,
fee_paid,
balance_short,
equity_short,
-abs(clock_entry_short[0]),
clock_entry_short[1],
-psize_short,
pprice_short,
"clock_entry_short",
)
)
while closes_short:
if closes_short[0][0] != 0.0 and lows[k] < closes_short[0][1]:
# close short pos
if "clock" in closes_short[0][2]:
prev_clock_fill_ts_close_short = timestamps[k]
close_qty = min(abs(psize_short), abs(closes_short[0][0]))
if close_qty == 0.0:
break
pnl = calc_pnl_short(pprice_short, closes_short[0][1], close_qty, inverse, c_mult)
fee_paid = (
-qty_to_cost(close_qty, closes_short[0][1], inverse, c_mult) * maker_fee
)
balance_short += pnl + fee_paid
psize_short = max(0.0, round_(psize_short - close_qty, qty_step))
if psize_short == 0.0:
pprice_short = 0.0
prev_clock_fill_ts_entry_short = 0
upnl = calc_pnl_short(pprice_short, closes[k], psize_short, inverse, c_mult)
equity_short = balance_short + upnl
fills_short.append(
(
k,
timestamps[k],
pnl,
fee_paid,
balance_short,
equity_short,
close_qty,
closes_short[0][1],
-psize_short,
pprice_short,
closes_short[0][2],
)
)
closes_short = closes_short[1:]
return fills_long, fills_short, stats