a
    öå‘jÇ  ã                   @   sŠ   d Z ddlZddlZddlZddlmZ ej dd¡ ej dd¡ ej	Z
edƒZed d Zd	Zd
d„ Zdd„ Zedkr†e eƒ ¡ dS )a!  Re-run the Poly-US maker P&L settlement and EMAIL the result.

WHY THIS EXISTS RATHER THAN A BARE SCHEDULED TASK. The 2026-08-24 read said
Poly-US market making is NOT worth it -- but on 13 settled books, which is
nowhere near enough to act on. A job whose output lands in a log nobody opens
is not a re-check, so this mails it.

TWO THINGS CHANGED THE DAY THE BASELINE WAS TAKEN, and both push the number
being measured:

  * WINDOW_H went 6h -> 72h (user, 2026-08-24). Completion is the whole
    equation, and fill rate is 25.3% at 0-0.5h against 6.8% at 24h+, so a wider
    window plausibly makes completion WORSE while making coverage better. If
    completion has fallen, the widening is the first suspect.
  * The probe budget stopped being spent on in-play books it could never open,
    which should raise the number of books quoted per cycle.

WHAT WOULD CHANGE THE VERDICT. Break-even was 74.2% completion against 53.8%
actual. Poly-US pays far more per complete book than Kalshi (+9.09c vs +1.78c)
but loses far more per partial (-26.12c vs -2.82c), because a partial here is a
naked short at a price chosen for being attractive. So the question is only ever
"did completion move", and the honest answer at n=13 was "unknown, leaning no".

Usage:  python research\pus_maker_pnl_rerun.py [--no-email]
é    N)ÚPathzE:\soccer\mainzE:\soccer\researchz	E:\soccerZlogszpus_maker_pnl_rerun.logaË  2026-08-28 baseline -- SUPERSEDES the 08-24 read, which was WRONG.

The 08-24 numbers (13 books, 53.8% completion, "not worth it") came from
pus_live_state.json, which detect_fills stops writing whenever the venue 503s --
and it 503s for hours. It undercounted books badly. This now reads the VENUE's
own settlement record (ACTIVITY_TYPE_POSITION_RESOLUTION) and uses
afterPosition.realized, the venue's own P&L, filtered to books the maker
actually quoted.

  venue settled books    62  ->  28 the maker quoted (rest are manual/futures)
  COMPLETE books   n=19   mean +10.94c/book
  PARTIAL  books   n= 9   mean -20.78c/book
  ALL      books   n=28   mean  +0.75c/book   (realised +$0.21)
  completion 67.9%  vs  BREAK-EVEN 65.5%   -> marginally ABOVE, not below

WHAT IS NOT THE PROBLEM (measured 2026-08-24, still stands):
  Every leg was offered AT the touch; fill rate there 21.6%, better than
  Kalshi's 11.9%. Spread sits at the 1c tick floor. Quoting level costs nothing.

THE GATE HYPOTHESIS IS DEAD, on both venues. It came from the same lossy data:
  thin books re-price more, re-priced legs missed the join, so they LOOKED like
  they failed to complete. On corrected Kalshi data completion by opening gross
  is flat (82.7% / 81.0% / 80.0%). Do not raise the gate on this evidence.

WHAT TO CHECK ON THE RERUN: completion against 65.5%, and whether the 72h
window (set 08-24) moved it. Margin is ~2pp on n=28 -- that is noise, so the
question is still open, not settled.c              	   C   s6   t jtg|  ddddttƒdd�}|jp*d|jp2d S )NTúutf-8Úreplacei  )Zcapture_outputÚtextÚencodingÚerrorsÚcwdZtimeoutÚ )Ú
subprocessÚrunÚPYÚstrÚROOTÚstdoutÚstderr)ÚcmdÚp© r   úresearch/pus_maker_pnl_rerun.pyr   C   s
    þr   c               
   C   s`  t j ¡  d¡} d| › d�dg}| tttd d ƒgƒ¡ | d¡ | d¡ | t¡ d 	|¡}zPt
jjd	d	d
� t
jddd��}| |d ¡ W d   ƒ n1 s¨0    Y  W n tyÆ   Y n0 t|ƒ dtjv rÞdS z<ddlm} d| dd¡ dd¡ d }|d| › �|ƒ W n@ t�yZ } z&tdt|ƒj› d|› �ƒ W Y d }~n
d }~0 0 dS )Nz%Y-%m-%d %H:%Mz=== Poly-US maker P&L re-run  z ===r	   Zresearchzpus_maker_pnl.pyzB==================================================================Ú
T)ÚparentsÚexist_okÚar   )r   z
--no-emailr   )Ú
send_emailz7<pre style="font:12px ui-monospace,Consolas,monospace">ú&z&amp;ú<z&lt;z</pre>z[MM] Poly-US maker P&L re-run zemail failed: z: )ÚdtÚdatetimeZnowÚstrftimeÚappendr   r   r   ÚBASELINEÚjoinÚLOGÚparentÚmkdirÚopenÚwriteÚOSErrorÚprintÚsysÚargvZdaily_signalsr   r   Ú	ExceptionÚtypeÚ__name__)ZstampÚoutr   Zfhr   ÚbodyÚer   r   r   ÚmainJ   s6    



0
ÿþ0r1   Ú__main__)Ú__doc__r   r   r
   r)   Úpathlibr   ÚpathÚinsertÚ
executabler   r   r"   r    r   r1   r-   Úexitr   r   r   r   Ú<module>   s   