
    'i4                        d dl mZmZmZmZ d dlZd dlZd dlZddlm	Z	m
Z
 ddl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      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      Z G d+ d,e      Z G d- d.e      Z  G d/ d0e      Z!y)1    )absolute_importdivisionprint_functionunicode_literalsN   )maprange   )	Indicatorc                   &     e Zd ZdZdZ fdZ xZS )PeriodNz
    Base class for indicators which take a period (__init__ has to be called
    either via super or explicitly)

    This class has no defined lines
    ))periodr
   c                 t    t         t        |           | j                  | j                  j
                         y N)superr   __init__addminperiodpr   self	__class__s    \/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/indicators/basicops.pyr   zPeriodN.__init__*   s&    gt%'$&&--(    __name__
__module____qualname____doc__paramsr   __classcell__r   s   @r   r   r   !   s     F) )r   r   c                       e Zd ZdZd Zd Zy)
OperationNa  
    Calculates "func" for a given period

    Serves as a base for classes that work with a period and can express the
    logic in a callable object

    Note:
      Base classes must provide a "func" attribute which is a callable

    Formula:
      - line = func(data, period)
    c                     | j                  | j                  j                  | j                  j                              | j
                  d<   y N)sizer   )funcdatagetr   r   liner   s    r   nextzOperationN.next<   s/    yyDFFMM!BC		!r   c                     | j                   j                  }| j                  j                  }| j                  j                  }| j
                  }t        ||      D ]  } ||||z
  dz   |dz          ||<    y Nr
   )r*   arrayr(   r   r   r'   r	   )r   startenddstsrcr   r'   is           r   oncezOperationN.once?   si    iiooiiooyyuc" 	6A#a&j1na!e45CF	6r   N)r   r   r   r   r,   r5    r   r   r#   r#   /   s    D6r   r#   c                   &     e Zd ZdZdZ fdZ xZS )
BaseApplyNay  
    Base class for ApplyN and others which may take a ``func`` as a parameter
    but want to define the lines in the indicator.

    Calculates ``func`` for a given period where func is given as a parameter,
    aka named argument or ``kwarg``

    Formula:
      - lines[0] = func(data, period)

    Any extra lines defined beyond the first (index 0) are not calculated
    ))r'   Nc                 `    | j                   j                  | _        t        t        |           y r   )r   r'   r   r8   r   r   s    r   r   zBaseApplyN.__init__X   s    FFKK	j$(*r   r   r!   s   @r   r8   r8   I   s     F+ +r   r8   c                       e Zd ZdZdZy)ApplyNz`
    Calculates ``func`` for a given period

    Formula:
      - line = func(data, period)
    )applyN)r   r   r   r   linesr6   r   r   r;   r;   ]   s     Er   r;   c                       e Zd ZdZdZdZeZy)Highestz
    Calculates the highest value for the data in a given period

    Uses the built-in ``max`` for the calculation

    Formula:
      - highest = max(data, period)
    )MaxN)highestN)r   r   r   r   aliasr=   maxr'   r6   r   r   r?   r?   g   s     EEDr   r?   c                       e Zd ZdZdZdZeZy)Lowestz
    Calculates the lowest value for the data in a given period

    Uses the built-in ``min`` for the calculation

    Formula:
      - lowest = min(data, period)
    )MinN)lowestN)r   r   r   r   rB   r=   minr'   r6   r   r   rE   rE   u   s     EEDr   rE   c                   >     e Zd ZdZdZej                  Z fdZ xZ	S )ReduceNa  
    Calculates the Reduced value of the ``period`` data points applying
    ``function``

    Uses the built-in ``reduce`` for the calculation plus the ``func`` that
    subclassess define

    Formula:
      - reduced = reduce(function(data, period)), initializer=initializer)

    Notes:

      - In order to mimic the python ``reduce``, this indicator takes a
        ``function`` non-named argument as the 1st argument, unlike other
        Indicators which take only named arguments
    )reducedc                     d|vr&t        j                  | j                  |      | _        n*t        j                  | j                  ||d         | _        t        t        |           y )Ninitializer)rM   )	functoolspartialr'   r   rJ   r   )r   functionkwargsr   s      r   r   zReduceN.__init__   sU    &!))$))X>DI!))$))X6<]6KMDI 	gt%'r   )
r   r   r   r   r=   rN   reducer'   r   r    r!   s   @r   rJ   rJ      s$      ED( (r   rJ   c                   ,    e Zd ZdZdZej                  Zy)SumNz
    Calculates the Sum of the data values over a given period

    Uses ``math.fsum`` for the calculation rather than the built-in ``sum`` to
    avoid precision errors

    Formula:
      - sumn = sum(data, period)
    )sumnN)r   r   r   r   r=   mathfsumr'   r6   r   r   rT   rT      s     E99Dr   rT   c                       e Zd ZdZdZeZy)AnyNz
    Has a value of ``True`` (stored as ``1.0`` in the lines) if *any* of the
    values in the ``period`` evaluates to non-zero (ie: ``True``)

    Uses the built-in ``any`` for the calculation

    Formula:
      - anyn = any(data, period)
    )anynN)r   r   r   r   r=   anyr'   r6   r   r   rY   rY           EDr   rY   c                       e Zd ZdZdZeZy)AllNz
    Has a value of ``True`` (stored as ``1.0`` in the lines) if *all* of the
    values in the ``period`` evaluates to non-zero (ie: ``True``)

    Uses the built-in ``all`` for the calculation

    Formula:
      - alln = all(data, period)
    )allnN)r   r   r   r   r=   allr'   r6   r   r   r^   r^      r\   r   r^   c                       e Zd ZdZdZdZd Zy)FindFirstIndexa7  
    Returns the index of the last data that satisfies equality with the
    condition generated by the parameter _evalfunc

    Note:
      Returned indexes look backwards. 0 is the current index and 1 is
      the previous bar.

    Formula:
      - index = first for which data[index] == _evalfunc(data)
    index)	_evalfuncNc                     | j                   j                  |      t        fdt        t	        |            D              S )Nc              3   4   K   | ]  \  }}|k(  s|  y wr   r6   .0r4   vms      r   	<genexpr>z&FindFirstIndex.func.<locals>.<genexpr>   s     J$!Q16AJ   )r   rf   r,   	enumeratereversed)r   iterablerl   s     @r   r'   zFindFirstIndex.func   s2    FFX&J)HX,>"?JJJr   Nr   r   r   r   r=   r   r'   r6   r   r   rb   rb      s    
 E#FKr   rb   c                       e Zd ZdZdeffZy)FindFirstIndexHighesta   
    Returns the index of the first data that is the highest in the period

    Note:
      Returned indexes look backwards. 0 is the current index and 1 is
      the previous bar.

    Formula:
      - index = index of first data which is the highest
    rf   Nr   r   r   r   rC   r   r6   r   r   rt   rt          	 C "Fr   rt   c                       e Zd ZdZdeffZy)FindFirstIndexLowestz
    Returns the index of the first data that is the lowest in the period

    Note:
      Returned indexes look backwards. 0 is the current index and 1 is
      the previous bar.

    Formula:
      - index = index of first data which is the lowest
    rf   Nr   r   r   r   rH   r   r6   r   r   rx   rx      rv   r   rx   c                       e Zd ZdZdZdZd Zy)FindLastIndexa6  
    Returns the index of the last data that satisfies equality with the
    condition generated by the parameter _evalfunc

    Note:
      Returned indexes look backwards. 0 is the current index and 1 is
      the previous bar.

    Formula:
      - index = last for which data[index] == _evalfunc(data)
    rc   re   c                     | j                   j                  |      t        fdt        |      D              }| j                   j                  |z
  dz
  S )Nc              3   4   K   | ]  \  }}|k(  s|  y wr   r6   ri   s      r   rm   z%FindLastIndex.func.<locals>.<genexpr>  s     A41a!q&QArn   r
   )r   rf   r,   ro   r   )r   rq   rd   rl   s      @r   r'   zFindLastIndex.func
  sF    FFX&A9X#6AA vv}}u$q((r   Nrr   r6   r   r   r{   r{      s    
 E#F)r   r{   c                       e Zd ZdZdeffZy)FindLastIndexHighestz
    Returns the index of the last data that is the highest in the period

    Note:
      Returned indexes look backwards. 0 is the current index and 1 is
      the previous bar.

    Formula:
      - index = index of last data which is the highest
    rf   Nru   r6   r   r   r   r     rv   r   r   c                       e Zd ZdZdeffZy)FindLastIndexLowestz
    Returns the index of the last data that is the lowest in the period

    Note:
      Returned indexes look backwards. 0 is the current index and 1 is
      the previous bar.

    Formula:
      - index = index of last data which is the lowest
    rf   Nry   r6   r   r   r   r   !  rv   r   r   c                   4    e Zd ZdZdZdZdZd Zd Zd Z	d Z
y	)
AccumzP
    Cummulative sum of the data values

    Formula:
      - accum += data
    )CumSumCumulativeSum)accum))seedg        c                 j    | j                   j                  | j                  d   z   | j                  d<   y Nr   )r   r   r(   r*   r+   s    r   	nextstartzAccum.nextstart>  s$    vv{{TYYq\1		!r   c                 \    | j                   d   | j                  d   z   | j                   d<   y Nr   )r*   r(   r+   s    r   r,   z
Accum.nextA  s$    yy}tyy|3		!r   c                     | j                   j                  }| j                  j                  }| j                  j                  }t        ||      D ]  }|||   z   x||<   } y r   )r*   r/   r(   r   r   r	   r   r0   r1   r2   r3   prevr4   s          r   	oncestartzAccum.oncestartD  sU    iiooiioovv{{uc" 	*A 3q6M)CFT	*r   c                     | j                   j                  }| j                  j                  }||dz
     }t        ||      D ]  }|||   z   x||<   } y r.   )r*   r/   r(   r	   r   s          r   r5   z
Accum.onceL  sU    iiooiioo519~uc" 	*A 3q6M)CFT	*r   N)r   r   r   r   rB   r=   r   r   r,   r   r5   r6   r   r   r   r   /  s,     )EEF24**r   r   c                   $    e Zd ZdZdZdZd Zd Zy)Averagez
    Averages a given data arithmetically over a period

    Formula:
      - av = data(period) / period

    See also:
      - https://en.wikipedia.org/wiki/Arithmetic_mean
    )ArithmeticMeanMeanavc                     t        j                  | j                  j                  | j                  j
                              | j                  j
                  z  | j                  d<   y r%   )rV   rW   r(   r)   r   r   r*   r+   s    r   r,   zAverage.nextb  s?    IIdiimmm78466==H 			!r   c                     | j                   j                  }| j                  j                  }| j                  j                  }t        ||      D ])  }t        j                  |||z
  dz   |dz          |z  ||<   + y r.   )r(   r/   r*   r   r   r	   rV   rW   )r   r0   r1   r3   r2   r   r4   s          r   r5   zAverage.oncef  sk    iiooiioouc" 	CAYYs1v:>!a%89FBCF	Cr   N)r   r   r   r   rB   r=   r,   r5   r6   r   r   r   r   U  s     (EEICr   r   c                   J     e Zd ZdZdZdZ fdZ fdZd Z fdZ	d Z
 xZS )	ExponentialSmoothingaC  
    Averages a given data over a period using exponential smoothing

    A regular ArithmeticMean (Average) is used as the seed value considering
    the first period values of data

    Formula:
      - av = prev * (1 - alpha) + data * alpha

    See also:
      - https://en.wikipedia.org/wiki/Exponential_smoothing
    )ExpSmoothing))alphaNc                     | j                   j                  | _        | j                  !dd| j                   j                  z   z  | _        d| j                  z
  | _        t        t
        |           y )Ng       @      ?)r   r   r   alpha1r   r   r   r   s    r   r   zExponentialSmoothing.__init__  sS    VV\\
::dffmm 34DJDJJ&"D24r   c                 *    t         t        |           y r   )r   r   r,   r   s    r   r   zExponentialSmoothing.nextstart  s    "D.0r   c                     | j                   d   | j                  z  | j                  d   | j                  z  z   | j                   d<   y r   r*   r   r(   r   r+   s    r   r,   zExponentialSmoothing.next  s6    yy}t{{2TYYq\DJJ5NN		!r   c                 .    t         t        |   ||       y r   )r   r   r5   )r   r0   r1   r   s      r   r   zExponentialSmoothing.oncestart  s    "D.uc:r   c                     | j                   j                  }| j                  j                  }| j                  }| j                  }||dz
     }t        ||      D ]  }||z  ||   |z  z   x||<   } y r.   r(   r/   r*   r   r   r	   	r   r0   r1   darraylarrayr   r   r   r4   s	            r   r5   zExponentialSmoothing.once  sv    

 eai uc" 	AA#f}vay5/@@@F1I	Ar   )r   r   r   r   rB   r   r   r   r,   r   r5   r    r!   s   @r   r   r   o  s/     EF51O;	Ar   r   c                   2     e Zd ZdZdZ fdZd Zd Z xZS )ExponentialSmoothingDynamica  
    Averages a given data over a period using exponential smoothing

    A regular ArithmeticMean (Average) is used as the seed value considering
    the first period values of data

    Note:
      - alpha is an array of values which can be calculated dynamically

    Formula:
      - av = prev * (1 - alpha) + data * alpha

    See also:
      - https://en.wikipedia.org/wiki/Exponential_smoothing
    )ExpSmoothingDynamicc                     t         t        |           t        d| j                  j
                  | j                  j                  z
        }| j                  d   j                  |       y r   )
r   r   r   rC   r   
_minperiodr   r   r=   incminperiod)r   minperioddiffr   s     r   r   z$ExponentialSmoothingDynamic.__init__  sL    )49;
 Atzz44tvv}}DE

1""=1r   c                     | j                   d   | j                  d   z  | j                  d   | j                  d   z  z   | j                   d<   y r   r   r+   s    r   r,   z ExponentialSmoothingDynamic.next  sA    IIbMDKKN*TYYq\DJJqM-II 			!r   c                    | j                   j                  }| j                  j                  }| j                  j                  }| j                  j                  }||dz
     }t        ||      D ]  }|||   z  ||   ||   z  z   x||<   } y r.   r   r   s	            r   r5   z ExponentialSmoothingDynamic.once  s    

  "" eai uc" 	GA#fQi/&)eAh2FFFF1I	Gr   )	r   r   r   r   rB   r   r,   r5   r    r!   s   @r   r   r     s      %E2J	Gr   r   c                   L     e Zd ZdZdZdZdd e       ffZ fdZd Z	d Z
 xZS )	WeightedAverageaw  
    Calculates the weighted average of the given data over a period

    The default weights (if none are provided) are linear to assigne more
    weight to the most recent data

    The result will be multiplied by a given "coef"

    Formula:
      - av = coef * sum(mul(data, period), weights)

    See:
      - https://en.wikipedia.org/wiki/Weighted_arithmetic_mean
    )AverageWeightedr   )coefr   weightsc                 *    t         t        |           y r   )r   r   r   r   s    r   r   zWeightedAverage.__init__  s    ot-/r   c                 4   | j                   j                  | j                  j                        }t	        t
        j                  || j                  j                        }| j                  j                  t        j                  |      z  | j                  d<   y r%   )r(   r)   r   r   r   operatormulr   r   rV   rW   r*   )r   r(   dataweighteds      r   r,   zWeightedAverage.next  s[    yy}}$&&--}08<<tvv~~>vv{{TYY|%<<		!r   c           	         | j                   j                  }| j                  j                  }| j                  j                  }| j                  j
                  }| j                  j                  }t        ||      D ]D  }|||z
  dz   |dz    }	|t        j                  t        t        j                  |	|            z  ||<   F y r.   )r(   r/   r*   r   r   r   r   r	   rV   rW   r   r   r   )
r   r0   r1   r   r   r   r   r   r4   r(   s
             r   r5   zWeightedAverage.once  s    vv{{&&..uc" 	KA!f*q.!a%0DtyyX\\4)IJJF1I	Kr   )r   r   r   r   rB   r=   tupler   r   r,   r5   r    r!   s   @r   r   r     s5     !EEi13F0=
	Kr   r   )"
__future__r   r   r   r   rN   rV   r   	utils.py3r   r	    r   r   r#   r8   r;   r?   rE   rJ   rT   rY   r^   rb   rt   rx   r{   r   r   r   r   r   r   r   r6   r   r   <module>r      s*  ** *    " )i )6 64+ +(Z j Z (j (<: : : KZ K(#N ##> #)J )0#= ##- ##*I #*LCg C4-A7 -A`(G"6 (GV$Kg $Kr   