
    'i                         d dl mZmZmZmZ d dlmZ d dlZ G d dej                  j                        Z
 G d d ej                  e
ej                              Zy)    )absolute_importdivisionprint_functionunicode_literals)datetimeNc                   (     e Zd Z fdZ fdZ xZS )MetaRollOverc                 0    t         t        |   |||       y)z+Class has already been created ... registerN)superr	   __init__)clsnamebasesdct	__class__s       W/var/www/app/trading-bot/venv/lib/python3.12/site-packages/backtrader/feeds/rollover.pyr   zMetaRollOver.__init__   s     	lC)$s;    c                     t        t        | 
  |i |\  }}}|r<|d   j                  |j                  _        |d   j                  |j                  _        |||fS )z<Intercept const. to copy timeframe/compression from 1st datar   )r   r	   donew
_timeframep	timeframe_compressioncompression)r   argskwargs_objr   s       r   r   zMetaRollOver.donew$   s^     #<;TLVLdF#Aw11DFF!%a!5!5DFFT6!!r   )__name__
__module____qualname__r   r   __classcell__r   s   @r   r	   r	      s    <
	" 	"r   r	   c                   T     e Zd ZdZdZd Zd Z fdZ fdZd Z	d Z
d	 Zd
 Z xZS )RollOvera.  Class that rolls over to the next future when a condition is met

    Params:

        - ``checkdate`` (default: ``None``)

          This must be a *callable* with the following signature::

            checkdate(dt, d):

          Where:

            - ``dt`` is a ``datetime.datetime`` object
            - ``d`` is the current data feed for the active future

          Expected Return Values:

            - ``True``: as long as the callable returns this, a switchover can
              happen to the next future

        If a commodity expires on the 3rd Friday of March, ``checkdate`` could
        return ``True`` for the entire week in which the expiration takes
        place.

            - ``False``: the expiration cannot take place

        - ``checkcondition`` (default: ``None``)

          **Note**: This will only be called if ``checkdate`` has returned
          ``True``

          If ``None`` this will evaluate to ``True`` (execute roll over)
          internally

          Else this must be a *callable* with this signature::

            checkcondition(d0, d1)

          Where:

            - ``d0`` is the current data feed for the active future
            - ``d1`` is the data feed for the next expiration

          Expected Return Values:

            - ``True``: roll-over to the next future

        Following with the example from ``checkdate``, this could say that the
        roll-over can only happend if the *volume* from ``d0`` is already less
        than the volume from ``d1``

            - ``False``: the expiration cannot take place
    ))	checkdateN)checkconditionNc                      y)z`Returns ``True`` to notify ``Cerebro`` that preloading and runonce
        should be deactivatedT selfs    r   islivezRollOver.islivem   s     r   c                     || _         y N)_rolls)r*   r   s     r   r   zRollOver.__init__r   s	    r   c                    t         t        |           | j                  D ]-  }|j	                  | j
                         |j                          / t        | j                        | _        | j                  r| j                  j                  d      nd | _
        d | _        | j                  D cg c]  }t        j                   c}| _        y c c}w )Nr   )r   r$   startr.   setenvironment_env_startlist_dspop_d_dexpr   min_dts)r*   dxxr   s      r   r0   zRollOver.startu   s    h#% 	ATYY'HHJ	
 $%)XX$((,,q/4
,0HH5bX\\5	5s   -Cc                 l    t         t        |           | j                  D ]  }|j                           y r-   )r   r$   stopr.   )r*   r;   r   s     r   r>   zRollOver.stop   s,    h"$ 	AFFH	r   c                     | j                   r| j                   d   j                         S t        j                  j                  j                  | j                  j                        S )zKTo be overriden by subclasses which may auto-calculate the
        timezoner   )r.   _gettzbtutilsdate	Localizerr   tzr)   s    r   r@   zRollOver._gettz   sC     ;;;;q>((**xx}}&&tvvyy11r   c                 h    | j                   j                  | j                   j                  ||      S y)NF)r   r%   )r*   dtr;   s      r   
_checkdatezRollOver._checkdate   s,    66'66##B**r   c                 h    | j                   j                  | j                   j                  ||      S y)NT)r   r&   )r*   d0d1s      r   _checkconditionzRollOver._checkcondition   s,    66  ,66((R00r   c                    | j                   | j                   j                         }|*|du rP| j                  r<| j                  j                  d      | _         | j                  j                  d       nd | _         ~| j                   j
                  j                         }t        t        | j                  | j                              D ]P  \  }}|\  }}||k  s|j                         |j
                  j                         x| j                  |<   }||k  r@R | j                  W| j                  j                         sd | _        n5| j                  j
                  j                         |k  rV| j                  W| j                  | j                  || j                         r| j                  ru| j                  | j                   | j                  d         rL| j                   | _        | j                  j                  d      | _         | j                  j                  d       | j                   j                  j
                  d   | j                  j
                  d<   | j                   j                  j                  d   | j                  j                  d<   | j                   j                  j                  d   | j                  j                  d<   | j                   j                  j                  d   | j                  j                  d<   | j                   j                  j                  d   | j                  j                  d<   | j                   j                  j                   d   | j                  j                   d<   | j                   j                  j"                  d   | j                  j"                  d<   yy)NFr   T)r7   nextr5   r6   r:   r   	enumeratezipr8   rH   rL   linesopenhighlowclosevolumeopeninterest)r*   _nextdt0id_dtr;   rG   s          r   _loadzRollOver._load   s   gg!GGLLNE}~88"hhll1oDGIIMM!$"DG''""++-C %S499%=> >423hvvx' ()

(;(;(==DIIaL2 3h> **(zz(!%DJ::&&//1C7 **( zz!dooc477&C 88 4 4TWWdhhqk J!%DJ"hhll1oDGIIMM!$ &*WW]]%;%;A%>DJJ"!%!3!3A!6DJJOOA!%!3!3A!6DJJOOA $ 1 1! 4DJJNN1"&''--"5"5a"8DJJQ#'77==#7#7#:DJJa )-)C)CA)FDJJ##A& r   )r   r   r    __doc__paramsr+   r   r0   r>   r@   rH   rL   r\   r!   r"   s   @r   r$   r$   0   s8    4lF

6
24r   r$   )
__future__r   r   r   r   r   
backtraderrA   DataBaser   r	   with_metaclassr$   r(   r   r   <module>rc      sO   ** *  "2;;(( "$] r  r{{; ]r   