@@ -55,7 +55,7 @@ def _sanitize_slug(slug: str) -> str:
5555
5656
5757def _save_chainlink_market_log (slug : str | None , record : dict ) -> str | None :
58- """Write beat + final-30s benchmark samples + end TWAP to logs/chainlink/."""
58+ """Write beat + final-30s TWAP samples + end TWAP to logs/chainlink/."""
5959 safe_slug = _sanitize_slug (slug or record .get ("slug" ) or "unknown-market" )
6060 record = {** record , "slug" : safe_slug }
6161 path = os .path .join (CHAINLINK_LOG_DIR , f"{ safe_slug } .json" )
@@ -76,23 +76,23 @@ def _save_chainlink_market_log(slug: str | None, record: dict) -> str | None:
7676 return path
7777
7878
79- # The end TWAP is the arithmetic mean of the per-second benchmark prices over
80- # the final TWAP_WINDOW_SECONDS. Once N samples are known, N/30 of the result is
81- # already fixed; the rest is estimated by holding the current benchmark flat.
79+ # End settlement TWAP ≈ mean of per-second TWAP-30 samples over the final window.
80+ # Once N samples are known, N/30 of the result is fixed; the rest is estimated
81+ # by holding the current TWAP flat.
8282TWAP_WINDOW_SECONDS = 30
8383PREDICT_AT_SECONDS_LEFT = 5
8484# Calls closer than this to the beat price sit inside the estimator's noise band.
8585MIN_PREDICT_MARGIN = 1.0
86- # A benchmark this old means the Chainlink stream stalled; the estimate is unusable.
87- MAX_BENCHMARK_AGE_MS = 3000.0
86+ # A TWAP update this old means the Chainlink stream stalled; estimate is unusable.
87+ MAX_TWAP_AGE_MS = 3000.0
8888
8989_pred_total = 0
9090_pred_correct = 0
9191_pred_abs_err_sum = 0.0
9292
9393
9494def _predict_end_twap (prices : list [float ], current : float ) -> float | None :
95- """Estimate the final TWAP from the benchmark samples seen so far.
95+ """Estimate the final TWAP from TWAP-30 samples seen so far.
9696
9797 Unknown seconds are filled with ``current`` (flat-hold assumption).
9898 """
@@ -638,14 +638,14 @@ def _main_impl():
638638 end_ts = bot .current_market_end_timestamp
639639 config .END_TWAP_PRICE = 0.0
640640
641- # Beat = Chainlink benchmark at market open.
642- beat = bot .polymarket_twap_client .wait_for_benchmark (timeout_sec = 10.0 )
641+ # Beat = Chainlink TWAP-30 at market open.
642+ beat = bot .polymarket_twap_client .wait_for_twap (timeout_sec = 10.0 )
643643 if beat is not None and beat > 0 :
644644 config .PRICE_TO_BEAT = float (beat )
645645 config .PRICE_TO_BEAT_COINBASE = float (beat )
646- print (f"PRICE TO BEAT (benchmark ): { config .PRICE_TO_BEAT :.2f} " )
646+ print (f"PRICE TO BEAT (TWAP-30 ): { config .PRICE_TO_BEAT :.2f} " )
647647 else :
648- print ("⚠️ Could not get opening benchmark from Polymarket stream" )
648+ print ("⚠️ Could not get opening TWAP-30 from Polymarket stream" )
649649
650650 # Wait until the final 30 seconds of this market.
651651 if end_ts :
@@ -654,39 +654,39 @@ def _main_impl():
654654 time .sleep (0.5 )
655655
656656 beat = config .PRICE_TO_BEAT
657- benchmark_final_30s : list [dict ] = []
657+ twap_final_30s : list [dict ] = []
658658 prediction : dict | None = None
659659
660- # Final 30s: Chainlink benchmark prices (T-30 → T-0), one sample per second.
660+ # Final 30s: Chainlink TWAP-30 (T-30 → T-0), one sample per second.
661661 while end_ts and time .time () < end_ts + 1 :
662662 _ensure_websocket_and_subscribe (bot , ws_url , prev_token_ids )
663663
664664 now = time .time ()
665665 seconds_left = end_ts - now
666- benchmark = config .CURRENT_PRICE
667- if benchmark > 0 and end_ts - 30 <= now <= end_ts :
668- side = "UP" if beat > 0 and benchmark > beat else ("DOWN" if beat > 0 else "?" )
666+ twap = config .CURRENT_TWAP_PRICE
667+ if twap > 0 and end_ts - 30 <= now <= end_ts :
668+ side = "UP" if beat > 0 and twap > beat else ("DOWN" if beat > 0 else "?" )
669669 dt = datetime .fromtimestamp (now , timezone .utc )
670- benchmark_final_30s .append ({
670+ twap_final_30s .append ({
671671 "ts" : round (now , 3 ),
672672 "iso" : dt .isoformat (),
673673 "seconds_left" : round (seconds_left , 1 ),
674- "benchmark_price " : round (benchmark , 2 ),
674+ "twap_price " : round (twap , 2 ),
675675 "vs_beat" : side ,
676676 })
677677 print (
678678 f"{ dt .strftime ('%d/%m/%Y, %-H:%M:%S %Z' )} | "
679- f"T-{ seconds_left :.0f} s | benchmark= { benchmark :.2f} | beat={ beat :.2f} | { side } "
679+ f"T-{ seconds_left :.0f} s | twap= { twap :.2f} | beat={ beat :.2f} | { side } "
680680 )
681681
682682 if prediction is None and seconds_left <= PREDICT_AT_SECONDS_LEFT :
683- prices = [s ["benchmark_price " ] for s in benchmark_final_30s ]
684- est = _predict_end_twap (prices , benchmark )
683+ prices = [s ["twap_price " ] for s in twap_final_30s ]
684+ est = _predict_end_twap (prices , twap )
685685 if est is not None :
686- age_ms = (now * 1000.0 ) - config .CURRENT_PRICE_TS_MS
686+ age_ms = (now * 1000.0 ) - config .CURRENT_TWAP_TS_MS
687687 stale = (
688- config .CURRENT_PRICE_TS_MS <= 0
689- or age_ms > MAX_BENCHMARK_AGE_MS
688+ config .CURRENT_TWAP_TS_MS <= 0
689+ or age_ms > MAX_TWAP_AGE_MS
690690 or len (set (prices )) < 2
691691 )
692692 margin = est - beat if beat > 0 else 0.0
@@ -698,11 +698,11 @@ def _main_impl():
698698 "seconds_left" : round (seconds_left , 1 ),
699699 "samples_known" : len (prices ),
700700 "locked_fraction" : round (len (prices ) / TWAP_WINDOW_SECONDS , 3 ),
701- "current_benchmark " : round (benchmark , 2 ),
701+ "current_twap " : round (twap , 2 ),
702702 "predicted_twap" : round (est , 2 ),
703703 "margin_vs_beat" : round (margin , 2 ),
704704 "predicted_side" : pred_side ,
705- "benchmark_age_ms " : round (max (age_ms , 0.0 ), 1 ),
705+ "twap_age_ms " : round (max (age_ms , 0.0 ), 1 ),
706706 "stale_feed" : stale ,
707707 }
708708 print (
@@ -721,7 +721,7 @@ def _main_impl():
721721
722722 result = None
723723 print (f"END TWAP: { end_twap } " )
724- print (f"PRICE TO BEAT (benchmark ): { config .PRICE_TO_BEAT } " )
724+ print (f"PRICE TO BEAT (TWAP-30 ): { config .PRICE_TO_BEAT } " )
725725
726726 if end_twap is not None and config .PRICE_TO_BEAT > 0 :
727727 if end_twap > config .PRICE_TO_BEAT :
@@ -734,7 +734,7 @@ def _main_impl():
734734 print ("⚠️ Settlement skipped — missing end TWAP or beat price" )
735735
736736 if prediction is None :
737- print ("🔮 PREDICTION: none made this market (no benchmark samples in final 5s)" )
737+ print ("🔮 PREDICTION: none made this market (no TWAP samples in final 5s)" )
738738 elif end_twap is None :
739739 print ("🔮 PREDICTION: cannot verify — no end TWAP" )
740740 else :
@@ -773,7 +773,7 @@ def _main_impl():
773773 "market_start_ts" : int (start_ts ) if start_ts else None ,
774774 "market_end_ts" : int (end_ts ) if end_ts else None ,
775775 "beat_price" : round (config .PRICE_TO_BEAT , 2 ) if config .PRICE_TO_BEAT else None ,
776- "benchmark_prices_final_30s " : benchmark_final_30s ,
776+ "twap_prices_final_30s " : twap_final_30s ,
777777 "end_twap_price" : round (end_twap , 2 ) if end_twap is not None else None ,
778778 "result" : result ,
779779 "prediction" : prediction ,
0 commit comments