a
    ×9‚jœ  ã                   @   sl   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¡ ddlZ	dZ
dZdd„ Zdd	„ Zed
krheƒ  dS )aÀ  How often does a stranded 2-of-3 book get rescued in play?

MOQSBA sold home 35c and away 33c pre-match, stranded on the draw, then the game
stayed 0-0, the draw drifted to 52c, and our resting offer was lifted there --
120c collected, +20c locked.

The bar is lower than it looks. If home and away were sold at their touches, the
book needs the draw to fetch roughly

    1.015 - (h + a)  ~=  d_prematch - 0.005

so the draw only has to hold its own pre-match price, not appreciate. Since the
maker now works books for 3 hours after kickoff, the question is simply: how
often does the in-play draw reach its pre-match level?

Measured on live_data, which carries both pre-kickoff and in-play snapshots.
é    NzE:\soccer\mainzE:\soccer\live_dataé   c              	   C   sH   z*t | ƒ}d|  k rdk r&n n|nd W S  ttfyB   Y d S 0 d S )Nr   é   )ÚfloatÚ	TypeErrorÚ
ValueError)ÚxÚv© r	   ú$E:\soccer\research\mm_draw_rescue.pyÚ_f   s
    "r   c                     sR  t dd„ t t¡D ƒƒt d … } t t¡}t t¡}| D ]ø}ttj	 
t|¡ddd��Ê}t |¡D ]®}| d¡}|dv rzqbzt|ƒ}W n tyœ   Y qbY n0 | d¡| d	¡| d
¡f}|dkrìt| d¡ƒ}|d urê||  ||f¡ qbt| d¡ƒ}	|	d urb||  |	¡ qbW d   ƒ q:1 �s(0    Y  q:g }
| ¡ D ]|\}}| |¡}|�r@t|ƒdk �rj�q@t |¡‰ tdd„ |D ƒƒ}t‡ fdd„t |ƒD ƒd ƒ}|
 ˆ |||ˆ kdœ¡ �q@t|
ƒ}|dk �rätd|› d�ƒ d S tdd„ |
D ƒƒ}t|› d�ƒ td|› d|› dd| | d›d�ƒ t dd„ |
D ƒƒ}|�rŠtd|t|ƒd  d ›d!|t|ƒd"  d ›d#|dt|ƒ d"  d ›�ƒ tƒ  td$ƒ d%D ]T\‰‰}t‡‡fd&d„|
D ƒƒ}|�rœtd'|d(›d)|d*›dd| | d+›d�ƒ �qœtd,d„ |
D ƒƒ}td'd-d(›d)|d*›dd| | d+›d�ƒ td.ƒ td/ƒ td0ƒ td1ƒ d S )2Nc                 s   s   | ]}|  d ¡r|V  qdS )z.csvN)Úendswith)Ú.0Úfr	   r	   r
   Ú	<genexpr>$   ó    zmain.<locals>.<genexpr>zutf-8Úreplace)ÚencodingÚerrorsÚelapsed)NÚ Z	game_dateÚhomeZawayr   Zkalshi_draw_midZkalshi_draw_aské   c                 s   s   | ]\}}|V  qd S ©Nr	   )r   Ú_Úmr	   r	   r
   r   B   r   c                 3   s   | ]\}}|ˆ kr|V  qd S r   r	   )r   Úer   )Úd0r	   r
   r   D   r   )r   ÚpeakÚfirstÚreachedé2   zonly z/ fixtures with both pre and in-play draw pricesc                 s   s   | ]}|d  rdV  qdS )r   r   Nr	   ©r   Úrr	   r	   r
   r   L   r   z3 fixtures with pre-kickoff and in-play draw prices
z-  in-play draw reaches its pre-match price : ú/z (g      Y@z.1fz%)c                 s   s"   | ]}|d  dur|d  V  qdS )r   Nr	   r!   r	   r	   r
   r   P   r   z4  when it first gets there (minutes)       : median é   z.0fz, p25 é   z, p75 z1  how far above the pre-match price does it peak:))r   çš™™™™™©?z+0-5c)r&   ç333333Ã?z+5-15c)r'   ç333333Ó?z+15-30c)r(   é	   z+30c or morec                 3   s6   | ].}ˆ|d  |d    kr&ˆ k rn qdV  qdS ©r   r   r   Nr	   r!   )ÚhiÚlor	   r
   r   Z   r   z    z>14ú z>4z>4.1fc                 s   s"   | ]}|d  |d k rdV  qdS r*   r	   r!   r	   r	   r
   r   ]   r   znever reacheszC
  The 3h in-play window covers the whole match, so a book strandedzC  on the draw gets this entire distribution as its second chance --zC  provided the offer is left resting, which is what the HOLD changez1  does. Cancelling on abandon forfeits all of it.)ÚsortedÚosÚlistdirÚLIVE_DIRÚDAYSÚcollectionsÚdefaultdictÚlistÚopenÚpathÚjoinÚcsvZ
DictReaderÚgetr   r   r   ÚappendÚitemsÚlenÚ
statisticsZmedianÚmaxÚnextÚprintÚsum)ÚfilesZpreZliveÚfnZfhr"   ZelÚkeyr   ÚaZrowsÚkZpsZlsr   r   ÚnZhitZfirstsZlblÚcZbelowr	   )r   r+   r,   r
   Úmain#   s‚    "

ÿ

2


ÿ
ÿ
ÿþý.*rJ   Ú__main__)Ú__doc__r9   r3   r/   r>   Úsysr7   ÚinsertZ	mm_settleÚSr1   r2   r   rJ   Ú__name__r	   r	   r	   r
   Ú<module>   s   (C