
    'i)                         d Z ddlmZmZmZmZ ddlZddlmZm	Z	 ddl
mZ  G d dej                        Z G d	 d
 e	ee            Z G d de      Z G d de      Zy)z

.. module:: lineroot

Definition of the base class LineRoot and base classes LineSingle/LineMultiple
to define interfaces and hierarchy for the real operational classes

.. moduleauthor:: Daniel Rodriguez

    )absolute_importdivisionprint_functionunicode_literalsN   )rangewith_metaclass)metabasec                   "     e Zd ZdZ fdZ xZS )MetaLineRootzc
    Once the object is created (effectively pre-init) the "owner" of this
    class is sought
    c                     t        t        | 
  |i |\  }}}|j                  dd       }t	        j
                  ||j                  xs t        |      |_        |||fS )N
_ownerskip)skip)	superr   donewpopr
   	findowner	_OwnerClsLineMultiple_owner)clsargskwargs_obj	ownerskip	__class__s        Q/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/lineroot.pyr   zMetaLineRoot.donew/   sd    "<;TLVLdF JJ|T2	(()-)G<.79
 T6!!    )__name__
__module____qualname____doc__r   __classcell__r   s   @r   r   r   )   s    
" "r   r   c                   r   e Zd ZdZdZdZdZ ed      \  ZZ	Z
d Zd Zd3dZd Zd4d	Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd5dZd6dZd Zd7dZd3dZd7dZ d Z!d Z"d Z#d Z$d Z%d  Z&d! Z'd" Z(d# Z)d$ Z*d% Z+d& Z,d' Z-d( Z.d) Z/d* Z0d+ Z1d, Z2d- Z3d. Z4d/ Z5d0 Z6d1 Z7d2 Z8e8Z9e:jv                  Z;y)8LineRootz
    Defines a common base and interfaces for Single and Multiple
    LineXXX instances

        Period management
        Iteration management
        Operation (dual/single operand) Management
        Rich Comparison operator definition
    Nr      c                     d| _         y Nr   _opstageselfs    r   _stage1zLineRoot._stage1M   	    r   c                     d| _         y )N   r*   r,   s    r   _stage2zLineRoot._stage2P   r/   r   c                 r    | j                   dk(  r| j                  ||||      S | j                  |||      S )Nr   rintify)r5   )r+   _operation_stage1_operation_stage2r-   other	operationr5   r6   s        r   
_operationzLineRoot._operationS   sI    ==A))yAf * 6 6 %%eY!%<<r   c                 d    | j                   dk(  r| j                  |      S | j                  |      S r)   )r+   _operationown_stage1_operationown_stage2r-   r;   s     r   _operationownzLineRoot._operationownZ   s0    ==A,,Y77((33r   c                     t         )z;Change the lines to implement a minimum size qbuffer schemeNotImplementedError)r-   savemems     r   qbufferzLineRoot.qbuffer`       !!r   c                     t         )z=Receive notification of how large the buffer must at least berC   )r-   sizes     r   	minbufferzLineRoot.minbufferd   rG   r   c                     || _         y)z
        Direct minperiod manipulation. It could be used for example
        by a strategy
        to not wait for all indicators to produce a value
        N
_minperiodr-   	minperiods     r   setminperiodzLineRoot.setminperiodh   s     $r   c                 :    t        | j                  |      | _        y)z
        Update the minperiod if needed. The minperiod will have been
        calculated elsewhere
        and has to take over if greater that self's
        N)maxrM   rN   s     r   updateminperiodzLineRoot.updateminperiodp   s     dooy9r   c                     t         )zH
        Add a minperiod to own ... to be defined by subclasses
        rC   rN   s     r   addminperiodzLineRoot.addminperiodx   
     "!r   c                     t         )@
        Increment the minperiod with no considerations
        rC   rN   s     r   incminperiodzLineRoot.incminperiod~   rV   r   c                      y)zQ
        It will be called during the "minperiod" phase of an iteration.
        N r,   s    r   prenextzLineRoot.prenext        	r   c                 $    | j                          y)z
        It will be called when the minperiod phase is over for the 1st
        post-minperiod value. Only called once and defaults to automatically
        calling next
        N)nextr,   s    r   	nextstartzLineRoot.nextstart   s     			r   c                      y)zG
        Called to calculate values when the minperiod is over
        Nr[   r,   s    r   r_   zLineRoot.next   r]   r   c                      y)zV
        It will be called during the "minperiod" phase of a "once" iteration
        Nr[   r-   startends      r   preoncezLineRoot.preonce   r]   r   c                 (    | j                  ||       y)z
        It will be called when the minperiod phase is over for the 1st
        post-minperiod value

        Only called once and defaults to automatically calling once
        N)oncerc   s      r   	oncestartzLineRoot.oncestart   s     			%r   c                      y)zQ
        Called to calculate values at "once" when the minperiod is over
        Nr[   rc   s      r   rh   zLineRoot.once   r]   r   c                     t         NrC   r-   r:   r;   r5   r   s        r   _makeoperationzLineRoot._makeoperation       !!r   c                     t         rl   rC   r-   r;   r   s      r   _makeoperationownzLineRoot._makeoperationown   ro   r   c                 (    | j                  ||       S )z?
        Operation with single operand which is "self"
        )r   )rr   r@   s     r   r>   zLineRoot._operationown_stage1   s     %%iD%AAr   c                 ,    | j                  ||d|      S )zh
        Relies on self._operation to and passes "r" True to define a
        reverse operation
        Tr4   )r<   )r-   r:   r;   r6   s       r   _roperationzLineRoot._roperation   s    
 ui4GGr   c                 h    t        |t              r|j                  d   }| j                  ||||       S )z
        Two operands' operation. Scanning of other happens to understand
        if other must be directly an operand or rather a subitem thereof
        r   )
isinstancer   linesrn   r9   s        r   r7   zLineRoot._operation_stage1   s2    
 e\*KKNE""5)Q==r   c                 `    t        |t              r|d   }|r ||| d         S  || d   |      S )z
        Rich Comparison operators. Scans other and returns either an
        operation with other directly or a subitem from other
        r   )rw   r&   )r-   r:   r;   r5   s       r   r8   zLineRoot._operation_stage2   s>    
 eX&!HE UDG,,a%((r   c                      || d         S Nr   r[   r@   s     r   r?   zLineRoot._operationown_stage2   s    a!!r   c                 B    | j                  |t        j                        S rl   )r<   operator__add__r-   r:   s     r   r~   zLineRoot.__add__       uh&6&677r   c                 B    | j                  |t        j                        S rl   )ru   r}   r~   r   s     r   __radd__zLineRoot.__radd__       x'7'788r   c                 B    | j                  |t        j                        S rl   )r<   r}   __sub__r   s     r   r   zLineRoot.__sub__   r   r   c                 B    | j                  |t        j                        S rl   )ru   r}   r   r   s     r   __rsub__zLineRoot.__rsub__   r   r   c                 B    | j                  |t        j                        S rl   )r<   r}   __mul__r   s     r   r   zLineRoot.__mul__   r   r   c                 B    | j                  |t        j                        S rl   )ru   r}   r   r   s     r   __rmul__zLineRoot.__rmul__   r   r   c                 B    | j                  |t        j                        S rl   )r<   r}   __div__r   s     r   r   zLineRoot.__div__   r   r   c                 B    | j                  |t        j                        S rl   )ru   r}   r   r   s     r   __rdiv__zLineRoot.__rdiv__   r   r   c                 B    | j                  |t        j                        S rl   )r<   r}   __floordiv__r   s     r   r   zLineRoot.__floordiv__   s    uh&;&;<<r   c                 B    | j                  |t        j                        S rl   )ru   r}   r   r   s     r   __rfloordiv__zLineRoot.__rfloordiv__   s    x'<'<==r   c                 B    | j                  |t        j                        S rl   )r<   r}   __truediv__r   s     r   r   zLineRoot.__truediv__   s    uh&:&:;;r   c                 B    | j                  |t        j                        S rl   )ru   r}   r   r   s     r   __rtruediv__zLineRoot.__rtruediv__   s    x';';<<r   c                 B    | j                  |t        j                        S rl   )r<   r}   __pow__r   s     r   r   zLineRoot.__pow__   r   r   c                 B    | j                  |t        j                        S rl   )ru   r}   r   r   s     r   __rpow__zLineRoot.__rpow__  r   r   c                 @    | j                  t        j                        S rl   )rA   r}   __abs__r,   s    r   r   zLineRoot.__abs__      !!("2"233r   c                 @    | j                  t        j                        S rl   )rA   r}   __neg__r,   s    r   r   zLineRoot.__neg__	  r   r   c                 B    | j                  |t        j                        S rl   )r<   r}   __lt__r   s     r   r   zLineRoot.__lt__      uhoo66r   c                 B    | j                  |t        j                        S rl   )r<   r}   __gt__r   s     r   r   zLineRoot.__gt__  r   r   c                 B    | j                  |t        j                        S rl   )r<   r}   __le__r   s     r   r   zLineRoot.__le__  r   r   c                 B    | j                  |t        j                        S rl   )r<   r}   __ge__r   s     r   r   zLineRoot.__ge__  r   r   c                 B    | j                  |t        j                        S rl   )r<   r}   __eq__r   s     r   r   zLineRoot.__eq__  r   r   c                 B    | j                  |t        j                        S rl   )r<   r}   __ne__r   s     r   r   zLineRoot.__ne__  r   r   c                 ,    | j                  t              S rl   )rA   boolr,   s    r   __nonzero__zLineRoot.__nonzero__  s    !!$''r   )FFr   FNrl   )F)<r   r    r!   r"   r   rM   r+   r   IndType	StratTypeObsTyper.   r2   r<   rA   rF   rJ   rP   rS   rU   rY   r\   r`   r_   rf   ri   rh   rn   rr   r>   ru   r7   r8   r?   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   __bool__object__hash__r[   r   r   r&   r&   =   s"    IJH"'(GY=4""$:""""BH>)"89898989=><=8944777777( H Hr   r&   c                   \     e Zd ZdZd Z fdZ fdZd Zd ZddZ	ddZ
dd	Zd
 Z xZS )r   zG
    Base class for LineXXX instances that hold more than one line
    c                 X    | j                          | j                  j                          y rl   )r.   rx   resetr,   s    r   r   zLineMultiple.reset,  s    

r   c                 l    t         t        |           | j                  D ]  }|j                           y rl   )r   r   r.   rx   r-   liner   s     r   r.   zLineMultiple._stage10  ,    lD)+JJ 	DLLN	r   c                 l    t         t        |           | j                  D ]  }|j                           y rl   )r   r   r2   rx   r   s     r   r2   zLineMultiple._stage25  r   r   c                 H    | j                   D ]  }|j                  |        yz:
        The passed minperiod is fed to the lines
        N)rx   rU   r-   rO   r   s      r   rU   zLineMultiple.addminperiod:  %    
 JJ 	)Di(	)r   c                 H    | j                   D ]  }|j                  |        yr   )rx   rY   r   s      r   rY   zLineMultiple.incminperiodB  r   r   c                 D    | j                   d   j                  ||||      S r{   )rx   rn   rm   s        r   rn   zLineMultiple._makeoperationJ  s!    zz!}++E9aLLr   c                 @    | j                   d   j                  ||      S r{   )rx   rr   rq   s      r   rr   zLineMultiple._makeoperationownM  s    zz!}..y*EEr   c                 J    | j                   D ]  }|j                  d        y )Nr   )rE   )rx   rF   )r-   rE   r   s      r   rF   zLineMultiple.qbufferP  s#    JJ 	$DLLL#	$r   c                 H    | j                   D ]  }|j                  |        y rl   )rx   rJ   )r-   rI   r   s      r   rJ   zLineMultiple.minbufferT  s!    JJ 	!DNN4 	!r   r   rl   r   )r   r    r!   r"   r   r.   r2   rU   rY   rn   rr   rF   rJ   r#   r$   s   @r   r   r   (  s7    

))MF$!r   r   c                       e Zd ZdZd Zd Zy)
LineSinglezB
    Base class for LineXXX instances that hold a single line
    c                 4    | xj                   |dz
  z  c_         y)zS
        Add the minperiod (substracting the overlapping 1 minimum period)
        r   NrL   rN   s     r   rU   zLineSingle.addminperiod]  s     	9q=(r   c                 .    | xj                   |z  c_         y)rX   NrL   rN   s     r   rY   zLineSingle.incminperiodc  s     	9$r   N)r   r    r!   r"   rU   rY   r[   r   r   r   r   Y  s    )%r   r   )r"   
__future__r   r   r   r   r}   	utils.py3r   r	    r
   
MetaParamsr   r   r&   r   r   r[   r   r   <module>r      s`   *	* *  , "8&& "(h~lF3 hV.!8 .!b% %r   