Spaces:
Sleeping
Sleeping
Naufal Arhab commited on
Commit ·
fe8dffc
1
Parent(s): 3ce30ae
Fix: guard trade quantity and buyer maker dictionary access against KeyError in CVD computations
Browse files- backend/engine/signal_engine.py +12 -12
backend/engine/signal_engine.py
CHANGED
|
@@ -123,8 +123,8 @@ def _compute_spot_futures_cvd(
|
|
| 123 |
}
|
| 124 |
|
| 125 |
if not spot_trades:
|
| 126 |
-
fut_buy = sum(t
|
| 127 |
-
fut_sell = sum(t
|
| 128 |
futures_cvd = fut_buy - fut_sell
|
| 129 |
return {
|
| 130 |
"value": f"Perp: {futures_cvd:+.1f} (No Spot)",
|
|
@@ -138,25 +138,25 @@ def _compute_spot_futures_cvd(
|
|
| 138 |
sorted_spot = sorted(spot_trades, key=lambda t: t.get("time", 0))
|
| 139 |
sorted_fut = sorted(futures_trades, key=lambda t: t.get("time", 0))
|
| 140 |
|
| 141 |
-
spot_buy = sum(t
|
| 142 |
-
spot_sell = sum(t
|
| 143 |
spot_cvd = spot_buy - spot_sell
|
| 144 |
|
| 145 |
-
fut_buy = sum(t
|
| 146 |
-
fut_sell = sum(t
|
| 147 |
futures_cvd = fut_buy - fut_sell
|
| 148 |
|
| 149 |
# Check momentum in second half
|
| 150 |
mid_s = len(sorted_spot) // 2
|
| 151 |
spot_rising = (
|
| 152 |
-
sum(t
|
| 153 |
-
> sum(t
|
| 154 |
) if mid_s > 0 else (spot_cvd > 0)
|
| 155 |
|
| 156 |
mid_f = len(sorted_fut) // 2
|
| 157 |
fut_rising = (
|
| 158 |
-
sum(t
|
| 159 |
-
> sum(t
|
| 160 |
) if mid_f > 0 else (futures_cvd > 0)
|
| 161 |
|
| 162 |
spot_bull = spot_cvd > 0 and spot_rising
|
|
@@ -364,10 +364,10 @@ def _compute_cvd(trades: list[dict]) -> dict[str, Any]:
|
|
| 364 |
mid = len(sorted_trades) // 2
|
| 365 |
if mid > 0:
|
| 366 |
first_half_cvd = sum(
|
| 367 |
-
(t
|
| 368 |
)
|
| 369 |
second_half_cvd = sum(
|
| 370 |
-
(t
|
| 371 |
)
|
| 372 |
cvd_rising = second_half_cvd > first_half_cvd
|
| 373 |
else:
|
|
|
|
| 123 |
}
|
| 124 |
|
| 125 |
if not spot_trades:
|
| 126 |
+
fut_buy = sum(t.get("qty", 0.0) for t in futures_trades if not t.get("is_buyer_maker", False))
|
| 127 |
+
fut_sell = sum(t.get("qty", 0.0) for t in futures_trades if t.get("is_buyer_maker", False))
|
| 128 |
futures_cvd = fut_buy - fut_sell
|
| 129 |
return {
|
| 130 |
"value": f"Perp: {futures_cvd:+.1f} (No Spot)",
|
|
|
|
| 138 |
sorted_spot = sorted(spot_trades, key=lambda t: t.get("time", 0))
|
| 139 |
sorted_fut = sorted(futures_trades, key=lambda t: t.get("time", 0))
|
| 140 |
|
| 141 |
+
spot_buy = sum(t.get("qty", 0.0) for t in sorted_spot if not t.get("is_buyer_maker", False))
|
| 142 |
+
spot_sell = sum(t.get("qty", 0.0) for t in sorted_spot if t.get("is_buyer_maker", False))
|
| 143 |
spot_cvd = spot_buy - spot_sell
|
| 144 |
|
| 145 |
+
fut_buy = sum(t.get("qty", 0.0) for t in sorted_fut if not t.get("is_buyer_maker", False))
|
| 146 |
+
fut_sell = sum(t.get("qty", 0.0) for t in sorted_fut if t.get("is_buyer_maker", False))
|
| 147 |
futures_cvd = fut_buy - fut_sell
|
| 148 |
|
| 149 |
# Check momentum in second half
|
| 150 |
mid_s = len(sorted_spot) // 2
|
| 151 |
spot_rising = (
|
| 152 |
+
sum(t.get("qty", 0.0) if not t.get("is_buyer_maker", False) else -t.get("qty", 0.0) for t in sorted_spot[mid_s:])
|
| 153 |
+
> sum(t.get("qty", 0.0) if not t.get("is_buyer_maker", False) else -t.get("qty", 0.0) for t in sorted_spot[:mid_s])
|
| 154 |
) if mid_s > 0 else (spot_cvd > 0)
|
| 155 |
|
| 156 |
mid_f = len(sorted_fut) // 2
|
| 157 |
fut_rising = (
|
| 158 |
+
sum(t.get("qty", 0.0) if not t.get("is_buyer_maker", False) else -t.get("qty", 0.0) for t in sorted_fut[mid_f:])
|
| 159 |
+
> sum(t.get("qty", 0.0) if not t.get("is_buyer_maker", False) else -t.get("qty", 0.0) for t in sorted_fut[:mid_f])
|
| 160 |
) if mid_f > 0 else (futures_cvd > 0)
|
| 161 |
|
| 162 |
spot_bull = spot_cvd > 0 and spot_rising
|
|
|
|
| 364 |
mid = len(sorted_trades) // 2
|
| 365 |
if mid > 0:
|
| 366 |
first_half_cvd = sum(
|
| 367 |
+
(t.get("qty", 0.0) if not t.get("is_buyer_maker", False) else -t.get("qty", 0.0)) for t in sorted_trades[:mid]
|
| 368 |
)
|
| 369 |
second_half_cvd = sum(
|
| 370 |
+
(t.get("qty", 0.0) if not t.get("is_buyer_maker", False) else -t.get("qty", 0.0)) for t in sorted_trades[mid:]
|
| 371 |
)
|
| 372 |
cvd_rising = second_half_cvd > first_half_cvd
|
| 373 |
else:
|