o
    ͿSi&                     @  sr   d dl mZ d dlmZmZmZ d dlZzd dlZd dl	Z
dZW n ey/   dZdZ
dZY nw G dd dZdS )    )annotations)DictTupleListNTFc                   @  s   e Zd Zed8ddZed9d	d
Zed:d9ddZe			d;d<ddZed=ddZed>ddZ	ed8dd Z
ed?d@d#d$ZedAdBd)d*ZedCd.d/ZedDdEd2d3Zed?dFd6d7Zd!S )GIndicatorCalculatordfpd.DataFramereturn	pd.Seriesc                 C  sr   t sd S d| vs| d  dkrtjtj| jdS | d | d  | d  d }|| d   }| d  }|| S )Nvolumer   indexhighlowclose   )
_PANDAS_OKsumpdSeriesnpnanr   cumsum)r   typical_pricecumulative_tp_volumecumulative_volume r   @/var/www/html/Trade-python/app/v1/services/zerodha/indicators.pycalculate_vwap   s   z"IndicatorCalculator.calculate_vwapperiodintc                 C  s   t sd S | d j|dd S )Nr   Fspanadjustr   ewmmean)r   r   r   r   r   calculate_ema   s   z!IndicatorCalculator.calculate_ema   c           	      C  s~   t sd S | d  }||dkd}||dk d }|jd| dd }|jd| dd }|| }ddd|   }|S )Nr   r      F)alphar#   d   )r   diffwherer%   r&   )	r   r   deltagainlossavg_gainavg_lossrsrsir   r   r   calculate_rsi!   s   z!IndicatorCalculator.calculate_rsi      	   fastslowsignal&Tuple[pd.Series, pd.Series, pd.Series]c           	      C  s`   t sdS | d j|dd }| d j|dd }|| }|j|dd }|| }|||fS )N)NNNr   Fr!   r$   )	r   r9   r:   r;   ema_fastema_slow	macd_linesignal_line	histogramr   r   r   calculate_macd1   s   
z"IndicatorCalculator.calculate_macdDict[str, float]c           	      C  s   t si S t| dk ri S | jd }|d |d  |d  d }d| |d  }d| |d  }||d |d   }||d |d   }|d|d |d    }|d|d |d    }|||||||dS )	z;Calculate Standard Pivot Points (R3, R2, R1, P, S1, S2, S3)r)   r   r   r   r      )R1R2R3PS1S2S3)r   leniloc)	r   prevpr1s1r2s2r3s3r   r   r   calculate_pivot_pointsA   s   
z*IndicatorCalculator.calculate_pivot_pointsperiodslistDict[str, pd.Series]c                 C  s0   t si S i }|D ]}t| ||d| < q|S )Nema_)r   r   r'   )r   rX   emasr   r   r   r   calculate_ema_stackS   s   z'IndicatorCalculator.calculate_ema_stackc                 C  s   t sd S | d  S )Nr   )r   r,   )r   r   r   r   calculate_volume_delta\   s   z*IndicatorCalculator.calculate_volume_deltaNema_periodsc                 C  s   t s| S |du rg d}t| | d< t| | d< t| \| d< | d< | d< t| |}| D ]\}}|| |< q2t| | d< | S )	z)Add all technical indicators to DataFrameN   r8            7   r+      vwapr4   macdmacd_signal	macd_histvolume_delta)r   r   r   r5   rB   r]   itemsr^   )r   r_   	ema_stacknameseriesr   r   r   add_all_indicatorsb   s   
z&IndicatorCalculator.add_all_indicators
         @
multiplierfloatc           
      C  s:  t sdS | d | d  d }| d | d dd  | }|||  }|||  }tj| jd}tjd| jd}tdt	| D ]U}	| d	 j
|	d  |j
|	d  krwt|j
|	 |j
|	d  |j
|	< | d	 j
|	 |j
|	 k rvd
|j
|	< qCt|j
|	 |j
|	d  |j
|	< | d	 j
|	 |j
|	 krd|j
|	< qC||fS )z
        Calculate Supertrend indicator
        Returns: SuperTrend values and direction (1 for uptrend, -1 for downtrend)
        )NNr   r   rE   c                 S  s   | | S Nr   )xyr   r   r   <lambda>   s    z:IndicatorCalculator.calculate_supertrend.<locals>.<lambda>r   r)   r   rD   )r   combineabsrollingr&   r   r   r   rangerM   rN   minmax)
r   r   rs   hl2atr
upper_band
lower_band
supertrend	directionir   r   r   calculate_supertrend}   s&   &  
 
z(IndicatorCalculator.calculate_supertrendtimeframe_dataDict[str, pd.DataFrame]Dict[str, Dict[str, float]]c                 C  sX   t si S i }|  D ]\}}|d u s|jrq
| dd}|dv r)t|||< q
|S )N  )30min30minutedayweekmonth)r   rl   emptylowerreplacer   rW   )r   pivotstfr   tf_normr   r   r    calculate_multi_timeframe_pivots   s   z4IndicatorCalculator.calculate_multi_timeframe_pivotsr+   lookbackc                 C  s   t r	| du s	| jri S | |}|jri S t|d  }t|d  }||kr+i S || }||d|  |d|  |d|  |d|  |d|  |d	}|d
|  |d|  d}||||dS )a	  Calculate simple Fibonacci retracement and extension levels.

        Uses the highest high and lowest low over the last `lookback`
        bars (or all available if fewer) to define a swing, then
        computes standard retracement and extension levels.
        Nr   r   gh|?5?g r?g      ?g-?gx&?)0z23.6z38.250z61.8z78.6100g rh?)z127.2z161.8)
swing_high	swing_lowretracements
extensions)r   r   tailrt   r~   r}   )r   r   windowr   r   r,   r   r   r   r   r   calculate_fibonacci_levels   s4   	







z.IndicatorCalculator.calculate_fibonacci_levels	List[int]Dict[str, any]c           	      C  s   t r	| du s	| jri S |du rg d}tj|  |d}|jd }dd }i }|D ]}d| }||jv r@||||t|< q)||d||d	||d
||d||d||dd|jv rq||dnd||d|d	}|S )a  Return a compact summary of key indicators for the latest bar.

        Expects a DataFrame with at least columns: high, low, close and
        ideally volume. This is intended to be used by higher-level
        services (like teGPT) to build structured numeric features for
        GPT prompts or internal scoring, without bloating the payload
        with full indicator series.
        Nr`   )r_   rD   c                 S  sL   z| d u s
t | trW d S t| }t|r|W S d W S  ty%   Y d S w ru   )
isinstanceboolrt   mathisfinite	Exception)vfr   r   r   _finite_float   s   z>IndicatorCalculator.summarize_dataframe.<locals>._finite_floatr[   r   rg   r4   rh   ri   rj   r   rk   )	r   rg   r4   rh   ri   rj   r   rk   ema)	r   r   r   rp   copyrN   columnsgetstr)	r   r_   df_indlastr   
ema_valuesr   colsummaryr   r   r   summarize_dataframe   s0   
	

z'IndicatorCalculator.summarize_dataframe)r   r   r	   r
   )r   r   r   r    r	   r
   )r(   )r6   r7   r8   )
r   r   r9   r    r:   r    r;   r    r	   r<   )r   r   r	   rC   )r   r   rX   rY   r	   rZ   ru   )r   r   r_   rY   r	   r   )rq   rr   )r   r   r   r    rs   rt   r	   r
   )r   r   r	   r   )r+   )r   r   r   r    r	   r   )r   r   r_   r   r	   r   )__name__
__module____qualname__staticmethodr   r'   r5   rB   rW   r]   r^   rp   r   r   r   r   r   r   r   r   r      s8    
/r   )
__future__r   typingr   r   r   r   pandasr   numpyr   r   r   r   r   r   r   r   <module>   s    