a
    [‚jK  ã                   @   sP   d Z ddlZddlZej dd¡ ddlZdZdd„ Zdd„ Z	e
d	krLe	ƒ  dS )
aL  If a leg fills, should we immediately CROSS to finish the book?

There is no atomic way to sell all three outcomes on Kalshi. Combos are
all-or-nothing parlays priced by RFQ and cannot be sold, so the only way to hold
a box is to leg into it and accept the risk between fills. That risk is the
strategy's whole loss: -72c of partials today against +0.94c of completions.

The alternative to waiting is to CROSS -- once one leg fills, hit the bid on the
other two immediately, turning an unhedged partial into a finished book at a
known cost. That cost is the spread plus the taker fee on two legs.

Worth it only if
    (first fill) + (bid on leg 2) + (bid on leg 3) - taker fees > 1.00

which is a much higher bar than the maker version, because selling into the bid
gives up the spread on both legs. This measures it on today's actual books.
é    NzE:\soccer\maingìQ¸…ë±?c                 C   s   t |  d|   S )Nç      ð?)Ú
TAKER_RATE)Úp© r   ú*E:\soccer\research\mm_cross_to_complete.pyÚ	taker_fee   s    r   c                     sJ  t  dd„ ¡‰ t ¡  d¡pg D ]¦} t|  dd¡ƒ d¡r |  d¡s t|  dd¡ƒ ¡ d	ks t|  d
d¡ƒ ¡ dkrvq ˆ | d  }t|  d¡p�dƒ}|d  |7  < |d  t|  d¡pºdƒ| 7  < q t  t	¡}ˆ D ]&}|d 
| d¡d d… ¡  |¡ qÖtdd›ddd›ddd›ddd›ddd›ddd›�ƒ d }}t| ¡ ƒD �]°\}}t|ƒd k�rd�qHt‡ fd!d"„|D ƒƒ}	tƒ }
d#D ]}|
 |› d|› �¡ �q„|D ]}|
 |¡ �q¤g }d$}|
t|ƒ D ]d}t |¡�pÜi  d%i ¡}| d&¡}|d u �s| d'¡�pdd(k�rd)} �q0| |t|ƒf¡ �qÊ|�rH|�s@�qHt|ƒdk�rR�qHtd*d"„ |D ƒƒ}td+d"„ |D ƒƒ}|	| | }|d7 }||d, 7 }td-| d.d¡d d/… d0›d|	d1 d2›d3t|ƒd›d|d1 d4›d3|d1 d5›d3|d d1 d6›d7�ƒ �qH|�r.td8|› d9|d1 d:›d;|| d1 d<›d=�ƒ td>ƒ td?ƒ td@ƒ d S )ANc                   S   s   ddgS )Ng        r   r   r   r   r   Ú<lambda>   ó    zmain.<locals>.<lambda>ÚfillsZcreated_timeÚ z
2026-08-16Zis_takerZsideZyesÚactionZsellZtickerZcount_fpr   é   Zyes_price_dollarsú-é   ZbookZ32ú Úhavez>6z	legs leftz>10z
cross costz>11Útotalz>7zvs 1.00z>8é   c                 3   s   | ]}ˆ | d  V  qdS )r   Nr   )Ú.0Út©r
   r   r   Ú	<genexpr>5   r	   zmain.<locals>.<genexpr>)ZTIETZmarketZyes_bid_dollarsZstatusZactiveFc                 s   s   | ]\}}|V  qd S ©Nr   ©r   Ú_Úbr   r   r   r   J   r	   c                 s   s   | ]\}}t |ƒV  qd S r   )r   r   r   r   r   r   K   r	   r   z  ZKXé   Z30éd   z>5.0fzc z>10.0fz>6.0fz>+7.1fÚcz
  z, books one leg short; crossing all of them: z+.1fz	c total, z+.2fzc eachzB
  Compare with holding: a partial is EV-neutral in theory but ranzC  -9.7c a book today. Crossing converts that variance into a known,z  usually small, loss.)ÚcollectionsÚdefaultdictÚktZ	get_fillsÚgetÚstrÚ
startswithÚlowerÚfloatÚlistÚjoinÚsplitÚappendÚprintÚsortedÚitemsÚlenÚsumÚsetÚaddZ
get_marketÚreplace)ÚfÚaÚqZbooksr   ZtotÚnZevZtsr   ZlegsÚsuffixZmissingZokÚmZbidZcrossZfeer   r   r   r   Úmain   sˆ    ÿþý$
$"ÿÿ
 (ÿÿÿþ
ÿÿ
r9   Ú__main__)Ú__doc__r   ÚsysÚpathÚinsertZkalshi_traderr!   r   r   r9   Ú__name__r   r   r   r   Ú<module>   s   <