a
    •Û�jK  ã                   @   s®   d Z ddl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lmZmZmZ dd„ Zd	d
„ Zdd„ Zedkrªeƒ  dS )aÓ  Test the two remaining ideas: leg the draw first, and raise the gate margin.

Rebates are settled by research rather than code -- Kalshi's open Liquidity
Incentive Program excludes the Sports category outright, and the Liquidity
Provider Program that does cover sports needs an executed Market Maker Agreement
and an auction. Neither is reachable here. That was the lever with real
magnitude, so what is left is the two policy changes.

  DRAW FIRST   Offer only the draw; add home and away once it fills. Legs fill
               independently (0.352^3 = 4.4% against 3.8% observed), so
               completion should not move. What changes is WHAT WE ARE LEFT
               HOLDING when it fails -- never a stranded favourite, which is
               where the live -78c came from.

  MARGIN       The gate needs sum > 1.00 + fees + TARGET_MARGIN. AJAHEE opened
               on 0.63c of margin and had nothing to absorb a goal. Raising the
               bar refuses those, at the cost of volume.

Both scored the same way: queue-aware fills from real trade prints, completions
priced by the gate and ratchet, and PARTIALS CARRIED TO SETTLEMENT. Scoring
completions only is what made the strategy look profitable earlier today.
é    NzE:\soccer\mainzE:\soccer\research)Ú	load_legsÚCACHE)Ú	load_metaÚload_resultsÚWINSc                 C   sì   i }|   ¡ D ]Ú\}}|| d }|› d|d  ¡ › �}||vrD dS d\}}	|| D ]ˆ\}
}}t |
¡}|sp qÞ|d }|jdu |jdu kr¨|jrš| ¡ n|j|jd�}||kr´ qÞ||d krT||7 }||d	 krTd
}	 qÞqT|	||< q|S )z4{leg_ticker: bool} queue-aware, priced at the touch.r   ú|ÚtsN)ç        FÚko)Útzinfog•Ö&è.>ZqueueT)ÚitemsZ	isoformatÚSZ_ptr   Z
astimezoneÚreplace)ÚlegsÚmetaÚtradesÚoutÚtÚvÚpxÚkeyZcumZhitZts_sÚpÚcr   r
   © r   ú%E:\soccer\research\mm_policy_sweep.pyÚ	fills_for!   s.    

r   c                    sž  d }}d}d}d}|   ¡ D �]h\}}	|	d |	d |	d |	d |	d f\}
}‰‰ }tdd	„ |
 ¡ D ƒƒ}t|
 ¡ ƒd
| | kr‚q|d7 }|rÒ‡fdd„|D ƒ}|rˆ |d  s²q|d g‡ ‡fdd„|D ƒ }n‡ fdd„|D ƒ}t|ƒdk�r|d7 }|t|
 ¡ ƒ| d
 7 }q|r|du �r$q|D ]\}ˆ| d ˆ| d  }}|t |¡ t| |k�rdd
nd }||7 }||7 }|d7 }�q(q|||d ||d fS )zC(n_books, completions, total_pnl_cents, partial_legs, partial_pnl).r   r	   Úasksr   r   ÚfilledÚresc                 s   s   | ]}t  |¡V  qd S )N)ÚMLÚ	maker_fee)Ú.0Úar   r   r   Ú	<genexpr>E   ó    zscore.<locals>.<genexpr>g      ð?é   c                    s    g | ]}ˆ | d  dkr|‘qS ©r%   Údrawr   ©r!   r   ©r   r   r   Ú
<listcomp>K   r$   zscore.<locals>.<listcomp>c                    s(   g | ] }ˆ| d  dkrˆ | r|‘qS r&   r   r(   ©r   r   r   r   r*   O   s   ÿc                    s   g | ]}ˆ | r|‘qS r   r   r(   )r   r   r   r*   R   r$   é   Néd   )r   ÚsumÚvaluesÚlenr   r    r   )ÚbooksÚmarginÚ
draw_firstÚnÚdoneZpnlÚplegsÚppnlÚevÚdr   r   r   Zfeesr'   Zshortr   r   Zsider   r   r+   r   Úscore<   s>    ÿ $r:   c                     s  t ƒ tƒ  } ‰ tƒ }t ttdd�¡}t t	¡}|  
¡ D ](\}}|ˆ v r8||| dd¡d  |< q8i }| 
¡ D ]�\}}t|ƒdkr„qnt‡ fdd„|D ƒƒsœqnt|ˆ |ƒ}	|	d u r²qnˆ tt|ƒƒ \}
}
}}}‡ fd	d
„|D ƒt|ƒˆ |	| |||f¡dœ||< qntt|ƒ› d�ƒ tdd›ddd›ddd›ddd›ddd›ddd›ddd›ddd›�ƒ dD ]”}|�rjdnd}d D ]z}t|||ƒ\}}}}}|�s–�qrt|d›d|d! d"›d#|d›d|d›dd$| | d%›d&|d›d|d'›d#|d(›d)�ƒ �qr�q\td*ƒ td+ƒ d S ),Nzutf-8)Úencodingú-r%   r   r,   c                 3   s   | ]}ˆ | d  dv V  qdS )r%   )Úhomer'   ÚawayNr   r(   r)   r   r   r#   p   r$   zmain.<locals>.<genexpr>c                    s   i | ]}|ˆ | d  “qS )r   r   r(   r)   r   r   Ú
<dictcomp>v   r$   zmain.<locals>.<dictcomp>)r   r   r   r   r   z( three-leg books with fills and results
Zpolicyz>14ú r2   z>7r1   z>6r5   z>5Zratezpartial legsz>13zpartial P&Lz>12ZTOTALz>9)FTz
draw firstz	all three)g{®Gázt?g{®Gáz„?g¸…ëQ¸Ž?g{®Gáz”?r-   z>6.1fzc g      Y@z>5.1fz% z>11.1fz>8.1fr   zA
TOTAL includes partials carried to settlement, which is the onlyz=honest basis -- completions alone flattered this all morning.)r   r   r   ÚjsonÚloadÚopenr   ÚcollectionsÚdefaultdictÚdictr   Úrsplitr0   Úallr   ÚnextÚiterÚlistÚgetÚprintr:   )Zlegs_allZres_mapr   Zgroupedr   r   r1   r8   r   ÚfÚ_Úgdr=   r>   r3   Zlblr2   r4   r5   Útotalr6   r7   r   r)   r   Úmainb   s\    
þ*ÿÿÿ&ÿÿÿþrR   Ú__main__)Ú__doc__ZcsvrD   rA   ÚosZ
statisticsÚsysÚpathÚinsertZ	mm_settler   Zmm_maker_liver   Zmm_volatility_cleanr   r   Zmm_partial_pnlr   r   r   r   r:   rR   Ú__name__r   r   r   r   Ú<module>   s   0&(