oracle3 1.1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. coinjure/__init__.py +0 -0
  2. coinjure/matching/__init__.py +66 -0
  3. coinjure/matching/_cache.py +35 -0
  4. coinjure/matching/_category.py +347 -0
  5. coinjure/matching/_date.py +123 -0
  6. coinjure/matching/_fetchers.py +284 -0
  7. coinjure/matching/_pipeline.py +263 -0
  8. coinjure/matching/_resolution.py +66 -0
  9. coinjure/matching/_template.py +240 -0
  10. coinjure/matching/_text.py +63 -0
  11. coinjure/matching/_types.py +72 -0
  12. oracle3/__init__.py +3 -0
  13. oracle3/agent/__init__.py +9 -0
  14. oracle3/agent/coordinator.py +328 -0
  15. oracle3/alerts/__init__.py +6 -0
  16. oracle3/alerts/alerter.py +173 -0
  17. oracle3/alerts/telegram_alerter.py +45 -0
  18. oracle3/analytics/performance_analyzer.py +336 -0
  19. oracle3/backtest/backtester.py +123 -0
  20. oracle3/blinks/__init__.py +1 -0
  21. oracle3/blinks/generator.py +62 -0
  22. oracle3/blinks/server.py +242 -0
  23. oracle3/cli/__init__.py +1 -0
  24. oracle3/cli/agent_commands.py +1008 -0
  25. oracle3/cli/cli.py +444 -0
  26. oracle3/cli/control.py +416 -0
  27. oracle3/cli/data_commands.py +260 -0
  28. oracle3/cli/market_commands.py +655 -0
  29. oracle3/cli/monitor.py +733 -0
  30. oracle3/cli/news_commands.py +246 -0
  31. oracle3/cli/research_commands.py +2369 -0
  32. oracle3/cli/textual_monitor.py +1129 -0
  33. oracle3/cli/trade_commands.py +231 -0
  34. oracle3/cli/utils.py +120 -0
  35. oracle3/config/__init__.py +23 -0
  36. oracle3/config/config.py +185 -0
  37. oracle3/core/trading_engine.py +822 -0
  38. oracle3/dashboard/__init__.py +5 -0
  39. oracle3/dashboard/server.py +423 -0
  40. oracle3/dashboard/static/demo.html +1691 -0
  41. oracle3/dashboard/static/index.html +1255 -0
  42. oracle3/dashboard/static/live.html +1208 -0
  43. oracle3/data/backtest/dflow_recorder.py +218 -0
  44. oracle3/data/backtest/historical_data_source.py +162 -0
  45. oracle3/data/backtest/history_reader.py +66 -0
  46. oracle3/data/backtest/kalshi_replay_data_source.py +212 -0
  47. oracle3/data/base_fetcher.py +144 -0
  48. oracle3/data/composite_data_source.py +82 -0
  49. oracle3/data/data_source.py +26 -0
  50. oracle3/data/live/coingecko_x402_data_source.py +213 -0
  51. oracle3/data/live/dflow_data_source.py +322 -0
  52. oracle3/data/live/dflow_ws_data_source.py +269 -0
  53. oracle3/data/live/google_news_data_source.py +517 -0
  54. oracle3/data/live/kalshi_data_source.py +308 -0
  55. oracle3/data/live/live_data_source.py +1073 -0
  56. oracle3/data/live/onchain_signal_source.py +207 -0
  57. oracle3/data/live/polymarket_orderflow.py +473 -0
  58. oracle3/data/live/twitter_fetcher.py +515 -0
  59. oracle3/data/market_data_manager.py +233 -0
  60. oracle3/data/preprocessing/__init__.py +1 -0
  61. oracle3/data/preprocessing/news_processor.py +678 -0
  62. oracle3/engine/__init__.py +1 -0
  63. oracle3/engine/control.py +363 -0
  64. oracle3/engine/registry.py +351 -0
  65. oracle3/events/events.py +165 -0
  66. oracle3/live/live_trader.py +789 -0
  67. oracle3/market/__init__.py +25 -0
  68. oracle3/market/relations.py +405 -0
  69. oracle3/market/validation.py +639 -0
  70. oracle3/mock/__init__.py +0 -0
  71. oracle3/mock/mock_mode.py +96 -0
  72. oracle3/onchain/__init__.py +5 -0
  73. oracle3/onchain/logger.py +182 -0
  74. oracle3/onchain/reputation.py +236 -0
  75. oracle3/order/order_book.py +50 -0
  76. oracle3/position/position_manager.py +189 -0
  77. oracle3/pricing/__init__.py +76 -0
  78. oracle3/pricing/calibrator.py +369 -0
  79. oracle3/pricing/contract_scorer.py +269 -0
  80. oracle3/pricing/distortion.py +338 -0
  81. oracle3/pricing/fair_value.py +330 -0
  82. oracle3/pricing/greeks.py +224 -0
  83. oracle3/pricing/premium_tracker.py +339 -0
  84. oracle3/pricing/wang_mle.py +620 -0
  85. oracle3/risk/correlation_risk_manager.py +301 -0
  86. oracle3/risk/onchain_risk_manager.py +208 -0
  87. oracle3/risk/risk_manager.py +345 -0
  88. oracle3/storage/__init__.py +6 -0
  89. oracle3/storage/serializers.py +202 -0
  90. oracle3/storage/state_store.py +177 -0
  91. oracle3/strategy/__init__.py +19 -0
  92. oracle3/strategy/agent_strategy.py +331 -0
  93. oracle3/strategy/contrib/__init__.py +25 -0
  94. oracle3/strategy/contrib/adaptive_onchain_strategy.py +585 -0
  95. oracle3/strategy/contrib/allocation_strategy.py +159 -0
  96. oracle3/strategy/contrib/coint_spread_strategy.py +409 -0
  97. oracle3/strategy/contrib/conditional_arb_strategy.py +364 -0
  98. oracle3/strategy/contrib/cross_market_arbitrage_strategy.py +820 -0
  99. oracle3/strategy/contrib/debate_strategy.py +850 -0
  100. oracle3/strategy/contrib/event_sum_arb_strategy.py +250 -0
  101. oracle3/strategy/contrib/exclusivity_arb_strategy.py +289 -0
  102. oracle3/strategy/contrib/fair_value_strategy.py +278 -0
  103. oracle3/strategy/contrib/implication_arb_strategy.py +276 -0
  104. oracle3/strategy/contrib/lead_lag_strategy.py +722 -0
  105. oracle3/strategy/contrib/llm_news_strategy.py +384 -0
  106. oracle3/strategy/contrib/multi_agent_strategy.py +106 -0
  107. oracle3/strategy/contrib/premium_decay_strategy.py +397 -0
  108. oracle3/strategy/contrib/prompts.py +222 -0
  109. oracle3/strategy/contrib/solana_agent_strategy.py +458 -0
  110. oracle3/strategy/contrib/structural_arb_strategy.py +356 -0
  111. oracle3/strategy/loader.py +52 -0
  112. oracle3/strategy/market_making_strategy.py +168 -0
  113. oracle3/strategy/momentum_mean_reversion_strategy.py +213 -0
  114. oracle3/strategy/orderbook_imbalance_strategy.py +161 -0
  115. oracle3/strategy/quant_strategy.py +34 -0
  116. oracle3/strategy/simple_strategy.py +1017 -0
  117. oracle3/strategy/strategy.py +373 -0
  118. oracle3/strategy/test_strategy.py +66 -0
  119. oracle3/ticker/ticker.py +139 -0
  120. oracle3/trader/atomic_trader.py +275 -0
  121. oracle3/trader/flash_loan.py +263 -0
  122. oracle3/trader/jito_submitter.py +253 -0
  123. oracle3/trader/kalshi_trader.py +296 -0
  124. oracle3/trader/paper_trader.py +192 -0
  125. oracle3/trader/polymarket_trader.py +356 -0
  126. oracle3/trader/solana_trader.py +426 -0
  127. oracle3/trader/spread_executor.py +308 -0
  128. oracle3/trader/trader.py +136 -0
  129. oracle3/trader/types.py +119 -0
  130. oracle3/trading/__init__.py +5 -0
  131. oracle3/trading/allocator.py +211 -0
  132. oracle3/trading/sizing.py +218 -0
  133. oracle3-1.1.2.dist-info/METADATA +166 -0
  134. oracle3-1.1.2.dist-info/RECORD +137 -0
  135. oracle3-1.1.2.dist-info/WHEEL +4 -0
  136. oracle3-1.1.2.dist-info/entry_points.txt +3 -0
  137. oracle3-1.1.2.dist-info/licenses/LICENSE +190 -0
coinjure/__init__.py ADDED
File without changes
@@ -0,0 +1,66 @@
1
+ """Cross-platform market matching pipeline.
2
+
3
+ Usage::
4
+
5
+ from coinjure.matching import MarketMatchingPipeline
6
+
7
+ pipeline = MarketMatchingPipeline()
8
+ matches = await pipeline.scan()
9
+ """
10
+
11
+ from __future__ import annotations
12
+
13
+ from typing import TYPE_CHECKING
14
+
15
+ from ._pipeline import MarketMatchingPipeline
16
+ from ._types import MatchConfidence, MatchResult, NormalizedMarket, StageScores
17
+
18
+ if TYPE_CHECKING:
19
+ from examples.strategies.cross_platform_arb_strategy import MatchedMarket
20
+
21
+ __all__ = [
22
+ 'MarketMatchingPipeline',
23
+ 'MatchConfidence',
24
+ 'MatchResult',
25
+ 'NormalizedMarket',
26
+ 'StageScores',
27
+ ]
28
+
29
+
30
+ def match_result_to_matched_market(result: MatchResult) -> MatchedMarket:
31
+ """Bridge function to convert a MatchResult to a legacy MatchedMarket.
32
+
33
+ Requires oracle3 to be importable. Returns a MatchedMarket instance
34
+ from ``examples.strategies.cross_platform_arb_strategy``.
35
+ """
36
+ from oracle3.ticker.ticker import KalshiTicker, PolyMarketTicker
37
+
38
+ poly = result.poly_market
39
+ kalshi = result.kalshi_market
40
+
41
+ poly_ticker = PolyMarketTicker(
42
+ symbol=poly.get_extra_str('token_id', poly.market_id) or poly.market_id,
43
+ name=poly.title,
44
+ token_id=poly.get_extra_str('token_id'),
45
+ market_id=poly.market_id,
46
+ event_id=poly.event_id,
47
+ no_token_id=poly.get_extra_str('no_token_id'),
48
+ )
49
+ kalshi_ticker = KalshiTicker(
50
+ symbol=kalshi.market_id,
51
+ name=kalshi.title,
52
+ market_ticker=kalshi.get_extra_str('market_ticker', kalshi.market_id),
53
+ event_ticker=kalshi.get_extra_str('event_ticker', kalshi.event_id),
54
+ series_ticker=kalshi.series_ticker,
55
+ )
56
+
57
+ # Import MatchedMarket from the strategy module
58
+ from examples.strategies.cross_platform_arb_strategy import MatchedMarket
59
+
60
+ return MatchedMarket(
61
+ poly_ticker=poly_ticker,
62
+ kalshi_ticker=kalshi_ticker,
63
+ similarity=result.score,
64
+ label=result.label,
65
+ confidence=result.confidence.value,
66
+ )
@@ -0,0 +1,35 @@
1
+ """Simple in-memory TTL cache."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import time
6
+ from typing import TypeVar
7
+
8
+ T = TypeVar('T')
9
+
10
+
11
+ class TTLCache:
12
+ """Thread-safe-ish TTL cache for API responses."""
13
+
14
+ def __init__(self, ttl_seconds: float = 300.0) -> None:
15
+ self._ttl = ttl_seconds
16
+ self._store: dict[str, tuple[float, object]] = {}
17
+
18
+ def get(self, key: str) -> object | None:
19
+ """Return cached value if not expired, else None."""
20
+ entry = self._store.get(key)
21
+ if entry is None:
22
+ return None
23
+ ts, value = entry
24
+ if time.monotonic() - ts > self._ttl:
25
+ del self._store[key]
26
+ return None
27
+ return value
28
+
29
+ def set(self, key: str, value: object) -> None:
30
+ """Store a value with the current timestamp."""
31
+ self._store[key] = (time.monotonic(), value)
32
+
33
+ def clear(self) -> None:
34
+ """Clear all entries."""
35
+ self._store.clear()
@@ -0,0 +1,347 @@
1
+ """Stage 1: Category bucketing to reduce search space."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+
7
+ from ._types import NormalizedMarket
8
+
9
+ # ---- Stopwords for keyphrase extraction ----
10
+
11
+ _KP_STOPWORDS = frozenset(
12
+ {
13
+ 'will', 'the', 'a', 'an', 'of', 'in', 'on', 'by', 'to', 'for',
14
+ 'be', 'is', 'at', 'before', 'after', 'this', 'that', 'or', 'and',
15
+ 'does', 'do', 'it', 'its', 'than', 'what', 'which', 'who',
16
+ }
17
+ )
18
+
19
+
20
+ def _normalize_for_keyphrases(text: str) -> str:
21
+ """Lower, strip punctuation, remove stopwords."""
22
+ text = re.sub(r'[^a-z0-9\s]', ' ', text.lower())
23
+ tokens = [t for t in text.split() if t not in _KP_STOPWORDS]
24
+ return ' '.join(tokens)
25
+
26
+
27
+ def _extract_keyphrases(text_norm: str) -> set[str]:
28
+ """Extract single keywords (4+ chars) and bigrams (3+ chars each)."""
29
+ words = text_norm.split()
30
+ phrases: set[str] = set()
31
+ for w in words:
32
+ if len(w) >= 4:
33
+ phrases.add(w)
34
+ for i in range(len(words) - 1):
35
+ if len(words[i]) >= 3 and len(words[i + 1]) >= 3:
36
+ phrases.add(f'{words[i]} {words[i + 1]}')
37
+ return phrases
38
+
39
+ # ---- Polymarket tag -> canonical category ----
40
+
41
+ _POLY_TAG_MAP: dict[str, str] = {
42
+ # Sports
43
+ 'nba': 'sports_basketball',
44
+ 'basketball': 'sports_basketball',
45
+ 'nfl': 'sports_football',
46
+ 'football': 'sports_football',
47
+ 'mlb': 'sports_baseball',
48
+ 'baseball': 'sports_baseball',
49
+ 'nhl': 'sports_hockey',
50
+ 'hockey': 'sports_hockey',
51
+ 'soccer': 'sports_soccer',
52
+ 'mls': 'sports_soccer',
53
+ 'premier-league': 'sports_soccer',
54
+ 'champions-league': 'sports_soccer',
55
+ 'tennis': 'sports_tennis',
56
+ 'golf': 'sports_golf',
57
+ 'pga': 'sports_golf',
58
+ 'mma': 'sports_mma',
59
+ 'ufc': 'sports_mma',
60
+ 'boxing': 'sports_boxing',
61
+ 'f1': 'sports_f1',
62
+ 'formula-1': 'sports_f1',
63
+ 'sports': 'sports',
64
+ # Politics
65
+ 'us-elections': 'politics_us_elections',
66
+ 'elections': 'politics_elections',
67
+ 'politics': 'politics',
68
+ 'congress': 'politics_congress',
69
+ 'senate': 'politics_congress',
70
+ 'house': 'politics_congress',
71
+ 'governor': 'politics_governor',
72
+ 'presidential': 'politics_presidential',
73
+ 'trump': 'politics',
74
+ 'biden': 'politics',
75
+ # Crypto
76
+ 'crypto': 'crypto',
77
+ 'bitcoin': 'crypto_btc',
78
+ 'btc': 'crypto_btc',
79
+ 'ethereum': 'crypto_eth',
80
+ 'eth': 'crypto_eth',
81
+ 'solana': 'crypto_sol',
82
+ 'sol': 'crypto_sol',
83
+ # Economy
84
+ 'fed': 'economy_fed',
85
+ 'federal-reserve': 'economy_fed',
86
+ 'interest-rates': 'economy_fed',
87
+ 'inflation': 'economy_inflation',
88
+ 'cpi': 'economy_inflation',
89
+ 'gdp': 'economy_gdp',
90
+ 'economy': 'economy',
91
+ 'jobs': 'economy_jobs',
92
+ 'unemployment': 'economy_jobs',
93
+ # Tech / AI
94
+ 'ai': 'tech_ai',
95
+ 'artificial-intelligence': 'tech_ai',
96
+ 'tech': 'tech',
97
+ # Entertainment
98
+ 'oscars': 'entertainment_awards',
99
+ 'grammys': 'entertainment_awards',
100
+ 'emmys': 'entertainment_awards',
101
+ 'entertainment': 'entertainment',
102
+ # Science / weather
103
+ 'weather': 'science_weather',
104
+ 'climate': 'science_weather',
105
+ 'science': 'science',
106
+ 'space': 'science_space',
107
+ }
108
+
109
+ # ---- Kalshi series_ticker prefix -> canonical category ----
110
+
111
+ _KALSHI_PREFIX_MAP: dict[str, str] = {
112
+ # Sports
113
+ 'KXNBA': 'sports_basketball',
114
+ 'KXNFL': 'sports_football',
115
+ 'KXMLB': 'sports_baseball',
116
+ 'KXNHL': 'sports_hockey',
117
+ 'KXMLS': 'sports_soccer',
118
+ 'KXUFC': 'sports_mma',
119
+ 'KXPGA': 'sports_golf',
120
+ 'KXF1': 'sports_f1',
121
+ 'KXTENNIS': 'sports_tennis',
122
+ 'KXBOX': 'sports_boxing',
123
+ 'KXSPORT': 'sports',
124
+ # Politics
125
+ 'KXSENATE': 'politics_congress',
126
+ 'KXHOUSE': 'politics_congress',
127
+ 'KXGOV': 'politics_governor',
128
+ 'KXPRES': 'politics_presidential',
129
+ 'KXELECT': 'politics_elections',
130
+ 'KXPOLITICS': 'politics',
131
+ # Crypto
132
+ 'KXBTC': 'crypto_btc',
133
+ 'KXETH': 'crypto_eth',
134
+ 'KXSOL': 'crypto_sol',
135
+ 'KXCRYPTO': 'crypto',
136
+ # Economy
137
+ 'KXFED': 'economy_fed',
138
+ 'KXRATE': 'economy_fed',
139
+ 'KXCPI': 'economy_inflation',
140
+ 'KXINFLATION': 'economy_inflation',
141
+ 'KXGDP': 'economy_gdp',
142
+ 'KXJOBS': 'economy_jobs',
143
+ 'KXECON': 'economy',
144
+ # Tech / AI
145
+ 'KXAI': 'tech_ai',
146
+ 'KXTECH': 'tech',
147
+ # Entertainment
148
+ 'KXOSCARS': 'entertainment_awards',
149
+ 'KXAWARD': 'entertainment_awards',
150
+ # Science / weather
151
+ 'KXWEATHER': 'science_weather',
152
+ 'KXTEMP': 'science_weather',
153
+ 'KXSPACE': 'science_space',
154
+ }
155
+
156
+ # ---- Kalshi series to exclude (parlays / cross-category) ----
157
+
158
+ EXCLUDED_KALSHI_SERIES: frozenset[str] = frozenset({
159
+ 'KXMVECROSSCATEGORY',
160
+ 'KXMVESPORTSMULTIGAMEEXTENDED',
161
+ 'KXMVESPORTSMULTIGAME',
162
+ 'KXMVEPOLITICSMULTI',
163
+ })
164
+
165
+
166
+ def _parent_category(cat: str) -> str:
167
+ """Return the parent category (e.g. 'sports_basketball' -> 'sports')."""
168
+ return cat.rsplit('_', 1)[0] if '_' in cat else cat
169
+
170
+
171
+ def categories_compatible(cat_a: str, cat_b: str) -> bool:
172
+ """Check if two categories are compatible (same or parent-child).
173
+
174
+ 'sports_basketball' and 'sports' are compatible (parent-child).
175
+ 'sports_basketball' and 'sports_football' are NOT compatible (siblings).
176
+ """
177
+ if not cat_a or not cat_b:
178
+ return True # unknown category -> don't filter
179
+ if cat_a == cat_b:
180
+ return True
181
+ # Parent-child compatibility (one is the parent of the other)
182
+ if _parent_category(cat_a) == cat_b or _parent_category(cat_b) == cat_a:
183
+ return True
184
+ # Two subcategories of the same parent are NOT compatible
185
+ return False
186
+
187
+
188
+ def categorize_poly(market: NormalizedMarket) -> str:
189
+ """Map a Polymarket market to its canonical category.
190
+
191
+ First tries exact match, then falls back to substring matching
192
+ (e.g. "NBA Basketball" matches via "nba" or "basketball").
193
+ """
194
+ # Pass 1: exact match
195
+ for tag in market.tags:
196
+ tag_lower = tag.lower().strip()
197
+ if tag_lower in _POLY_TAG_MAP:
198
+ return _POLY_TAG_MAP[tag_lower]
199
+
200
+ # Pass 2: substring match (normalized: remove hyphens, split tokens)
201
+ for tag in market.tags:
202
+ tag_normalized = tag.lower().strip().replace('-', ' ')
203
+ for token in tag_normalized.split():
204
+ if token in _POLY_TAG_MAP:
205
+ return _POLY_TAG_MAP[token]
206
+ # Also try the full normalized tag as a substring of known keys
207
+ for known_tag, category in _POLY_TAG_MAP.items():
208
+ known_normalized = known_tag.replace('-', ' ')
209
+ if known_normalized in tag_normalized or tag_normalized in known_normalized:
210
+ return category
211
+ return ''
212
+
213
+
214
+ def categorize_kalshi(market: NormalizedMarket) -> str:
215
+ """Map a Kalshi market to its canonical category via series_ticker prefix."""
216
+ series = market.series_ticker.upper()
217
+ if series in EXCLUDED_KALSHI_SERIES:
218
+ return '__excluded__'
219
+ # Try longest prefix first
220
+ for prefix in sorted(_KALSHI_PREFIX_MAP, key=len, reverse=True):
221
+ if series.startswith(prefix):
222
+ return _KALSHI_PREFIX_MAP[prefix]
223
+ return ''
224
+
225
+
226
+ def categorize_market(market: NormalizedMarket) -> str:
227
+ """Assign canonical category to a market based on its platform."""
228
+ if market.platform == 'polymarket':
229
+ return categorize_poly(market)
230
+ if market.platform == 'kalshi':
231
+ return categorize_kalshi(market)
232
+ return ''
233
+
234
+
235
+ def build_category_buckets(
236
+ poly_markets: list[NormalizedMarket],
237
+ kalshi_markets: list[NormalizedMarket],
238
+ ) -> dict[str, tuple[list[NormalizedMarket], list[NormalizedMarket]]]:
239
+ """Group markets into buckets by compatible category.
240
+
241
+ Returns a dict of category -> (poly_list, kalshi_list).
242
+ Markets with no category go into a special '' bucket that is compared
243
+ against all Kalshi markets (and vice-versa).
244
+ """
245
+ poly_by_cat: dict[str, list[NormalizedMarket]] = {}
246
+ kalshi_by_cat: dict[str, list[NormalizedMarket]] = {}
247
+
248
+ for m in poly_markets:
249
+ cat = categorize_market(m)
250
+ m.category = cat
251
+ poly_by_cat.setdefault(cat, []).append(m)
252
+
253
+ kalshi_filtered: list[NormalizedMarket] = []
254
+ for m in kalshi_markets:
255
+ cat = categorize_market(m)
256
+ m.category = cat
257
+ if cat == '__excluded__':
258
+ continue
259
+ kalshi_filtered.append(m)
260
+ kalshi_by_cat.setdefault(cat, []).append(m)
261
+
262
+ # Build compatible buckets
263
+ buckets: dict[str, tuple[list[NormalizedMarket], list[NormalizedMarket]]] = {}
264
+
265
+ all_cats = set(poly_by_cat) | set(kalshi_by_cat)
266
+ for cat in all_cats:
267
+ if cat == '__excluded__':
268
+ continue
269
+ p_list = poly_by_cat.get(cat, [])
270
+ k_list = kalshi_by_cat.get(cat, [])
271
+ if p_list and k_list:
272
+ buckets[cat] = (p_list, k_list)
273
+
274
+ # Uncategorized markets: use keyphrase overlap to avoid O(n*m) blowup
275
+ uncategorized_poly = poly_by_cat.get('', [])
276
+ uncategorized_kalshi = kalshi_by_cat.get('', [])
277
+
278
+ if uncategorized_poly or uncategorized_kalshi:
279
+ # Build keyphrase inverted index on Kalshi side
280
+ kalshi_by_phrase: dict[str, list[NormalizedMarket]] = {}
281
+ kalshi_candidates = kalshi_filtered if uncategorized_poly else []
282
+ all_poly_list = [m for ms in poly_by_cat.values() for m in ms] if uncategorized_kalshi else []
283
+
284
+ for km in (kalshi_candidates if uncategorized_poly else []) + uncategorized_kalshi:
285
+ norm = _normalize_for_keyphrases(km.title)
286
+ for phrase in _extract_keyphrases(norm):
287
+ kalshi_by_phrase.setdefault(phrase, []).append(km)
288
+
289
+ matched_poly: set[int] = set()
290
+ matched_kalshi: set[int] = set()
291
+ uncat_poly_list: list[NormalizedMarket] = []
292
+ uncat_kalshi_list: list[NormalizedMarket] = []
293
+
294
+ # Match uncategorized poly against kalshi via shared keyphrases
295
+ for pm in uncategorized_poly:
296
+ norm = _normalize_for_keyphrases(pm.title)
297
+ phrases = _extract_keyphrases(norm)
298
+ # Count how many keyphrases each kalshi market shares
299
+ kalshi_hits: dict[int, int] = {}
300
+ for phrase in phrases:
301
+ for km in kalshi_by_phrase.get(phrase, []):
302
+ kalshi_hits[id(km)] = kalshi_hits.get(id(km), 0) + 1
303
+ # Require >=2 shared keyphrases
304
+ has_match = False
305
+ for km_id, count in kalshi_hits.items():
306
+ if count >= 2:
307
+ matched_kalshi.add(km_id)
308
+ has_match = True
309
+ if has_match:
310
+ matched_poly.add(id(pm))
311
+ uncat_poly_list.append(pm)
312
+
313
+ # Match uncategorized kalshi against all poly via shared keyphrases
314
+ poly_by_phrase: dict[str, list[NormalizedMarket]] = {}
315
+ for pm in all_poly_list:
316
+ norm = _normalize_for_keyphrases(pm.title)
317
+ for phrase in _extract_keyphrases(norm):
318
+ poly_by_phrase.setdefault(phrase, []).append(pm)
319
+
320
+ for km in uncategorized_kalshi:
321
+ norm = _normalize_for_keyphrases(km.title)
322
+ phrases = _extract_keyphrases(norm)
323
+ poly_hits: dict[int, int] = {}
324
+ for phrase in phrases:
325
+ for pm in poly_by_phrase.get(phrase, []):
326
+ poly_hits[id(pm)] = poly_hits.get(id(pm), 0) + 1
327
+ has_match = False
328
+ for pm_id, count in poly_hits.items():
329
+ if count >= 2:
330
+ matched_poly.add(pm_id)
331
+ has_match = True
332
+ if has_match:
333
+ matched_kalshi.add(id(km))
334
+ if id(km) not in {id(k) for k in uncat_kalshi_list}:
335
+ uncat_kalshi_list.append(km)
336
+
337
+ # Collect all matched kalshi markets for the bucket
338
+ all_kalshi_pool = (kalshi_candidates if uncategorized_poly else []) + uncategorized_kalshi
339
+ kalshi_in_bucket = list({id(k): k for k in all_kalshi_pool if id(k) in matched_kalshi}.values())
340
+ # Collect all matched poly markets for the bucket
341
+ poly_in_bucket_extra = [pm for pm in all_poly_list if id(pm) in matched_poly and id(pm) not in {id(p) for p in uncat_poly_list}]
342
+ final_poly = list({id(p): p for p in uncat_poly_list + poly_in_bucket_extra}.values())
343
+
344
+ if final_poly and kalshi_in_bucket:
345
+ buckets['__uncategorized__'] = (final_poly, kalshi_in_bucket)
346
+
347
+ return buckets
@@ -0,0 +1,123 @@
1
+ """Stage 2: Date proximity filtering."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import re
6
+ from datetime import datetime, timezone
7
+
8
+ from ._types import NormalizedMarket
9
+
10
+ # ---- Regex patterns for date extraction from titles ----
11
+
12
+ _MONTH_MAP: dict[str, int] = {
13
+ 'january': 1, 'jan': 1,
14
+ 'february': 2, 'feb': 2,
15
+ 'march': 3, 'mar': 3,
16
+ 'april': 4, 'apr': 4,
17
+ 'may': 5,
18
+ 'june': 6, 'jun': 6,
19
+ 'july': 7, 'jul': 7,
20
+ 'august': 8, 'aug': 8,
21
+ 'september': 9, 'sep': 9, 'sept': 9,
22
+ 'october': 10, 'oct': 10,
23
+ 'november': 11, 'nov': 11,
24
+ 'december': 12, 'dec': 12,
25
+ }
26
+
27
+ # "by December 31, 2025" / "before March 15, 2026"
28
+ _FULL_DATE_RE = re.compile(
29
+ r'(?:by|before|on|after)?\s*'
30
+ r'(?P<month>' + '|'.join(_MONTH_MAP) + r')\s+'
31
+ r'(?P<day>\d{1,2}),?\s+'
32
+ r'(?P<year>20\d{2})',
33
+ re.IGNORECASE,
34
+ )
35
+
36
+ # "March 2026" / "in March 2026"
37
+ _MONTH_YEAR_RE = re.compile(
38
+ r'(?:in|by|before|during)?\s*'
39
+ r'(?P<month>' + '|'.join(_MONTH_MAP) + r')\s+'
40
+ r'(?P<year>20\d{2})',
41
+ re.IGNORECASE,
42
+ )
43
+
44
+ # "in 2026" / "2026 NBA" / "by 2026"
45
+ _YEAR_RE = re.compile(
46
+ r'(?:in|by|before|during)?\s*(?P<year>20\d{2})\b',
47
+ re.IGNORECASE,
48
+ )
49
+
50
+ # "Q1 2026" / "Q3 2025"
51
+ _QUARTER_RE = re.compile(
52
+ r'Q(?P<quarter>[1-4])\s+(?P<year>20\d{2})',
53
+ re.IGNORECASE,
54
+ )
55
+
56
+
57
+ def extract_date(market: NormalizedMarket) -> datetime | None:
58
+ """Extract the best date estimate from a market.
59
+
60
+ Priority: API end_date > full date regex > month+year > quarter > year.
61
+ """
62
+ if market.end_date is not None:
63
+ return market.end_date
64
+
65
+ title = market.title
66
+
67
+ # Full date: "December 31, 2025"
68
+ m = _FULL_DATE_RE.search(title)
69
+ if m:
70
+ month = _MONTH_MAP.get(m.group('month').lower())
71
+ if month:
72
+ try:
73
+ return datetime(
74
+ int(m.group('year')),
75
+ month,
76
+ int(m.group('day')),
77
+ tzinfo=timezone.utc,
78
+ )
79
+ except ValueError:
80
+ pass
81
+
82
+ # Month + year: "March 2026"
83
+ m = _MONTH_YEAR_RE.search(title)
84
+ if m:
85
+ month = _MONTH_MAP.get(m.group('month').lower())
86
+ if month:
87
+ return datetime(int(m.group('year')), month, 15, tzinfo=timezone.utc)
88
+
89
+ # Quarter: "Q1 2026"
90
+ m = _QUARTER_RE.search(title)
91
+ if m:
92
+ quarter = int(m.group('quarter'))
93
+ mid_month = {1: 2, 2: 5, 3: 8, 4: 11}[quarter]
94
+ return datetime(int(m.group('year')), mid_month, 15, tzinfo=timezone.utc)
95
+
96
+ # Year only: "2026"
97
+ m = _YEAR_RE.search(title)
98
+ if m:
99
+ return datetime(int(m.group('year')), 7, 1, tzinfo=timezone.utc)
100
+
101
+ return None
102
+
103
+
104
+ def date_proximity_score(
105
+ date_a: datetime | None,
106
+ date_b: datetime | None,
107
+ max_delta_days: int = 30,
108
+ ) -> float:
109
+ """Score date proximity between two markets.
110
+
111
+ Returns 1.0 if within 7 days, linearly decays to 0.0 at max_delta_days.
112
+ Returns 0.5 if either date is missing (neutral).
113
+ """
114
+ if date_a is None or date_b is None:
115
+ return 0.5 # no penalty, no bonus
116
+
117
+ delta_days = abs((date_a - date_b).days)
118
+ if delta_days <= 7:
119
+ return 1.0
120
+ if delta_days >= max_delta_days:
121
+ return 0.0
122
+ # Linear decay from 1.0 at 7 days to 0.0 at max_delta_days
123
+ return 1.0 - (delta_days - 7) / (max_delta_days - 7)