o
    ?Biu`                     @   s\  d Z ddlZddlZddlZddlmZm	Z	 G dd deZ
G dd deZG dd	 d	eZG d
d deZG dd deZG dd deZG dd deZG dd deZG dd deZd6ddZd6ddZd7ddZd8dd Zd9d"d#Zd9d$d%Z	d:d&eje d'efd(d)Zd6d*d+Zd9d,d-Z	!	d9d.ejd/ejd0ejd1ejd2ed3efd4d5Z dS );zi
.. module:: volume
   :synopsis: Volume Indicators.

.. moduleauthor:: Dario Lopez Padial (Bukosabino)

    N)IndicatorMixin_emac                   @   sR   e Zd ZdZ	ddejdejdejdejdef
dd	Zd
d ZdejfddZ	dS )AccDistIndexIndicatora  Accumulation/Distribution Index (ADI)

    Acting as leading indicator of price movements.

    https://school.stockcharts.com/doku.php?id=technical_indicators:accumulation_distribution_line

    Args:
        high(pandas.Series): dataset 'High' column.
        low(pandas.Series): dataset 'Low' column.
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        fillna(bool): if True, fill nan values.
    Fhighlowclosevolumefillnac                 C   *   || _ || _|| _|| _|| _|   d S N)_high_low_close_volume_fillna_run)selfr   r   r   r   r	    r   I/var/www/html/Trade-python/venv/lib/python3.10/site-packages/ta/volume.py__init__       zAccDistIndexIndicator.__init__c                 C   sF   | j | j | j| j   | j| j  }|d}|| j }| | _d S N        )r   r   r   r	   r   cumsum_adi)r   clvadir   r   r   r   /   s   


zAccDistIndexIndicator._runreturnc                 C      | j | jdd}tj|ddS )zrAccumulation/Distribution Index (ADI)

        Returns:
            pandas.Series: New feature generated.
        r   valuer   name)_check_fillnar   pdSeries)r   r   r   r   r   acc_dist_index7      z$AccDistIndexIndicator.acc_dist_indexNF)
__name__
__module____qualname____doc__r$   r%   boolr   r   r&   r   r   r   r   r      s     
r   c                   @   D   e Zd ZdZddejdejdefddZdd	 Zd
ejfddZ	dS )OnBalanceVolumeIndicatora_  On-balance volume (OBV)

    It relates price and volume in the stock market. OBV is based on a
    cumulative total volume.

    https://en.wikipedia.org/wiki/On-balance_volume

    Args:
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        fillna(bool): if True, fill nan values.
    Fr   r   r	   c                 C      || _ || _|| _|   d S r   r   r   r   r   r   r   r   r	   r   r   r   r   O      z!OnBalanceVolumeIndicator.__init__c                 C   s>   t | j| jdk | j | j}tj|| jjd | _	d S )N   )index)
npwherer   shiftr   r$   r%   r5   r   _obvr   obvr   r   r   r   U   s   "zOnBalanceVolumeIndicator._runr   c                 C   r   )zdOn-balance volume (OBV)

        Returns:
            pandas.Series: New feature generated.
        r   r   r;   r!   )r#   r9   r$   r%   r:   r   r   r   on_balance_volumeY   r'   z*OnBalanceVolumeIndicator.on_balance_volumeNr(   )
r)   r*   r+   r,   r$   r%   r-   r   r   r<   r   r   r   r   r/   A   s
    r/   c                   @   X   e Zd ZdZ		ddejdejdejdejded	efd
dZdd Z	dejfddZ
dS )ChaikinMoneyFlowIndicatora  Chaikin Money Flow (CMF)

    It measures the amount of Money Flow Volume over a specific period.

    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:chaikin_money_flow_cmf

    Args:
        high(pandas.Series): dataset 'High' column.
        low(pandas.Series): dataset 'Low' column.
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        window(int): n period.
        fillna(bool): if True, fill nan values.
       Fr   r   r   r   windowr	   c                 C   0   || _ || _|| _|| _|| _|| _|   d S r   r   r   r   r   _windowr   r   r   r   r   r   r   r@   r	   r   r   r   r   s      	z"ChaikinMoneyFlowIndicator.__init__c                 C   sx   | j | j | j| j   | j| j  }|d}|| j9 }| jr!dn| j}|j| j|d | jj| j|d  | _	d S )Nr   r   min_periods)
r   r   r   r	   r   r   rC   rollingsum_cmf)r   mfvrG   r   r   r   r      s   


zChaikinMoneyFlowIndicator._runr   c                 C   r   )zeChaikin Money Flow (CMF)

        Returns:
            pandas.Series: New feature generated.
        r   r   cmfr!   )r#   rJ   r$   r%   )r   rL   r   r   r   chaikin_money_flow   r'   z,ChaikinMoneyFlowIndicator.chaikin_money_flowNr?   F)r)   r*   r+   r,   r$   r%   intr-   r   r   rM   r   r   r   r   r>   c   s&    
r>   c                	   @   sL   e Zd ZdZ		ddejdejdedefdd	Zd
d Z	dejfddZ
dS )ForceIndexIndicatora  Force Index (FI)

    It illustrates how strong the actual buying or selling pressure is. High
    positive values mean there is a strong rising trend, and low values signify
    a strong downward trend.

    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:force_index

    Args:
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        window(int): n period.
        fillna(bool): if True, fill nan values.
       Fr   r   r@   r	   c                 C   s$   || _ || _|| _|| _|   d S r   )r   r   rC   r   r   )r   r   r   r@   r	   r   r   r   r      s
   zForceIndexIndicator.__init__c                 C   s0   | j | j d | j }t|| j| jd| _d S )Nr4   )r	   )r   r8   r   r   rC   r   _fir   	fi_seriesr   r   r   r      s   zForceIndexIndicator._runr   c                 C   &   | j | jdd}tj|d| j dS )z]Force Index (FI)

        Returns:
            pandas.Series: New feature generated.
        r   r   fi_r!   )r#   rR   r$   r%   rC   rS   r   r   r   force_index      zForceIndexIndicator.force_indexNrQ   F)r)   r*   r+   r,   r$   r%   rO   r-   r   r   rW   r   r   r   r   rP      s    
rP   c                   @   sb   e Zd ZdZ		ddejdejdejdedef
d	d
Zdd Z	dejfddZ
dejfddZdS )EaseOfMovementIndicatora  Ease of movement (EoM, EMV)

    It relate an asset's price change to its volume and is particularly useful
    for assessing the strength of a trend.

    https://en.wikipedia.org/wiki/Ease_of_movement

    Args:
        high(pandas.Series): dataset 'High' column.
        low(pandas.Series): dataset 'Low' column.
        volume(pandas.Series): dataset 'Volume' column.
        window(int): n period.
        fillna(bool): if True, fill nan values.
       Fr   r   r   r@   r	   c                 C   r
   r   )r   r   r   rC   r   r   )r   r   r   r   r@   r	   r   r   r   r      r   z EaseOfMovementIndicator.__init__c                 C   sB   | j d| jd | j | j  d| j  | _|  jd9  _d S )Nr4      i )r   diffr   r   _emv)r   r   r   r   r      s   
zEaseOfMovementIndicator._runr   c                 C   rU   )zhEase of movement (EoM, EMV)

        Returns:
            pandas.Series: New feature generated.
        r   r   eom_r!   )r#   r^   r$   r%   rC   )r   emvr   r   r   ease_of_movement   rX   z(EaseOfMovementIndicator.ease_of_movementc                 C   sJ   | j rdn| j}| jj| j|d }| j|dd}tj|d| j dS )zoSignal Ease of movement (EoM, EMV)

        Returns:
            pandas.Series: New feature generated.
        r   rF   r   sma_eom_r!   )r   rC   r^   rH   meanr#   r$   r%   )r   rG   r`   r   r   r   sma_ease_of_movement   s   z,EaseOfMovementIndicator.sma_ease_of_movementNr[   F)r)   r*   r+   r,   r$   r%   rO   r-   r   r   ra   rd   r   r   r   r   rZ      s$    
	rZ   c                   @   sX   e Zd ZdZ			ddejdejdedeje	 def
d	d
Z
dd ZdejfddZdS )VolumePriceTrendIndicatora  Volume-price trend (VPT)

    Is based on a running cumulative volume that adds or substracts a multiple
    of the percentage change in share price trend and current volume, depending
    upon the investment's upward or downward movements.

    https://en.wikipedia.org/wiki/Volume%E2%80%93price_trend

    Args:
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        fillna(bool)=False: if True, fill nan values. DO NOT RECCOMEND to set it True.
        smoothing_factor(int)=None: will smooth default VPT implementation with SMA.
        dropnans(bool)=False: drop nans after indicator calculated.
    FNr   r   r	   smoothing_factordropnansc                 C   r
   r   )r   r   r   _smoothing_factor	_dropnansr   )r   r   r   r	   rg   rh   r   r   r   r     r   z"VolumePriceTrendIndicator.__init__c                 C   s^   | j  | j  | _| jr"| jrdn| j}| jj| j|d | _| j	r-| j
 | _d S d S )Nr   rF   )r   
pct_changer   r   _vptri   r   rH   rc   rj   dropna)r   rG   r   r   r   r   !  s   zVolumePriceTrendIndicator._runr   c                 C   r   )zeVolume-price trend (VPT)

        Returns:
            pandas.Series: New feature generated.
        r   r   vptr!   )r#   rl   r$   r%   )r   rn   r   r   r   volume_price_trend+  r'   z,VolumePriceTrendIndicator.volume_price_trendFNF)r)   r*   r+   r,   r$   r%   r-   tpOptionalrO   r   r   ro   r   r   r   r   rf     s$    

rf   c                   @   r.   )NegativeVolumeIndexIndicatora9  Negative Volume Index (NVI)

    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:negative_volume_inde

    Args:
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        fillna(bool): if True, fill nan values with 1000.
    Fr   r   r	   c                 C   r0   r   r1   r2   r   r   r   r   @  r3   z%NegativeVolumeIndexIndicator.__init__c                 C   s   | j  }| jd| jk}tjtj| j jddd| _	d| j	j
d< tdt| j	D ]'}|j
| rD| j	j
|d  d|j
|   | j	j
|< q)| j	j
|d  | j	j
|< q)d S )Nr4   float64nvi)datar5   dtyper"     r   g      ?)r   rk   r   r8   r$   r%   r6   nanr5   _nviilocrangelen)r   price_changevol_decreaseir   r   r   r   F  s   

(z!NegativeVolumeIndexIndicator._runr   c                 C   r   )zhNegative Volume Index (NVI)

        Returns:
            pandas.Series: New feature generated.
        rx   r   ru   r!   )r#   rz   r$   r%   )r   ru   r   r   r   negative_volume_indexS  s   z2NegativeVolumeIndexIndicator.negative_volume_indexNr(   )
r)   r*   r+   r,   r$   r%   r-   r   r   r   r   r   r   r   rs   5  s
    
rs   c                   @   r=   )MFIIndicatora  Money Flow Index (MFI)

    Uses both price and volume to measure buying and selling pressure. It is
    positive when the typical price rises (buying pressure) and negative when
    the typical price declines (selling pressure). A ratio of positive and
    negative money flow is then plugged into an RSI formula to create an
    oscillator that moves between zero and one hundred.

    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:money_flow_index_mfi

    Args:
        high(pandas.Series): dataset 'High' column.
        low(pandas.Series): dataset 'Low' column.
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        window(int): n period.
        fillna(bool): if True, fill nan values.
    r[   Fr   r   r   r   r@   r	   c                 C   rA   r   rB   rD   r   r   r   r   r  rE   zMFIIndicator.__init__c              
   C   s   | j | j | j d }t||dkdt||dk dd}|| j | }| jr-dn| j}|j	| j|dj
dd dd	}t|j	| j|dj
d
d dd	}|| }ddd|   | _d S )N      @r4   r   rF   c                 S   s   t t | dk| dS r   r6   rI   r7   xr   r   r   <lambda>      z#MFIIndicator._run.<locals>.<lambda>T)rawc                 S   s   t t | dk | dS r   r   r   r   r   r   r     r   d   )r   r   r   r6   r7   r8   r   r   rC   rH   applyabs_mfi)r   typical_priceup_downmfrrG   n_positive_mfn_negative_mfmfir   r   r   r     s$   
zMFIIndicator._runr   c                 C   rU   )zcMoney Flow Index (MFI)

        Returns:
            pandas.Series: New feature generated.
        2   r   mfi_r!   )r#   r   r$   r%   rC   )r   r   r   r   r   money_flow_index  rX   zMFIIndicator.money_flow_indexNre   )r)   r*   r+   r,   r$   r%   rO   r-   r   r   r   r   r   r   r   r   ^  s&    
r   c                   @   r=   )VolumeWeightedAveragePrice  Volume Weighted Average Price (VWAP)

    VWAP equals the dollar value of all trading periods divided
    by the total trading volume for the current day.
    The calculation starts when trading opens and ends when it closes.
    Because it is good for the current trading day only,
    intraday periods and data are used in the calculation.

    https://school.stockcharts.com/doku.php?id=technical_indicators:vwap_intraday

    Args:
        high(pandas.Series): dataset 'High' column.
        low(pandas.Series): dataset 'Low' column.
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        window(int): n period.
        fillna(bool): if True, fill nan values.

    Returns:
        pandas.Series: New feature generated.
    r[   Fr   r   r   r   r@   r	   c                 C   rA   r   rB   rD   r   r   r   r     rE   z#VolumeWeightedAveragePrice.__init__c                 C   sh   | j | j | j d }|| j }| jrdn| j}|j| j|d }| jj| j|d }|| | _d S )Nr   r   rF   )	r   r   r   r   r   rC   rH   rI   vwap)r   r   typical_price_volumerG   total_pvtotal_volumer   r   r   r     s   
zVolumeWeightedAveragePrice._runr   c                 C   s"   |  | j}tj|d| j dS )zqVolume Weighted Average Price (VWAP)

        Returns:
            pandas.Series: New feature generated.
        vwap_r!   )r#   r   r$   r%   rC   )r   r   r   r   r   volume_weighted_average_price  s   z8VolumeWeightedAveragePrice.volume_weighted_average_priceNre   )r)   r*   r+   r,   r$   r%   rO   r-   r   r   r   r   r   r   r   r     s&    
r   Fc                 C      t | ||||d S )a  Accumulation/Distribution Index (ADI)

    Acting as leading indicator of price movements.

    https://en.wikipedia.org/wiki/Accumulation/distribution_index

    Args:
        high(pandas.Series): dataset 'High' column.
        low(pandas.Series): dataset 'Low' column.
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        fillna(bool): if True, fill nan values.

    Returns:
        pandas.Series: New feature generated.
    r   r   r   r   r	   )r   r&   r   r   r   r   r&     s
   
r&   c                 C      t | ||d S )a  On-balance volume (OBV)

    It relates price and volume in the stock market. OBV is based on a
    cumulative total volume.

    https://en.wikipedia.org/wiki/On-balance_volume

    Args:
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        fillna(bool): if True, fill nan values.

    Returns:
        pandas.Series: New feature generated.
    r   r   r	   )r/   r<   r   r   r   r   r<     s
   r<   r?   c                 C   s   t | |||||d S )a8  Chaikin Money Flow (CMF)

    It measures the amount of Money Flow Volume over a specific period.

    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:chaikin_money_flow_cmf

    Args:
        high(pandas.Series): dataset 'High' column.
        low(pandas.Series): dataset 'Low' column.
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        window(int): n period.
        fillna(bool): if True, fill nan values.

    Returns:
        pandas.Series: New feature generated.
    r   r   r   r   r@   r	   )r>   rM   r   r   r   r   rM     s
   rM   rQ   c                 C   s   t | |||d S )a1  Force Index (FI)

    It illustrates how strong the actual buying or selling pressure is. High
    positive values mean there is a strong rising trend, and low values signify
    a strong downward trend.

    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:force_index

    Args:
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        window(int): n period.
        fillna(bool): if True, fill nan values.

    Returns:
        pandas.Series: New feature generated.
    r   r   r@   r	   )rP   rW   r   r   r   r   rW   .  s
   rW   r[   c                 C   r   a  Ease of movement (EoM, EMV)

    It relate an asset's price change to its volume and is particularly useful
    for assessing the strength of a trend.

    https://en.wikipedia.org/wiki/Ease_of_movement

    Args:
        high(pandas.Series): dataset 'High' column.
        low(pandas.Series): dataset 'Low' column.
        volume(pandas.Series): dataset 'Volume' column.
        window(int): n period.
        fillna(bool): if True, fill nan values.

    Returns:
        pandas.Series: New feature generated.
    r   r   r   r@   r	   )rZ   ra   r   r   r   r   ra   E  
   
ra   c                 C   r   r   )rZ   rd   r   r   r   r   rd   \  r   rd   rg   rh   c                 C   r   )a  Volume-price trend (VPT)

    Is based on a running cumulative volume that adds or substracts a multiple
    of the percentage change in share price trend and current volume, depending
    upon the investment's upward or downward movements.

    https://en.wikipedia.org/wiki/Volume%E2%80%93price_trend

    Args:
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        fillna(bool)=False: if True, fill nan values. DO NOT RECCOMEND to set it True.
        smoothing_factor(int)=None: will smooth default VPT implementation with SMA.
        dropnans(bool)=False: drop nans after indicator calculated.

    Returns:
        pandas.Series: New feature generated.
    r   r   r	   rg   rh   )rf   ro   r   r   r   r   ro   s  s   ro   c                 C   r   )aO  Negative Volume Index (NVI)

    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:negative_volume_inde

    The Negative Volume Index (NVI) is a cumulative indicator that uses the
    change in volume to decide when the smart money is active. Paul Dysart
    first developed this indicator in the 1930s. [...] Dysart's Negative Volume
    Index works under the assumption that the smart money is active on days
    when volume decreases and the not-so-smart money is active on days when
    volume increases.

    The cumulative NVI line was unchanged when volume increased from one
    period to the other. In other words, nothing was done. Norman Fosback, of
    Stock Market Logic, adjusted the indicator by substituting the percentage
    price change for Net Advances.

    This implementation is the Fosback version.

    If today's volume is less than yesterday's volume then:
        nvi(t) = nvi(t-1) * ( 1 + (close(t) - close(t-1)) / close(t-1) )
    Else
        nvi(t) = nvi(t-1)

    Please note: the "stockcharts.com" example calculation just adds the
    percentange change of price to previous NVI when volumes decline; other
    sources indicate that the same percentage of the previous NVI value should
    be added, which is what is implemented here.

    Args:
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        fillna(bool): if True, fill nan values with 1000.

    Returns:
        pandas.Series: New feature generated.

    See also:
        https://en.wikipedia.org/wiki/Negative_volume_index
    r   )rs   r   r   r   r   r   r     s
   (r   c                 C      t | |||||d}| S )aT  Money Flow Index (MFI)

    Uses both price and volume to measure buying and selling pressure. It is
    positive when the typical price rises (buying pressure) and negative when
    the typical price declines (selling pressure). A ratio of positive and
    negative money flow is then plugged into an RSI formula to create an
    oscillator that moves between zero and one hundred.

    http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:money_flow_index_mfi

    Args:
        high(pandas.Series): dataset 'High' column.
        low(pandas.Series): dataset 'Low' column.
        close(pandas.Series): dataset 'Close' column.
        volume(pandas.Series): dataset 'Volume' column.
        window(int): n period.
        fillna(bool): if True, fill nan values.

    Returns:
        pandas.Series: New feature generated.

    r   )r   r   r   r   r   r   r@   r	   	indicatorr   r   r   r     s   r   r   r   r   r   r@   r	   c                 C   r   )r   r   )r   r   r   r   r   r   r     s   r   r(   rN   rY   re   rp   )!r,   typingrq   numpyr6   pandasr$   ta.utilsr   r   r   r/   r>   rP   rZ   rf   rs   r   r   r&   r<   rM   rW   ra   rd   rr   rO   r-   ro   r   r   r%   r   r   r   r   r   <module>   sV    0"7+<4)J
D







-"