a
    Tâ�j±  ã                   @   sœ   d Z ddlZddlZddlZddlZddlZej dd¡ ej dd¡ ddlZ	ddl
ZddlmZmZ ddd	„Zd
d„ Zdd„ Zdd„ Zedkr˜eƒ  dS )a�  Charge favourites more than draws: does the book balance, or just stop filling?

Favourites fill at 49.6% and draws at 17.7% (queue-aware, at the touch). That
imbalance is the whole loss mechanism: the favourite goes first, the draw never
comes, and we are left short the leg most likely to win. Today that cost -78c
against completions worth +2 to +4c.

If favourites fill twice as easily, we are underpricing them. Charging a premium
on the favourite alone would, in principle, do two good things at once: collect
more, and slow the favourite down so the legs finish together instead of the
easy one running ahead.

The counter-argument is the strongest single result in the fill data: cents
above the touch is the best predictor of not filling (AUC 0.690). A premium may
simply stop the favourite filling at all, which does not balance the book, it
empties it.

So measure fill rate against premium SEPARATELY BY LEG TYPE. The question is
whether the favourite's fill curve is flatter than the draw's -- if it is, there
is a premium that equalises them; if both collapse together, there is not.

Queue-aware fills from real Kalshi prints throughout.
é    NzE:\soccer\mainzE:\soccer\research)Ú	load_legsÚCACHEç        ç      ð?c              	   C   sH   z*t | ƒ}||  k r|k r&n n|nd W S  ttfyB   Y d S 0 d S )N)ÚfloatÚ	TypeErrorÚ
ValueError)ÚxÚloÚhiÚv© r   ú+E:\soccer\research\mm_asymmetric_pricing.pyÚ_f!   s
    "r   c                  C   s¾   i } t dddd��˜}t |¡D ]|}| d¡}|r|| v s| d¡pDd ¡ sLqt| d¡ƒt| d	¡ƒ }}| d
¡pvd ¡ }|r|r|dv r|||f| |< qW d  ƒ n1 s°0    Y  | S )zC{leg: (touch, our_ask, side)} at the first quote with depth logged.z&E:\soccer\research\mm_paper_quotes.csvúutf-8Úreplace)ÚencodingÚerrorsZ
leg_tickerZask_sizeÚ Zk_askZour_askÚside)ÚhomeÚdrawZawayN)ÚopenÚcsvZ
DictReaderÚgetÚstripr   Úlower)ÚoutZfhÚrÚtÚkÚor   r   r   r   Ú	load_cols)   s    ÿ
.r"   c           
      C   s˜   || vrd S d}| | D ]z\}}}t  |¡}	|	s4 q”|	jd u |jd u krd|	jrV|	 ¡ n|	j|jd�}	|	|krp q”||d kr||7 }||kr dS qdS )Nr   )Útzinfog•Ö&è.>TF)ÚSZ_ptr#   Z
astimezoner   )
ÚtradesÚkeyZpriceÚqueueÚkoZcumZts_sÚpÚcÚtsr   r   r   Ú	filled_at9   s     
r,   c                     s  t ƒ tƒ  } }t ttdd�¡}d}g }|  ¡ D ]²\}}||vrBq0|| \}}}	|› d|d  ¡ › �}
|
|vrpq0|	|dœ}d}|D ]P}||d  }|d	kr¢d
} qÔt||
||d |d ƒ}|d u rÊd
} qÔ|||< q‚|r0| 	|¡ q0t
|ƒdk �r
tdt
|ƒ› d�ƒ d S tt
|ƒ› d�ƒ tdd›ddd›d�d dd„ |D ƒ¡ ƒ dd„ dd„ dd„ dœ}i }| ¡ D ]x\}‰‡fdd„|D ƒ‰t
ˆƒdk �r˜�ql‡fd d„|D ƒ}|||< t|d›dt
ˆƒd›d�d d!d„ |D ƒ¡ ƒ �qld"|v �r¼d#|v �r¼|d" |d#  }‰ td$|d% d&›d'|d( d&›d)|d( td*|d% ƒ d+›d,�ƒ td-ˆ d% d&›d.�ƒ t‡ fd/d„t||ƒD ƒd ƒ}|d u�r¨td0|› d1�ƒ td2|› d3�ƒ ntd4|d( › d5�ƒ td6ƒ td7ƒ td8ƒ t
|ƒd9k�r | d#d%g¡d% d }| d:| d"¡¡d% d }d }t|| d"g ¡ƒD ]x\}}|d | | }|d;|  d< }|d u �r^|n|}td=|› d>|d< d?›d@d;| dA›dB|dC›dD|| dE›dF�ƒ �q(tdGƒ tdHƒ t|| d"g ¡ƒD ]:\}}|d dI||   }td=|› dJ|d< dK›d.�ƒ �qÄd S )LNr   )r   )r   é   é   é   ú|r+   )r   ÚtouchTç      Y@g®Gáz®ï?Fr'   r(   éÈ   zonly z legs -- too fewz. legs, queue-aware, priced at touch + premium
zleg typez>10ú Úlegsz>6c                 s   s"   | ]}d t |ƒ d d›V  qdS )ú+r*   z>8N)Ústr)Ú.0r*   r   r   r   Ú	<genexpr>p   ó    zmain.<locals>.<genexpr>c                 S   s   | d dko| d dkS ©Nr   r   r1   gÍÌÌÌÌÌÜ?r   ©r   r   r   r   Ú<lambda>q   r:   zmain.<locals>.<lambda>c                 S   s   | d dko| d dk S r;   r   r<   r   r   r   r=   r   r:   c                 S   s   | d dkS )Nr   r   r   r<   r   r   r   r=   s   r:   )Ú	favouriteúmid H/Ar   c                    s   g | ]}ˆ |ƒr|‘qS r   r   ©r8   r   )Úselr   r   Ú
<listcomp>v   r:   zmain.<locals>.<listcomp>é(   c                    s.   g | ]&‰ d t ‡ fdd„ˆD ƒƒ tˆƒ ‘qS )r2   c                 3   s   | ]}|ˆ  rd V  qdS )r-   Nr   r@   ©r*   r   r   r9   y   r:   z"main.<locals>.<listcomp>.<genexpr>)ÚsumÚlen)r8   )ÚgrD   r   rB   y   r:   c                 s   s   | ]}|d ›d�V  qdS )z>7.1fú%Nr   )r8   r	   r   r   r   r9   |   r:   r>   r   z
  favourite decay 0->+3c : r   z.1fz% -> éÿÿÿÿz% (gš™™™™™¹?z.2fzx retained)z  draw at the touch      : rH   c                 3   s"   | ]\}}|ˆ d  kr|V  qdS )r   Nr   )r8   r*   r	   )r   r   r   r9   „   r:   z.  -> favourite fill rate meets the draw's at +r*   z     which would add zc to every completed bookz  -> even at +zBc the favourite still fills faster than the draw does at the touchz!
=== what it does to the BOOK ===zD  A premium only helps if books still complete. Completion needs allz<  three legs, so it is the PRODUCT of the three fill rates.
r.   r?   g       @éd   z   favourite +zc: completion ~z>5.2fz%  book pays z>4.1fzc  ->  z>6.3fzc per book quoted   (z+.3fzc vs touch)r   z;   and what it does to the STRANDING that actually cost us:r-   z)c: P(short a favourite, book unfinished) z>5.1f)r   r"   ÚjsonÚloadr   r   ÚitemsZ	isoformatr,   ÚappendrF   ÚprintÚjoinÚmaxÚnextÚzipr   )r5   Zcolsr%   ZpremsZrowsr   r   r1   Ú_r   r&   r   Zokr*   ZpxÚfÚgroupsZcurvesÚnameZcurZfavÚmatchÚdZmid0ÚbaseZcompÚvalZstrandr   )r   rG   rA   r   ÚmainL   s¤    

ÿþÿ
ÿ

ÿÿþ
ÿr\   Ú__main__)r   r   )Ú__doc__r   ÚcollectionsrK   Z
statisticsÚsysÚpathÚinsertZ	mm_settler$   Zmm_maker_liveZMLZmm_volatility_cleanr   r   r   r"   r,   r\   Ú__name__r   r   r   r   Ú<module>   s   (
Z