o
    ÷+¿jÐ  ã                   @   sÊ   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
ZedƒZej deeƒ¡ ddlZe	dƒZed d	 d
 Zed d d ZdZdd„ Zdd„ Zddd„ZG dd„ dƒZdS )aê  
lib_v2.py -- [2026-10-01] Playbook matching library v2: the user asked to add information that
may carry direction. Per stock-day, per minute (forward-filled), 6 channels:
  P    price vs open / the stock's ATR%                         (as before)
  W    price vs VWAP / ATR%                                     (as before)
  X    0.5 * log(1 + volume vs its 14-day normal)               (as before)
  SPY  SPY price vs its open, %   (market)                      NEW
  IWM  IWM price vs its open, % / 1.3                           NEW
  GAP  (open / previous close - 1) * 100 / ATR%, same every minute   NEW
(buy/sell flow will be channel 7 once the history flow download is done)
Library cache: data/playbook/library_v2.npz (F, C, H, L, keys)
é    N)Údatetime)ÚPath)ÚZoneInfoz/var/www/screener/tradezAmerica/New_YorkÚreportsÚopen_windowÚbarsÚdataÚplaybookzlibrary_v2.npzi†  c                 C   sv   i }| D ]4}t |d tƒst |d t¡n|d  t¡}|jd d |j d }d|  kr2tk r8n q|||< q|S )Nr   é	   é<   é   )Ú
isinstancer   ÚfromtimestampÚETÚ
astimezoneÚhourÚminuteÚN)ÚrowsÚbyÚbÚtÚm© r   ú2/var/www/screener/trade/reports/playbook/lib_v2.pyÚ_minutes!   s   ,€r   c           
      C   sØ   t d| › d� }i }| ¡ rVt t |d¡¡}dD ]<}t| |g ¡ƒ}t 	t
tj¡}|rQ|t|ƒ d }|}tt
ƒD ]}	|	|v rF||	 d }|| d d ||	< q:|||< q| dt 	t
tj¡¡| d	t 	t
tj¡¡fS )
NÚbench_z.json.gzÚrt)ÚSPYÚIWMé   é   éd   r   r   )ÚBARSÚexistsÚjsonÚloadÚgzipÚopenr   ÚgetÚnpÚzerosr   Úfloat32ÚminÚrange)
ÚdayÚpÚoutÚdÚsr   ÚarrÚoÚlastr   r   r   r   Úbench_paths+   s   
,r7   c              
   C   s  t | ƒ}t|ƒdk rd S |t|ƒ d }| d¡r(| d¡r(|d |d  d nd}| d¡r;||d  d d | nd}t t¡t t¡t t¡t t¡f\}}	}
}t tdftj¡}|d urb|nt|ƒ\}}|dd}}}t	tƒD ]„}| |¡}d}|r²|d |d |d	 |d
 |d f\||< |	|< |
|< }}||d |d	  |d
  d	 | 7 }||7 }n| ||<  |	|< |
|< |||< |rÈ|| n|}|| d d | || d d | dt
 |t|d | dƒ ¡ || || d |f||< qt|||	|
||dœS )Nr"   r    Úatr14Ú
prev_closeg      @g        é   é   é   r!   é   g      à?Úvol_per_mingÍÌÌÌÌÌô?)r5   ÚCÚHÚLÚOÚF)r   Úlenr-   r)   r*   r+   r   r,   r7   r.   ÚmathÚlog1pÚmax)r   Úrefr/   Úbenchr   r5   ÚatrpÚgapr?   r@   rA   rB   rC   ÚspyÚiwmr6   ÚpvÚcvr   r   ÚvÚvwr   r   r   Ú
day_arrays=   s,   ,&,8.$0
ÿrR   c                   @   s   e Zd Zdd„ ZdS )ÚLibc              	   C   sÌ  t  ¡ r)tjt dd�}|d |d |d |d t|d ƒf\| _| _| _| _| _	n¯g g g g g f\}}}}}t
t d¡ƒD ]l}t t |d	¡¡}|d
 }	t|	ƒ}
|d D ]O}t |	|¡}|s^qSt|d | d ||	|
ƒ}|d u rpqS| |d ¡ | |d  tj¡¡ | |d  tj¡¡ | |d  tj¡¡ | |	› d|› �¡ qStj ¡  q<t |¡t |¡t |¡t |¡|f\| _| _| _| _| _	tjt | j| j| j| jt |¡d� t dd„ | j	D ƒ¡| _d S )NT)Úallow_picklerC   r?   r@   rA   Úkeyszbars_20*.json.gzr   ÚdateÚtop30_currentÚsymbolsr   ú|)rC   r?   r@   rA   rU   c                 S   s   g | ]	}|  d ¡d ‘qS )rY   r   )Úsplit)Ú.0Úkr   r   r   Ú
<listcomp>l   s    z Lib.__init__.<locals>.<listcomp>)ÚCACHEr$   r*   r&   ÚlistrC   r?   r@   rA   rU   Úsortedr#   Úglobr%   r'   r(   r7   ÚREFrR   ÚappendÚastyper,   Ú_CACHEÚclearÚarrayÚsavezÚdates)ÚselfÚzrC   r?   r@   rA   rU   r0   r2   r/   rI   r3   rH   Úar   r   r   Ú__init__W   s(   <:,:$zLib.__init__N)Ú__name__Ú
__module__Ú__qualname__rm   r   r   r   r   rS   V   s    rS   )N)Ú__doc__r'   r%   rE   Úsysr   Úpathlibr   Úzoneinfor   Únumpyr*   ÚTRADEÚpathÚinsertÚstrÚ	referencerb   r   r#   r^   r   r   r7   rR   rS   r   r   r   r   Ú<module>   s(    

