
    'i4                        d dl mZmZmZmZ ddlmZmZmZm	Z	m
Z
  G d de      Z G d de      Z G d d	e      Z G d
 de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d dee      Z G d dee      Zy)    )absolute_importdivisionprint_functionunicode_literals   )	IndicatorAndIfMovAvATRc                   &     e Zd ZdZdZ fdZ xZS )UpMovea  
    Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
    Technical Trading Systems"* as part of the Directional Move System to
    calculate Directional Indicators.

    Positive if the given data has moved higher than the previous day

    Formula:
      - upmove = data - data(-1)

    See:
      - https://en.wikipedia.org/wiki/Average_directional_movement_index
    )upmovec                     | j                   | j                  d      z
  | j                  _        t        t        |           y N)datalinesr   superr   __init__self	__class__s    c/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/indicators/directionalmove.pyr   zUpMove.__init__+   s-     II		"5

fd$&    __name__
__module____qualname____doc__r   r   __classcell__r   s   @r   r   r      s     E' 'r   r   c                   &     e Zd ZdZdZ fdZ xZS )DownMovea  
    Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
    Technical Trading Systems"* as part of the Directional Move System to
    calculate Directional Indicators.

    Positive if the given data has moved lower than the previous day

    Formula:
      - downmove = data(-1) - data

    See:
      - https://en.wikipedia.org/wiki/Average_directional_movement_index
    )downmovec                     | j                  d      | j                   z
  | j                  _        t        t        |           y r   )r   r   r%   r   r$   r   r   s    r   r   zDownMove.__init__@   s-    "iimdii7

h&(r   r   r"   s   @r   r$   r$   0   s     E) )r   r$   c                   z     e Zd ZdZddej
                  ffZ e ed       ed            Zd Z	d
 fd		Z
 xZS )_DirectionalIndicatora  
    This class serves as the root base class for all "Directional Movement
    System" related indicators, given that the calculations are first common
    and then derived from the common calculations.

    It can calculate the +DI and -DI values (using kwargs as the hint as to
    what to calculate) but doesn't assign them to lines. This is left for
    sublcases of this class.
    )period   movav+DI_name-DIplusDIminusDIc                     | j                   j                  g}|| j                   j                  g| j                   j                  d      z  z  }|S )Nr+   )pr)   r+   
notdefault)r   plabelss     r   
_plotlabelz _DirectionalIndicator._plotlabelS   s>    66==/DFFLL>DFF$5$5g$>>>r   c                    t        | j                  | j                  j                  | j                  j                        }| j                  j
                  | j                  j                  d      z
  }| j                  j                  d      | j                  j                  z
  }|r]t        ||kD  |dkD        }t        ||d      }| j                  j	                  || j                  j                        }d|z  |z  | _	        |r]t        ||kD  |dkD        }	t        |	|d      }
| j                  j	                  |
| j                  j                        }d|z  |z  | _
        t        t        | 7          y )N)r)   r+   r   g        r)         Y@)r   r   r4   r)   r+   highlowr	   r
   DIplusDIminusr   r(   r   )r   _plus_minusatrr   r%   plusplusDMplusDMavminusminusDM	minusDMavr   s               r   r   z_DirectionalIndicator.__init__X   s   $))DFFMMF$)).."4499==$tyy}}4v(&3,7Dfc*Fvv||F466==|AH(*S0DK6)8c>:E#.GWTVV]]CI 9,s2DL#T35r   )TT)r   r   r   r    r   Smoothedparamsdict	plotlinesr7   r   r!   r"   s   @r   r(   r(   E   sB     w78FDu-t%7HII
6 6r   r(   c                   *     e Zd ZdZdZdZ fdZ xZS )DirectionalIndicatora-  
    Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
    Technical Trading Systems"*.

    Intended to measure trend strength

    This indicator shows +DI, -DI:
      - Use PlusDirectionalIndicator (PlusDI) to get +DI
      - Use MinusDirectionalIndicator (MinusDI) to get -DI
      - Use AverageDirectionalIndex (ADX) to get ADX
      - Use AverageDirectionalIndexRating (ADXR) to get ADX, ADXR
      - Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
      - Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI

    Formula:
      - upmove = high - high(-1)
      - downmove = low(-1) - low
      - +dm = upmove if upmove > downmove and upmove > 0 else 0
      - -dm = downmove if downmove > upmove and downmove > 0 else 0
      - +di = 100 * MovingAverage(+dm, period) / atr(period)
      - -di = 100 * MovingAverage(-dm, period) / atr(period)

    The moving average used is the one originally defined by Wilder,
    the SmoothedMovingAverage

    See:
      - https://en.wikipedia.org/wiki/Average_directional_movement_index
    )DIr0   c                     t         t        |           | j                  | j                  _        | j                  | j                  _        y )N)r   rM   r   r=   r   r1   r>   r2   r   s    r   r   zDirectionalIndicator.__init__   s1    "D24 KK

!\\

r   )r   r   r   r    aliasr   r   r!   r"   s   @r   rM   rM   o   s    8 E"E* *r   rM   c                   <     e Zd ZdZdZdZ ed      Z fdZ xZ	S )PlusDirectionalIndicatora  
    Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
    Technical Trading Systems"*.

    Intended to measure trend strength

    This indicator shows +DI:
      - Use MinusDirectionalIndicator (MinusDI) to get -DI
      - Use Directional Indicator (DI) to get +DI, -DI
      - Use AverageDirectionalIndex (ADX) to get ADX
      - Use AverageDirectionalIndexRating (ADXR) to get ADX, ADXR
      - Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
      - Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI

    Formula:
      - upmove = high - high(-1)
      - downmove = low(-1) - low
      - +dm = upmove if upmove > downmove and upmove > 0 else 0
      - +di = 100 * MovingAverage(+dm, period) / atr(period)

    The moving average used is the one originally defined by Wilder,
    the SmoothedMovingAverage

    See:
      - https://en.wikipedia.org/wiki/Average_directional_movement_index
    ))PlusDIr,   )r1   z+DirectionalIndicatorplotnamec                 d    t         t        |   d       | j                  | j                  _        y )NF)r@   )r   rR   r   r=   r   r1   r   s    r   r   z!PlusDirectionalIndicator.__init__   s&    &6e6D KK

r   
r   r   r   r    rP   r   rJ   plotinfor   r!   r"   s   @r   rR   rR      s)    4 !EE45H( (r   rR   c                   <     e Zd ZdZdZdZ ed      Z fdZ xZ	S )MinusDirectionalIndicatora  
    Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
    Technical Trading Systems"*.

    Intended to measure trend strength

    This indicator shows -DI:
      - Use PlusDirectionalIndicator (PlusDI) to get +DI
      - Use Directional Indicator (DI) to get +DI, -DI
      - Use AverageDirectionalIndex (ADX) to get ADX
      - Use AverageDirectionalIndexRating (ADXR) to get ADX, ADXR
      - Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
      - Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI

    Formula:
      - upmove = high - high(-1)
      - downmove = low(-1) - low
      - -dm = downmove if downmove > upmove and downmove > 0 else 0
      - -di = 100 * MovingAverage(-dm, period) / atr(period)

    The moving average used is the one originally defined by Wilder,
    the SmoothedMovingAverage

    See:
      - https://en.wikipedia.org/wiki/Average_directional_movement_index
    ))MinusDIr/   )r2   z-DirectionalIndicatorrT   c                 d    t         t        |   d       | j                  | j                  _        y )NF)r?   )r   rZ   r   r>   r   r2   r   s    r   r   z"MinusDirectionalIndicator.__init__   s&    '7e7D!\\

r   rW   r"   s   @r   rZ   rZ      s)    4 "EE45H* *r   rZ   c                   J     e Zd ZdZdZdZ e ed            Z fdZ xZ	S )AverageDirectionalMovementIndexa  
    Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
    Technical Trading Systems"*.

    Intended to measure trend strength

    This indicator only shows ADX:
      - Use PlusDirectionalIndicator (PlusDI) to get +DI
      - Use MinusDirectionalIndicator (MinusDI) to get -DI
      - Use Directional Indicator (DI) to get +DI, -DI
      - Use AverageDirectionalIndexRating (ADXR) to get ADX, ADXR
      - Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
      - Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI

    Formula:
      - upmove = high - high(-1)
      - downmove = low(-1) - low
      - +dm = upmove if upmove > downmove and upmove > 0 else 0
      - -dm = downmove if downmove > upmove and downmove > 0 else 0
      - +di = 100 * MovingAverage(+dm, period) / atr(period)
      - -di = 100 * MovingAverage(-dm, period) / atr(period)
      - dx = 100 * abs(+di - -di) / (+di + -di)
      - adx = MovingAverage(dx, period)

    The moving average used is the one originally defined by Wilder,
    the SmoothedMovingAverage

    See:
      - https://en.wikipedia.org/wiki/Average_directional_movement_index
    )ADX)adxr_   r-   c                 (   t         t        |           t        | j                  | j
                  z
        | j                  | j
                  z   z  }d| j                  j                  || j                  j                        z  | j                  _
        y )Nr:   r9   )r   r^   r   absr=   r>   r4   r+   r)   r   r`   )r   dxr   s     r   r   z(AverageDirectionalMovementIndex.__init__  sc    -t=?t||+,dll0JKb!GG

r   
r   r   r   r    rP   r   rJ   rK   r   r!   r"   s   @r   r^   r^      s/    < EEE*+IH Hr   r^   c                   J     e Zd ZdZdZdZ e ed            Z fdZ xZ	S )%AverageDirectionalMovementIndexRatinga  
    Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
    Technical Trading Systems"*.

    Intended to measure trend strength.

    ADXR is the average of ADX with a value period bars ago

    This indicator shows the ADX and ADXR:
      - Use PlusDirectionalIndicator (PlusDI) to get +DI
      - Use MinusDirectionalIndicator (MinusDI) to get -DI
      - Use Directional Indicator (DI) to get +DI, -DI
      - Use AverageDirectionalIndex (ADX) to get ADX
      - Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI
      - Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI

    Formula:
      - upmove = high - high(-1)
      - downmove = low(-1) - low
      - +dm = upmove if upmove > downmove and upmove > 0 else 0
      - -dm = downmove if downmove > upmove and downmove > 0 else 0
      - +di = 100 * MovingAverage(+dm, period) / atr(period)
      - -di = 100 * MovingAverage(-dm, period) / atr(period)
      - dx = 100 * abs(+di - -di) / (+di + -di)
      - adx = MovingAverage(dx, period)
      - adxr = (adx + adx(-period)) / 2

    The moving average used is the one originally defined by Wilder,
    the SmoothedMovingAverage

    See:
      - https://en.wikipedia.org/wiki/Average_directional_movement_index
    )ADXR)adxrrg   r-   c                     t         t        |           | j                  j                  | j                  j	                  | j
                  j                         z   dz  | j                  _        y )Ng       @)	r   rf   r   lr`   r4   r)   r   rh   r   s    r   r   z.AverageDirectionalMovementIndexRating.__init__5  sE    3TCE66::

DFFMM>(BBcI

r   rd   r"   s   @r   rf   rf     s0     B EE$V,-IJ Jr   rf   c                       e Zd ZdZdZy)DirectionalMovementIndexa  
    Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
    Technical Trading Systems"*.

    Intended to measure trend strength

    This indicator shows the ADX, +DI, -DI:
      - Use PlusDirectionalIndicator (PlusDI) to get +DI
      - Use MinusDirectionalIndicator (MinusDI) to get -DI
      - Use Directional Indicator (DI) to get +DI, -DI
      - Use AverageDirectionalIndex (ADX) to get ADX
      - Use AverageDirectionalIndexRating (ADXRating) to get ADX, ADXR
      - Use DirectionalMovement (DM) to get ADX, ADXR, +DI, -DI

    Formula:
      - upmove = high - high(-1)
      - downmove = low(-1) - low
      - +dm = upmove if upmove > downmove and upmove > 0 else 0
      - -dm = downmove if downmove > upmove and downmove > 0 else 0
      - +di = 100 * MovingAverage(+dm, period) / atr(period)
      - -di = 100 * MovingAverage(-dm, period) / atr(period)
      - dx = 100 * abs(+di - -di) / (+di + -di)
      - adx = MovingAverage(dx, period)

    The moving average used is the one originally defined by Wilder,
    the SmoothedMovingAverage

    See:
      - https://en.wikipedia.org/wiki/Average_directional_movement_index
    )DMINr   r   r   r    rP    r   r   rl   rl   ;  s    < Er   rl   c                       e Zd ZdZdZy)DirectionalMovementa  
    Defined by J. Welles Wilder, Jr. in 1978 in his book *"New Concepts in
    Technical Trading Systems"*.

    Intended to measure trend strength

    This indicator shows ADX, ADXR, +DI, -DI.

      - Use PlusDirectionalIndicator (PlusDI) to get +DI
      - Use MinusDirectionalIndicator (MinusDI) to get -DI
      - Use Directional Indicator (DI) to get +DI, -DI
      - Use AverageDirectionalIndex (ADX) to get ADX
      - Use AverageDirectionalIndexRating (ADXR) to get ADX, ADXR
      - Use DirectionalMovementIndex (DMI) to get ADX, +DI, -DI

    Formula:
      - upmove = high - high(-1)
      - downmove = low(-1) - low
      - +dm = upmove if upmove > downmove and upmove > 0 else 0
      - -dm = downmove if downmove > upmove and downmove > 0 else 0
      - +di = 100 * MovingAverage(+dm, period) / atr(period)
      - -di = 100 * MovingAverage(-dm, period) / atr(period)
      - dx = 100 * abs(+di - -di) / (+di + -di)
      - adx = MovingAverage(dx, period)

    The moving average used is the one originally defined by Wilder,
    the SmoothedMovingAverage

    See:
      - https://en.wikipedia.org/wiki/Average_directional_movement_index
    )DMNrn   ro   r   r   rq   rq   ^  s    > Er   rq   N)
__future__r   r   r   r    r   r	   r
   r   r   r   r$   r(   rM   rR   rZ   r^   rf   rl   rq   ro   r   r   <module>ru      s   ** * - ,'Y '*)y )*'6I '6T$*0 $*N#(4 #(L#* 5 #*L)H&; )HX*J,K *JZ >3 F!?.!r   