-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathquantum_grid_allocator.py
More file actions
492 lines (461 loc) · 23.8 KB
/
quantum_grid_allocator.py
File metadata and controls
492 lines (461 loc) · 23.8 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
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
from decimal import Decimal
from typing import Dict, List, Set, Union
import pandas_ta as ta # noqa: F401
from pydantic import Field, field_validator
from hummingbot.core.data_type.common import OrderType, PositionMode, PriceType, TradeType
from hummingbot.data_feed.candles_feed.data_types import CandlesConfig
from hummingbot.strategy_v2.controllers import ControllerBase, ControllerConfigBase
from hummingbot.strategy_v2.executors.data_types import ConnectorPair
from hummingbot.strategy_v2.executors.grid_executor.data_types import GridExecutorConfig
from hummingbot.strategy_v2.executors.position_executor.data_types import TripleBarrierConfig
from hummingbot.strategy_v2.models.executor_actions import CreateExecutorAction, StopExecutorAction
from hummingbot.strategy_v2.models.executors_info import ExecutorInfo
class QGAConfig(ControllerConfigBase):
controller_name: str = "quantum_grid_allocator"
candles_config: List[CandlesConfig] = []
# Portfolio allocation zones
long_only_threshold: Decimal = Field(default=Decimal("0.2"), json_schema_extra={"is_updatable": True})
short_only_threshold: Decimal = Field(default=Decimal("0.2"), json_schema_extra={"is_updatable": True})
hedge_ratio: Decimal = Field(default=Decimal("2"), json_schema_extra={"is_updatable": True})
# Grid allocation multipliers
base_grid_value_pct: Decimal = Field(default=Decimal("0.08"), json_schema_extra={"is_updatable": True})
max_grid_value_pct: Decimal = Field(default=Decimal("0.15"), json_schema_extra={"is_updatable": True})
# Order frequency settings
safe_extra_spread: Decimal = Field(default=Decimal("0.0001"), json_schema_extra={"is_updatable": True})
favorable_order_frequency: int = Field(default=2, json_schema_extra={"is_updatable": True})
unfavorable_order_frequency: int = Field(default=5, json_schema_extra={"is_updatable": True})
max_orders_per_batch: int = Field(default=1, json_schema_extra={"is_updatable": True})
# Portfolio allocation
portfolio_allocation: Dict[str, Decimal] = Field(
default={
"SOL": Decimal("0.50"), # 50%
},
json_schema_extra={"is_updatable": True})
# Grid parameters
grid_range: Decimal = Field(default=Decimal("0.002"), json_schema_extra={"is_updatable": True})
tp_sl_ratio: Decimal = Field(default=Decimal("0.8"), json_schema_extra={"is_updatable": True})
min_order_amount: Decimal = Field(default=Decimal("5"), json_schema_extra={"is_updatable": True})
# Risk parameters
max_deviation: Decimal = Field(default=Decimal("0.05"), json_schema_extra={"is_updatable": True})
max_open_orders: int = Field(default=2, json_schema_extra={"is_updatable": True})
# Exchange settings
connector_name: str = "binance"
leverage: int = 1
position_mode: PositionMode = PositionMode.HEDGE
quote_asset: str = "FDUSD"
fee_asset: str = "BNB"
# Grid price multipliers
min_spread_between_orders: Decimal = Field(
default=Decimal("0.0001"), # 0.01% between orders
json_schema_extra={"is_updatable": True})
grid_tp_multiplier: Decimal = Field(
default=Decimal("0.0001"), # 0.2% take profit
json_schema_extra={"is_updatable": True})
# Grid safety parameters
limit_price_spread: Decimal = Field(
default=Decimal("0.001"), # 0.1% spread for limit price
json_schema_extra={"is_updatable": True})
activation_bounds: Decimal = Field(
default=Decimal("0.0002"), # Activation bounds for orders
json_schema_extra={"is_updatable": True})
bb_length: int = 100
bb_std_dev: float = 2.0
interval: str = "1s"
dynamic_grid_range: bool = Field(default=False, json_schema_extra={"is_updatable": True})
show_terminated_details: bool = False
@property
def quote_asset_allocation(self) -> Decimal:
"""Calculate the implicit quote asset (FDUSD) allocation"""
return Decimal("1") - sum(self.portfolio_allocation.values())
@field_validator("portfolio_allocation")
@classmethod
def validate_allocation(cls, v):
total = sum(v.values())
if total >= Decimal("1"):
raise ValueError(f"Total allocation {total} exceeds or equals 100%. Must leave room for FDUSD allocation.")
if "FDUSD" in v:
raise ValueError("FDUSD should not be explicitly allocated as it is the quote asset")
return v
def update_markets(self, markets: Dict[str, Set[str]]) -> Dict[str, Set[str]]:
if self.connector_name not in markets:
markets[self.connector_name] = set()
for asset in self.portfolio_allocation:
markets[self.connector_name].add(f"{asset}-{self.quote_asset}")
return markets
class QuantumGridAllocator(ControllerBase):
def __init__(self, config: QGAConfig, *args, **kwargs):
self.config = config
self.metrics = {}
# Track unfavorable grid IDs
self.unfavorable_grid_ids = set()
# Track held positions from unfavorable grids
self.unfavorable_positions = {
f"{asset}-{config.quote_asset}": {
'long': {'size': Decimal('0'), 'value': Decimal('0'), 'weighted_price': Decimal('0')},
'short': {'size': Decimal('0'), 'value': Decimal('0'), 'weighted_price': Decimal('0')}
}
for asset in config.portfolio_allocation
}
self.config.candles_config = [CandlesConfig(
connector=config.connector_name,
trading_pair=trading_pair + "-" + config.quote_asset,
interval=config.interval,
max_records=config.bb_length + 100
) for trading_pair in config.portfolio_allocation.keys()]
super().__init__(config, *args, **kwargs)
self.initialize_rate_sources()
def initialize_rate_sources(self):
fee_pair = ConnectorPair(connector_name=self.config.connector_name, trading_pair=f"{self.config.fee_asset}-{self.config.quote_asset}")
self.market_data_provider.initialize_rate_sources([fee_pair])
async def update_processed_data(self):
# Get the bb width to use it as the range for the grid
for asset in self.config.portfolio_allocation:
trading_pair = f"{asset}-{self.config.quote_asset}"
candles = self.market_data_provider.get_candles_df(
connector_name=self.config.connector_name,
trading_pair=trading_pair,
interval=self.config.interval,
max_records=self.config.bb_length + 100
)
if len(candles) == 0:
bb_width = self.config.grid_range
else:
bb = ta.bbands(candles["close"], length=self.config.bb_length, lower_std=self.config.bb_std_dev, upper_std=self.config.bb_std_dev)
bb_width = bb[f"BBB_{self.config.bb_length}_{self.config.bb_std_dev}_{self.config.bb_std_dev}"].iloc[-1] / 100
self.processed_data[trading_pair] = {
"bb_width": bb_width
}
def update_portfolio_metrics(self):
"""
Calculate theoretical vs actual portfolio allocations
"""
metrics = {
"theoretical": {},
"actual": {},
"difference": {},
}
# Get real balances and calculate total portfolio value
quote_balance = self.market_data_provider.get_balance(self.config.connector_name, self.config.quote_asset)
total_value_quote = quote_balance
# Calculate actual allocations including positions
for asset in self.config.portfolio_allocation:
trading_pair = f"{asset}-{self.config.quote_asset}"
price = self.get_mid_price(trading_pair)
# Get balance and add any position from active grid
balance = self.market_data_provider.get_balance(self.config.connector_name, asset)
value = balance * price
total_value_quote += value
metrics["actual"][asset] = value
# Calculate theoretical allocations and differences
for asset in self.config.portfolio_allocation:
theoretical_value = total_value_quote * self.config.portfolio_allocation[asset]
metrics["theoretical"][asset] = theoretical_value
metrics["difference"][asset] = metrics["actual"][asset] - theoretical_value
# Add quote asset metrics
metrics["actual"][self.config.quote_asset] = quote_balance
metrics["theoretical"][self.config.quote_asset] = total_value_quote * self.config.quote_asset_allocation
metrics["difference"][self.config.quote_asset] = quote_balance - metrics["theoretical"][self.config.quote_asset]
metrics["total_portfolio_value"] = total_value_quote
self.metrics = metrics
def get_active_grids_by_asset(self) -> Dict[str, List[ExecutorInfo]]:
"""Group active grids by asset using filter_executors"""
active_grids = {}
for asset in self.config.portfolio_allocation:
if asset == self.config.quote_asset:
continue
trading_pair = f"{asset}-{self.config.quote_asset}"
active_executors = self.filter_executors(
executors=self.executors_info,
filter_func=lambda e: (
e.is_active and
e.config.trading_pair == trading_pair
)
)
if active_executors:
active_grids[asset] = active_executors
return active_grids
def to_format_status(self) -> List[str]:
"""Generate a detailed status report with portfolio, grid, and position information"""
status_lines = []
total_value = self.metrics.get("total_portfolio_value", Decimal("0"))
# Portfolio Status
status_lines.append(f"Total Portfolio Value: ${total_value:,.2f}")
status_lines.append("")
status_lines.append("Portfolio Status:")
status_lines.append("-" * 80)
status_lines.append(
f"{'Asset':<8} | "
f"{'Actual':>10} | "
f"{'Target':>10} | "
f"{'Diff':>10} | "
f"{'Dev %':>8}"
)
status_lines.append("-" * 80)
# Show metrics for each asset
for asset in self.config.portfolio_allocation:
actual = self.metrics["actual"].get(asset, Decimal("0"))
theoretical = self.metrics["theoretical"].get(asset, Decimal("0"))
difference = self.metrics["difference"].get(asset, Decimal("0"))
deviation_pct = (difference / theoretical * 100) if theoretical != Decimal("0") else Decimal("0")
status_lines.append(
f"{asset:<8} | "
f"${actual:>9.2f} | "
f"${theoretical:>9.2f} | "
f"${difference:>+9.2f} | "
f"{deviation_pct:>+7.1f}%"
)
# Add quote asset metrics
quote_asset = self.config.quote_asset
actual = self.metrics["actual"].get(quote_asset, Decimal("0"))
theoretical = self.metrics["theoretical"].get(quote_asset, Decimal("0"))
difference = self.metrics["difference"].get(quote_asset, Decimal("0"))
deviation_pct = (difference / theoretical * 100) if theoretical != Decimal("0") else Decimal("0")
status_lines.append("-" * 80)
status_lines.append(
f"{quote_asset:<8} | "
f"${actual:>9.2f} | "
f"${theoretical:>9.2f} | "
f"${difference:>+9.2f} | "
f"{deviation_pct:>+7.1f}%"
)
# Active Grids Summary
active_grids = self.get_active_grids_by_asset()
if active_grids:
status_lines.append("")
status_lines.append("Active Grids:")
status_lines.append("-" * 140)
status_lines.append(
f"{'Asset':<8} {'Side':<6} | "
f"{'Total ($)':<10} {'Position':<10} {'Volume':<10} | "
f"{'PnL':<10} {'RPnL':<10} {'Fees':<10} | "
f"{'Start':<10} {'Current':<10} {'End':<10} {'Limit':<10}"
)
status_lines.append("-" * 140)
for asset, executors in active_grids.items():
for executor in executors:
config = executor.config
custom_info = executor.custom_info
trading_pair = config.trading_pair
current_price = self.get_mid_price(trading_pair)
# Get grid metrics
total_amount = Decimal(str(config.total_amount_quote))
position_size = Decimal(str(custom_info.get('position_size_quote', '0')))
volume = executor.filled_amount_quote
pnl = executor.net_pnl_quote
realized_pnl_quote = custom_info.get('realized_pnl_quote', Decimal('0'))
fees = executor.cum_fees_quote
status_lines.append(
f"{asset:<8} {config.side.name:<6} | "
f"${total_amount:<9.2f} ${position_size:<9.2f} ${volume:<9.2f} | "
f"${pnl:>+9.2f} ${realized_pnl_quote:>+9.2f} ${fees:>9.2f} | "
f"{config.start_price:<10.4f} {current_price:<10.4f} {config.end_price:<10.4f} {config.limit_price:<10.4f}"
)
status_lines.append("-" * 100 + "\n")
return status_lines
def tp_multiplier(self):
return self.config.tp_sl_ratio
def sl_multiplier(self):
return 1 - self.config.tp_sl_ratio
def determine_executor_actions(self) -> List[Union[CreateExecutorAction, StopExecutorAction]]:
actions = []
self.update_portfolio_metrics()
active_grids_by_asset = self.get_active_grids_by_asset()
for asset in self.config.portfolio_allocation:
if asset == self.config.quote_asset:
continue
trading_pair = f"{asset}-{self.config.quote_asset}"
# Check if there are any active grids for this asset
if asset in active_grids_by_asset:
self.logger().debug(f"Skipping {trading_pair} - Active grid exists")
continue
theoretical = self.metrics["theoretical"][asset]
difference = self.metrics["difference"][asset]
deviation = difference / theoretical if theoretical != Decimal("0") else Decimal("0")
mid_price = self.get_mid_price(trading_pair)
# Calculate dynamic grid value percentage based on deviation
abs_deviation = abs(deviation)
grid_value_pct = self.config.max_grid_value_pct if abs_deviation > self.config.max_deviation else self.config.base_grid_value_pct
self.logger().info(
f"{trading_pair} Grid Sizing - "
f"Deviation: {deviation:+.1%}, "
f"Grid Value %: {grid_value_pct:.1%}"
)
if self.config.dynamic_grid_range:
grid_range = Decimal(self.processed_data[trading_pair]["bb_width"])
else:
grid_range = self.config.grid_range
# Determine which zone we're in by normalizing the deviation over the theoretical allocation
if deviation < -self.config.long_only_threshold:
# Long-only zone - only create buy grids
if difference < Decimal("0"): # Only if we need to buy
grid_value = min(abs(difference), theoretical * grid_value_pct)
start_price = mid_price * (1 - grid_range * self.sl_multiplier())
end_price = mid_price * (1 + grid_range * self.tp_multiplier())
grid_action = self.create_grid_executor(
trading_pair=trading_pair,
side=TradeType.BUY,
start_price=start_price,
end_price=end_price,
grid_value=grid_value,
is_unfavorable=False
)
if grid_action is not None:
actions.append(grid_action)
elif deviation > self.config.short_only_threshold:
# Short-only zone - only create sell grids
if difference > Decimal("0"): # Only if we need to sell
grid_value = min(abs(difference), theoretical * grid_value_pct)
start_price = mid_price * (1 - grid_range * self.tp_multiplier())
end_price = mid_price * (1 + grid_range * self.sl_multiplier())
grid_action = self.create_grid_executor(
trading_pair=trading_pair,
side=TradeType.SELL,
start_price=start_price,
end_price=end_price,
grid_value=grid_value,
is_unfavorable=False
)
if grid_action is not None:
actions.append(grid_action)
else:
# we create a buy and a sell grid with higher range pct and the base grid value pct
# to hedge the position
grid_value = theoretical * grid_value_pct
if difference < Decimal("0"): # create a bigger buy grid and sell grid
# Create buy grid
start_price = mid_price * (1 - 2 * grid_range * self.sl_multiplier())
end_price = mid_price * (1 + grid_range * self.tp_multiplier())
buy_grid_action = self.create_grid_executor(
trading_pair=trading_pair,
side=TradeType.BUY,
start_price=start_price,
end_price=end_price,
grid_value=grid_value,
is_unfavorable=False
)
if buy_grid_action is not None:
actions.append(buy_grid_action)
# Create sell grid
start_price = mid_price * (1 - grid_range * self.tp_multiplier())
end_price = mid_price * (1 + 2 * grid_range * self.sl_multiplier())
sell_grid_action = self.create_grid_executor(
trading_pair=trading_pair,
side=TradeType.SELL,
start_price=start_price,
end_price=end_price,
grid_value=grid_value,
is_unfavorable=False
)
if sell_grid_action is not None:
actions.append(sell_grid_action)
if difference > Decimal("0"):
# Create sell grid
start_price = mid_price * (1 - 2 * grid_range * self.tp_multiplier())
end_price = mid_price * (1 + grid_range * self.sl_multiplier())
sell_grid_action = self.create_grid_executor(
trading_pair=trading_pair,
side=TradeType.SELL,
start_price=start_price,
end_price=end_price,
grid_value=grid_value,
is_unfavorable=False
)
if sell_grid_action is not None:
actions.append(sell_grid_action)
# Create buy grid
start_price = mid_price * (1 - grid_range * self.sl_multiplier())
end_price = mid_price * (1 + 2 * grid_range * self.tp_multiplier())
buy_grid_action = self.create_grid_executor(
trading_pair=trading_pair,
side=TradeType.BUY,
start_price=start_price,
end_price=end_price,
grid_value=grid_value,
is_unfavorable=False
)
if buy_grid_action is not None:
actions.append(buy_grid_action)
return actions
def create_grid_executor(
self,
trading_pair: str,
side: TradeType,
start_price: Decimal,
end_price: Decimal,
grid_value: Decimal,
is_unfavorable: bool = False
) -> CreateExecutorAction:
"""Creates a grid executor with dynamic sizing and range adjustments"""
# Get trading rules and minimum notional
trading_rules = self.market_data_provider.get_trading_rules(self.config.connector_name, trading_pair)
min_notional = max(
self.config.min_order_amount,
trading_rules.min_notional_size if trading_rules else Decimal("5.0")
)
# Add safety margin and check if grid value is sufficient
min_grid_value = min_notional * Decimal("5") # Ensure room for at least 5 levels
if grid_value < min_grid_value:
self.logger().info(
f"Grid value {grid_value} is too small for {trading_pair}. "
f"Minimum required for viable grid: {min_grid_value}"
)
return None # Skip grid creation if value is too small
# Select order frequency based on grid favorability
order_frequency = (
self.config.unfavorable_order_frequency if is_unfavorable
else self.config.favorable_order_frequency
)
# Calculate limit price to be more aggressive than grid boundaries
if side == TradeType.BUY:
# For buys, limit price should be lower than start price
limit_price = start_price * (1 - self.config.limit_price_spread)
else:
# For sells, limit price should be higher than end price
limit_price = end_price * (1 + self.config.limit_price_spread)
# Create the executor action
action = CreateExecutorAction(
controller_id=self.config.id,
executor_config=GridExecutorConfig(
timestamp=self.market_data_provider.time(),
connector_name=self.config.connector_name,
trading_pair=trading_pair,
side=side,
start_price=start_price,
end_price=end_price,
limit_price=limit_price,
leverage=self.config.leverage,
total_amount_quote=grid_value,
safe_extra_spread=self.config.safe_extra_spread,
min_spread_between_orders=self.config.min_spread_between_orders,
min_order_amount_quote=self.config.min_order_amount,
max_open_orders=self.config.max_open_orders,
order_frequency=order_frequency, # Use dynamic order frequency
max_orders_per_batch=self.config.max_orders_per_batch,
activation_bounds=self.config.activation_bounds,
keep_position=True, # Always keep position for potential reversal
coerce_tp_to_step=True,
triple_barrier_config=TripleBarrierConfig(
take_profit=self.config.grid_tp_multiplier,
open_order_type=OrderType.LIMIT_MAKER,
take_profit_order_type=OrderType.LIMIT_MAKER,
stop_loss=None,
time_limit=None,
trailing_stop=None,
)))
# Track unfavorable grid configs
if is_unfavorable:
self.unfavorable_grid_ids.add(action.executor_config.id)
self.logger().info(
f"Created unfavorable grid for {trading_pair} - "
f"Side: {side.name}, Value: ${grid_value:,.2f}, "
f"Order Frequency: {order_frequency}s"
)
else:
self.logger().info(
f"Created favorable grid for {trading_pair} - "
f"Side: {side.name}, Value: ${grid_value:,.2f}, "
f"Order Frequency: {order_frequency}s"
)
return action
def get_mid_price(self, trading_pair: str) -> Decimal:
return self.market_data_provider.get_price_by_type(self.config.connector_name, trading_pair, PriceType.MidPrice)