
    i_                    <    d Z ddlmZ ddlmZmZ ddlZddZddZy)	u=  
Intraday VWAP (cumulative from UTC midnight) — port fedele da V15.

Falls back to "VWAP over the whole DataFrame" when:
  - no timestamp column / DatetimeIndex available
  - fewer than 3 bars since UTC midnight (session just started)

Returns (vwap, deviation_pct) where deviation_pct = (close - vwap)/vwap * 100.
    )annotations)datetimetimezoneNc                   d| j                   v rdnd}|| j                   vs| j                  ry| d   | d   z   | d   z   dz  }|| |   z  j                         }| |   j                         }t        |j                  d   |j                  d   d	z   z        }t        | d   j                  d         }||z
  |d	z   z  d
z  }t        |d      t        |d      fS )z5Fallback: VWAP over the whole DF (no session filter).volumetick_volume        r
   highlowclose   &.>d      )columnsemptycumsumfloatilocround)dfvol_coltypical
cum_tp_volcum_volvwap
last_closedeviation_pcts           //home/work/apex_v16/analysis/indicators/vwap.py_vwap_simpler"      s    "bjj0hmGbjj BHH&zBuI%73q8GBwK'//1Jk  "G$R(84(?@ADr'{''+,J$&4$;7#=Mq>5222    c                   | j                   ryd}dD ]  }|| j                  v s|} n |Qt        | j                  t        j
                        r"| j                         }|j                  |d<   d}nt        |       S t        j                  t        j                        j                  dddd      }| j                         }t        j                  j                  j                  ||         s	 t	        j                   ||   d      ||<   n:||   j$                  j&                  !||   j$                  j)                  d	      ||<   |||   |k\     j                         }t+        |      d
k  rt        |       S d|j                  v rdnd}||j                  vrt        |       S |d   |d   z   |d   z   d
z  }|||   z  j-                         }||   j-                         }	t/        |j0                  d   |	j0                  d   dz   z        }
t/        |d   j0                  d         }||
z
  |
dz   z  dz  }t3        |
d      t3        |d
      fS # t"        $ r t        |       cY S w xY w)a   
    Cumulative VWAP from UTC midnight of the current day.

    Args:
        df5: M5 OHLCV DataFrame with `time`/`timestamp`/`datetime` column
             OR a DatetimeIndex; volume column required.

    Returns:
        (vwap, deviation_pct). (0.0, 0.0) if computation impossible.
    r	   N)	timestamptimer   _tsr   )hourminutesecondmicrosecondT)utcUTCr   r   r   r   r   r   r   r   r   r   )r   r   
isinstanceindexpdDatetimeIndexcopyr"   r   nowr   r,   replaceapitypesis_datetime64_any_dtypeto_datetime	Exceptiondttztz_localizelenr   r   r   r   )df5ts_colcr   today_startdf_todayr   r   r   r   r   r   r    s                r!   calc_vwap_intradayrC       sS    yyF. F
 ~cii!1!12BBuIF$$,,x||,44q 5 K 
B66<<//6
;	%6
=BvJ f:==#F2259BvJ"V*+,113H
8}qC  "h&6&66hMGh&&&C  (5/1HW4EEJGHW--557Jw&&(G$R(84(?@ADx(--b12J$&4$;7#=Mq>5222)  	%$$	%s   /I I$#I$)r   pd.DataFramereturntuple[float, float])r>   rD   rE   rF   )	__doc__
__future__r   r   r   pandasr0   r"   rC    r#   r!   <module>rK      s    # ' 383r#   