Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/gemini-review.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ jobs:
github_pr_number: '${{ github.event.pull_request.number }}'
settings: |-
{
"security": {
"trustAllWorkspaces": true
},
"model": {
"maxSessionTurns": 25
},
Expand Down
16 changes: 10 additions & 6 deletions advanced_predictors.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ def __init__(
self,
impact_bps_per_imbalance: Decimal,
min_confidence: Decimal,
trade_flow_volatility_scaling: Decimal,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve AdvancedPredictor constructor compatibility

Making trade_flow_volatility_scaling a required constructor argument introduces a runtime TypeError for callers still using the previous two-argument API. In this repo, tests/test_strategy_components.py still constructs AdvancedPredictor without the new parameter, so this commit breaks that execution path unless a default is provided or all callers are updated.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gemini-cli /review 帮我回答以及修复这个问题

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gemini-cli /review 帮我回答以及修复这个问题

volatility_window: int = 10, # 计算波动率使用的窗口大小
) -> None:
self.impact_bps_per_imbalance = impact_bps_per_imbalance
self.min_confidence = min_confidence
self.trade_flow_volatility_scaling = trade_flow_volatility_scaling
# 使用 deque 可以高效地在固定大小的窗口上进行操作
self.mid_price_history = deque(maxlen=volatility_window)

Expand All @@ -33,20 +35,22 @@ def predict(self, features: AdvancedMarketFeatures) -> Prediction:
# 1. 计算波动率因子
self.mid_price_history.append(features.quote.mid)
volatility = Decimal(statistics.stdev(self.mid_price_history)) if len(self.mid_price_history) > 1 else Decimal("0")
# 将波动率归一化,使其成为一个相对指标,避免受价格本身大小的影响
normalized_volatility = volatility / features.quote.mid if features.quote.mid > 0 else Decimal("0")

# 2. 计算基础价格偏移 (来自原始ImbalancePredictor)
base_edge_bps = self._microprice_edge_bps(features) + features.imbalance * self.impact_bps_per_imbalance

# 3. 计算交易流调整量 (我们的新alpha!)
# 规则:如果交易流为正(买方更激进),我们稍微调高预测价,反之亦然
# 调整的幅度可以根据置信度等进行缩放。
trade_flow_adjustment_bps = features.trade_flow * Decimal("10") # 示例:每0.1的flow,价格偏移1 bps
# 3. 计算交易流调整量 (新的Alpha)
# 规则:交易流的影响力与波动率成反比。波动越大,交易流的权重越低
volatility_dampening_factor = (Decimal("1") + normalized_volatility * Decimal("100")) # 波动率越高,这个因子越大
trade_flow_adjustment_bps = (features.trade_flow * self.trade_flow_volatility_scaling) / volatility_dampening_factor

# 4. 组合所有因子
total_edge_bps = base_edge_bps + trade_flow_adjustment_bps

# 5. 风险调整:如果波动率过高,降低预测的偏移量,甚至不进行预测
if volatility > (features.quote.mid * Decimal("0.01")):
# 5. 风险调整:如果波动率过高,降低预测的偏移量
if normalized_volatility > Decimal("0.01"): # 价格在窗口期内波动超过1%
total_edge_bps *= Decimal("0.5")
reason_suffix = ";vol_dampen"
else:
Expand Down
2 changes: 2 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ class BotConfig:
cancel_unfilled_after_ms: int = _int("CANCEL_UNFILLED_AFTER_MS", "0")
prediction_latency_ms: int = _int("PREDICTION_LATENCY_MS", "180")
prediction_edge_bps: Decimal = _decimal("PREDICTION_EDGE_BPS", "8")
# 交易流对价格预测的影响力,会根据波动率进行缩放。值越大,交易流的影响越大。
trade_flow_volatility_scaling: Decimal = _decimal("TRADE_FLOW_VOLATILITY_SCALING", "20")
min_prediction_confidence: Decimal = _decimal("MIN_PREDICTION_CONFIDENCE", "0.10")
orderbook_imbalance_levels: int = _int("ORDERBOOK_IMBALANCE_LEVELS", "3")
dry_run: bool = _bool("DRY_RUN", "true")
Expand Down
1 change: 1 addition & 0 deletions market_maker.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def bootstrap(self) -> None:
self.predictor = AdvancedPredictor(
impact_bps_per_imbalance=self.config.prediction_edge_bps,
min_confidence=self.config.min_prediction_confidence,
trade_flow_volatility_scaling=self.config.trade_flow_volatility_scaling,
)
if self.data_recorder is None and self.condition_id:
self.data_recorder = DataRecorder(self.condition_id)
Expand Down
13 changes: 11 additions & 2 deletions polymarket_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,19 @@ def today_rewards_estimate(self, condition_id: str) -> Decimal:
except Exception as exc:
LOGGER.debug("Reward estimate unavailable: %s", exc)
return Decimal("0")

if isinstance(result, dict):
value = result.get("total") or result.get("earnings") or result.get("amount") or "0"
return Decimal(str(value))
return Decimal(str(result or "0"))
try:
return Decimal(str(value))
except ArithmeticError:
LOGGER.warning("Invalid decimal value for reward from dict: %s", value)
return Decimal("0")
try:
return Decimal(str(result or "0"))
except ArithmeticError:
LOGGER.warning("Invalid decimal value for reward: %s", result)
return Decimal("0")
Comment thread
Jerryy959 marked this conversation as resolved.
Comment thread
Jerryy959 marked this conversation as resolved.

@staticmethod
def _close_only_time(market: dict[str, Any]) -> tuple[str, str]:
Expand Down
Loading