-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.py
1332 lines (1052 loc) · 53.4 KB
/
strategy.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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
This module implements a perpetual market making strategy.
It provides classes and functions for pricing, order management, and risk control
in perpetual futures markets.
Classes:
RawFairPrice: Represents a fair price with base value.
BasePricer: Abstract base class for pricing logic.
PerpMarketMaker: Main class implementing the market making strategy.
Key features:
1. External Price Reference: Uses spot prices from Binance as a reference for fair value.
2. Dynamic Pricing: Adjusts quotes based on perpetual basis, funding rate, market volatility, and current account positions.
3. Risk Management: Implements position limits and adjusts quotes based on current exposure.
4. Adaptive Spreads: Dynamically adjusts bid-ask spreads based on market conditions and volatility.
5. Multi-level Quoting: Places multiple orders on each side with increasing spreads and sizes.
When a market event occurs, the strategy processes it as follows:
1. Market Data Update:
- Receives market data through the `on_market_data` method.
- Updates exponential moving averages (EMAs) for spot price, basis, and funding rate.
- Triggers a reevaluation of the strategy.
2. Strategy Reevaluation (`reeval` method):
- Checks if the strategy is enabled and if system health is okay.
- Verifies if market data is ready and up-to-date.
3. Proposal Creation:
a. Creates a base proposal (`create_base_proposal`):
- Calculates fair prices for buy and sell sides.
- Generates multiple order levels with increasing spreads and sizes.
b. Applies order level modifiers (`apply_order_levels_modifiers`):
- Implements price bands to limit order placement.
4. Proposal Refinement:
a. Filters out taker orders (`filter_out_takers`):
- Ensures orders are not placed too aggressively.
b. Applies budget constraints (`apply_budget_constraint`):
- Adjusts orders based on available budget and position limits.
c. Quantizes order values (`quantize_values`):
- Rounds prices and sizes to exchange-allowed increments.
5. Order Management:
a. Cancels active orders outside tolerance (`cancel_active_orders`):
- Removes orders that deviate significantly from new proposal.
b. Cancels orders below minimum spread (`cancel_orders_below_min_spread`):
- Ensures maintained orders meet minimum profitability criteria.
6. Proposal Execution:
- Places new orders based on the final proposal (`execute_orders_proposal`).
This process ensures that the strategy continuously adapts to market conditions, maintains risk parameters, and provides liquidity efficiently.
"""
import asyncio
import os
import re
import time
import traceback
from decimal import Decimal as D
from typing import List, Optional, Tuple, Union
import numpy as np
import structlog
from connectors.base_connector import _get_connector
from utils.async_utils import safe_ensure_future
from utils.data_methods import (
ConnectorBase,
ExponentialMovingAverage,
Order,
OrderType,
PriceSize,
PriceType,
Proposal,
RollingAnnualizedVolatility,
Side,
Ticker,
TriggerType,
UpdateType,
)
from utils.metrics_publisher import MetricsMessage, MetricsPublisher
from utils.misc_utils import load_config
from utils.parameters_manager import Param, ParamsManager
from utils.risk_manager import RiskManager
class RawFairPrice:
"""
Represents a raw fair price with base value.
This class encapsulates a fair price and its corresponding base value,
which are used in pricing calculations for the market making strategy.
Attributes:
fair (Decimal): The fair price value.
base (Decimal): The base value associated with the fair price.
"""
def __init__(self, fair: D, base: D):
self.fair = fair
self.base = base
class BasePricer:
"""
Abstract base class for pricing logic.
This class defines the interface for pricing logic, which must be implemented
by concrete subclasses.
Attributes:
strategy: The parent strategy instance.
"""
def __init__(self, strategy):
self.strategy = strategy
def get_raw_fair_price(self, side: Side) -> RawFairPrice:
raise NotImplementedError("Subclasses must implement get_base_price")
def publish_metrics(self):
raise NotImplementedError("Subclasses must implement publish_metrics")
class PerpMarketMaker:
"""
A class representing a perpetual market maker strategy.
This class implements a market making strategy for perpetual futures markets.
It manages order placement, pricing, risk management, and market data processing.
"""
PARAM_CLOSE_ONLY_MODE = "close_only_mode"
PARAM_ENABLED = "enabled"
PARAM_ORDER_LEVEL_SPREAD = "order_level_spread"
PARAM_ORDER_LEVEL_AMOUNT_PCT = "order_level_amount_pct"
PARAM_ORDER_INSERT_TIME_SEC = "order_insert_time_sec"
PARAM_REEVAL_TIME_SEC = "reeval_time_sec"
PARAM_ORDER_REFRESH_TOLERANCE_PCT = "order_refresh_tolerance_pct"
PARAM_PRICE_ADJUSTMENT_BPS = "price_adjustment_bps"
PARAM_BUY_LEVELS = "buy_levels"
PARAM_SELL_LEVELS = "sell_levels"
PARAM_BID_SPREAD = "bid_spread"
PARAM_ASK_SPREAD = "ask_spread"
PARAM_MINIMUM_SPREAD = "minimum_spread"
PARAM_ORDER_AMOUNT_USD = "order_amount_usd"
PARAM_POS_LEAN_BPS_PER_100K_USD = "pos_lean_bps_per_100k_usd"
PARAM_MAX_POSITION_USD = "max_position_usd"
PARAM_TAKER_THRESHOLD_BPS = "taker_threshold_bps"
PARAM_PRICE_EMA_SEC = "price_ema_sec"
PARAM_FR_EMA_SEC = "fr_ema_sec"
PARAM_BASIS_EMA_SEC = "basis_ema_sec"
PARAM_MAX_LEVERAGE = "max_leverage"
PARAM_MAX_MARGIN_RATIO = "max_margin_ratio"
PARAM_GLOBAL_POS_LEAN_BPS_PER_100K_USD = "global_pos_lean_bps_per_100k_usd"
PARAM_PRICING_BASIS_FACTOR = "pricing_basis_factor"
PARAM_PRICING_VOLATILITY_FACTOR = "pricing_volatility_factor"
PARAM_VOL_WINDOW_SIZE = "vol_window_size"
PARAM_PRICING_FUNDING_RATE_FACTOR = "pricing_funding_rate_factor"
PARAM_EMPTY_BOOK_PENALTY = "empty_book_penalty"
PARAM_MAX_MARKET_LATENCY_SEC = "max_market_latency_sec"
PARAM_MAX_DATA_DELAY_SEC = "max_data_delay_sec"
PARAM_ORDER_LEVEL_SPREAD_LAMBDA = "order_level_spread_lambda"
PARAM_ORDER_SIZE_SPREAD_LAMBDA = "order_size_spread_lambda"
PARAM_PRICE_CEILING = "price_ceiling"
PARAM_PRICE_FLOOR = "price_floor"
PARAM_BULK_REQUESTS = "bulk_requests"
PARAM_BASE_VOLATILITY = "base_volatility"
PARAM_VOLATILITY_EXPONENT = "volatility_exponent"
PARAM_VOLATILITY_CAP = "volatility_cap"
PARAM_ANCHOR_PRICE = "anchor_price"
PARAM_EXTERNAL_PRICE_MULTIPLIER = "external_price_multiplier"
PARAM_FIXED_ORDER_SIZE = "fixed_order_size"
PARAM_PUBLISH_ORDER_LATENCY = "publish_order_latency"
PARAM_ORDER_SIZE_OBFUSCATION_FACTOR_PCT = "order_size_obfuscation_factor_pct"
PARAM_PREMIUM_FACTOR = "premium_factor"
PARAM_PREMIUM_WINDOW_SIZE_SEC = "premium_window_size_sec"
PARAM_PREMIUM_ADJUSTMENT_CAP = "premium_adjustment_cap"
PARAM_CANCEL_BY_EXCHANGE_ORDER_ID = "cancel_by_exchange_order_id"
PARAM_ORDER_RATIO_TO_CANCEL_ALL = "order_ratio_to_cancel_all"
def __init__(self, loop: asyncio.AbstractEventLoop,
rm: RiskManager=RiskManager,
pm: ParamsManager=ParamsManager,
mp: MetricsPublisher=MetricsPublisher,
PricerClass: BasePricer=BasePricer,
config_path: str=None
):
self.logger = structlog.get_logger(self.__class__.__name__)
self.loop = loop
if config_path is not None:
self.config = load_config(config_path, raise_error=False)
else:
self.config = {}
self.market_connector = _get_connector('paradex_perp', loop=self.loop)
self.pricer = PricerClass(self)
self.algo_name = f"PARABOT_MM_{os.getenv('PARADEX_ID', 'default')}".upper()
self.market: str = os.getenv("ALGO_PARAMS_MARKET")
self.external_markets: str = os.getenv('ALGO_PARAMS_PRICE_SOURCES')
if self.external_markets not in [None, '']:
self.external_market_symbol = self.external_markets.split(':')[-1]
self.external_market_exchange = self.external_markets.split(':')[0]
self.external_connector = _get_connector(self.external_market_exchange, loop=self.loop)
else:
self.external_connector = None
self.external_market_symbol = None
self.external_market_exchange = None
self._smoothen_spot_price: ExponentialMovingAverage = None
self._smoothen_basis: ExponentialMovingAverage = None
self._smoothen_funding_rate: ExponentialMovingAverage = None
self._rolling_vol: RollingAnnualizedVolatility = None
self._rolling_premium: ExponentialMovingAverage = None
self._next_order_timestamp = 0
self._next_reeval_timestamp = 0
self._last_system_health_ok = self.now_ms()
self._last_re_sub_timestamp = self.now_ms()
self.close_vol_factor = D(0.1)
self.processing = False
strategy_parameters = [
Param(self.PARAM_CLOSE_ONLY_MODE, 'False', bool),
Param(self.PARAM_ENABLED, 'False', bool),
Param(self.PARAM_PRICE_ADJUSTMENT_BPS, '0', D),
Param(self.PARAM_ORDER_LEVEL_SPREAD, '2', D),
Param(self.PARAM_ORDER_LEVEL_SPREAD_LAMBDA, '0.5', D),
Param(self.PARAM_ORDER_SIZE_SPREAD_LAMBDA, '0.8', D),
Param(self.PARAM_ORDER_LEVEL_AMOUNT_PCT, '20', D),
Param(self.PARAM_REEVAL_TIME_SEC, '1', float),
Param(self.PARAM_ORDER_INSERT_TIME_SEC, '2', float),
Param(self.PARAM_ORDER_REFRESH_TOLERANCE_PCT, '0.1', D),
Param(self.PARAM_BUY_LEVELS, '4', int),
Param(self.PARAM_SELL_LEVELS, '4', int),
Param(self.PARAM_BID_SPREAD, '0.01', D),
Param(self.PARAM_ASK_SPREAD, '0.01', D),
Param(self.PARAM_MINIMUM_SPREAD, '0', D),
Param(self.PARAM_ORDER_AMOUNT_USD, '400', D),
Param(self.PARAM_FIXED_ORDER_SIZE, '0', D),
Param(self.PARAM_POS_LEAN_BPS_PER_100K_USD, '200', D),
Param(self.PARAM_MAX_POSITION_USD, '2000', D),
Param(self.PARAM_TAKER_THRESHOLD_BPS, '10', D),
Param(self.PARAM_PRICE_EMA_SEC, '60', float),
Param(self.PARAM_FR_EMA_SEC, '2880', float),
Param(self.PARAM_BASIS_EMA_SEC, '2880', float),
Param(self.PARAM_MAX_LEVERAGE, '4', D),
Param(self.PARAM_MAX_MARGIN_RATIO, '10', D),
Param(self.PARAM_GLOBAL_POS_LEAN_BPS_PER_100K_USD, '200', D),
Param(self.PARAM_PRICING_BASIS_FACTOR, '0.5', D),
Param(self.PARAM_PRICING_VOLATILITY_FACTOR, '0.1', D),
Param(self.PARAM_PRICING_FUNDING_RATE_FACTOR, '0.5', D),
Param(self.PARAM_VOL_WINDOW_SIZE, '1000', int),
Param(self.PARAM_EMPTY_BOOK_PENALTY, '0.01', D),
Param(self.PARAM_MAX_MARKET_LATENCY_SEC, '10', float),
Param(self.PARAM_MAX_DATA_DELAY_SEC, '800', float),
Param(self.PARAM_PRICE_CEILING, '0', D),
Param(self.PARAM_PRICE_FLOOR, '0', D),
Param(self.PARAM_BULK_REQUESTS, 'True', bool),
Param(self.PARAM_BASE_VOLATILITY, '0.05', D),
Param(self.PARAM_VOLATILITY_EXPONENT, '2', D),
Param(self.PARAM_VOLATILITY_CAP, '1', D),
Param(self.PARAM_EXTERNAL_PRICE_MULTIPLIER, '1', D),
Param(self.PARAM_ANCHOR_PRICE, '0', D),
Param(self.PARAM_PUBLISH_ORDER_LATENCY, 'False', bool),
Param(self.PARAM_ORDER_SIZE_OBFUSCATION_FACTOR_PCT, '0', D),
Param(self.PARAM_PREMIUM_FACTOR, '0', D),
Param(self.PARAM_PREMIUM_WINDOW_SIZE_SEC, '1800', float),
Param(self.PARAM_PREMIUM_ADJUSTMENT_CAP, '0.001', D),
Param(self.PARAM_CANCEL_BY_EXCHANGE_ORDER_ID, 'True', bool),
Param(self.PARAM_ORDER_RATIO_TO_CANCEL_ALL, "0.5", D),
]
self._metrics_pub = mp()
self._risk_manager = rm(parent=self)
self._params_manager = pm(parent=self, params=strategy_parameters, config=self.config.get('parameters', {}))
self._reeval_task = None
self._publish_metrics = time.time()
self._metrics_publish_interval = 30
self.already_tracked_orders = set()
@property
def is_enabled(self):
return self._params_manager.get_param_value(self.PARAM_ENABLED)
@property
def publish_order_latency(self) -> bool:
return self._params_manager.get_param_value(self.PARAM_PUBLISH_ORDER_LATENCY)
@property
def premium_correction_factor(self) -> D:
return self._params_manager.get_param_value(self.PARAM_PREMIUM_FACTOR)
@property
def price_adjustment(self) -> D:
return self._params_manager.get_param_value(self.PARAM_PRICE_ADJUSTMENT_BPS) / D(10_000)
@property
def order_insert_time_ms(self) -> float:
return self._params_manager.get_param_value(self.PARAM_ORDER_INSERT_TIME_SEC) * 1000
@property
def reevaluation_time_sec(self) -> float:
return self._params_manager.get_param_value(self.PARAM_REEVAL_TIME_SEC) * 1000
@property
def base_volatility(self) -> D:
return self._params_manager.get_param_value(self.PARAM_BASE_VOLATILITY)
@property
def volatility_cap(self) -> D:
return self._params_manager.get_param_value(self.PARAM_VOLATILITY_CAP)
@property
def premium_adjustment_cap(self) -> D:
return self._params_manager.get_param_value(self.PARAM_PREMIUM_ADJUSTMENT_CAP)
@property
def volatility_exponent(self) -> D:
return self._params_manager.get_param_value(self.PARAM_VOLATILITY_EXPONENT)
@property
def active_orders(self) -> List[Order]:
return list(self.market_connector.active_orders.values())
@property
def pricing_basis_factor(self) -> D:
return self._params_manager.get_param_value(self.PARAM_PRICING_BASIS_FACTOR)
@property
def pricing_volatility_factor(self) -> D:
return self._params_manager.get_param_value(self.PARAM_PRICING_VOLATILITY_FACTOR)
@property
def vol_window_size(self) -> int:
return self._params_manager.get_param_value(self.PARAM_VOL_WINDOW_SIZE)
@property
def pricing_funding_rate_factor(self) -> D:
return self._params_manager.get_param_value(self.PARAM_PRICING_FUNDING_RATE_FACTOR)
@property
def premium_window_size_sec(self) -> float:
return self._params_manager.get_param_value(self.PARAM_PREMIUM_WINDOW_SIZE_SEC)
@property
def price_ema_sec(self) -> float:
return self._params_manager.get_param_value(self.PARAM_PRICE_EMA_SEC)
@property
def fr_ema_sec(self) -> float:
return self._params_manager.get_param_value(self.PARAM_FR_EMA_SEC)
@property
def basis_ema_sec(self) -> float:
return self._params_manager.get_param_value(self.PARAM_BASIS_EMA_SEC)
@property
def pos_lean_bps_per_100k(self) -> D:
return self._params_manager.get_param_value(self.PARAM_POS_LEAN_BPS_PER_100K_USD) / D(100_000) / D(10_000)
@property
def pos_global_lean_bps_per_100k(self) -> D:
return self._params_manager.get_param_value(self.PARAM_GLOBAL_POS_LEAN_BPS_PER_100K_USD) / D(100_000) / D(10_000)
@property
def empty_book_penalty(self) -> D:
return self._params_manager.get_param_value(self.PARAM_EMPTY_BOOK_PENALTY)
@property
def max_market_latency_ms(self) -> float:
return self._params_manager.get_param_value(self.PARAM_MAX_MARKET_LATENCY_SEC) * 1000
@property
def max_data_delay_ms(self) -> float:
return self._params_manager.get_param_value(self.PARAM_MAX_DATA_DELAY_SEC) * 1000
@property
def order_level_spread(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ORDER_LEVEL_SPREAD)
@property
def order_level_amount_bps(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ORDER_LEVEL_AMOUNT_PCT) / D('100')
@property
def buy_levels(self) -> int:
return self._params_manager.get_param_value(self.PARAM_BUY_LEVELS)
@property
def sell_levels(self) -> int:
return self._params_manager.get_param_value(self.PARAM_SELL_LEVELS)
@property
def bid_spread(self) -> D:
return self._params_manager.get_param_value(self.PARAM_BID_SPREAD) / D('100')
@property
def ask_spread(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ASK_SPREAD) / D('100')
@property
def order_level_spread_lambda(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ORDER_LEVEL_SPREAD_LAMBDA)
@property
def order_size_spread_lambda(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ORDER_SIZE_SPREAD_LAMBDA)
@property
def price_ceiling(self) -> D:
return self._params_manager.get_param_value(self.PARAM_PRICE_CEILING)
@property
def price_floor(self) -> D:
return self._params_manager.get_param_value(self.PARAM_PRICE_FLOOR)
@property
def taker_threshold_bps(self) -> D:
return self._params_manager.get_param_value(self.PARAM_TAKER_THRESHOLD_BPS)
@property
def order_amount_usd(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ORDER_AMOUNT_USD)
@property
def max_leverage(self) -> D:
return self._params_manager.get_param_value(self.PARAM_MAX_LEVERAGE)
@property
def max_margin_ratio(self) -> D:
return self._params_manager.get_param_value(self.PARAM_MAX_MARGIN_RATIO)
@property
def max_position_usd(self) -> D:
return self._params_manager.get_param_value(self.PARAM_MAX_POSITION_USD)
@property
def order_refresh_tolerance(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ORDER_REFRESH_TOLERANCE_PCT) / D('100')
@property
def order_size_obfuscation_factor_pct(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ORDER_SIZE_OBFUSCATION_FACTOR_PCT) / D('100')
@property
def minimum_spread(self) -> D:
return self._params_manager.get_param_value(self.PARAM_MINIMUM_SPREAD) / D('100')
@property
def bulk_requests(self) -> bool:
return self._params_manager.get_param_value(self.PARAM_BULK_REQUESTS)
@property
def external_price_multiplier(self) -> D:
return self._params_manager.get_param_value(self.PARAM_EXTERNAL_PRICE_MULTIPLIER)
@property
def fixed_order_size(self) -> D:
return self._params_manager.get_param_value(self.PARAM_FIXED_ORDER_SIZE)
@property
async def is_close_only_mode(self):
if self._params_manager.get_param_value(self.PARAM_CLOSE_ONLY_MODE):
return True
return not (await self._risk_manager.can_quote_to_open())
@property
def use_anchor_price(self) -> bool:
return self._params_manager.get_param_value(self.PARAM_ANCHOR_PRICE) is not None and \
self._params_manager.get_param_value(self.PARAM_ANCHOR_PRICE).is_finite() and \
self._params_manager.get_param_value(self.PARAM_ANCHOR_PRICE) != D('0')
@property
def anchor_price(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ANCHOR_PRICE)
@property
def min_order_amount(self) -> D:
if self.get_price_by_type(PriceType.Mid) is not None:
return self.market_connector.trading_rules[self.market].min_notional_size / self.get_price_by_type(PriceType.Mid)
elif self.get_fair_price() is not None:
return self.market_connector.trading_rules[self.market].min_notional_size / self.get_fair_price()
elif self.use_anchor_price:
return self.market_connector.trading_rules[self.market].min_notional_size / self.anchor_price
return D('0')
@property
def min_price_allowed(self) -> D:
return self.market_connector.trading_rules[self.market].min_price_increment
@property
def order_amount(self) -> D:
if self.get_price_by_type(PriceType.Mid) is not None:
return self.order_amount_usd / self.get_price_by_type(PriceType.Mid)
elif self.get_fair_price() is not None:
return self.order_amount_usd / self.get_fair_price()
elif self.use_anchor_price:
return self.order_amount_usd / self.anchor_price
return D('0')
@property
def market_is_swap(self) -> bool:
pattern = re.compile(r'^[A-Z]+-[A-Z]+-PERP$', re.IGNORECASE)
return bool(pattern.match(self.market))
@property
def cancel_by(self) -> str:
by_exchange_order_id = self._params_manager.get_param_value(self.PARAM_CANCEL_BY_EXCHANGE_ORDER_ID)
if by_exchange_order_id or by_exchange_order_id is None:
order_identifier = "exchange_order_id"
else:
order_identifier = "client_order_id"
return order_identifier
@property
def order_ratio_to_cancel_all(self) -> D:
return self._params_manager.get_param_value(self.PARAM_ORDER_RATIO_TO_CANCEL_ALL)
def get_order_amount(self, price: D = None) -> D:
if price is None:
return self.order_amount
return self.order_amount_usd / price
def now_ns(self) -> int:
"""
Return the current time in nanoseconds.
"""
return int(time.time_ns())
def now_ms(self) -> int:
"""
Return the current time in milliseconds.
"""
return int(self.now_ns() / 1e6)
def _publish_strat_metric(self, tag: str, val: Union[D, float]) -> None:
"""
Publish a metric to the metrics publisher.
"""
msg = MetricsMessage(
timestamp=int(time.time() * 1000),
process_name=self.algo_name,
tag_name=tag,
market=self.market,
value=val,
account=self.market_connector.account_info.get('account', 'N/A')
)
if isinstance(val, D):
if not val.is_finite():
self.logger.warning(f"Value for {tag} is not finite. Skipping publish.")
return
if isinstance(val, float):
if not np.isfinite(val):
self.logger.warning(f"Value for {tag} is not finite. Skipping publish.")
return
self._metrics_pub.stream_metrics(msg)
def create_base_proposal(self) -> Proposal:
"""
Create a base proposal for the market making strategy.
"""
market: ConnectorBase = self.market_connector
buys = []
sells = []
fair_buy = self.get_fair_price(side=Side.BUY)
fair_sell = self.get_fair_price(side=Side.SELL)
_num_ticks_increment = self.order_level_spread * market.trading_rules[self.market].min_price_increment
_order_increment = self.order_level_amount_bps / D(10_000) * self.order_amount
vol_ratio = self.get_vol_ratio()
_num_ticks_increment = _num_ticks_increment * (1 + D(vol_ratio))
_obfuscation_factor = 1 + self.order_size_obfuscation_factor_pct * D(np.random.uniform(0.8, 1.2))
for level in range(0, self.buy_levels):
price = fair_buy - ((np.exp(self.order_level_spread_lambda * level) - 1) * _num_ticks_increment)
if self.fixed_order_size > 0:
size = self.fixed_order_size
else:
size = self.get_order_amount(price)
size += (_order_increment * (np.exp(self.order_size_spread_lambda * level) - 1))
size *= _obfuscation_factor
if size > 0:
buys.append(PriceSize(price, size))
_obfuscation_factor = 1 + self.order_size_obfuscation_factor_pct * D(np.random.uniform(0.8, 1.2))
for level in range(0, self.sell_levels):
price = fair_sell + ((np.exp(self.order_level_spread_lambda * level) - 1) * _num_ticks_increment)
if self.fixed_order_size > 0:
size = self.fixed_order_size
else:
size = self.get_order_amount(price)
size += (_order_increment * (np.exp(self.order_size_spread_lambda * level) - 1))
size *= _obfuscation_factor
if size > 0:
sells.append(PriceSize(price, size))
return Proposal(buys, sells)
def quantize_values(self, proposal: Proposal) -> None:
"""
Quantize the prices and sizes of the orders in the proposal.
"""
market: ConnectorBase = self.market_connector
for buy in proposal.buys:
buy.price = market.quantize_order_price(self.market, buy.price)
buy.size = market.quantize_order_amount(self.market, buy.size)
for sell in proposal.sells:
sell.price = market.quantize_order_price(self.market, sell.price)
sell.size = market.quantize_order_amount(self.market, sell.size)
# filter if size is less than min_order_amount
proposal.buys = [buy for buy in proposal.buys if buy.size > 0]
proposal.sells = [sell for sell in proposal.sells if sell.size > 0]
# filter if price is less than min_price_allowed
proposal.buys = [buy for buy in proposal.buys if buy.price >= self.min_price_allowed]
proposal.sells = [sell for sell in proposal.sells if sell.price >= self.min_price_allowed]
def outside_tolerance(self, current_prices: List[D], proposal_prices: List[D]) -> Tuple[List[int], List[int]]:
"""
Check which orders are outside the tolerance and which are within the tolerance.
"""
within_tolerance = []
deviated = []
tolerances = [self.order_refresh_tolerance * D(1 + 2 * np.sqrt(i)) for i in range(len(current_prices))]
for idx, px in enumerate(proposal_prices):
if idx >= len(current_prices):
break
dev = abs(px - current_prices[idx]) / current_prices[idx]
if dev < tolerances[idx]:
within_tolerance.append(idx)
else:
deviated.append(idx)
if len(proposal_prices) < len(current_prices):
deviated.extend(range(len(proposal_prices), len(current_prices)))
return deviated, within_tolerance
def cancel_orders_below_min_spread(self) -> None:
"""
Cancel orders below the minimum spread.
"""
if self.minimum_spread <= 0:
return
price = self.get_price_by_type(PriceType.Mid)
if not price.is_finite():
price = self._smoothen_spot_price.value
ids_to_cancel = []
info_template = f"Order is below minimum spread ({self.minimum_spread}). Canceling Order "+"{} below min spread."
for order in self.active_orders:
negation = -1 if order.side == Side.BUY else 1
if (negation * (order.price - price) / price) < self.minimum_spread / 2:
ids_to_cancel.append(order.client_order_id)
self.cancel_multiple_orders(ids_to_cancel, info_template=info_template)
def cancel_active_orders(self, proposal: Proposal) -> None:
"""
Cancel orders outside the tolerance.
"""
_active_orders = [o for o in self.active_orders if o.status not in ['CANCELLING']]
if len(_active_orders) == 0:
return
buys_to_cancel = []
sells_to_cancel = []
buys_to_keep = []
sells_to_keep = []
active_buy_orders = [o for o in _active_orders if o.side == Side.BUY]
active_sell_orders = [o for o in _active_orders if o.side == Side.SELL]
active_buy_orders.sort(key=lambda x: x.price, reverse=True)
active_sell_orders.sort(key=lambda x: x.price)
active_buy_prices = [D(str(o.price)) for o in active_buy_orders]
active_sell_prices = [D(str(o.price)) for o in active_sell_orders]
active_buy_ids = [o.client_order_id for o in active_buy_orders]
active_sell_ids = [o.client_order_id for o in active_sell_orders]
if proposal is not None and self.order_refresh_tolerance >= 0:
proposal_buys = [buy.price for buy in proposal.buys]
proposal_sells = [sell.price for sell in proposal.sells]
self.logger.debug(f"active_buy_prices: {active_buy_prices}")
self.logger.debug(f"proposal_buys: {proposal_buys}")
buys_to_cancel, buys_to_keep = self.outside_tolerance(active_buy_prices, proposal_buys)
sells_to_cancel, sells_to_keep = self.outside_tolerance(active_sell_prices, proposal_sells)
self.logger.debug(f"buys_to_cancel: {buys_to_cancel}")
self.logger.debug(f"buys_to_keep: {buys_to_keep}")
else:
buys_to_cancel = range(len(_active_orders))
sells_to_cancel = range(len(_active_orders))
proposal.buys = [item for idx, item in enumerate(proposal.buys) if idx not in buys_to_keep]
proposal.sells = [item for idx, item in enumerate(proposal.sells) if idx not in sells_to_keep]
if len(buys_to_cancel) > 0 or len(sells_to_cancel) > 0:
buy_ids_to_cancel = [active_buy_ids[idx] for idx in buys_to_cancel]
sell_ids_to_cancel = [active_sell_ids[idx] for idx in sells_to_cancel]
ids_to_cancel = buy_ids_to_cancel + sell_ids_to_cancel
info_template = "(z) Canceling order {} outside of tolerance."
self.cancel_multiple_orders(ids_to_cancel, info_template=info_template)
else:
self.logger.info(f"Not canceling active orders since difference between new order prices "
f"and current order prices is within "
f"{self.order_refresh_tolerance:.2%} order_refresh_tolerance_pct")
async def apply_budget_constraint(self, proposal: Proposal) -> None:
"""
Apply the budget constraint to the proposal.
"""
if await self.is_close_only_mode:
tot_pos_usd = self.get_active_position(self.market)
if tot_pos_usd >= 0:
proposal.buys = []
elif tot_pos_usd <= 0:
proposal.sells = []
else:
proposal.buys = []
proposal.sells = []
def filter_out_takers(self, proposal: Proposal) -> None:
"""
Filter out takers, unless within the taker threshold.
"""
market: ConnectorBase = self.market_connector
top_ask =self.get_price_by_type(PriceType.BestAsk)
if top_ask is None or not top_ask.is_finite():
top_ask = self.get_fair_price(side=Side.SELL)
top_bid = self.get_price_by_type(PriceType.BestBid)
if top_bid is None or not top_bid.is_finite():
top_bid = self.get_fair_price(side=Side.BUY)
price_tick = market.trading_rules[self.market].min_price_increment * D(10)
if not top_ask.is_nan() and len(proposal.buys) > 0:
for idx, buy in enumerate(proposal.buys):
self.logger.debug(f"buy price: {buy.price}, top_ask: {top_ask}, price_tick: {price_tick}, thresh: {self.taker_threshold_bps}")
if idx == 0 and (buy.price / (top_ask+price_tick) - 1) * D(10_000) > self.taker_threshold_bps:
new_size = market.quantize_order_amount(self.market, max(min(buy.size, self.order_amount), self.min_order_amount))
proposal.buys[idx] = PriceSize(top_ask, new_size, OrderType.LIMIT)
elif buy.price >= top_ask:
proposal.buys[idx].price = top_bid - ((np.exp(self.order_level_spread_lambda * idx) - 1) * price_tick)
if not top_bid.is_nan() and len(proposal.sells) > 0:
for idx, sell in enumerate(proposal.sells):
self.logger.debug(f"sell price: {sell.price}, top_bid: {top_bid}, price_tick: {price_tick}, thresh: {self.taker_threshold_bps}")
if idx == 0 and ((top_bid-price_tick) / sell.price - 1) * D(10_000) > self.taker_threshold_bps:
new_size = market.quantize_order_amount(self.market, max(min(sell.size, self.order_amount), self.min_order_amount))
proposal.sells[idx] = PriceSize(top_bid, new_size, OrderType.LIMIT)
elif sell.price <= top_bid:
proposal.sells[idx].price = top_ask + ((np.exp(self.order_level_spread_lambda * idx) - 1) * price_tick)
def apply_order_levels_modifiers(self, proposal: Proposal) -> None:
"""
Apply the order levels modifiers to the proposal.
"""
self.apply_price_band(proposal)
def apply_price_band(self, proposal: Proposal) -> None:
"""
Apply the price band to the proposal.
"""
if self.price_ceiling > 0 and self.get_price_by_type(PriceType.BestAsk) >= self.price_ceiling:
proposal.buys = []
if self.price_floor > 0 and self.get_price_by_type(PriceType.BestBid) <= self.price_floor:
proposal.sells = []
def execute_orders_proposal(self, proposal: Proposal) -> None:
"""
Execute the orders proposal.
"""
if self.now_ms() < self._next_order_timestamp:
return
else:
self._next_order_timestamp = self.now_ms() + self.order_insert_time_ms
all_orders = []
for oreq in proposal.buys:
all_orders.append(Order(self.market, Side.BUY, oreq.price, oreq.size, oreq.type))
for oreq in proposal.sells:
all_orders.append(Order(self.market, Side.SELL, oreq.price, oreq.size, oreq.type))
if self.bulk_requests:
self.market_connector.bulk_insert_orders(all_orders)
else:
for order in all_orders:
self.market_connector.insert_order(order)
self._publish_strat_metric("order_insert", len(all_orders))
def market_data_ready(self) -> bool:
"""
Check if the market data is ready.
"""
if self.market not in self.market_connector.orderbooks:
self.logger.warning(f"Market data not ready for {self.market}.")
return False
if self._smoothen_basis is None or self._smoothen_basis.value is None:
self.logger.warning("Funding rate not ready.")
return False
if self._smoothen_spot_price is None or self._smoothen_spot_price.value is None:
self.logger.warning("Spot price not ready.")
return False
if self._rolling_vol is None:
self.logger.warning("Volatility not ready.")
return False
return True
async def reeval(self, trigger: TriggerType) -> None:
"""
Reevaluate the strategy.
"""
if self.processing:
self.logger.warning("Already processing. Skipping reeval.")
return
self.processing = True
try:
if not self.is_enabled:
self.logger.info("Strategy is disabled. Skipping reeval.")
self.cancel_all_orders()
self.processing = False
return
if not self._risk_manager.is_system_health_ok():
self.logger.warning("System health deteriorated. Market making will be halted.")
self.cancel_all_orders()
self.processing = False
if self.now_ms() - self._last_system_health_ok > 2 * 60 * 1000 and self.now_ms() - self._last_re_sub_timestamp > 30 * 1000:
self._last_re_sub_timestamp = self.now_ms()
self.logger.warning("Re-subscribing to data channels.")
# re-subscribe to data channels
await self._subscribe_to_data()
return
else:
self._last_system_health_ok = self.now_ms()
if self.now_ms() < self._next_reeval_timestamp:
self.processing = False
return
else:
self._next_reeval_timestamp = self.now_ms() + self.reevaluation_time_sec
if not self.market_data_ready():
self.processing = False
return
proposal = self.create_base_proposal()
self.logger.debug(f"Initial proposals: {proposal}")
# 2. Apply functions that limit numbers of buys and sells proposal
self.apply_order_levels_modifiers(proposal)
self.logger.debug(f"Proposals after order level modifier: {proposal}")
# 4. Apply taker threshold, i.e. don't take more than a certain percentage of the market.
self.filter_out_takers(proposal)
self.logger.debug(f"Proposals after takers filter: {proposal}")
await self.apply_budget_constraint(proposal)
self.logger.debug(f"Proposals after budget constraint: {proposal}")
self.quantize_values(proposal)
self.logger.debug(f"Proposals after quantization: {proposal}")
self.cancel_active_orders(proposal)
self.logger.debug(f"Filtered proposal: {proposal}")
self.cancel_orders_below_min_spread()
self.logger.info(f"final proposal: {proposal}")
self.execute_orders_proposal(proposal)
except Exception as e:
self.logger.error(f"Error in reeval: {e}")
self.logger.error(traceback.format_exc())
finally:
self.processing = False
def cancel_order(self, mkt: str, client_order_id: str, info_template: Union[None, str] = None) -> None:
"""
Cancel an order by its client order ID. Which endpoint to use at the exchange side is set by 'PARAM_CANCEL_BY_EXCHANGE_ORDER_ID'
"""
cancel_by = self.cancel_by
self.market_connector.cancel_order(client_order_id, by=cancel_by)
if info_template is not None:
self.logger.info(info_template.format(client_order_id))
def cancel_multiple_orders(self, client_order_ids: List[str], info_template : Union[None, str] = None) -> None:
"""
Cancel a list of orders. If the (# ordersToCancel) / (# activeOrders) > PARAM_ORDER_RATIO_TO_CANCEL_ALL, then will cancel all.
"""
n_orders_to_cancel = len(client_order_ids)
if n_orders_to_cancel == 0:
self.logger.warning(f"Got passed an empty client_order_ids, do nothing and return None")
return
n_active_orders = len(self.active_orders)
cancel_all = False
inform = True
order_ratio_to_cancel_all = self.order_ratio_to_cancel_all
if n_active_orders > 0:
cancel_ratio = n_orders_to_cancel / n_active_orders
if abs(cancel_ratio - 1) < 1e-10: # wanted to cancel all anyway
inform = False
cancel_all = (cancel_ratio > order_ratio_to_cancel_all)
if cancel_all:
if inform:
self.logger.info(f"Canceling {n_orders_to_cancel} out of {n_active_orders} active, which is > {order_ratio_to_cancel_all}, will just cancel all")
if info_template is not None:
active_coids = [order.client_order_id for order in self.active_orders]
self.logger.info(info_template.format(active_coids))
self.cancel_all_orders()
else:
for coid in client_order_ids:
self.cancel_order(self.market, coid, info_template=info_template)
def cancel_all_orders(self) -> None:
"""
Cancel all active orders.
"""
if self.bulk_requests:
self.market_connector.cancel_all_orders(self.market)
else:
for order in self.active_orders:
self.cancel_order(self.market, order.client_order_id)
def get_price_by_type(self, price_type: PriceType = None) -> D:
"""
Get the price by type.
"""
if price_type == PriceType.BestBid:
_val = self.market_connector.orderbooks[self.market].get_best_bid()
elif price_type == PriceType.BestAsk:
_val = self.market_connector.orderbooks[self.market].get_best_ask()
else:
_val = self.market_connector.orderbooks[self.market].get_mid()
return D(_val) if _val is not None else None
def get_external_connector_price(self, mkt, price_type: PriceType = None) -> D:
"""
Get the price by type from the external connector.
"""
if mkt not in self.external_connector.orderbooks:
self.logger.warning(f"External connector does not have {mkt}.")
return None
if price_type == PriceType.BestBid: