a
    ì€€jp  ã                   @   sr   d Z ddlZddlZddlZddlmZ ddlmZ edƒZdZ	g d¢Z
dd	„ Zd
d„ Zdd„ Zedkrneƒ  dS )a€  Poly-US version of touch_premium_curve.py -- what premium maximises E[book]?

Same question, different venue, and Poly-US differs in one way that matters:
it CREDITS the maker (MAKER_THETA = 0.0125 x p(1-p)) where Kalshi pays nothing.
That rebate is added per leg, so the comparison is on what each venue actually
pays rather than on gross spread alone.

WEAKER EVIDENCE THAN THE KALSHI CURVE, and the difference should be stated
rather than buried. Kalshi exposes a trade tape, so its curve is built from
actual prints. Poly-US does not; `last_px` is a LEVEL sampled every 20s, so a
print is inferred from a CHANGE in that level. That is conservative -- it misses
consecutive trades at the same price -- so Poly-US fill rates here are a FLOOR
where the Kalshi ones are a ceiling. The two are not directly comparable at the
level; compare the SHAPE.

Usage:  python research\pus_premium_curve.py
é    N)Údefaultdict)ÚPathz'E:\soccer\research\mm_polyus_quotes.csvgš™™™™™‰?)r   é   é   é   é   é   c              	   C   s(   z
t | ƒW S  ttfy"   Y d S 0 d S )N)ÚfloatÚ	TypeErrorÚ
ValueError)Úx© r   ú'E:\soccer\research\pus_premium_curve.pyÚ_f   s    
r   c                 C   s   t |  d|   d S )Nr   ç      Y@)ÚMAKER_THETA)Úpr   r   r   Úmaker_rebate_c$   s    r   c                     sT  t t ¡ ƒd } t t| ¡ ttƒ}| jdddd��J}t	 
|¡D ].}| d¡r@| d¡s@ds@||d   |¡ q@W d   ƒ n1 s„0    Y  | ¡ D ]}|jdd	„ d
� q–i }| ¡ D ]ð\}}|d }t| d¡ƒt| d¡ƒ }	}
|	d u s¸|
d u s¸d|	  k �rdk �sq¸ q¸d\}}|D ]R}t| d¡ƒ}|d u �r>�q|d u�rj||k�rj|d u �r`|nt||ƒ}|}�q| d¡|	|
d d |d u �r”d n
||	 d dœ||< q¸ttƒ}| ¡ D ]}||d   |¡ �qºdd„ | ¡ D ƒ}tt|ƒ› dt|ƒ› d�ƒ |�sd S tdd›ddd›ddd ›dd!d›dd"d›dd#d›�ƒ tD ]ø‰ t‡ fd$d%„| ¡ D ƒƒ}d&\}}}| ¡ D ]X}t‡ fd'd%„|D ƒƒ�rv|d7 }||d d( d)ˆ   7 }|t‡ fd*d%„|D ƒƒ7 }�qvt|ƒ}tˆ d+›d,d| t|ƒ d-›d.|d+›d/|d0›dd| | d1›d.|d2›d,|d3›d,|| | d4›d5�ƒ �qFtd6ƒ td7ƒ d S )8Nzmm_polyus_quotes.snap.csvÚ zutf-8Úreplace)ÚnewlineÚencodingÚerrorsZmarket_slugÚreasonc                 S   s   |   d¡pdS )NZtsr   )Úget)Úrr   r   r   Ú<lambda>1   ó    zmain.<locals>.<lambda>)Úkeyr   Zp_askZover_askr   )NNZlast_pxZ
event_slugg      ð?r   )ÚeventÚtouchÚover_cÚheadroomr   c                 S   s"   i | ]\}}t |ƒd kr||“qS )r   )Úlen)Ú.0ÚeÚvr   r   r   Ú
<dictcomp>H   r   zmain.<locals>.<dictcomp>z legs, z three-leg books
Zpremiumz>8ú zleg fillz>9zbooks completez>16ÚgrossZrebatezE[book]c                 3   s*   | ]"}|d  dur|d  ˆ krdV  qdS )r"   Nr   r   )r$   r&   ©Údr   r   Ú	<genexpr>P   s   ÿzmain.<locals>.<genexpr>)r   ç        r-   c                 3   s&   | ]}|d  duo|d  ˆ kV  qdS )r"   Nr   ©r$   r   r*   r   r   r,   T   r   r!   r   c                 3   s"   | ]}t |d  ˆ d  ƒV  qdS )r    r   N)r   r.   r*   r   r   r,   W   r   z>7zc z>8.0fz% z / z<5z>3.0fz>7.0fz>7.1fz>8.2fÚczC
E[book] is per book QUOTED and INCLUDES the maker rebate, which iszEcredited win or lose. Fills inferred from last_px changes -- a FLOOR.)r   ÚtempfileZ
gettempdirÚshutilZcopyfileÚQUOTESr   ÚlistÚopenÚcsvZ
DictReaderr   ÚappendÚvaluesÚsortÚitemsr   ÚmaxÚprintr#   ÚPREMIUMSÚsumÚall)ÚtmpZlegsZfhr   r&   ÚinfoZslugZrowsZr0r    ZoverÚprevZmxZlpZbooksZfullZlfZcompr)   ZrebZv3Únr   r*   r   Úmain(   sz    2*


þÿÿÿ
.ÿÿÿþrC   Ú__main__)Ú__doc__r5   r1   r0   Úcollectionsr   Úpathlibr   r2   r   r<   r   r   rC   Ú__name__r   r   r   r   Ú<module>   s   8