@@ -204,6 +204,41 @@ def test_build_polymarket_watchlist_keeps_external_signals_under_cap(self):
204204 signal = next (row for row in rows if row ["market_id" ] == "signal" )
205205 self .assertIn ("external_signal" , signal ["priority_reasons" ])
206206
207+ def test_build_polymarket_watchlist_cap_prefers_latest_external_signal (self ):
208+ gamma_rows = [
209+ _gamma_row ("old-signal" , "" , ["old-yes" , "old-no" ], "" , liquidity = 0 , volume24hr = 0 ),
210+ _gamma_row ("new-signal" , "" , ["new-yes" , "new-no" ], "" , liquidity = 0 , volume24hr = 0 ),
211+ _gamma_row ("top" , "" , ["top-yes" , "top-no" ], "" , liquidity = 1000 , volume24hr = 1000 ),
212+ ]
213+ rules = {"mutually_exclusive" : []}
214+ old_signal = {
215+ "type" : "external_signal" ,
216+ "legs" : [{"venue" : "polymarket" , "market_id" : "old-signal" }],
217+ }
218+ new_signal = {
219+ "type" : "external_signal" ,
220+ "legs" : [{"venue" : "polymarket" , "market_id" : "new-signal" }],
221+ }
222+
223+ with tempfile .TemporaryDirectory () as tmp :
224+ gamma_path = Path (tmp ) / "gamma.ndjson"
225+ rules_path = Path (tmp ) / "rules.json"
226+ signals_path = Path (tmp ) / "signals.ndjson"
227+ gamma_path .write_text ("\n " .join (json .dumps (row ) for row in gamma_rows ) + "\n " )
228+ rules_path .write_text (json .dumps (rules ))
229+ signals_path .write_text ("\n " .join (json .dumps (row ) for row in [old_signal , old_signal , old_signal , new_signal ]) + "\n " )
230+
231+ rows = build_polymarket_watchlist (
232+ gamma_path ,
233+ rules_path ,
234+ external_signals_path = signals_path ,
235+ include_top_markets = 1 ,
236+ max_markets = 1 ,
237+ )
238+
239+ self .assertIn ("new-signal" , {row ["market_id" ] for row in rows })
240+ self .assertNotIn ("old-signal" , {row ["market_id" ] for row in rows })
241+
207242 def test_build_polymarket_watchlist_keeps_external_signal_neg_risk_group_atomic (self ):
208243 gamma_rows = [
209244 _gamma_row ("group-a" , "group" , ["ga-yes" , "ga-no" ], "1" , liquidity = 100 , volume24hr = 10 ),
0 commit comments