a
    øÄ�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Z
dddd	œZd
Zdd„ Zdd„ Zdd„ Zdd„ Zedkr’eƒ  dS )a¥  In-play swings: how big, and what would they do to a resting ask?

Clarification from the user: "volatility" meant the GAME swinging -- a goal, an
equaliser, a winner in stoppage time -- not the pre-kickoff price drift I had
been measuring. That is a different regime entirely, and one the maker has never
touched: both quoters require `0 < hrs`, so everything stops at kickoff.

Two things decide whether it is worth going there.

  SIZE     -- how much bigger are in-play moves than pre-match ones? If a goal
              reprices a leg by 20c, a resting ask is certain to be reached.

  POISON   -- and that is exactly the problem. A leg fills BECAUSE the goal made
              it more likely, so we sell at the old price into the new truth.
              Pre-match that adverse selection is worth fractions of a cent; a
              goal is worth tens. Measured here as: after a leg trades up
              through a level, where does it settle?

The three-leg structure is the only reason this could work at all -- a completed
book does not care who scores. But it takes one leg filling on a goal, with the
other two left behind, to lose more than a hundred completed books earn.
é    NzE:\soccer\mainzE:\soccer\researchzE:\soccer\live_dataZkalshi_home_midZkalshi_draw_midZkalshi_away_mid)ÚhomeZdrawÚawayé   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_inplay_swings.pyÚ_f#   s
    "r   c                  C   sR  t dd„ t t¡D ƒƒt d … } t dd„ ¡}| D �]}ttj 	t|¡ddd��è}t
 |¡D ]Ì}t | d¡¡}| d	¡}|d u s^|d
v rŽq^zt|ƒ}W n ty°   Y q^Y n0 | d¡| d¡| d¡f}t ¡ D ]R\}}	t| |	¡ƒ}
|
d u röqÖ|||f  }|dk�r|d n|d  ||
f¡ qÖq^W d   ƒ q41 �sB0    Y  q4|S )Nc                 s   s   | ]}|  d ¡r|V  qdS )z.csvN)Úendswith)Ú.0Úfr   r   r   Ú	<genexpr>,   ó    zload.<locals>.<genexpr>c                   S   s
   g g dœS )N)ÚpreÚliver   r   r   r   r   Ú<lambda>-   r   zload.<locals>.<lambda>zutf-8Úreplace)ÚencodingÚerrorsZ	timestampÚelapsed)NÚ Z	game_dater   r   r   r   r   )ÚsortedÚosÚlistdirÚLIVE_DIRÚDAYSÚcollectionsÚdefaultdictÚopenÚpathÚjoinÚcsvZ
DictReaderÚSZ_ptÚgetr   r   ÚSIDESÚitemsr   Úappend)ÚfilesÚlegsÚfnZfhÚrZtsZelÚbaseZsideÚcolÚmÚdr   r   r   Úload+   s0    "
ÿ

Jr3   c                 C   s$   t | ƒ}dd„ t||dd… ƒD ƒS )z.Absolute snapshot-to-snapshot moves, in cents.c                 S   s(   g | ] \\}}\}}t || ƒd  ‘qS )éd   )Úabs)r   Ú_ÚaÚbr   r   r   Ú
<listcomp>G   r   zmoves.<locals>.<listcomp>r   N)r   Úzip)ZseriesÚsr   r   r   ÚmovesD   s    r<   c                  C   s  t ƒ } g g  }}g }|  ¡ D ]¼\}}t|d ƒdkrD|t|d ƒ7 }t|d ƒ}t|ƒdkr|t|ƒ7 }tt|ƒd ƒD ]^}||d  d || d  d }|dkrx||d  d ||d  d  d }	| ||	f¡ qxqtt|ƒd›dt|ƒd›d	�ƒ d
|fd|ffD ]º\}
}|�s�q
t|ƒ}t|ƒ}tdd„ |D ƒƒ}t|
d›d||d  d›d|t	|d ƒ d›d|t	|d ƒ d›d|d d›d�
ƒ tdd›d|d›d|d›dd| | d›d�ƒ �q
|�r|�rtd t|ƒt	t|ƒd ƒ t
d!t|ƒt	t|ƒd ƒ ƒ d"›d#�ƒ |�rtd$t|ƒd›d%�ƒ td&d„ |D ƒƒ}	t|	ƒ}td'ƒ td(|	|d  d)›d*|	|d+  d)›d,|	d-| d+  d)›d�ƒ td.d„ |	D ƒƒ}td/|d›d0|d›dd| | d›d�ƒ td1ƒ td2ƒ td3ƒ t d4d„ |D ƒ¡}td5|d"›d6�ƒ d S )7Nr   é   r   é   r   r4   ú,z pre-match moves, z in-play moves
z	PRE-MATCHzIN-PLAYc                 s   s   | ]}|d krdV  qdS )r=   r   Nr   )r   r	   r   r   r   r   a   r   zmain.<locals>.<genexpr>z>10z	: median é   z.2fzc  p90 gÍÌÌÌÌÌì?zc  p99 g®Gáz®ï?zc  max éÿÿÿÿz.0fÚcr   z  moves >= 5c: z of z (g      Y@z%)z
  in-play p99 is g{®Gáz„?z.1fzx the pre-match p99z
=== the poison: z# in-play up-moves of 5c or more ===c                 s   s   | ]\}}|V  qd S ©Nr   )r   r6   r7   r   r   r   r   l   r   z?  where the price went in the NEXT ~5 snapshots, after jumping:z    median z+.2fzc   p25 é   zc   p75 é   c                 s   s   | ]}|d krdV  qdS )rA   r   Nr   )r   r7   r   r   r   r   q   r   z    held or kept rising: ú/z?
  A resting ask is lifted BY that jump, at the pre-jump price.z@  If the price then holds, the seller is short a leg that is nowz:  worth much more than they got. That is the cost to beat.c                 s   s   | ]\}}|V  qd S rC   r   )r   Újr6   r   r   r   r   v   r   z
  mean jump size z,c -- against a completed book that nets ~2c.)r3   r)   Úlenr<   r   Úranger*   ÚprintÚsumÚintÚmaxÚ
statisticsZmean)r,   Zpre_mZlive_mZjumpsÚkeyr2   r;   ÚiZjumpZafterZlabelr1   Zm2ÚnZbigZheldZavg_jumpr   r   r   ÚmainJ   s\    
 $ .ÿÿ
46ÿ
$ÿ
*rR   Ú__main__)Ú__doc__r%   r    r   rN   Úsysr#   ÚinsertZ	mm_settler&   r   r(   r   r   r3   r<   rR   Ú__name__r   r   r   r   Ú<module>   s   (ÿ1