a
    žD�jß  ã                   @   s`   d Z ddlZddlZddlZdZdZdd„ Zdd	d
„Zdd„ Zdd„ Z	dd„ Z
edkr\e
ƒ  dS )aù  What did COMPLETED books actually collect, over four months of paper trading?

Today's three live books each collected less than the $1.00 they owed. Three is
an anecdote. mm_paper_results.csv holds every leg the paper maker quoted since
April with what it filled at and whether it settled, so the same question can be
asked at n in the hundreds:

  - of books where all three legs filled, how many collected more than 1.00?
  - how much more, after fees?
  - and does quoting a premium over the touch actually fix it, or does it just
    trade completed books for unfilled ones?

The last question is the one that decides whether this strategy exists. A
premium always improves the books that DO complete; it only makes money if
enough of them still complete.
é    Nz'E:\soccer\research\mm_paper_results.csvgìQ¸…ë‘?c                 C   s   t |  d|   S )Nç      ð?)ÚMAKER_FEE_RATE)Úp© r   ú(E:\soccer\research\mm_completed_books.pyÚ	maker_fee   s    r   ç        r   c              	   C   sH   z*t | ƒ}||  k r|k r&n n|nd W S  ttfyB   Y d S 0 d S ©N)ÚfloatÚ	TypeErrorÚ
ValueError)ÚxÚloÚhiÚvr   r   r   Ú_f   s
    "r   c                 C   s   | r|   dd¡d S d S )Nú-é   r   )Úrsplit)Útr   r   r   Úevent_of#   s    r   c                  C   sê   t  t¡} ttddd��¾}t |¡D ]¢}t| d¡ƒ}| d¡pBd 	¡ }|r$|sRq$t
| d¡ƒ}|d u rjq$t| d¡pxd	ƒ ¡ d
v }|r–t
| d¡ƒnd }|||d urª|n|| d¡p¸ddœ| | |< q$W d   ƒ n1 sÜ0    Y  | S )Nzutf-8Úreplace)ÚencodingÚerrorsZ
leg_tickerÚsideÚ Zour_askÚfilledÚ0)Ú1ÚTrueÚtrueZfill_pxÚregimeú?)Úaskr   Úfillr!   )ÚcollectionsÚdefaultdictÚdictÚopenÚRESULTSÚcsvZ
DictReaderr   ÚgetÚlowerr   ÚstrÚstrip)ÚbooksZfhÚrÚevr   r#   r   r$   r   r   r   Úload'   s"    
þ.r2   c                     sÊ  t ƒ } dd„ |  ¡ D ƒ}dd„ | ¡ D ƒ}tt| ƒd›dt|ƒd›dt|ƒd›ddt|ƒ td	t|ƒƒ d
›d�ƒ |s|d S tdƒ g }| ¡ D ]b\}}tdd„ | ¡ D ƒƒ}tdd„ | ¡ D ƒƒ}| ||||| d t| ¡ ƒd d dœ¡ q�t	dd„ |D ƒƒ}t|ƒ}tdt
 |¡d›d||d  d›d|d| d  d›�ƒ tdd„ |D ƒƒ}	td|	› d|› dd|	 | d›d �ƒ d!d"„ |D ƒ}
td#d„ |
D ƒƒ}td$|› d|› dd| | d›d �ƒ td%t
 |
¡d& d'›d(�ƒ td)t
 |
¡d& d'›d(�ƒ td*|› d+t|
ƒd& d,›d(�ƒ d-d"„ | ¡ D ƒ}td.d„ |D ƒƒ�rbtd/t
 |¡d& d0›d(�ƒ ntd1ƒ td2ƒ td3ƒ td4ƒ td5ƒ td6d7d8›d9d:d;›d9d<d=›d9d>d=›�ƒ d?D ]î}|d ‰ g d@ }}| ¡ D ]€\}}tdAd„ | ¡ D ƒƒ�sø�qÔ‡ fdBd"„| ¡ D ƒ}tdCd„ |D ƒƒ�r&�qÔt|ƒ}tdDd„ |D ƒƒ}| || d ¡ �qÔ|�s`�q¶t|ƒ}t|dE›dFt|ƒd;›d9t
 |¡d& dG›dF|d& dH›d(�ƒ �q¶tdIƒ tdJƒ tdKƒ tdLƒ d S )MNc                 S   s"   i | ]\}}t |ƒd kr||“qS )é   )Úlen©Ú.0ÚeÚbr   r   r   Ú
<dictcomp>=   ó    zmain.<locals>.<dictcomp>c                 S   s,   i | ]$\}}t d d„ | ¡ D ƒƒr||“qS )c                 s   s   | ]}|d  V  qdS ©r   Nr   ©r6   Úlr   r   r   Ú	<genexpr>>   r:   z"main.<locals>.<dictcomp>.<genexpr>)ÚallÚvaluesr5   r   r   r   r9   >   r:   ú,z books quoted, z with all three legs, z  completed (all three filled) = g      Y@r   z.1fz%
z&=== what completed books collected ===c                 s   s   | ]}|d  V  qdS ©r$   Nr   r<   r   r   r   r>   I   r:   zmain.<locals>.<genexpr>c                 s   s   | ]}t |d  ƒV  qdS rB   ©r   r<   r   r   r   r>   J   r:   r   r   r!   )r1   ÚcollÚfeesÚnetr!   c                 s   s   | ]}|d  V  qdS )rD   Nr   ©r6   r0   r   r   r   r>   N   r:   z  collected: median z.4fz  p25 é   z  p75 r3   c                 s   s   | ]}|d krdV  qdS )r   r   Nr   )r6   Úcr   r   r   r>   R   r:   z  above 1.00 before fees : ú/z (z.0fz%)c                 S   s   g | ]}|d  ‘qS )rF   r   rG   r   r   r   Ú
<listcomp>T   r:   zmain.<locals>.<listcomp>c                 s   s   | ]}|d krdV  qdS )r   r   Nr   )r6   r   r   r   r   r>   U   r:   z  PROFITABLE after fees  : z  mean net per book      : éd   z+.2frI   z  median net per book    : z  TOTAL over z books    : z+.1fc                 S   s*   g | ]"}|  ¡ D ]}|d  |d  ‘qqS )r$   r#   )r@   )r6   r8   r=   r   r   r   rK   \   r:   c                 s   s   | ]}t |ƒd kV  qdS )g•Ö&è.>N)Úabs)r6   Úsr   r   r   r>   ]   r:   z
  fill vs quote: median z+.3fz8
  fills land exactly at our quote (no per-leg slippage)z-
=== would quoting a premium have helped? ===zD  Premium is added to EVERY leg at quote time. A book completes onlyzB  if each leg still fills -- proxied here by the leg having filledz=  with enough headroom between our ask and 1.00 to absorb it.Ú
zpremium/legz>12ú z
books donez>11zmean netz>10Útotal)r   g      Ð?g      à?g      è?r   g      ø?g       @r   c                 s   s   | ]}|d  V  qdS r;   r   r<   r   r   r   r>   m   r:   c                    s   g | ]}|d  ˆ  ‘qS )r$   r   r<   ©Zpremr   r   rK   o   r:   c                 s   s   | ]}|d kV  qdS )g®Gáz®ï?Nr   ©r6   r   r   r   r   r>   p   r:   c                 s   s   | ]}t |ƒV  qd S r	   rC   rS   r   r   r   r>   s   r:   z>10.2fzc z>9.2fz>9.1fz@
  NOTE: this holds completion FIXED, so it is an upper bound --zD  it shows what a premium is worth IF fills survive it. Whether theyz>  do is a queue question the paper log cannot answer; the livez'  fill data is the only thing that can.)r2   ÚitemsÚprintr4   ÚmaxÚsumr@   ÚappendÚlistÚsortedÚ
statisticsZmedianZmeanÚanyr?   )r/   ZthreeZdoneZrowsr7   r8   rD   rE   ÚnZaboverF   ÚwinZslipZprem_cZkeptZtotZlegsrI   Úfr   rR   r   Úmain;   s‚    ÿþ


þ
ÿÿ&& (
(ÿr`   Ú__main__)r   r   )Ú__doc__r*   r%   r[   r)   r   r   r   r   r2   r`   Ú__name__r   r   r   r   Ú<module>   s   
E