o
    I¶Ëj¤  ã                   @   sf   d Z ddlZddlZddlZddlmZ ddlmZ edƒZee	ƒ 
¡ jZe d¡ZG dd„ dƒZdS )	aì  
book_recorder.py -- [2026-10-11] RECORD ONLY (the user): top-of-book pressure per stock per minute, so the V / entry
studies can test bid-size vs ask-size pressure later (the historical data has no order book).
From every live quote (best bid / best ask with their sizes) it keeps, per symbol and minute:
  n quotes, mean bid size, mean ask size, imbalance = (bid size - ask size) / (bid size + ask size) over the minute,
  mean spread %, how often the best bid moved up / down and the best ask moved up / down, and the last quote.
Output: data/book/<date>.jsonl (one row per symbol-minute, written when the next minute's first quote arrives).
Never raises into the stream; never touches trading. Sizes are as Alpaca sends them (SIP: round lots).
é    N)ÚPath)ÚZoneInfozAmerica/New_Yorkztrade_bot.book_recorderc                   @   s&   e Zd Zddd„Zdd„ Zdd„ ZdS )	ÚBookRecorderNc                 C   s$   |pi | _ i | _t ¡ | _d| _d S )Nr   )ÚcfgÚcurÚ	threadingÚLockÚlockÚerrors)Úselfr   © r   úbook_recorder.pyÚ__init__   s   


zBookRecorder.__init__c                 C   sb  zý|  t¡}| d¡}| j�å | j |¡}	|	d ur'|	d |kr'|  ||	¡ d }	|	d u rp|dddddddd| j |¡p;i  d¡| j |¡pEi  d¡dœ}	|	d d u rk|| jv rk| j|  d¡| j|  d¡|	d< |	d	< |	| j|< |	d
  d7  < |	d  |7  < |	d  |7  < || d }
|	d  |
rœ|| |
 d nd7  < |	 d|	d ¡|	 d|	d	 ¡}}|d urÉ|	d  ||k7  < |	d  ||k 7  < |d urá|	d  ||k7  < |	d  ||k 7  < |	j||||d� W d   ƒ W d S 1 söw   Y  W d S  t�y0 } z&|  j	d7  _	| j	dk�r%t
 d|› d|› �¡ W Y d }~d S W Y d }~d S d }~ww )Nz%Y-%m-%d %H:%MÚminuter   g        Úlast_bidÚlast_ask)r   ÚnÚbszÚaszÚsprÚbid_upÚbid_dnÚask_upÚask_dnÚpbÚpar   r   r   é   r   r   é   r   éd   r   r   r   r   )r   r   Úlast_bszÚlast_aszé   z[BOOK] z: )Ú
astimezoneÚETÚstrftimer	   r   ÚgetÚ_flushÚupdateÚ	Exceptionr
   ÚlogÚwarning)r   ÚsymÚtsÚbidÚaskr   r   ÚtÚkeyÚaÚmidr   r   Úer   r   r   Úon_quote   s@   

*ÿ*
0$"((&í$ÿ€þzBookRecorder.on_quotec                 C   s8  |d }|sd S |d |d  }||d dd … |t |d | dƒt |d | dƒ|r8t |d |d  | dƒnd t |d | dƒ|d	 |d
 |d |d | d¡| d¡| d¡| d¡gdœ}td d |d d d… › d� }|jjddd� t|dƒ�}| t |¡d ¡ W d   ƒ d S 1 s•w   Y  d S )Nr   r   r   r   é   r   é   r   r   r   r   r   r   r   r   r    )r+   r   r   Úbid_sizeÚask_sizeÚ	imbalanceÚ
spread_pctr   r   r   r   ÚlastÚdataÚbooké
   z.jsonlT)ÚparentsÚexist_okr1   Ú
)	Úroundr%   ÚBASEÚparentÚmkdirÚopenÚwriteÚjsonÚdumps)r   r+   r1   r   ÚtotÚrowÚpÚfhr   r   r   r&   9   s   20"ý""ÿzBookRecorder._flush)N)Ú__name__Ú
__module__Ú__qualname__r   r4   r&   r   r   r   r   r      s    
r   )Ú__doc__rH   Úloggingr   Úpathlibr   Úzoneinfor   r#   Ú__file__ÚresolverD   rC   Ú	getLoggerr)   r   r   r   r   r   Ú<module>   s    	
